From Tensorflow Version (2.2), when model is saved using tf.keras.models.save_model, the model will be saved in a folder and not just as a .pb file, which have the following directory structure, in addition to the saved_model.pb file.
If the model is saved with the name, “best_model”, it can be loaded using the name of the folder, “best_model“, instead of saved_model.pb
# Loading the Tensorflow Saved Model (PB) model = tf.keras.models.load_model("best_model")
And for saving, following can be used
# Saving the Model tf.keras.models.save_model
Code to convert a model from tensorflow Saved Model Format (pb) to Keras Saved Model Format (h5) is shown below.
import os import tensorflow as tf from tensorflow.keras.preprocessing import image pb_model_dir = "./auto_model/best_model" h5_model = "./mymodel.h5" # Loading the Tensorflow Saved Model (PB) model = tf.keras.models.load_model(pb_model_dir) print(model.summary()) # Saving the Model in H5 Format tf.keras.models.save_model(model, h5_model) # Loading the H5 Saved Model loaded_model_from_h5 = tf.keras.models.load_model(h5_model) print(loaded_model_from_h5.summary())
Tensorflow .pb model summary
Model: "functional_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) [(None, 4, 4, 10)] 0 _________________________________________________________________ tf_op_layer_Cast (TensorFlow (None, 4, 4, 10) 0 _________________________________________________________________ conv2d (Conv2D) (None, 4, 4, 16) 1456 _________________________________________________________________ conv2d_1 (Conv2D) (None, 4, 4, 256) 37120 _________________________________________________________________ conv2d_2 (Conv2D) (None, 4, 4, 32) 73760 _________________________________________________________________ conv2d_3 (Conv2D) (None, 4, 4, 32) 9248 _________________________________________________________________ conv2d_4 (Conv2D) (None, 4, 4, 32) 9248 _________________________________________________________________ conv2d_5 (Conv2D) (None, 4, 4, 16) 4624 _________________________________________________________________ flatten (Flatten) (None, 256) 0 _________________________________________________________________ dense (Dense) (None, 128) 32896 _________________________________________________________________ re_lu (ReLU) (None, 128) 0 _________________________________________________________________ regression_head_1 (Dense) (None, 20) 2580 ================================================================= Total params: 170,932 Trainable params: 170,932 Non-trainable params: 0 _________________________________________________________________ None
Keras .h5 model summary
Model: "functional_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) [(None, 4, 4, 10)] 0 _________________________________________________________________ tf_op_layer_Cast (TensorFlow (None, 4, 4, 10) 0 _________________________________________________________________ conv2d (Conv2D) (None, 4, 4, 16) 1456 _________________________________________________________________ conv2d_1 (Conv2D) (None, 4, 4, 256) 37120 _________________________________________________________________ conv2d_2 (Conv2D) (None, 4, 4, 32) 73760 _________________________________________________________________ conv2d_3 (Conv2D) (None, 4, 4, 32) 9248 _________________________________________________________________ conv2d_4 (Conv2D) (None, 4, 4, 32) 9248 _________________________________________________________________ conv2d_5 (Conv2D) (None, 4, 4, 16) 4624 _________________________________________________________________ flatten (Flatten) (None, 256) 0 _________________________________________________________________ dense (Dense) (None, 128) 32896 _________________________________________________________________ re_lu (ReLU) (None, 128) 0 _________________________________________________________________ regression_head_1 (Dense) (None, 20) 2580 ================================================================= Total params: 170,932 Trainable params: 170,932 Non-trainable params: 0 _________________________________________________________________ None