Compile CoreML Models

Author: Joshua Z. Zhang, Kazutaka Morita, Zhao Wu

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

For us to begin with, coremltools module is required to be installed.

A quick solution is to install via pip

pip install -U coremltools --user

or please refer to official site

import tvm
from tvm import te
import tvm.relay as relay
from import download_testdata
import coremltools as cm
import numpy as np
from PIL import Image

Load pretrained CoreML model

We will download and load a pretrained mobilenet classification network provided by apple in this example

model_url = ""
model_file = "mobilenet.mlmodel"
model_path = download_testdata(model_url, model_file, module="coreml")
# Now you have mobilenet.mlmodel on disk
mlmodel = cm.models.MLModel(model_path)


File /workspace/.tvm_test_data/coreml/mobilenet.mlmodel exists, skip.

Load a test image

A single cat dominates the examples!

img_url = ""
img_path = download_testdata(img_url, "cat.png", module="data")
img =, 224))
# Mobilenet.mlmodel's input is BGR format
img_bgr = np.array(img)[:, :, ::-1]
x = np.transpose(img_bgr, (2, 0, 1))[np.newaxis, :]


File /workspace/.tvm_test_data/data/cat.png exists, skip.

Compile the model on Relay

We should be familiar with the process right now.

target = "llvm"
shape_dict = {"image": x.shape}

# Parse CoreML model and convert into Relay computation graph
mod, params = relay.frontend.from_coreml(mlmodel, shape_dict)

with tvm.transform.PassContext(opt_level=3):
    lib =, target, params=params)

Execute on TVM

The process is no different from other example

from tvm.contrib import graph_executor

dev = tvm.cpu(0)
dtype = "float32"
m = graph_executor.GraphModule(lib["default"](dev))
# set inputs
m.set_input("image", tvm.nd.array(x.astype(dtype)))
# execute
# get outputs
tvm_output = m.get_output(0)
top1 = np.argmax(tvm_output.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(
# You should see the following result: Top-1 id 282 class name tiger cat
print("Top-1 id", top1, "class name", synset[top1])


File /workspace/.tvm_test_data/data/imagenet1000_clsid_to_human.txt exists, skip.
Top-1 id 282 class name tiger cat

Gallery generated by Sphinx-Gallery