Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert images color space in Keras?

Tags:

keras

I am feeding RGB color images to a Neural Network implemented with Keras. How can I have Keras convert the images to a different color space (e.g. YUV, Lab, or some grayscale)?

I tried with a Lambda() layer, but got an error:

model.add(Lambda(lambda x: cv2.cvtColor(x, cv2.COLOR_RGB2LAB), input_shape=(160, 320, 3)))

gave me

TypeError: src is not a numpy array, neither a scalar

I believe the issue is that x is a Tensor, and I don't know how to convert it to something OpenCV accepts.

Even better, if I can have it done in the GPU instead. E.g. with Tensorflow I would use functions such as tf.image.rgb_to_hsv() and tf.image.rgb_to_grayscale().

Thanks!

like image 331
Fanta Avatar asked Mar 26 '17 19:03

Fanta


1 Answers

If you import tensorflow, you can use the tf.image.rgb_to_hsv() function in the lambda:

def hsv_conversion(x):
    import tensorflow as tf    
    return tf.image.rgb_to_hsv(x)

model.add(Lambda(hsv_conversion, input_shape=(160, 320, 3)))
like image 64
Ryan Avatar answered Sep 23 '22 12:09

Ryan