Saving Keras Model

Saving Keras Model

The Python3 command line is great to test out commands, and the ipython is the improvement on is as it provided a more interactive control. Jupyter Notebook is the web version derived from ipython.

This article is on saving a trained model to a file, and being able to retrieve it again in the Keras format. This assumes that the there is a working TensorFlow.

For a dataset, many models may be developed and proposed before one is agreed on. Saving it to disk allows a Data Engineer to manage and identify models that best suit the situation.

Step 1. Create the directory

[Ubuntu 24] Create the directory "models" to store the models files.

mkdir models

Step 2. Create the model and save the model as "my_model.keras"

[Ubuntu 24] Change to the python environment and start ipython, then enter the following codes

import tensorflow as tf
import os; os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
mnist = tf.keras.datasets.mnist; data = mnist.load_data(); type(data)
(x_train, y_train), (x_test, y_test) = mnist.load_data()

import matplotlib.pyplot as plt
import matplotlib
import numpy as py
matplotlib.use('tkagg')

num_classes = 10
input_shape = (28, 28, 1)
x_train = x_train / 255;  x_test = x_test / 255
y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)
model = tf.keras.models.Sequential(
 [
 tf.keras.layers.Input(shape=input_shape),
 tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
 tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
 tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
 tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
 tf.keras.layers.Flatten(),
 tf.keras.layers.Dropout(0.5),
 tf.keras.layers.Dense(num_classes, activation="softmax"),
 ])

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

batch_size = 50
epoch = 10
model.fit(x_train, y_train, batch_size=batch_size, epochs=epoch, shuffle=True, validation_split=0.1)

model.summary( )        

The function summary( ) will show the model layers information. Here is an example to display the test result of this trained model.

scores = model.evaluate(x_test, y_test, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))        


Step 3. Save and load the trained model

This is saved in keras format. Open ipython and declare the imports as previously.

In ipython, to save the model

model.save('models/my_model.keras')        

Load the trained model

new_model = tf.keras.models.load_model('models/conv2d_mnist_model.keras')        


Step 4. Save and load model only

This is saved in json format. Open ipython and declare the imports as previously.

from tensorflow.keras.models import model_from_json
model_json = model.to_json()
with open("models/my_model.json", "w") as json_file:
    json_file.write(model_json)        

Load the model and train with provided data

json_file = open('models/my_model.json', 'r')
model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(model_json)
loaded_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
loaded_model.fit(x_train, y_train, batch_size=batch_size, epochs=epoch, shuffle=True, validation_split=0.1)        

Hopefully, you have found it useful to know how to save and load a Keras model to disk.


要查看或添加评论,请登录

Nicholas S.的更多文章

  • Machine Learning with TensorFlow and Keras on Ubuntu - Part 3

    Machine Learning with TensorFlow and Keras on Ubuntu - Part 3

    Having installed TensorFlow on a machine with GPU and being able to verify that the GPU is in use, this next article…

  • Machine Learning with TensorFlow and Keras on Ubuntu - Part 2

    Machine Learning with TensorFlow and Keras on Ubuntu - Part 2

    So far, the base system and drivers have been installed and these are what I have installed on Windows 11, WSL2. One…

  • Machine Learning with TensorFlow and Keras on Ubuntu - Part 1

    Machine Learning with TensorFlow and Keras on Ubuntu - Part 1

    I was interested to understand better how computers solve complex problems like recognising images and extract meaning…

  • Show git differences between commits

    Show git differences between commits

    In managing code sources, GIT approach expects the code to be at different stages, which are "staged" or "commit"…

  • Server performance limits

    Server performance limits

    Stress test tools provide a planned approach to simulate multiple user access to servers. The most commonly used is…

  • VUE for Single Page Application

    VUE for Single Page Application

    AngularJS (the oldest of the 3) and ReactJS have been at the limelight of the latest technologies in the area of Single…

  • Amazing Malaysians - Hew Ah Kow

    Amazing Malaysians - Hew Ah Kow

    What does a half boiled egg maker and a 3-pin plug have in common? When Hew Ah Kow introduced a plastic ware to make…

  • KVM virtual machine on Centos 8 - Change name and Create snapshots

    KVM virtual machine on Centos 8 - Change name and Create snapshots

    Virtualisation provide a more efficient use of memory, disk and all CPU on a server. Separate servers can be managed…

  • Software version control with Git

    Software version control with Git

    Software development involving more than 1 developers will surely benefit from the use of a software version control…

  • Raspberry Pi: Getting Started

    Raspberry Pi: Getting Started

    Was considering on several options for "embedded systems" and finally settled with Raspberry Pi. Getting Started

社区洞察

其他会员也浏览了