我正在使用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)我应该在哪个方向上实现这一点?
发布于 2018-09-26 01:57:23
更新:事实证明,Keras中的load_img函数已经实现in such a way,如果正在加载的图像的颜色模式和给定的color_mode参数(默认情况下为'RGB')不同,则图像将转换为给定的color_mode。因此,在这种情况下,灰度图像将自动转换为RGB。
您也可以使用ImageDataGenerator的preprocessing_function参数(假设您使用的是color_mode='grayscale',否则上面的说明也适用):
import numpy as np
def gray_to_rgb(img):
return np.repeat(img, 3, 2)
generator = ImageDataGenerator(..., preprocessing_function=gray_to_rgb)
train_gen = generator.flow_from_directory(color_mode='grayscale', ...)只需注意,此函数是在任何图像增强之后应用的:
将隐含在每个输入上的
preprocessing_function:函数。该函数将在图像调整大小并放大后运行。该函数应该有一个参数:一个图像(秩为3的Numpy张量),并且应该输出一个具有相同形状的Numpy张量。
发布于 2018-09-26 01:22:35
我没有直接用ImageDataGenerator转换的方法,但是几天前我也遇到过同样的问题,你可以间接地用opencv2把它转换成rgb,然后我用imageio把它读到numpy。
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).shapeexpand_dims可以像下面这样与tensorflow 1.10.1辅助方法一起使用,这些方法与ImageDataGenerator紧密相关,可以直接执行相同的操作,但您必须以某种方式将其从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)如果这不能解决你的问题,或者至少提供了一个解决方案的模板,请留下评论,我会相应地更新它:)
发布于 2018-09-26 13:18:41
我想我有一个更好的解决方案,那就是编写一个包装层
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图像。
https://stackoverflow.com/questions/52503396
复制相似问题