From 31fe44c4d922af96c918c097ac004d0226c39709 Mon Sep 17 00:00:00 2001 From: Ziyuan Ma Date: Sun, 26 Sep 2021 16:20:26 +0800 Subject: [PATCH 1/7] add paddle tutorial --- docs/conf.py | 1 + tutorials/frontend/from_paddle.py | 111 ++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 tutorials/frontend/from_paddle.py diff --git a/docs/conf.py b/docs/conf.py index eaa17abef5de..03e0fae97090 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -257,6 +257,7 @@ def git_describe_version(original_version): "from_coreml.py", "from_darknet.py", "from_caffe2.py", + "from_paddle.py", ], "language": [ "schedule_primitives.py", diff --git a/tutorials/frontend/from_paddle.py b/tutorials/frontend/from_paddle.py new file mode 100644 index 000000000000..8d68ac8a8fec --- /dev/null +++ b/tutorials/frontend/from_paddle.py @@ -0,0 +1,111 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +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.3.1 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 +""" +import tarfile +import paddle +import numpy as np +import tvm +import tvm.relay as relay +from tvm.contrib.download import download_testdata + +###################################################################### +# Load pretrained ResNet50 model +# --------------------------------------------- +# We load a pretrained ResNet50 provided by PaddlePaddle. +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') + +###################################################################### +# Load a test image +# --------------------------------------------- +# A single cat dominates the examples! + +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) + +###################################################################### +# 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 + ).evaluate() + +###################################################################### +# 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( + [ + "https://gist.githubusercontent.com/zhreshold/", + "4d0b62f3d01426887599d4f7ede23ee5/raw/", + "596b27d23537e5a1b5751d2b0481ef172f58b539/", + "imagenet1000_clsid_to_human.txt", + ] +) +synset_name = "imagenet1000_clsid_to_human.txt" +img_path = download_testdata(img_url, "cat.png", module="data") +synset_path = download_testdata(synset_url, synset_name, module="data") +with open(synset_path) as f: + synset = eval(f.read()) + +top1 = np.argmax(tvm_output[0]) +print(f"TVM prediction top-1 id: {top1}, class name: {synset[top1]}") \ No newline at end of file From 67b3e2decbde709daf92e583d824e8f0b4aff67f Mon Sep 17 00:00:00 2001 From: Ziyuan Ma Date: Mon, 27 Sep 2021 11:16:31 +0800 Subject: [PATCH 2/7] fix some format issues --- tutorials/frontend/from_paddle.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tutorials/frontend/from_paddle.py b/tutorials/frontend/from_paddle.py index 8d68ac8a8fec..1f2e564aa7be 100644 --- a/tutorials/frontend/from_paddle.py +++ b/tutorials/frontend/from_paddle.py @@ -37,15 +37,15 @@ # Load pretrained ResNet50 model # --------------------------------------------- # We load a pretrained ResNet50 provided by PaddlePaddle. -url = 'https://bj.bcebos.com/x2paddle/models/paddle_resnet50.tar' +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, './') + tar.extract(name, "./") -model = paddle.jit.load('./paddle_resnet50/model') +model = paddle.jit.load("./paddle_resnet50/model") ###################################################################### # Load a test image @@ -56,11 +56,14 @@ 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])]) +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") From 161ac7ec8beb6c0b92f1b28b86974dc8dcc23ff9 Mon Sep 17 00:00:00 2001 From: Ziyuan Ma Date: Mon, 27 Sep 2021 14:18:32 +0800 Subject: [PATCH 3/7] fix code style --- tutorials/frontend/from_paddle.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tutorials/frontend/from_paddle.py b/tutorials/frontend/from_paddle.py index 1f2e564aa7be..463896d281d5 100644 --- a/tutorials/frontend/from_paddle.py +++ b/tutorials/frontend/from_paddle.py @@ -30,7 +30,7 @@ import paddle import numpy as np import tvm -import tvm.relay as relay +from tvm import relay from tvm.contrib.download import download_testdata ###################################################################### @@ -108,7 +108,7 @@ img_path = download_testdata(img_url, "cat.png", module="data") synset_path = download_testdata(synset_url, synset_name, module="data") with open(synset_path) as f: - synset = eval(f.read()) + synset = f.readlines() top1 = np.argmax(tvm_output[0]) -print(f"TVM prediction top-1 id: {top1}, class name: {synset[top1]}") \ No newline at end of file +print(f"TVM prediction top-1 id: {top1}, class name: {synset[top1]}") From 0174ec831b7f22868349fddde1a8cbd2d7e400b2 Mon Sep 17 00:00:00 2001 From: Ziyuan Ma Date: Mon, 27 Sep 2021 14:29:10 +0800 Subject: [PATCH 4/7] clean --- tutorials/frontend/from_paddle.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tutorials/frontend/from_paddle.py b/tutorials/frontend/from_paddle.py index 463896d281d5..1e4b437e7d0b 100644 --- a/tutorials/frontend/from_paddle.py +++ b/tutorials/frontend/from_paddle.py @@ -105,7 +105,6 @@ ] ) synset_name = "imagenet1000_clsid_to_human.txt" -img_path = download_testdata(img_url, "cat.png", module="data") synset_path = download_testdata(synset_url, synset_name, module="data") with open(synset_path) as f: synset = f.readlines() From bf7cd495dab047414e9a5a5e0fde6122693d19ee Mon Sep 17 00:00:00 2001 From: Ziyuan Ma Date: Mon, 27 Sep 2021 19:21:21 +0800 Subject: [PATCH 5/7] fix format --- tutorials/frontend/from_paddle.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tutorials/frontend/from_paddle.py b/tutorials/frontend/from_paddle.py index 1e4b437e7d0b..9a1637dd4e69 100644 --- a/tutorials/frontend/from_paddle.py +++ b/tutorials/frontend/from_paddle.py @@ -18,11 +18,15 @@ 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.3.1 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 """ From 119dd0da0ba9867f6a3e29e78b0ba8b6dd6236a1 Mon Sep 17 00:00:00 2001 From: Ziyuan Ma Date: Tue, 28 Sep 2021 10:14:05 +0800 Subject: [PATCH 6/7] fix title underline --- tutorials/frontend/from_paddle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/frontend/from_paddle.py b/tutorials/frontend/from_paddle.py index 9a1637dd4e69..89a8a2b186ab 100644 --- a/tutorials/frontend/from_paddle.py +++ b/tutorials/frontend/from_paddle.py @@ -16,7 +16,7 @@ # under the License. """ Compile PaddlePaddle Models -=================== +=========================== **Author**: `Ziyuan Ma `_ This article is an introductory tutorial to deploy PaddlePaddle models with Relay. From 368630c6c27f322c12f52c42cae7284d2e1ae4e8 Mon Sep 17 00:00:00 2001 From: Ziyuan Ma <50344320+ZiyuanMa@users.noreply.github.com> Date: Wed, 29 Sep 2021 17:41:53 +0800 Subject: [PATCH 7/7] PaddlePaddle>=2.1.3 --- tutorials/frontend/from_paddle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/frontend/from_paddle.py b/tutorials/frontend/from_paddle.py index 89a8a2b186ab..9d67cbcdf9ff 100644 --- a/tutorials/frontend/from_paddle.py +++ b/tutorials/frontend/from_paddle.py @@ -20,7 +20,7 @@ **Author**: `Ziyuan Ma `_ This article is an introductory tutorial to deploy PaddlePaddle models with Relay. -For us to begin with, PaddlePaddle>=2.3.1 is required to be installed. +For us to begin with, PaddlePaddle>=2.1.3 is required to be installed. A quick solution is .. code-block:: bash