我正在为以下问题编写代码:
我正在尝试正确设置我的数据拆分,但我不知道如何...
到目前为止我的代码:
train_batches = ImageDataGenerator(preprocessing_function=mobilenet_v2.preprocess_input, validation_split=0.20).flow_from_directory(
train_path, target_size=(im_height, im_width), batch_size=batch_size)
test_batches = ImageDataGenerator(preprocessing_function=mobilenet_v2.preprocess_input).flow_from_directory(
test_path, target_size=(im_height, im_width), batch_size=batch_size)
mobv2 = tf.keras.applications.mobilenet_v2.MobileNetV2()
x = mobv2.layers[-2].output
output_layer = Dense(units=6, activation='softmax')(x)
model = tf.keras.models.Model(inputs=mobv2.input, outputs=output_layer)
for layer in model.layers[:-25]:
layer.trainable = False
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=.0001),
loss='categorical_crossentropy',
metrics=['accuracy']
)
这是我的合身,但尚未完成,因为我不确定验证和测试要包含哪些内容....
model.fit(train_batches, steps_per_epoch=4, )
答案 0 :(得分:1)
看下面的代码
train_batches = ImageDataGenerator(preprocessing_function=mobilenet_v2.preprocess_input, validation_split=0.20).flow_from_directory(
train_path, target_size=(im_height, im_width), batch_size=batch_size,
subset='training)
valid_batches=ImageDataGenerator(preprocessing_function=mobilenet_v2.preprocess_input, validation_split=0.20).flow_from_directory(
train_path, target_size=(im_height, im_width), batch_size=batch_size,
subset='validation')
epochs = 15 # set the number of epochs to run
history=model.fit(train_batches, epochs=epochs, verbose=1,
validation_data=valid_batches, verbose=1)'
为了获得更好的结果,我还建议您考虑使用可调整的学习 率使用 Keras 回调 ReduceLROnPlateau。文档是 here. 设置它以监视验证丢失。使用以下代码:
rlronp=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss", factor=0.5,
patience=10, verbose=1)
我还建议您使用 Keras 回调 EarlyStopping。文档是 here. 使用下面的代码
es=tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=3, verbose=1,
restore_best_weights=True)
现在在model.fit中包含
callbacks=[rlronp, es]