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

2024-04-26 10:32:26 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我已经研究了stackoverflow的所有相关问题,但到目前为止还没有任何结果[1][2][3][4]

这是我的密码:

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])

运行后,将计算第一个历元的验证精度,但从第二个历元开始,它将发出上述警告,并且不会保存最佳模型


Tags: from模型test警告sizemodelmoderange
1条回答
网友
1楼 · 发布于 2024-04-26 10:32:26

这似乎与我之前看到的另一个issue类似,只有在docs中所述的某些条件下才允许使用validation_steps

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

您提供的数据并非来自所需的类型,因此,通过这种方式进行验证需要给定的数据集非常大,或者使用repeat方法重复自身,以适应所需的验证批数,我相信这就是为什么验证只在第一时间起作用,然后在其余时间停止

还要检查参数steps_per_epoch and validation_steps的更好解释以及何时使用它们

相关问题 更多 >