Skip to content

Import Keras Model

How to import Keras models in DJL

In DJL TensorFlow engine and model zoo, only SavedModel format (.pb files) is supported. However, many Keras users save their model using API and it produce a .h5 file.

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(), "resnet/1/")

However, if you already saved your model in .h5 file using like below, you need a simple python script to convert it to SavedModel format."resnet.h5")

Just load your .h5 model file back to Keras and save it to SavedModel:

loaded_model = keras.models.load_model("resnet.h5"), "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.