Keras:如何将 fit_generator 与不同类型的多个输出一起使用 [英] Keras: How to use fit_generator with multiple outputs of different type
问题描述
在具有功能 API 的 Keras 模型中,我需要调用 fit_generator 以使用 ImageDataGenerator 训练增强图像数据.
问题是我的模型有两个输出:我试图预测的掩码和一个二进制值.
我显然只想增加输入和掩码输出,而不是二进制值.
我怎样才能做到这一点?
In a Keras model with the Functional API I need to call fit_generator to train on augmented images data using an ImageDataGenerator.
The problem is my model has two outputs: the mask I'm trying to predict and a binary value.
I obviously only want to augment the input and the mask output and not the binary value.
How can I achieve this?
推荐答案
下面的例子可能不言自明!虚拟"模型接受 1 个输入(图像)并输出 2 个值.该模型计算每个输出的 MSE.
The example below might be self-explanatory! The 'dummy' model takes 1 input (image) and it outputs 2 values. The model computes the MSE for each output.
x = Convolution2D(8, 5, 5, subsample=(1, 1))(image_input)
x = Activation('relu')(x)
x = Flatten()(x)
x = Dense(50, W_regularizer=l2(0.0001))(x)
x = Activation('relu')(x)
output1 = Dense(1, activation='linear', name='output1')(x)
output2 = Dense(1, activation='linear', name='output2')(x)
model = Model(input=image_input, output=[output1, output2])
model.compile(optimizer='adam', loss={'output1': 'mean_squared_error', 'output2': 'mean_squared_error'})
下面的函数生成批次以在训练期间为模型提供数据.它需要训练数据 x
和标签 y
其中 y=[y1, y2]
The function below generates batches to feed the model during training. It takes the training data x
and the label y
where y=[y1, y2]
def batch_generator(x, y, batch_size, is_train):
sample_idx = 0
while True:
X = np.zeros((batch_size, input_height, input_width, n_channels), dtype='float32')
y1 = np.zeros((batch_size, mask_height, mask_width), dtype='float32')
y2 = np.zeros((batch_size, 1), dtype='float32')
# fill up the batch
for row in range(batch_sz):
image = x[sample_idx]
mask = y[0][sample_idx]
binary_value = y[1][sample_idx]
# transform/preprocess image
image = cv2.resize(image, (input_width, input_height))
if is_train:
image, mask = my_data_augmentation_function(image, mask)
X_batch[row, ;, :, :] = image
y1_batch[row, :, :] = mask
y2_batch[row, 0] = binary_value
sample_idx += 1
# Normalize inputs
X_batch = X_batch/255.
yield(X_batch, {'output1': y1_batch, 'output2': y2_batch} ))
最后,我们调用 fit_generator()
Finally, we call the fit_generator()
model.fit_generator(batch_generator(X_train, y_train, batch_size, is_train=1))
这篇关于Keras:如何将 fit_generator 与不同类型的多个输出一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!