'KeyError: 'Failed to format this callback filepath: "skintype_64_rmsprop_{val_loss:.3f}.h5". Reason: \'val_loss\''
I have been trying to train my skin type classification model but it shows error.
model_name = f"skintype_{batch_size}_{optimizer}"
tensorboard = tf.keras.callbacks.TensorBoard(log_dir=os.path.join("logs", model_name))
# saves model checkpoint whenever we reach better weights
modelcheckpoint = tf.keras.callbacks.ModelCheckpoint(model_name + "_{val_loss:.3f}.h5", save_best_only=True, verbose=1)
history = m.fit(train_ds, validation_data=valid_ds,
steps_per_epoch=n_training_samples // batch_size,
validation_steps=n_validation_samples // batch_size, verbose=1, epochs=30,
callbacks=[tensorboard, modelcheckpoint])
Solution 1:[1]
I don't know anything about Keras, and I only took a brief look at your code, but I think I see what's wrong. In the line:
modelcheckpoint = tf.keras.callbacks.ModelCheckpoint(model_name + "_{val_loss:.3f}.h5", save_best_only=True, verbose=1)
Keras is complaining about val_loss
. I bet you want the format string containing that reference ("_{val_loss:.3f}.h5"
) to have a f
at the front of it so that the reference gets interpreted and replaced with a value before it is passed into ModelCheckpoint
. I think you need just a one character addition to get over this hurdle:
modelcheckpoint = tf.keras.callbacks.ModelCheckpoint(model_name + f"_{val_loss:.3f}.h5", save_best_only=True, verbose=1)
I hope I'm right and that this knowledge gets you cruising along again.
Solution 2:[2]
There could be a problem with the metrics you have mentioned in model.compile() as well.
For example, my ModelCheckpoint looks like this
path = "ops/model-{epoch:02d}-{val_categorical_accuracy:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath = path ,monitor ='val_categorical_accuracy',save_best_only=True,save_weights_only=True,mode='max',shuffle=True,verbose = 1,save_freq='epoch')
and my model.compile() looks like this:
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=[tf.keras.metrics.CategoricalAccuracy()])
Most likely the reason why keras has an issue with val_loss is that you havent mentioned the metrics properly while compiling.
Solution 3:[3]
Have you checked the available storage on your Google Drive? If there is not enough storage, it cannot be saved.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Dharman |
Solution 3 | Sarach Rujiranurak |