Import Keras Model
How to import Keras models in DJL¶
This document shows you how to convert a .h5 model file into TensorFlow SavedModel(.pb) file so it can be imported in DJL. All the code here are Python code, you need to install TensorFlow for Python.
For example, if you have a ResNet50 with trained weight, you can directly save it in SavedModel format using
Here we just use pre-trained weights of ResNet50 from Keras Applications:
import tensorflow as tf import tensorflow.keras as keras resnet = keras.applications.ResNet50() tf.saved_model.save(resnet, "resnet/1/")
However, if you already saved your model in .h5 file using
keras.model.save like below, you need a simple python script
to convert it to SavedModel format.
Just load your .h5 model file back to Keras and save it to SavedModel:
loaded_model = keras.models.load_model("resnet.h5") tf.saved_model.save(loaded_model, "resnet/1/")
Once you have a SavedModel, you can load your Keras model using DJL TensorFlow engine. Refer to How to load models in DJL.