是否可以为预训练的 Inception 模型(tensorflow 2.0/Keras)提供 2D 灰度图像?

Is it possible to feed the pretrained Inception model (tensorflow 2.0/Keras) with 2D grayscale images?

根据 Keras 2.0 文档,关于可以馈送到预训练初始模型的图像的输入形状:

input_shape: optional shape tuple, only to be specified if include_top is False (otherwise the input shape has to be (299, 299, 3) (with 'channels_last' data format) or (3, 299, 299) (with 'channels_first' data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 75. E.g. (150, 150, 3) would be one valid value.

但是,我正在处理二维的灰度图像。我应该如何处理这种情况?

伪RGB图像可以复制3次灰度图

import numpy as np
# img=np.zeros((224,224))

如果您的图像的形状长度为 2,只有宽度和高度,您首先需要添加一个额外的尺寸:

img = np.expand_dims(img,-1)

您将最后一个维度重复 3 次:

img = np.repeat(img,3,2)
print(img.shape)
# (224,224,3)