'How to see metrics of a loaded keras model?

I have trained a model with keras and saved it, can I see what the computed metrics during training were, after I load back the mode with keras.models import load_model() ?



Solution 1:[1]

When I built mine using keras I used

print(model.summary())

Solution 2:[2]

If you've saved a model using the .save() method, the training history is unfortunately not saved with it. There is no way to recover the training history from file using .load_model(), so if you haven't saved your training history to file separately, and you've closed your session / cleared the Python memory, it's gone.

In order to save your training history, you want to save the history to a dictionary when calling the .fit() method, and then pickle that dictionary to file, as in the following working example (Python 3.8, Tensorflow / Keras 2.7):

# Imports
import pickle
from tensorflow.keras import datasets, layers, models, losses
from matplotlib import pyplot as plt

# Get training and validation data
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0

# Build and compile model
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(10))
model.compile(optimizer='adam',
              loss= losses.SparseCategoricalCrossentropy(from_logits = True),
              metrics=['accuracy'])

# Train model, saving the history as a dictionary
history = model.fit(train_images, train_labels, batch_size = 32, validation_data = (test_images, test_labels), epochs = 10)
history = history.history

# Pickle the history to file
with open('file_name.pkl', 'wb') as f:
    pickle.dump(history, f)

# Save the model
model.save('file.h5')
    
# Plot the results
acc = history['accuracy']
val_acc = history['val_accuracy']
loss = history['loss']
val_loss = history['val_loss']

epochs_range = range(10)

plt.figure(figsize=(10, 10))
plt.subplot(2, 1, 1)
plt.plot(epochs_range, acc, label = 'Training Accuracy')
plt.plot(epochs_range, val_acc, label = 'Validation Accuracy')
plt.legend(loc = 'lower right')
plt.ylim(0,1)
plt.title('Training and Validation Accuracy', fontsize = 15)

plt.subplot(2, 1, 2)
plt.plot(epochs_range, loss, label = 'Training Loss')
plt.plot(epochs_range, val_loss, label = 'Validation Loss')
plt.legend(loc = 'upper right')
plt.ylim(bottom = 0)
plt.title('Training and Validation Loss', fontsize = 15)
plt.show()

When loading the model from file, you can now load the history with pickle as well:

model = models.load_model('file.h5')
with open('file_name.pkl', 'rb') as f:
    history = pickle.load(f)

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 Leo Gaunt
Solution 2 Angus Maiden