我正在尝试构建一个具有多个输出的CNN。我的数据生成器如下:
datagen = ImageDataGenerator()
train_generator = datagen.flow(X_train, y_train, yt_train, y_on_deck_train, yt_on_deck_train, batch_size=26, shuffle=True)
test_generator = datagen.flow(X_test, y_test, yt_test, y_on_deck_test, yt_on_deck_test, batch_size=15, shuffle=True)
我的模型声明如下:
out_00 = tf.keras.layers.Dense(len(home_cards.columns), activation='sigmoid', name='y')(x)
out_01 = tf.keras.layers.Dense(len(home_cards.columns), activation='sigmoid', name='yt')(x)
out_02 = tf.keras.layers.Dense(len(home_cards.columns), activation='sigmoid', name='y_on_deck')(x)
out_03 = tf.keras.layers.Dense(len(home_cards.columns), activation='sigmoid', name='yt_on_deck')(x)
model = tf.keras.Model(inputs=input, outputs=[out_00, out_01, out_02, out_03])
# opt = tf.keras.optimizers.SGD(learning_rate=0.00005, momentum=0.6, nesterov=False, name='SGD')
opt = tf.keras.optimizers.Nadam(learning_rate=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, name='Nadam')
checkpoint = ModelCheckpoint(config.CLASH_PATH() + '/models/step_02.h5', monitor='accuracy', verbose=1, save_best_only=True,
save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='accuracy', patience=150, verbose=1, mode='auto')
reduce_lr = ReduceLROnPlateau(monitor='val_accuracy', factor=0.2,
patience=15, min_lr=0.001, verbose=1)
loss = {'y': 'binary_crossentropy',
'yt': 'binary_crossentropy',
'y_on_deck': 'binary_crossentropy',
'yt_on_deck': 'binary_crossentropy',
}
metrics = {'y': 'accuracy',
'yt': 'accuracy',
'y_on_deck': 'accuracy',
'yt_on_deck': 'accuracy',
}
model.compile(optimizer=opt, loss=loss, metrics=metrics)
model.summary()
model.fit(train_generator,
validation_data=test_generator,
epochs=1000, verbose=1,
callbacks=[checkpoint, early, reduce_lr])
不幸的是,我在尝试运行时收到以下错误:
TypeError: flow() got multiple values for argument 'batch_size'
我试图在网上找到一个例子来复制,但不幸的是,很少有多标签和多输出的例子。非常感谢。
以下修改(插入修改后的生成器(解决了我的问题:
def make_multi_output_flow(image_gen, X, y_list, batch_size):
y_item_0 = y_list[0]
y_indices = np.arange(y_item_0.shape[0])
orig_flow = image_gen.flow(X, y=y_indices, batch_size=batch_size)
while True:
(X, y_next_i) = next(orig_flow)
y_next = [y_item[y_next_i] for y_item in y_list]
yield X, y_next
new_y_train = [y_train.values, yt_train.values, y_on_deck_train.values, yt_on_deck_train.values]
multi_output_flow_train = make_multi_output_flow(datagen, X_train, new_y_train, batch_size=36)
new_y_test = [y_test.values, yt_test.values, y_on_deck_test.values, yt_on_deck_test.values]
multi_output_flow_test = make_multi_output_flow(datagen, X_train, new_y_test, batch_size=16)
...
model.fit(multi_output_flow_train,
validation_data=multi_output_flow_test,
epochs=1000, verbose=1,
callbacks=[checkpoint, early, reduce_lr])