From 8946f7d506a8b35a02d04295197a2e66bb68a25d Mon Sep 17 00:00:00 2001 From: Jiawei Li Date: Wed, 17 Jul 2024 16:33:36 +0800 Subject: [PATCH] add onnx runtime docs (#37) --- index.rst | 7 ++- sources/onnxruntime/index.rst | 8 +++ sources/onnxruntime/install.rst | 33 ++++++++++ sources/onnxruntime/quick_start.rst | 97 +++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 sources/onnxruntime/index.rst create mode 100644 sources/onnxruntime/install.rst create mode 100644 sources/onnxruntime/quick_start.rst diff --git a/index.rst b/index.rst index 2b76359..75278b4 100644 --- a/index.rst +++ b/index.rst @@ -22,6 +22,7 @@ sources/llamafactory/index.rst sources/accelerate/index.rst sources/transformers/index.rst + sources/onnxruntime/index.rst .. warning:: @@ -82,11 +83,11 @@
- 官方链接 + 官方链接 | - 安装指南 + 安装指南 | - 快速上手 + 快速上手
diff --git a/sources/onnxruntime/index.rst b/sources/onnxruntime/index.rst new file mode 100644 index 0000000..1108f40 --- /dev/null +++ b/sources/onnxruntime/index.rst @@ -0,0 +1,8 @@ +ONNX Runtime +============ + +.. toctree:: + :maxdepth: 2 + + install.rst + quick_start.rst diff --git a/sources/onnxruntime/install.rst b/sources/onnxruntime/install.rst new file mode 100644 index 0000000..8837bda --- /dev/null +++ b/sources/onnxruntime/install.rst @@ -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 --build_shared_lib --parallel --use_cann + + +从pip安装 +^^^^^^^^^^^^ + +.. code-block:: shell + :linenos: + + pip3 install onnxruntime-cann diff --git a/sources/onnxruntime/quick_start.rst b/sources/onnxruntime/quick_start.rst new file mode 100644 index 0000000..60cf448 --- /dev/null +++ b/sources/onnxruntime/quick_start.rst @@ -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 `_ 中下载模型。 +2. 从 torch、TensorFlow 等框架导出 ONNX 模型。 +3. 使用转换工具,完成其他类型到 ONNX 模型的转换。 + +本教程使用的 resnet50 模型是从 ONNX Model Zoo 中直接下载的,具体的 `下载链接 `_ + +类别标签 +----------- + +类别标签用于将输出权重转换成人类可读的类别信息,具体的 `下载链接 `_ + +模型推理 +----------- + +.. 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)