[英]using Keras' flow_from_directory with FCNN
I trained a constitutional neural net for image segmentation with Keras successfully. 我成功地使用Keras训练了构造神经网络进行图像分割。 Now I am trying to improve performance applying some data augmentation to my images.
现在,我尝试通过对图像进行一些数据增强来提高性能。 To do so I use the
ImageDataGenerator
and then flow_from_directory
to load only batches into memory (I tried without but I get memory error). 为此,我使用
ImageDataGenerator
,然后使用flow_from_directory
仅将批处理加载到内存中(我尝试了不带但出现内存错误)。 Code example is: 代码示例为:
training_images = np.array(training_images)
training_masks = np.array(training_masks)[:, :, :, 0].reshape(len(training_masks), 400, 400, 1)
# generators for data augmentation -------
seed = 1
generator_x = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=180,
horizontal_flip=True,
fill_mode='reflect')
generator_y = ImageDataGenerator(
featurewise_center=False,
featurewise_std_normalization=False,
rotation_range=180,
horizontal_flip=True,
fill_mode='reflect')
generator_x.fit(training_images, augment=True, seed=seed)
generator_y.fit(training_masks, augment=True, seed=seed)
image_generator = generator_x.flow_from_directory(
'data',
target_size=(400, 400),
class_mode=None,
seed=seed)
mask_generator = generator_y.flow_from_directory(
'masks',
target_size=(400, 400),
class_mode=None,
seed=seed)
train_generator = zip(image_generator, mask_generator)
model = unet(img_rows, img_cols)
model.fit_generator(train_generator, steps_per_epoch=int(len(training_images)/4), epochs=1)
However when I run the code I get the following error (I am using Tensorflow back-end): 但是,当我运行代码时,出现以下错误(我正在使用Tensorflow后端):
InvalidArgumentError (see above for traceback): Incompatible shapes: [14400000] vs. [4800000]
[[Node: loss/out_loss/mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](loss/out_loss/Reshape, loss/out_loss/Reshape_1)]]
In the error it complains about incompatible shapes 14400000 (400x400x9) vs. 4800000 (400x400x3). 在错误中,它抱怨不兼容的形状为14400000(400x400x9)与4800000(400x400x3)。 I am using a custom loss function here (and if you look at the error it says something about the loss) that is the Dice coefficient, defined as follows:
我在这里使用自定义损失函数(如果您查看错误,它表示损失的某些方面)就是Dice系数,定义如下:
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + 1.) / (K.sum(y_true_f) + K.sum(y_pred_f) + 1.)
Here I use (400,400,3) images with masks for 1 class of shape (400,400,1). 在这里,我将(400,400,3)带有遮罩的图像用于1类形状(400,400,1)。 My NN has inputs defined as
Input((img_rows, img_cols, 3))
and output as Conv2D(1, (1, 1), activation='sigmoid', name='out')(conv9)
(but this was working fine when training without data augmentation). 我的NN的输入定义为
Input((img_rows, img_cols, 3))
并输出为Conv2D(1, (1, 1), activation='sigmoid', name='out')(conv9)
(但这工作正常在没有数据扩充的情况下进行训练时)。
The error occurs because you're reading the masks in RGB color mode. 发生错误是因为您正在以RGB颜色模式读取蒙版。
The default color_mode
in flow_from_directory
is 'rgb'
. 默认
color_mode
在flow_from_directory
为'rgb'
。 So without specifying color_mode
, your masks are loaded into (batch_size, 400, 400, 3)
arrays. 因此,无需指定
color_mode
,您的遮罩将被加载到(batch_size, 400, 400, 3)
color_mode
(batch_size, 400, 400, 3)
数组中。 That's why y_true_f
is 3 times larger than y_pred_f
in your error message. 这就是为什么
y_true_f
大于3倍y_pred_f
在您的错误信息。
To read the masks in grayscale, use color_mode='grayscale'
: 要读取灰度蒙版,请使用
color_mode='grayscale'
:
mask_generator = generator_y.flow_from_directory(
'masks',
target_size=(400, 400),
class_mode=None,
color_mode='grayscale',
seed=seed)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.