Checkpoints,
Saving & Loading Complete Model
AND Converting for mobile to TFLite
TIP: Once you have a model you like --save it as a Saved Model format rather than just a checkpoint as it is easier (1 line of code) to reinstate the model
Checkpoints
Check points are the weights of your model that are saved periodically (you must set this up) as you train.
USES:
-
They are useful because the last checkpoint may not be the best model in terms of performance (think overtraining) and you may want to use a different checkpoint earlier in the training cycle for your production model.
-
Also they are useful when you need to stop training or training is interrupted and you must restart training and want to load up the model from the last check point.
IMPORTANT: make sure to check for updates at https://www.tensorflow.org/tutorials/keras/save_and_load
How to setup Checkpoints --save multiple as you train (weights only)& reload to continue
checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
# Create a callback that saves the model's weights
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True,
verbose=1)
# Train the model with the new callback
model.fit(train_images,
train_labels,
epochs=10,
validation_data=(test_images,test_labels),
callbacks=[cp_callback]) # Pass callback to training
# This may generate warnings related to saving the state of the optimizer.
# These warnings (and similar warnings throughout this notebook)
# are in place to discourage outdated usage, and can be ignored.
How to Load a Checkpoint (weights-only)
MUST HAVE A MODEL SETUP (without weights): Create a new, untrained model. When restoring a model from weights-only, you must have a model with the same architecture as the original model. Since it's the same model architecture, you can share weights despite that it's a different instance of the model.
EXAMPLE 1: : in this code below a simple Sequential model is created by defining each layers, then the weights are restored from a checkpoint
# Define a simple sequential model |
EXAMPLE 2: Using the Object Detection API we recreate our model using information from a configuraton file(pipeline.config) then the weights are restored
pipeline_file = '/content/drive/My Drive/maskDetection/fine_tuned_model/pipeline.config'
#get configuration information by parsing the pipeline.config file
#get model information from the appropriate configs info
# Restore checkpoint |
FOR FUN......USING Example 1 we can rebuild a fresh, untrained model, and evaluate it on the test set. An untrained model will perform at chance levels (~10% accuracy):
# Create a basic model instance
model = create_model()
# Evaluate the model
loss, acc = model.evaluate(test_images, test_labels, verbose=2)
print("Untrained model, accuracy: {:5.2f}%".format(100*acc)) 1000/1 - 0s - loss: 2.3598 - accuracy: 0.1360 Untrained model, accuracy: 13.60%
Then load the weights from the checkpoint and re-evaluate:
# Loads the weights
model.load_weights(checkpoint_path)
# Re-evaluate the model
loss,acc = model.evaluate(test_images, test_labels, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
1000/1 - 0s - loss: 0.3937 - accuracy: 0.8650 Restored model, accuracy: 86.50%
SAVE AND LOAD MODEL using SavedModel or HDF5 formats (older option)
...............preference is to use SavedModel format
make sure to check for updates at https://www.tensorflow.org/tutorials/keras/save_and_load
How to save & Load your Model to a file
Save the entire model
Call model.save to save the a model's architecture, weights, and training configuration in a single file/folder. This allows you to export a model so it can be used without access to the original Python code*. Since the optimizer-state is recovered, you can resume training from exactly where you left off.
Saving a fully-functional model is very useful—you can load them in TensorFlow.js (HDF5, Saved Model) and then train and run them in web browsers, or convert them to run on mobile devices using TensorFlow Lite (HDF5, Saved Model)
*Custom objects (e.g. subclassed models or layers) require special attention when saving and loading. See the Saving custom objects section below
OPTION 1. Save via SavedModel Format
# Create and train a new model instance.
model = create_model()
model.fit(train_images, train_labels, epochs=5)
# Save the entire model as a SavedModel.
!mkdir -p saved_model
model.save('saved_model/my_model') ///read documentation of saved method
NOTE: contents of SavedModel directory looks like:
my_model
assets keras_metadata.pb saved_model.pb variables
OPTION 2. Save via HDF5 format
Keras provides a basic save format using the HDF5 standard.
# Create and train a new model instance.
model = create_model()
model.fit(train_images, train_labels, epochs=5)
# Save the entire model to a HDF5 file.
# The '.h5' extension indicates that the model shuold be saved to HDF5.
model.save('my_model.h5')
OPTION 1: Load a Model from SavedModel format
new_model = tf.keras.models.load_model('saved_model/my_model')
# Check its architecture
new_model.summary()
Model: "sequential_5" _________________________________________________________________ Layer (type) Output Shape Param # ==== ============================================================= dense_10 (Dense) (None, 512) 401920 ________ _________________________________________________________ dropout_5 (Dropout) (None, 512) 0 ________ _________________________________________________________ dense_11 (Dense) (None, 10) 5130 ======== ========================================================= Total params: 407,050 Trainable params: 407,050 Non-trainable params: 0 _________ ________________________________________________________
OPTION 2: Load A Model: recreate the model from H5 file format
# Recreate the exact same model, including its weights and the optimizer
new_model = tf.keras.models.load_model('my_model.h5')
# Show the model architecture
new_model.summary()
Model: "sequential_5" _________________________________________________________________ Layer (type) Output Shape Param # ==== ============================================================= dense_10 (Dense) (None, 512) 401920 ________ _________________________________________________________ dropout_5 (Dropout) (None, 512) 0 ________ _________________________________________________________ dense_11 (Dense) (None, 10) 5130 ======== ========================================================= Total params: 407,050 Trainable params: 407,050 Non-trainable params: 0 _________ ________________________________________________________
For Fun.....Check its accuracy:
loss, acc = new_model.evaluate(test_images, test_labels, verbose=2)
print('Restored model, accuracy: {:5.2f}%'.format(100*acc))
1000/1 - 0s - loss: 0.5667 - accuracy: 0.8630 Restored model, accuracy: 86.30%
CONVERT a tf.Keras Model to TFLite (must do in python)
MAKE SURE to check for updates on current proces at https://www.tensorflow.org/lite/convert/python_api
IMPORTANT: depending on version of Tensorflow and WINDOWS machine you may have a problem --there is a bug in Tensorflow stable 2.0. Solution is to install latest tensorflow nightly build until 2.1.* is stable official releasepip install tf-nightly |
Converting a tf.Keras model
The following example shows how to convert a tf.keras model into a TensorFlow Lite FlatBuffer.
import tensorflow as tf
# Create a simple Keras model.
x = [-1, 0, 1, 2, 3, 4]
y = [-3, -1, 1, 3, 5, 7]
model = tf.keras.models.Sequential(
[tf.keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(x, y, epochs=50)
# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert() #now save the tflite model to the file
#tflite_model.save(tflite_file) #Note this does not seem to work although in google documentation
open(tflite_file, "wb").write(tflite_model)