Compile PaddlePaddle Models

Author: Ziyuan Ma

This article is an introductory tutorial to deploy PaddlePaddle models with Relay. To begin, we’ll install PaddlePaddle>=2.1.3:

pip install paddlepaddle -i

For more details, refer to the official install instructions at:

import tarfile
import paddle
import numpy as np
import tvm
from tvm import relay
from import download_testdata

Load pretrained ResNet50 model

We load a pretrained ResNet50 provided by PaddlePaddle.

url = ""
model_path = download_testdata(url, "paddle_resnet50.tar", module="model")

with as tar:
    names = tar.getnames()
    for name in names:
        tar.extract(name, "./")

model = paddle.jit.load("./paddle_resnet50/model")

Load a test image

A single cat dominates the examples!

from PIL import Image
import as T

transforms = T.Compose(
        T.Resize((256, 256)),
        T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),

img_url = ""
img_path = download_testdata(img_url, "cat.png", module="data")
img =, 224))

img = transforms(img)
img = np.expand_dims(img, axis=0)

Compile the model with relay

target = "llvm"
shape_dict = {"inputs": img.shape}
mod, params = relay.frontend.from_paddle(model, shape_dict)

with tvm.transform.PassContext(opt_level=3):
    executor = relay.build_module.create_executor(
        "graph", mod, tvm.cpu(0), target, params

Execute on TVM

dtype = "float32"
tvm_output = executor(tvm.nd.array(img.astype(dtype))).numpy()

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 = f.readlines()

top1 = np.argmax(tvm_output[0])
print(f"TVM prediction top-1 id: {top1}, class name: {synset[top1]}")
TVM prediction top-1 id: 282, class name:  282: 'tiger cat',

Total running time of the script: ( 1 minutes 4.693 seconds)

Gallery generated by Sphinx-Gallery