Compile TFLite Models

Author: Zhao Wu

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

To get started, Flatbuffers and TFLite package needs to be installed as prerequisites. A quick solution is to install Flatbuffers via pip

pip install flatbuffers --user

To install TFlite packages, you could use our prebuilt wheel:

# For python3:
pip3 install -U tflite-1.13.1-py3-none-any.whl --user

# For python2:
pip install -U tflite-1.13.1-py2-none-any.whl --user

or you could generate TFLite package yourself. The steps are the following:

# Get the flatc compiler.
# Please refer to for details
# and make sure it is properly installed.
flatc --version

# Get the TFLite schema.

# Generate TFLite package.
flatc --python schema.fbs

# Add current folder (which contains generated tflite module) to PYTHONPATH.

Now please check if TFLite package is installed successfully, python -c "import tflite"

Below you can find an example on how to compile TFLite model using TVM.

Utils for downloading and extracting zip files

import os

def extract(path):
    import tarfile
    if path.endswith("tgz") or path.endswith("gz"):
        dir_path = os.path.dirname(path)
        tar =
        raise RuntimeError('Could not decompress the file: ' + path)

Load pretrained TFLite model

Load mobilenet V1 TFLite model provided by Google

from import download_testdata

model_url = ""

# Download model tar file and extract it to get mobilenet_v1_1.0_224.tflite
model_path = download_testdata(model_url, "mobilenet_v1_1.0_224.tgz", module=['tf', 'official'])
model_dir = os.path.dirname(model_path)

# Now we can open mobilenet_v1_1.0_224.tflite
tflite_model_file = os.path.join(model_dir, "mobilenet_v1_1.0_224.tflite")
tflite_model_buf = open(tflite_model_file, "rb").read()

# Get TFLite model from buffer
    import tflite
    tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
except AttributeError:
    import tflite.Model
    tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)


File /workspace/.tvm_test_data/tf/official/mobilenet_v1_1.0_224.tgz exists, skip.

Load a test image

A single cat dominates the examples!

from PIL import Image
from matplotlib import pyplot as plt
import numpy as np

image_url = ''
image_path = download_testdata(image_url, 'cat.png', module='data')
resized_image =, 224))
image_data = np.asarray(resized_image).astype("float32")

# Add a dimension to the image so that we have NHWC format layout
image_data = np.expand_dims(image_data, axis=0)

# Preprocess image as described here:
image_data[:, :, :, 0] = 2.0 / 255.0 * image_data[:, :, :, 0] - 1
image_data[:, :, :, 1] = 2.0 / 255.0 * image_data[:, :, :, 1] - 1
image_data[:, :, :, 2] = 2.0 / 255.0 * image_data[:, :, :, 2] - 1
print('input', image_data.shape)


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

Compile the model with relay

# TFLite input tensor name, shape and type
input_tensor = "input"
input_shape = (1, 224, 224, 3)
input_dtype = "float32"

# Parse TFLite model and convert it to a Relay module
from tvm import relay
mod, params = relay.frontend.from_tflite(tflite_model,
                                         shape_dict={input_tensor: input_shape},
                                         dtype_dict={input_tensor: input_dtype})

# Build the module against to x86 CPU
target = "llvm"
with relay.build_config(opt_level=3):
    graph, lib, params =, target, params=params)

Execute on TVM

import tvm
from tvm import te
from tvm.contrib import graph_runtime as runtime

# Create a runtime executor module
module = runtime.create(graph, lib, tvm.cpu())

# Feed input data
module.set_input(input_tensor, tvm.nd.array(image_data))

# Feed related params

# Run

# Get output
tvm_output = module.get_output(0).asnumpy()

Display results

# Load label file
label_file_url = ''.join(['',
label_file = "labels_mobilenet_quant_v1_224.txt"
label_path = download_testdata(label_file_url, label_file, module='data')

# List of 1001 classes
with open(label_path) as f:
    labels = f.readlines()

# Convert result to 1D data
predictions = np.squeeze(tvm_output)

# Get top 1 prediction
prediction = np.argmax(predictions)

# Convert id to class name and show the result
print("The image prediction result is: id " + str(prediction) + " name: " + labels[prediction])


File /workspace/.tvm_test_data/data/labels_mobilenet_quant_v1_224.txt exists, skip.
The image prediction result is: id 283 name: tiger cat

Gallery generated by Sphinx-Gallery