|
| 1 | +--- |
| 2 | +sidebar_position: 6 |
| 3 | +--- |
| 4 | + |
| 5 | +# DeepLabv3 完整示例 |
| 6 | + |
| 7 | +此文档使用 CIX P1 NPU SDK 将 [deeplabv3](https://pytorch.org/vision/main/models/generated/torchvision.models.segmentation.deeplabv3_resnet50.html) 移植到 CIX SOC 内部的硬件加速模块实现使用 NPU 推理神经网络模型, |
| 8 | +请在参考此文档前请先在 X86 工作站按照 [安装 NPU SDK](./npu-introduction#安装-npu-sdk) 安装 NOE Compiler,在 X86 工作站与 Orion O6 按照 [下载 CIX AI Model Hub](./ai-hub#下载-cix-ai-model-hub) 文档配置所需环境。 |
| 9 | + |
| 10 | +论文链接: [Rethinking Atrous Convolution for Semantic Image Segmentation](https://arxiv.org/abs/1706.05587) |
| 11 | + |
| 12 | +## DeepLabv3 工程目录列表 |
| 13 | + |
| 14 | +在 CIX AI Model Hub 中包含了 DeepLabv3 的所需文件, 请用户按照 [下载 CIX AI Model Hub](./ai-hub#下载-cix-ai-model-hub) 下载 |
| 15 | + |
| 16 | +```bash |
| 17 | +cd ai_model_hub/models/ComputeVision/Semantic_Segmentation/onnx_deeplab_v3 |
| 18 | +``` |
| 19 | + |
| 20 | +```bash |
| 21 | +. |
| 22 | +├── cfg |
| 23 | +│ └── onnx_deeplab_v3_build.cfg |
| 24 | +├── datasets |
| 25 | +│ └── calibration_data.npy |
| 26 | +├── graph.json |
| 27 | +├── inference_npu.py |
| 28 | +├── inference_onnx.py |
| 29 | +├── ReadMe.md |
| 30 | +├── test_data |
| 31 | +│ └── ILSVRC2012_val_00004704.JPEG |
| 32 | +└── Tutorials.ipynb |
| 33 | +``` |
| 34 | + |
| 35 | +## (可选)下载 CIX 模型 |
| 36 | + |
| 37 | +用户可无需从头编译模型,radxa 提供下载预编译好的 deeplab_v3.cix 模型方法 |
| 38 | + |
| 39 | +- 下载模型 |
| 40 | + ```bash |
| 41 | + wget https://modelscope.cn/models/cix/ai_model_hub_24_Q4/resolve/master/models/ComputeVision/Semantic_Segmentation/onnx_deeplab_v3/deeplab_v3.cix |
| 42 | + ``` |
| 43 | + |
| 44 | +## 编译模型 |
| 45 | + |
| 46 | +### 准备 onnx 模型 |
| 47 | + |
| 48 | +- 下载 onnx 模型 |
| 49 | + |
| 50 | + [deeplabv3_resnet50.onnx](https://modelscope.cn/models/cix/ai_model_hub_24_Q4/resolve/master/models/ComputeVision/Semantic_Segmentation/onnx_deeplab_v3/model/deeplabv3_resnet50.onnx) |
| 51 | + |
| 52 | +- 简化模型 |
| 53 | + |
| 54 | + 这里使用 onnxsim 进行模型输入固化和模型简化 |
| 55 | + |
| 56 | + ```bash |
| 57 | + pip3 install onnxsim onnxruntime |
| 58 | + onnxsim deeplabv3_resnet50.onnx deeplabv3_resnet50-sim.onnx --overwrite-input-shape 1,3,520,520 |
| 59 | + ``` |
| 60 | + |
| 61 | +### 编译模型 |
| 62 | + |
| 63 | +CIX SOC NPU 支持 INT8 计算,在编译模型前,我们需要使用 NOE Compiler 对模型进行 INT8 量化 |
| 64 | + |
| 65 | +- 准备校准集 |
| 66 | + |
| 67 | + - 使用 `datasets` 现有校准集 |
| 68 | + |
| 69 | + ```bash |
| 70 | + . |
| 71 | + └── calibration_data.npy |
| 72 | + ``` |
| 73 | + |
| 74 | + - 自行准备校准集 |
| 75 | + |
| 76 | + 在 `test_data` 目录下已经包含多张校准集的图片文件 |
| 77 | + |
| 78 | + ```bash |
| 79 | + . |
| 80 | + ├── 1.jpeg |
| 81 | + └── 2.jpeg |
| 82 | + ``` |
| 83 | + |
| 84 | + 参考以下脚本生成校准文件 |
| 85 | + |
| 86 | + ```python |
| 87 | + import sys |
| 88 | + import os |
| 89 | + import numpy as np |
| 90 | + _abs_path = os.path.join(os.getcwd(), "../../../../") |
| 91 | + sys.path.append(_abs_path) |
| 92 | + from utils.image_process import preprocess_image_deeplabv3 |
| 93 | + from utils.tools import get_file_list |
| 94 | + # Get a list of images from the provided path |
| 95 | + images_path = "test_data" |
| 96 | + images_list = get_file_list(images_path) |
| 97 | + data = [] |
| 98 | + for image_path in images_list: |
| 99 | + input = preprocess_image_deeplabv3(image_path) |
| 100 | + data.append(input) |
| 101 | + # concat the data and save calib dataset |
| 102 | + data = np.concatenate(data, axis=0) |
| 103 | + np.save("datasets/calib_data_tmp.npy", data) |
| 104 | + print("Generate calib dataset success.") |
| 105 | + ``` |
| 106 | +
|
| 107 | +- 使用 NOE Compiler 量化与编译模型 |
| 108 | +
|
| 109 | + - 制作量化 cfg 配置文件, 请参考以下配置 |
| 110 | +
|
| 111 | + ```bash |
| 112 | + [Common] |
| 113 | + mode = build |
| 114 | +
|
| 115 | + [Parser] |
| 116 | + model_type = onnx |
| 117 | + model_name = deeplab_v3 |
| 118 | + detection_postprocess = |
| 119 | + model_domain = image_segmentation |
| 120 | + input_model = ./deeplabv3_resnet50-sim.onnx |
| 121 | + input = input |
| 122 | + input_shape = [1, 3, 520, 520] |
| 123 | + output = output |
| 124 | + output_dir = ./ |
| 125 | +
|
| 126 | + [Optimizer] |
| 127 | + output_dir = ./ |
| 128 | + calibration_data = ./datasets/calib_data_tmp.npy |
| 129 | + calibration_batch_size = 1 |
| 130 | + metric_batch_size = 1 |
| 131 | + dataset = NumpyDataset |
| 132 | + quantize_method_for_weight = per_channel_symmetric_restricted_range |
| 133 | + quantize_method_for_activation = per_tensor_asymmetric |
| 134 | + save_statistic_info = True |
| 135 | +
|
| 136 | + [GBuilder] |
| 137 | + outputs = deeplab_v3.cix |
| 138 | + target = X2_1204MP3 |
| 139 | + profile = True |
| 140 | + tiling = fps |
| 141 | + ``` |
| 142 | +
|
| 143 | + - 量化模型 |
| 144 | + :::tip |
| 145 | + 如果遇到 cixbuild 报错 `[E] Optimizing model failed! CUDA error: no kernel image is available for execution on the device ...` |
| 146 | + 这意味着当前版本的 torch 不支持此 GPU,请完全卸载当前版本的 torch, 然后在 torch 官网下载最新版本。 |
| 147 | + ::: |
| 148 | + ```bash |
| 149 | + cixbuild ./onnx_deeplab_v3_build.cfg |
| 150 | + ``` |
| 151 | +
|
| 152 | +## 板端部署 |
| 153 | +
|
| 154 | +### NPU 推理 |
| 155 | +
|
| 156 | +将使用 NOE Compiler 编译好的 .cix 格式的模型复制到 Orion O6 开发板上进行模型验证 |
| 157 | +
|
| 158 | +```bash |
| 159 | +python3 inference_npu.py --images ./test_data/ --model_path ./deeplab_v3.ci |
| 160 | +``` |
| 161 | +
|
| 162 | +```bash |
| 163 | +(.venv) radxa@orion-o6:~/NOE/ai_model_hub/models/ComputeVision/Semantic_Segmentation/onnx_deeplab_v3$ time python3 inference_npu.py --images ./test_data/ --model_path ./deeplab_v3.cix |
| 164 | +npu: noe_init_context success |
| 165 | +npu: noe_load_graph success |
| 166 | +Input tensor count is 1. |
| 167 | +Output tensor count is 1. |
| 168 | +npu: noe_create_job success |
| 169 | +save output: noe_ILSVRC2012_val_00004704.JPEG |
| 170 | +npu: noe_clean_job success |
| 171 | +npu: noe_unload_graph success |
| 172 | +npu: noe_deinit_context success |
| 173 | +
|
| 174 | +real 0m9.047s |
| 175 | +user 0m4.314s |
| 176 | +sys 0m0.478s |
| 177 | +``` |
| 178 | +
|
| 179 | +结果保存在 `output` 文件夹中 |
| 180 | +
|
| 181 | + |
| 182 | +
|
| 183 | +### CPU 推理 |
| 184 | +
|
| 185 | +使用 CPU 对 onnx 模型进行推理验证正确性,可在 X86 工作站或 O6 上运行 |
| 186 | +
|
| 187 | +```bash |
| 188 | +python3 inference_onnx.py --images ./test_data/ --onnx_path ./deeplabv3_resnet50-sim.onnx |
| 189 | +``` |
| 190 | +
|
| 191 | +```bash |
| 192 | +(.venv) radxa@orion-o6:~/NOE/ai_model_hub/models/ComputeVision/Semantic_Segmentation/onnx_deeplab_v3$ time python3 inference_onnx.py --images ./test_data/ --onnx_path ./deeplabv3_resnet50-sim.onnx |
| 193 | +save output: onnx_ILSVRC2012_val_00004704.JPEG |
| 194 | +
|
| 195 | +real 0m7.605s |
| 196 | +user 0m33.235s |
| 197 | +sys 0m0.558s |
| 198 | +
|
| 199 | +``` |
| 200 | +
|
| 201 | +结果保存在 `output` 文件夹中 |
| 202 | + |
| 203 | +
|
| 204 | +可以看到 NPU 和 CPU 上推理的结果一致,但运行速度大幅缩短 |
0 commit comments