.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "how_to/compile_models/from_coreml.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_how_to_compile_models_from_coreml.py: 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 .. code-block:: bash pip install -U coremltools --user or please refer to official site https://github.com/apple/coremltools .. GENERATED FROM PYTHON SOURCE LINES 37-46 .. code-block:: default import tvm from tvm import te import tvm.relay as relay from tvm.contrib.download import download_testdata import coremltools as cm import numpy as np from PIL import Image .. GENERATED FROM PYTHON SOURCE LINES 52-56 Load pretrained CoreML model ---------------------------- We will download and load a pretrained mobilenet classification network provided by apple in this example .. GENERATED FROM PYTHON SOURCE LINES 56-62 .. code-block:: default model_url = "https://docs-assets.developer.apple.com/coreml/models/MobileNet.mlmodel" 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) .. GENERATED FROM PYTHON SOURCE LINES 63-66 Load a test image ------------------ A single cat dominates the examples! .. GENERATED FROM PYTHON SOURCE LINES 66-73 .. code-block:: default img_url = "https://github.com/dmlc/mxnet.js/blob/main/data/cat.png?raw=true" img_path = download_testdata(img_url, "cat.png", module="data") img = Image.open(img_path).resize((224, 224)) # Mobilenet.mlmodel's input is BGR format img_bgr = np.array(img)[:, :, ::-1] x = np.transpose(img_bgr, (2, 0, 1))[np.newaxis, :] .. GENERATED FROM PYTHON SOURCE LINES 74-77 Compile the model on Relay --------------------------- We should be familiar with the process right now. .. GENERATED FROM PYTHON SOURCE LINES 77-86 .. code-block:: default 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 = relay.build(mod, target, params=params) .. GENERATED FROM PYTHON SOURCE LINES 87-90 Execute on TVM ------------------- The process is no different from other example .. GENERATED FROM PYTHON SOURCE LINES 90-103 .. code-block:: default 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 m.run() # get outputs tvm_output = m.get_output(0) top1 = np.argmax(tvm_output.numpy()[0]) .. GENERATED FROM PYTHON SOURCE LINES 104-107 Look up synset name ------------------- Look up prediction top 1 index in 1000 class synset. .. GENERATED FROM PYTHON SOURCE LINES 107-121 .. code-block:: default synset_url = "".join( [ "https://gist.githubusercontent.com/zhreshold/", "4d0b62f3d01426887599d4f7ede23ee5/raw/", "596b27d23537e5a1b5751d2b0481ef172f58b539/", "imagenet1000_clsid_to_human.txt", ] ) 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(f.read()) # You should see the following result: Top-1 id 282 class name tiger cat print("Top-1 id", top1, "class name", synset[top1]) .. rst-class:: sphx-glr-script-out .. code-block:: none Top-1 id 282 class name tiger cat .. _sphx_glr_download_how_to_compile_models_from_coreml.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: from_coreml.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: from_coreml.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_