0

I am building a CNN model for image classification using keras. I am unable to save the best model using ModelCheckPoint as it throws this warning:

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

I have researched on stackoverflow for all the related questions, but nothing has worked so far. [1] [2] [3] [4] and more

Here's my code:

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

After augmentation, model checkpoint

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

fit the model

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

After running, validation accuracy is calculated for 1st epoch, but from 2nd epoch it starts giving the aforementioned warning and doesn't save the best model.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 1
    I remember seeing a similar issue before, it was due to using `validation_steps` when the validation data wasn't of type `tf.data.Dataset`, can you try removing `validation_steps` parameter and try again?. – Mohamed abdelmagid Oct 09 '21 at 07:42
  • @aim97 Hey this actually worked :D I'll test 2-3 times more and let you know if it works always. – Shubham Agrawal Oct 09 '21 at 09:12
  • 1
    @aim97 please post the solution as an answer, so that OP can accept and be useful for others in the future – desertnaut Oct 09 '21 at 22:09

1 Answers1

1

This seems similar to another issue 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. ...

The data you provided is not from the required type to make it work, validating this way requires the given dataset to be either very large or to use repeat method to repeat itself to accommodate the number of validation batches you want, i believe that's why validation only worked at the first time then stopped for the rest.

Check also this better explanation of the parameters steps_per_epoch and validation_steps and when to use them.