Python 将灰度图像内容复制到3个通道

Python 将灰度图像内容复制到3个通道,python,tensorflow,keras,deep-learning,Python,Tensorflow,Keras,Deep Learning,我正在使用ImageDataGenerator批量加载灰度图像。我需要将每个灰度图像的内容复制到3个通道中。我尝试了以下代码,但似乎不起作用: 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

我正在使用
ImageDataGenerator
批量加载灰度图像。我需要将每个灰度图像的内容复制到3个通道中。我尝试了以下代码,但似乎不起作用:

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




train_images_orign= grayscale_to_rgb(train_images_orign)
valid_images_orign= grayscale_to_rgb(valid_images_orign)
test_images_orign= grayscale_to_rgb(test_images_orign)

x_train, y_train = next(train_images_orign)
x_valid, y_valid = next(valid_images_orign)
x_test, y_test = next(test_images_orign)

我应该朝哪个方向来完成这个任务?

我没有直接的方法使用ImageDataGenerator来转换它,但几天前我遇到了同样的问题,你可以间接地使用opencv2将它转换为rgb,然后我使用imageio来读取它

import cv2
cv2.imread('path/to/img/a.png') # look at glob for reading from folder
cv2.cvtColor(gray,cv2.COLOR_GRAY2RGB)
cv2.imwrite('path/to/output/a.png')

import imageio
import skimage.transform as transform

image_size = 64
dimensions=4
array = []
for image_path in glob.glob("path/to/output/*.png"):
    try:
        im = imageio.imread(image_path)
        array = [*array, transform.resize(im, (image_size, image_size, dimensions))]
        array_names = [*array_names, image_path.split("output/")[1].split(".png")[0]]
    except ValueError:
        ""

np.array(array).shape
expand_dims可与tensorflow 1.10.1 helper方法一起使用,tensorflow 1.10.1 helper方法与ImageDataGenerator密切相关,可直接执行相同的操作,但您必须以某种方式将其从gray2rgb转换为gray2rgb,以改进此答案

preprocess_input = tf.keras.applications.resnet50.preprocess_input
preds = model.predict(preprocess_input(np.expand_dims(array[0], axis=0))) # where model is some keras model
您将需要以下内容来解码输出

decode_predictions = tf.keras.applications.resnet50.decode_predictions
decode_predictions(preds, top=3)

如果这不能解决您的问题或至少提供一个解决方案模板,请留下评论,我会相应地更新:)

更新:结果表明,如果加载图像的颜色模式和给定的
颜色模式
参数(默认情况下为
“RGB”
),然后图像将转换为给定的
颜色模式
。因此,在这种情况下,灰度图像将自动转换为RGB


您也可以使用的
preprocessing\u函数
参数(假设您使用的是
color\u mode='grayscale'
,否则上述注释适用):

请注意,此函数在任何图像增强后应用:

预处理函数:将在每个输入上隐含的函数。 该函数将在图像大小调整和增强后运行 函数应该有一个参数:一个图像(带秩的Numpy张量 3) ,并应输出具有相同形状的Numpy张量


我想我有一个更好的解决方案,那就是编写一个包装层

class MyPreprocess( Layer ) :
    def call( self, inputs ) :
        # expand your input from gray scale to rgb
        # if your inputs.shape = (None,None,1)
        fake_rgb = K.concatenate( [inputs for i in range(3)], axis=-1 ) 
        fake_rgb = K.cast( fake_rgb, 'float32' )
        # else use K.stack( [inputs for i in range(3)], axis=-1 ) 
        # preprocess for uint8 image
        x = preprocess_input( fake_rgb )
        return x
    def compute_output_shape( self, input_shape ) :
        return input_shape[:3] + (3,)


gray_in = Input(shape=(None,None,1), name='gray_uint8')
tensor_in = MyPreprocess(name='preproc')( gray_in )
pred_out = PretrainedModel( tensor_in )
new_model = Model( inputs=gray_in, outputs=pred_out )

通过这种方式,
new_model
可以直接用于预测
unit8
图像。

现在,我得到了错误:ValueError:无法将输入数组从shape(227227,9)广播到shape(227227,3)。我打印了该形状,它是(227227,3)。原因是默认情况下,ImageDataGenerator将灰度转换为RGB。因此,实际上我没有什么事可做。@PervaizNiazi您是对的。
load\u img
实现为如果图像的颜色模式和
color\u mode
参数(默认为RGB)不同,则会自动转换为
color\u模式
。但是,我不知道在PIL模块(用于加载Keras中的图像)中将灰度转换为RGB时使用了什么转换方法@devssh我们不需要修改任何东西,因为ImageDataGenerator默认情况下会将灰度转换为RGB。在我的情况下,所有图像都是灰度。下面是代码:trainpath='C:/Users/dataset/train'train\u images\u orign=ImageDataGenerator()
class MyPreprocess( Layer ) :
    def call( self, inputs ) :
        # expand your input from gray scale to rgb
        # if your inputs.shape = (None,None,1)
        fake_rgb = K.concatenate( [inputs for i in range(3)], axis=-1 ) 
        fake_rgb = K.cast( fake_rgb, 'float32' )
        # else use K.stack( [inputs for i in range(3)], axis=-1 ) 
        # preprocess for uint8 image
        x = preprocess_input( fake_rgb )
        return x
    def compute_output_shape( self, input_shape ) :
        return input_shape[:3] + (3,)


gray_in = Input(shape=(None,None,1), name='gray_uint8')
tensor_in = MyPreprocess(name='preproc')( gray_in )
pred_out = PretrainedModel( tensor_in )
new_model = Model( inputs=gray_in, outputs=pred_out )