我正在为以下问题编写代码:
- 我有一个包含训练和测试目录的水果数据集。这两个目录中都包含6个类别(新鲜/腐烂的苹果、新鲜/腐烂的橙子、新鲜/腐烂的香蕉)。
- 我正在使用MobileNetV2模型进行迁移学习
我试图正确设置我的数据拆分,但对如何…
- 设置训练、验证和测试拆分感到困惑
- 如何检查它们是否确实正确设置(例如,确保数据没有重叠)
- 如何保存训练过程中的进度。(例如:我运行了一个训练10个epochs的脚本。如何确保在再次运行脚本进行x个epochs时,训练从上次停止的地方继续进行。)
到目前为止我的代码如下:
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].outputoutput_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 = Falsemodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=.0001), loss='categorical_crossentropy', metrics=['accuracy'] )
这是我的fit方法,但尚未完成,因为我不确定如何包含验证和测试…
model.fit(train_batches, steps_per_epoch=4, )
回答:
请看下面的代码
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 # 设置运行的epochs数量history=model.fit(train_batches, epochs=epochs, verbose=1, validation_data=valid_batches, verbose=1)'
为了获得更好的结果,我还建议你考虑使用Keras回调ReduceLROnPlateau来调整学习率。文档在这里。设置它来监控验证损失。使用下面的代码:
rlronp=tf.keras.callbacks.ReduceLROnPlateau( monitor="val_loss", factor=0.5, patience=10, verbose=1)
我还建议你使用Keras回调EarlyStopping。文档在这里。使用下面的代码
es=tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=3, verbose=1, restore_best_weights=True)
现在在model.fit中包含
callbacks=[rlronp, es]