Home > Mobile >  tf.keras.callbacks.ModelCheckpoint ignores the montior parameter and always use loss
tf.keras.callbacks.ModelCheckpoint ignores the montior parameter and always use loss

Time:04-12

I am running tf.keras.callbacks.ModelCheckpoint with the accuracy metric but loss is used to save the best checkpoints. I have tested this in different places (my computer and collab) and two different code and faced the same issue. Here is an example code and the results:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import os
import shutil

def get_uncompiled_model():
    inputs = keras.Input(shape=(784,), name="digits")
    x = layers.Dense(64, activation="relu", name="dense_1")(inputs)
    x = layers.Dense(64, activation="relu", name="dense_2")(x)
    outputs = layers.Dense(10, activation="softmax", name="predictions")(x)
    model = keras.Model(inputs=inputs, outputs=outputs)
    return model

def get_compiled_model():
    model = get_uncompiled_model()
    model.compile(
        optimizer="rmsprop",
        loss="sparse_categorical_crossentropy",
        metrics=["accuracy"],
    )
    return model

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Preprocess the data (these are NumPy arrays)
x_train = x_train.reshape(60000, 784).astype("float32") / 255
x_test = x_test.reshape(10000, 784).astype("float32") / 255

y_train = y_train.astype("float32")
y_test = y_test.astype("float32")

# Reserve 10,000 samples for validation
x_val = x_train[-10000:]
y_val = y_train[-10000:]
x_train = x_train[:-10000]
y_train = y_train[:-10000]


ckpt_folder = os.path.join(os.getcwd(), 'ckpt')
if os.path.exists(ckpt_folder):
    shutil.rmtree(ckpt_folder)

ckpt_path = os.path.join(r'D:\deep_learning\tf_keras\semantic_segmentation\logs', 'mymodel_{epoch}')


callbacks = [
    tf.keras.callbacks.ModelCheckpoint(
        # Path where to save the model
        # The two parameters below mean that we will overwrite
        # the current checkpoint if and only if
        # the `val_loss` score has improved.
        # The saved model name will include the current epoch.
        filepath=ckpt_path,
        montior="val_accuracy",
        # save the model weights with best validation accuracy
        mode='max',
        save_best_only=True,  # only save the best weights
        save_weights_only=False,
        # only save model weights (not whole model)
        verbose=1
    )
]

model = get_compiled_model()


model.fit(
    x_train, y_train, epochs=3, batch_size=1, callbacks=callbacks, validation_split=0.2, steps_per_epoch=1
)

1/1 [==============================] - ETA: 0s - loss: 2.6475 - accuracy: 0.0000e 00 Epoch 1: val_loss improved from -inf to 2.32311, saving model to D:\deep_learning\tf_keras\semantic_segmentation\logs\mymodel_1 1/1 [==============================] - 6s 6s/step - loss: 2.6475 - accuracy: 0.0000e 00 - val_loss: 2.3231 - val_accuracy: 0.1142

Epoch 2/3 1/1 [==============================] - ETA: 0s - loss: 1.9612 - accuracy: 1.0000 Epoch 2: val_loss improved from 2.32311 to 2.34286, saving model to D:\deep_learning\tf_keras\semantic_segmentation\logs\mymodel_2 1/1 [==============================] - 5s 5s/step - loss: 1.9612 - accuracy: 1.0000 - val_loss: 2.3429 - val_accuracy: 0.1187

Epoch 3/3 1/1 [==============================] - ETA: 0s - loss: 2.8378 - accuracy: 0.0000e 00 Epoch 3: val_loss did not improve from 2.34286 1/1 [==============================] - 5s 5s/step - loss: 2.8378 - accuracy: 0.0000e 00 - val_loss: 2.2943 - val_accuracy: 0.1346

CodePudding user response:

In your code, You write montior instead of monitor, and the function doesn't have this word as param then use the default value, If you write like below, You get what you want:

callbacks = [
    tf.keras.callbacks.ModelCheckpoint(
        filepath=ckpt_path,
        monitor="val_accuracy",
        mode='max',
        save_best_only=True,
        save_weights_only=False,
        verbose=1
    )
]
  • Related