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!
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With