-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add PaddlePaddle tutorial #9124
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
31fe44c
add paddle tutorial
ZiyuanMa 67b3e2d
fix some format issues
ZiyuanMa 727e01b
Merge branch 'apache:main' into main
ZiyuanMa 161ac7e
fix code style
ZiyuanMa 0174ec8
clean
ZiyuanMa a552554
Merge branch 'main' of github.com:ZiyuanMa/tvm
ZiyuanMa bf7cd49
fix format
ZiyuanMa 119dd0d
fix title underline
ZiyuanMa 368630c
PaddlePaddle>=2.1.3
ZiyuanMa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# 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 <https://github.com/ZiyuanMa/>`_ | ||
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]}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PaddlePaddle>=2.1.3