CNN: 拆分训练集、测试集和验证集并保存训练进度

我正在为以下问题编写代码:

  • 我有一个包含训练和测试目录的水果数据集。这两个目录中都包含6个类别(新鲜/腐烂的苹果、新鲜/腐烂的橙子、新鲜/腐烂的香蕉)。
  • 我正在使用MobileNetV2模型进行迁移学习

我试图正确设置我的数据拆分,但对如何…

  1. 设置训练、验证和测试拆分感到困惑
  2. 如何检查它们是否确实正确设置(例如,确保数据没有重叠)
  3. 如何保存训练过程中的进度。(例如:我运行了一个训练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]

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注