Skip to content
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 onnx runtime docs #37

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
sources/llamafactory/index.rst
sources/accelerate/index.rst
sources/transformers/index.rst
sources/onnxruntime/index.rst

.. warning::

Expand Down Expand Up @@ -82,11 +83,11 @@
</div>
<div class="flex-grow"></div>
<div class="flex space-x-4 text-blue-600">
<a href="#">官方链接</a>
<a href="https://github.com/microsoft/onnxruntime">官方链接</a>
<span class="split">|</span>
<a href="#">安装指南</a>
<a href="sources/onnxruntime/install.html">安装指南</a>
<span class="split">|</span>
<a href="#">快速上手</a>
<a href="sources/onnxruntime/quick_start.html">快速上手</a>
</div>
</div>
<!-- Card 4 -->
Expand Down
8 changes: 8 additions & 0 deletions sources/onnxruntime/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ONNX Runtime
============

.. toctree::
:maxdepth: 2

install.rst
quick_start.rst
33 changes: 33 additions & 0 deletions sources/onnxruntime/install.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
安装指南
===========

本教程面向使用 ONNX Runtime & Ascend NPU 的开发者,帮助完成昇腾环境下 ONNX Runtime 的安装。

.. note::

阅读本篇前,请确保已按照 :doc:`安装教程 <../ascend/quick_install>` 准备好昇腾环境!

ONNX Runtime 安装
-------------------

ONNX Runtime 目前提供了 源码编译 和 二进制包 两种安装方式,其中二进制包当前只支持Python。

从源码安装
^^^^^^^^^^^^

.. code-block:: shell
:linenos:

# Default path, change it if needed.
source /usr/local/Ascend/ascend-toolkit/set_env.sh

./build.sh --config <Release|Debug|RelWithDebInfo> --build_shared_lib --parallel --use_cann


从pip安装
^^^^^^^^^^^^

.. code-block:: shell
:linenos:

pip3 install onnxruntime-cann
97 changes: 97 additions & 0 deletions sources/onnxruntime/quick_start.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
快速开始
===========

.. note::
阅读本篇前,请确保已按照 :doc:`安装指南 <./install>` 准备好昇腾环境及 ONNX Runtime!

本教程以一个简单的 resnet50 模型为例,讲述如何在 Ascend NPU上使用 ONNX Runtime 进行模型推理。

环境准备
-----------

安装本教程所依赖的额外必要库。

.. code-block:: shell
:linenos:

pip install numpy Pillow onnx

模型准备
-----------

ONNX Runtime 推理需要 ONNX 格式模型作为输入,目前有以下几种主流途径获得 ONNX 模型。

1. 从 `ONNX Model Zoo <https://onnx.ai/models/>`_ 中下载模型。
2. 从 torch、TensorFlow 等框架导出 ONNX 模型。
3. 使用转换工具,完成其他类型到 ONNX 模型的转换。

本教程使用的 resnet50 模型是从 ONNX Model Zoo 中直接下载的,具体的 `下载链接 <https://github.com/onnx/models/blob/main/Computer_Vision/resnet50_Opset16_torch_hub/resnet50_Opset16.onnx>`_

类别标签
-----------

类别标签用于将输出权重转换成人类可读的类别信息,具体的 `下载链接 <https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json>`_

模型推理
-----------

.. code-block:: python
:linenos:

import onnxruntime as ort
import numpy as np
import onnx
from PIL import Image

def preprocess(image_path):
img = Image.open(image_path)
img = img.resize((224, 224))
img = np.array(img).astype(np.float32)

img = np.transpose(img, (2, 0, 1))
img = img / 255.0
mean = np.array([0.485, 0.456, 0.406]).reshape(3, 1, 1)
std = np.array([0.229, 0.224, 0.225]).reshape(3, 1, 1)
img = (img - mean) / std
img = np.expand_dims(img, axis=0)
return img

def inference(model_path, img):
options = ort.SessionOptions()
providers = [
(
"CANNExecutionProvider",
{
"device_id": 0,
"arena_extend_strategy": "kNextPowerOfTwo",
"npu_mem_limit": 2 * 1024 * 1024 * 1024,
"op_select_impl_mode": "high_performance",
"optypelist_for_implmode": "Gelu",
"enable_cann_graph": True
},
),
"CPUExecutionProvider",
]

session = ort.InferenceSession(model_path, sess_options=options, providers=providers)
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name

result = session.run([output_name], {input_name: img})
return result

def display(classes_path, result):
with open(classes_path) as f:
labels = [line.strip() for line in f.readlines()]

pred_idx = np.argmax(result)
print(f'Predicted class: {labels[pred_idx]} ({result[0][0][pred_idx]:.4f})')

if __name__ == '__main__':
model_path = '~/model/resnet/resnet50.onnx'
image_path = '~/model/resnet/cat.jpg'
classes_path = '~/model/resnet/imagenet_classes.txt'

img = preprocess(image_path)
result = inference(model_path, img)
display(classes_path, result)