.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "how_to/compile_models/from_paddle.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_paddle.py: Compile PaddlePaddle Models =========================== **Author**: `Ziyuan Ma `_ This article is an introductory tutorial to deploy PaddlePaddle models with Relay. For us to begin with, PaddlePaddle>=2.1.3 is required to be installed. A quick solution is .. code-block:: bash pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple or please refer to official site. https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html .. GENERATED FROM PYTHON SOURCE LINES 33-41 .. code-block:: default import tarfile import paddle import numpy as np import tvm from tvm import relay from tvm.contrib.download import download_testdata .. rst-class:: sphx-glr-script-out .. code-block:: none /usr/local/lib/python3.7/dist-packages/paddle/vision/transforms/functional_pil.py:36: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.NEAREST or Dither.NONE instead. 'nearest': Image.NEAREST, /usr/local/lib/python3.7/dist-packages/paddle/vision/transforms/functional_pil.py:37: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead. 'bilinear': Image.BILINEAR, /usr/local/lib/python3.7/dist-packages/paddle/vision/transforms/functional_pil.py:38: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BICUBIC instead. 'bicubic': Image.BICUBIC, /usr/local/lib/python3.7/dist-packages/paddle/vision/transforms/functional_pil.py:39: DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BOX instead. 'box': Image.BOX, /usr/local/lib/python3.7/dist-packages/paddle/vision/transforms/functional_pil.py:40: DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead. 'lanczos': Image.LANCZOS, /usr/local/lib/python3.7/dist-packages/paddle/vision/transforms/functional_pil.py:41: DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.HAMMING instead. 'hamming': Image.HAMMING .. GENERATED FROM PYTHON SOURCE LINES 47-50 Load pretrained ResNet50 model --------------------------------------------- We load a pretrained ResNet50 provided by PaddlePaddle. .. GENERATED FROM PYTHON SOURCE LINES 50-60 .. code-block:: default url = "https://bj.bcebos.com/x2paddle/models/paddle_resnet50.tar" model_path = download_testdata(url, "paddle_resnet50.tar", module="model") with tarfile.open(model_path) as tar: names = tar.getnames() for name in names: tar.extract(name, "./") model = paddle.jit.load("./paddle_resnet50/model") .. rst-class:: sphx-glr-script-out .. code-block:: none /usr/local/lib/python3.7/dist-packages/paddle/fluid/backward.py:1666: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working return list(x) if isinstance(x, collections.Sequence) else [x] .. GENERATED FROM PYTHON SOURCE LINES 61-64 Load a test image --------------------------------------------- A single cat dominates the examples! .. GENERATED FROM PYTHON SOURCE LINES 64-85 .. code-block:: default from PIL import Image import paddle.vision.transforms as T transforms = T.Compose( [ T.Resize((256, 256)), T.CenterCrop(224), T.ToTensor(), T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ] ) 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)) img = transforms(img) img = np.expand_dims(img, axis=0) .. rst-class:: sphx-glr-script-out .. code-block:: none /usr/local/lib/python3.7/dist-packages/paddle/tensor/creation.py:125: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations if data.dtype == np.object: .. GENERATED FROM PYTHON SOURCE LINES 86-88 Compile the model with relay --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 88-98 .. code-block:: default 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 ).evaluate() .. GENERATED FROM PYTHON SOURCE LINES 99-101 Execute on TVM --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 101-104 .. code-block:: default dtype = "float32" tvm_output = executor(tvm.nd.array(img.astype(dtype))).numpy() .. GENERATED FROM PYTHON SOURCE LINES 105-108 Look up synset name --------------------------------------------- Look up prediction top 1 index in 1000 class synset. .. GENERATED FROM PYTHON SOURCE LINES 108-124 .. 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 = f.readlines() top1 = np.argmax(tvm_output[0]) print(f"TVM prediction top-1 id: {top1}, class name: {synset[top1]}") .. rst-class:: sphx-glr-script-out .. code-block:: none TVM prediction top-1 id: 282, class name: 282: 'tiger cat', .. _sphx_glr_download_how_to_compile_models_from_paddle.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_paddle.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: from_paddle.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_