我想知道是否有一种方法可以在Python中使用“纯”Keras(即不导入Tensorflow)将图像从灰度转换为RGB。
我现在做的是:
x_rgb = tf.image.grayscale_to_rgb(x_grayscale)
也许你会认为这是“欺骗”(就像 keras.backend 最终可能会在幕后调用Tensorflow),但这里有一个解决方案:
keras.backend
from keras import backend as K def grayscale_to_rgb(images, channel_axis=-1): images= K.expand_dims(images, axis=channel_axis) tiling = [1] * 4 # 4 dimensions: B, H, W, C tiling[channel_axis] *= 3 images= K.tile(images, tiling) return images
(假设您的灰度图像有一个形状 B x H x W 而不是,例如。 B x H x W x 1 ; 否则只需删除函数的第一行)
B x H x W
B x H x W x 1