警告:Tensorflow:只能在 val_accuracy 可用的情况下保存最佳模型,跳过

Warning : Tensorflow: can save best model only with val_accuracy available, skipping

我正在使用 keras 构建用于图像分类的 CNN 模型。我无法使用 ModelCheckPoint 保存最佳模型,因为它会抛出此警告:

WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.

我已经在 Whosebug 上研究了所有相关问题,但到目前为止没有任何效果。 及更多

这是我的代码:

model.compile(loss='categorical_crossentropy',metrics=['accuracy', Precision(), Recall()],optimizer='adam')

train_datagen = ImageDataGenerator(rescale = 1/255.,
                                rotation_range   =40,
                                width_shift_range=0.2,
                                height_shift_range=0.2,
                                shear_range=0.2,
                                zoom_range =0.2,
                                horizontal_flip=True,
                                fill_mode ='nearest')
test_datagen = ImageDataGenerator(rescale = 1/255.)

train_generator = train_datagen.flow_from_directory(r"./datasetcrop512/train", target_size=(512,512), batch_size=32, class_mode='categorical')
test_generator = test_datagen.flow_from_directory(r"./datasetcrop512/test", target_size=(512,512), batch_size=32, class_mode='categorical')

增强后,模型检查点

from keras.callbacks import ModelCheckpoint
filepath = 'weights_best_model3_6.hdf5'
checkpoint = ModelCheckpoint(filepath,monitor = 'val_accuracy',verbose = 1, save_best_only=True, mode='max')

适合模型

history = model.fit(train_generator, steps_per_epoch = stepsPerEpoch,
       epochs = 15, validation_data=test_generator, validation_steps = stepsPerEpoch,
       callbacks = [ PlotLossesKeras(),checkpoint])

在运行之后,计算了第一个epoch的验证准确率,但是从第二个epoch开始,它开始给出上述警告并且没有保存最好的模型。

这似乎与另一个相似 that i saw before, the use of validation_steps is only allowed under some conditions as stated in the docs

validation_steps : Only relevant if validation_data is provided and is a tf.data dataset. ...

您提供的数据不是来自使其工作所需的类型,以这种方式验证需要给定的数据集非常大或使用 repeat 方法重复自身以适应验证的数量你想要的批次,我相信这就是为什么验证只在第一次有效然后停止其余部分的原因。

另请查看对参数 steps_per_epoch and validation_steps 以及何时使用它们的更好解释。