Compile Keras Models

Author: Yuwei Hu

This article is an introductory tutorial to deploy keras models with Relay.

For us to begin with, keras should be installed. Tensorflow is also required since it’s used as the default backend of keras.

A quick solution is to install via pip

pip install -U keras --user
pip install -U tensorflow --user

or please refer to official site

import tvm
from tvm import te
import tvm.relay as relay
from import download_testdata
import keras
import numpy as np

Load pretrained keras model

We load a pretrained resnet-50 classification model provided by keras.

if tuple(keras.__version__.split(".")) < ("2", "4", "0"):
    weights_url = "".join(
    weights_file = "resnet50_keras_old.h5"
    weights_url = "".join(
    weights_file = "resnet50_keras_new.h5"

weights_path = download_testdata(weights_url, weights_file, module="keras")
keras_resnet50 = keras.applications.resnet50.ResNet50(
    include_top=True, weights=None, input_shape=(224, 224, 3), classes=1000


File /workspace/.tvm_test_data/keras/resnet50_keras_new.h5 exists, skip.

Load a test image

A single cat dominates the examples!

from PIL import Image
from matplotlib import pyplot as plt
from keras.applications.resnet50 import preprocess_input

img_url = ""
img_path = download_testdata(img_url, "cat.png", module="data")
img =, 224))
# input preprocess
data = np.array(img)[np.newaxis, :].astype("float32")
data = preprocess_input(data).transpose([0, 3, 1, 2])
print("input_1", data.shape)


File /workspace/.tvm_test_data/data/cat.png exists, skip.
input_1 (1, 3, 224, 224)

Compile the model with Relay

convert the keras model(NHWC layout) to Relay format(NCHW layout).

shape_dict = {"input_1": data.shape}
mod, params = relay.frontend.from_keras(keras_resnet50, shape_dict)
# compile the model
target = "cuda"
dev = tvm.cuda(0)
with tvm.transform.PassContext(opt_level=3):
    executor = relay.build_module.create_executor("graph", mod, dev, target)

Execute on TVM

dtype = "float32"
tvm_out = executor.evaluate()(tvm.nd.array(data.astype(dtype)), **params)
top1_tvm = np.argmax(tvm_out.numpy()[0])

Look up synset name

Look up prediction top 1 index in 1000 class synset.

synset_url = "".join(
synset_name = "imagenet1000_clsid_to_human.txt"
synset_path = download_testdata(synset_url, synset_name, module="data")
with open(synset_path) as f:
    synset = eval(
print("Relay top-1 id: {}, class name: {}".format(top1_tvm, synset[top1_tvm]))
# confirm correctness with keras output
keras_out = keras_resnet50.predict(data.transpose([0, 2, 3, 1]))
top1_keras = np.argmax(keras_out)
print("Keras top-1 id: {}, class name: {}".format(top1_keras, synset[top1_keras]))


File /workspace/.tvm_test_data/data/imagenet1000_clsid_to_human.txt exists, skip.
Relay top-1 id: 285, class name: Egyptian cat
Keras top-1 id: 285, class name: Egyptian cat

Gallery generated by Sphinx-Gallery