已合并
[东北大学软件学院][高校贡献][PyTorch离线推理][SKNet50]-初次提交 #318
AtomGit-Bot创建于 2022年5月11日
[东北大学软件学院][高校贡献][PyTorch离线推理][SKNet50]-初次提交 #318
已合并
AtomGit-Bot创建于 2022年5月11日
refs/pull/318/head合入到master
10 个文件变更+207-181
@@ -1,4 +1,4 @@
1-# SK-ResNet50 Onnx 模型端到端推理指导1+# SK-ResNet50 Onnx 模型端到端推理指导
2 2 
3- [1. 模型概述](#1)3- [1. 模型概述](#1)
4 - [论文地址](#11)4 - [论文地址](#11)
@@ -6,6 +6,7 @@
6- [2. 环境说明](#2)6- [2. 环境说明](#2)
7 - [深度学习框架](#21)7 - [深度学习框架](#21)
8 - [python第三方库](#22)8 - [python第三方库](#22)
9+ - [环境导入命令](#23)
9- [3. 模型转换](#3)10- [3. 模型转换](#3)
10 - [pth转onnx模型](#31)11 - [pth转onnx模型](#31)
11- [4. 数据预处理](#4)12- [4. 数据预处理](#4)
@@ -20,6 +21,8 @@
20 - [精度对比](#62)21 - [精度对比](#62)
21- [7. 性能对比](#7)22- [7. 性能对比](#7)
22 - [npu性能数据](#71)23 - [npu性能数据](#71)
24+ - [T4性能数据](#72)
25+ - [性能对比](#73)
23 26 
24## <a name="1">1. 模型概述</a>27## <a name="1">1. 模型概述</a>
25 28 
@@ -40,17 +43,30 @@ commit_id: 2864528f8b83f52c3df76f7c3804aa468b91e5cf
40### <a name="21">2.1 深度学习框架</a>43### <a name="21">2.1 深度学习框架</a>
41 44 
42```45```
43-pytorch == 1.5.046+pytorch == 1.8.2+cpu
44-torchvision == 0.6.047+torchvision == 0.9.2+cpu
45onnx == 1.9.048onnx == 1.9.0
46```49```
47 50 
48### <a name="22">2.2 python第三方库</a>51### <a name="22">2.2 python第三方库</a>
49 52 
50```53```
51-numpy == 1.19.254+numpy == 1.21.6
52-Pillow == 8.2.055+Pillow == 9.1.0
53-opencv-python == 4.5.256+opencv-python == 4.5.5.64
57+sympy == 1.10.1
58+decorator == 5.1.1
59+ 
60+```
61+ 
62+### <a name="23">2.3 环境导入命令</a>
63+ 
64+```
65+pip install sympy
66+pip install decorator
67+pip install onnx==1.9.0
68+pip install torch==1.8.2+cpu torchvision==0.9.2+cpu torchaudio==0.8.2 -f https://download.pytorch.org/whl/lts/1.8/torch_lts.html
69+pip install opencv-python
54```70```
55 71 
56> **说明:**72> **说明:**
@@ -76,14 +92,16 @@ opencv-python == 4.5.2
76 ```bash92 ```bash
77 git clone https://github.com/implus/PytorchInsight.git93 git clone https://github.com/implus/PytorchInsight.git
78 ```94 ```
95+ 仓库代码克隆到Sknet50文件夹下
96+ 仓库代码包含pth权重文件
79 97 
803. 使用 sknet2onnx.py 转换pth为onnx文件,在命令行运行如下指令:983. 使用 sknet2onnx.py 转换pth为onnx文件,在命令行运行如下指令:
81 99 
82 ```bash100 ```bash
83- python3.7 sknet2onnx.py --pth sk_resnet50.pth.tar --onnx sknet50_bs1101+ python sknet2onnx.py --pth sk_resnet50.pth.tar --onnx sk_resnet50.onnx
84 ```102 ```
85 103
86- sk_resnet50.pth.tar文件为步骤1中下载的预训练权重文件,该条指令将在运行处生成一个sknet50_bs1文件,此文件即为目标onnx文件104+ sk_resnet50.pth.tar文件为步骤1中下载的预训练权重文件,该条指令将在运行处生成一个sknet50.onnx文件,此文件即为目标onnx文件
87 105 
88**模型转换要点:**106**模型转换要点:**
89 107 
@@ -96,33 +114,30 @@ opencv-python == 4.5.2
961. 设置 atc 工作所需要的环境变量1141. 设置 atc 工作所需要的环境变量
97 115 
98 ```bash116 ```bash
99- export install_path=/usr/local/Ascend/ascend-toolkit/latest117+ source set_env.sh
Liang Zhen
Liang ZhenLiang Zhen2022年5月18日

/usr/local/Ascend/ascend-toolkit/ 路径下已经存在 set_env.sh 文件了,直接source环境变量就好

likedislike
100- export PATH=/usr/local/python3.7.5/bin:${install_path}/atc/ccec_compiler/bin:${install_path}/atc/bin:$PATH
101- export PYTHONPATH=${install_path}/atc/python/site-packages:$PYTHONPATH
102- export LD_LIBRARY_PATH=${install_path}/atc/lib64:${install_path}/acllib/lib64:$LD_LIBRARY_PATH
103- export ASCEND_OPP_PATH=${install_path}/opp
104 ```118 ```
105 119 
1062. 使用atc工具将onnx模型转换为om模型,命令参考1202. 使用atc工具将onnx模型转换为om模型,命令参考
107 121 
108 ```bash122 ```bash
109- atc --framework=5 --model=sknet50.onnx --output=sknet50_bs1 --input_format=NCHW --input_shape="image:1,3,224,224" --log=debug --soc_version=Ascend310123+ 1batch命令:
124+ atc --framework=5 --model=sk_resnet50.onnx --output=sk_resnet50_bs1_310p --input_format=NCHW --input_shape="image:1,3,224,224" --log=debug --soc_version=Ascend710
110 ```125 ```
111- 126+
112- 命令将在运行路径下生成一个sknet50_1bs.om文件,此文件即为目标om模型文件127+ 1batch命令将在运行路径下生成一个Sk_resnet50_bs1_310p.om文件,此文件即为1batch的om模型文件,其他batch同理
113 128 
114## <a name="4">4. 数据预处理</a>129## <a name="4">4. 数据预处理</a>
115 130 
116### <a name="41">4.1 数据集获取</a>131### <a name="41">4.1 数据集获取</a>
117 132 
118-该模型使用[ImageNet官网](http://www.image-net.org/)的5万张验证集进行测试,图片与标签分别存放在/opt/npu/imagenet/val与/opt/npu/imagenet/val_label.txt。133+该模型使用[ImageNet官网](http://www.image-net.org/)的5万张验证集进行测试,图片与标签分别存放在/opt/npu/imageNet/val与/opt/npu/imageNet/val_label_LSL.txt。
119 134 
120### <a name="42">4.2 数据集预处理</a>135### <a name="42">4.2 数据集预处理</a>
121 136 
Liang Zhen
Liang ZhenLiang Zhen2022年5月11日

使用shell命令for循环

likedislike
122使用 sknet_preprocess.py 脚本进行数据预处理,脚本执行命令:137使用 sknet_preprocess.py 脚本进行数据预处理,脚本执行命令:
123 138 
124```bash139```bash
125-python3.7 sknet_preprocess.py -s /opt/npu/imagenet/val -d ./prep_data140+python sknet_preprocess.py -s /opt/npu/imageNet/val -d ./prep_data
126```141```
127 142 
128### <a name="43">4.3 生成数据集信息文件</a>143### <a name="43">4.3 生成数据集信息文件</a>
@@ -132,7 +147,7 @@ python3.7 sknet_preprocess.py -s /opt/npu/imagenet/val -d ./prep_data
1322. 执行生成数据集信息脚本,生成数据集信息文件1472. 执行生成数据集信息脚本,生成数据集信息文件
133 148 
134 ```bash149 ```bash
135- python3.7 get_info.py bin ./prep_data ./sknet_prep_bin.info 224 224150+ python get_info.py bin ./prep_data ./sknet_prep_bin_lsl.info 224 224
136 ```151 ```
137 152 
138 第一个参数为模型输入的类型,第二个参数为生成的bin文件路径,第三个为输出的info文件,后面为宽高信息153 第一个参数为模型输入的类型,第二个参数为生成的bin文件路径,第三个为输出的info文件,后面为宽高信息
@@ -141,15 +156,18 @@ python3.7 sknet_preprocess.py -s /opt/npu/imagenet/val -d ./prep_data
141 156 
142### <a name="51">5.1 benchmark工具概述</a>157### <a name="51">5.1 benchmark工具概述</a>
143 158 
144-benchmark工具为华为自研的模型推理工具,支持多种模型的离线推理,能够迅速统计出模型在Ascend310上的性能,支持真实数据和纯推理两种模式,配合后处理脚本,可以实现诸多模型的端到端过程,获取工具及使用方法可以参考CANN V100R020C10 推理benchmark工具用户指南 01159+benchmark工具为华为自研的模型推理工具,支持多种模型的离线推理,能够迅速统计出模型在Ascend310p上的性能,支持真实数据和纯推理两种模式,配合后处理脚本,可以实现诸多模型的端到端过程,获取工具及使用方法可以参考CANN V100R020C10 推理benchmark工具用户指南 01
145 160 
146### <a name="52">5.2 离线推理</a>161### <a name="52">5.2 离线推理</a>
147 162 
148```bash163```bash
149-./benchmark.x86_64 -model_type=vision -device_id=0 -batch_size=1 -om_path=sknet50_bs1.om -input_text_path=sknet_prep_bin.info -input_width=224 -input_height=224 -output_binary=False -useDvpp=False164+chmod +x benchmark.x86_64
150-```165+1batch:
166+./benchmark.x86_64 -model_type=vision -device_id=0 -batch_size=1 -om_path=sk_resnet50_bs1_310p.om -input_text_path=sknet_prep_bin_lsl.info -input_width=224 -input_height=224 -output_binary=False -useDvpp=False
151 167 
152-输出结果默认保存在当前目录result/dumpOutput_device{0},模型只有一个名为class的输出,shape为bs * 1000,数据类型为FP32,对应1000个分类的预测结果,每个输入对应的输出对应一个_x.bin文件。168+```
169+首先要添加benchmark.x86_64可执行权限再进行推理
170+输出结果默认保存在当前目录result/dumpOutput_device{0},模型只有一个名为class的输出,shape为bs * 1000,数据类型为FP32,对应1000个分类的预测结果,每个输入对应的输出对应一个_x.bin文件。上面为1batch,其他batch同理
153 171 
154## <a name="6">6. 精度对比</a>172## <a name="6">6. 精度对比</a>
155 173 
@@ -158,13 +176,13 @@ benchmark工具为华为自研的模型推理工具,支持多种模型的离
158后处理统计TopN精度,调用imagenet_acc_eval.py脚本推理结果与label比对,可以获得Accuracy Top5数据,结果保存在result.json中:176后处理统计TopN精度,调用imagenet_acc_eval.py脚本推理结果与label比对,可以获得Accuracy Top5数据,结果保存在result.json中:
159 177 
160```bash178```bash
161-python3.7 vision_metric_ImageNet.py result/dumpOutput_device0/ ../data/sknet/val_label.txt ./ result.json179+python vision_metric_ImageNet.py result/dumpOutput_device0/ /opt/npu/imageNet/val_label_LSL.txt ./ result_bs1.json
162```180```
163 181 
164第一个为benchmark输出目录,第二个为数据集配套标签,第三个是生成文件的保存目录,第四个是生成的文件名。查看输出结果:182第一个为benchmark输出目录,第二个为数据集配套标签,第三个是生成文件的保存目录,第四个是生成的文件名。查看输出结果:
165 183 
166```json184```json
167-{"title": "Overall statistical evaluation", "value": [{"key": "Number of images", "value": "50000"}, {"key": "Number of classes", "value": "1000"}, {"key": "Top1 accuracy", "value": "77.54%"}, {"key": "Top2 accuracy", "value": "87.12%"}, {"key": "Top3 accuracy", "value": "90.73%"}, {"key": "Top4 accuracy", "value": "92.55%"}, {"key": "Top5 accuracy", "value": "93.71%"}]}185+{"title": "Overall statistical evaluation", "value": [{"key": "Number of images", "value": "50000"}, {"key": "Number of classes", "value": "1000"}, {"key": "Top1 accuracy", "value": "77.54%"}, {"key": "Top2 accuracy", "value": "87.12%"}, {"key": "Top3 accuracy", "value": "90.73%"}, {"key": "Top4 accuracy", "value": "92.55%"}, {"key": "Top5 accuracy", "value": "93.70%"}]}
168```186```
169 187 
170经过对bs1与bs16的om测试,本模型batch1的精度与batch16的精度没有差别,精度数据均如上188经过对bs1与bs16的om测试,本模型batch1的精度与batch16的精度没有差别,精度数据均如上
Liang Zhen
Liang ZhenLiang Zhen2022年5月11日

使用shell命令for循环

likedislike
@@ -173,8 +191,8 @@ python3.7 vision_metric_ImageNet.py result/dumpOutput_device0/ ../data/sknet/val
173 191 
174| | TOP1 | TOP5 |192| | TOP1 | TOP5 |
175| :----------------: | :------: | :------: |193| :----------------: | :------: | :------: |
176-| 原github仓库精度 | 77.5380% | 93.7000% |194+| 310精度 | 77.54% | 93.70% |
177-| om模型离线推理精度 | 77.54% | 93.71% |195+| 310p精度 | 77.54% | 93.70% |
178 196 
179将得到的om离线模型推理TopN精度与该模型github代码仓上公布的精度对比,精度下降在1%范围之内,故精度达标。197将得到的om离线模型推理TopN精度与该模型github代码仓上公布的精度对比,精度下降在1%范围之内,故精度达标。
180 198 
@@ -187,83 +205,79 @@ benchmark工具在整个数据集上推理时也会统计性能数据,但是
187benchmark工具作纯推理时使用的命令参考如下:205benchmark工具作纯推理时使用的命令参考如下:
188 206 
189```bash207```bash
190-./benchmark.x86_64 -round=20 -om_path=sknet50_bs1.om -batch_size=1 208+./benchmark.x86_64 -round=20 -om_path=sknet50_1bs.om -batch_size=1
191```209```
192 210 
1931. batch1 性能2111. batch1 性能
194 212 
195 使用benchmark工具在整个数据集上推理时获得的性能数据:213 使用benchmark工具在整个数据集上推理时获得的性能数据:
196 214 
197- ```215+ Interface throughputRate: 812.681 即是batch1 310p单卡吞吐率
198- [e2e] throughputRate: 143.402, latency: 348669
199- [data read] throughputRate: 152.003, moduleLatency: 6.57881
200- [preprocess] throughputRate: 151.416, moduleLatency: 6.60433
201- [infer] throughputRate: 143.733, Interface throughputRate: 210.306, moduleLatency: 6.16176
202- [post] throughputRate: 143.732, moduleLatency: 6.95737
203- ```
204- 
205- Interface throughputRate: 210.306 * 4 = 841.224 即是batch1 310单卡吞吐率
206 216 
2072. batch4 性能2172. batch4 性能
208 218 
209- ```219+ Interface throughputRate: 2062.16 即是batch4 310p单卡吞吐率
210- [INFO] ave_throughputRate: 315.424samples/s, ave_latency: 3.30141ms
211- ```
212- 
213- Interface throughputRate: 315.424 * 4 = 1261.696 即是batch4 310单卡吞吐率
214 220 
2153. batch8 性能2213. batch8 性能
216 222 
217- ```223+ Interface throughputRate: 2174.18 即是batch8 310p单卡吞吐率
218- [INFO] ave_throughputRate: 365.813samples/s, ave_latency: 2.76526ms
219- ```
220- 
221- Interface throughputRate: 365.813 * 4 = 1463.252 即是batch8 310单卡吞吐率
222 224 
2234. batch16 性能2254. batch16 性能
224 226 
225- ```227+ Interface throughputRate: 2041.62 即是batch16 310p单卡吞吐率
226- [e2e] throughputRate: 196.399, latency: 254584
227- [data read] throughputRate: 208.891, moduleLatency: 4.78718
228- [preprocess] throughputRate: 207.779, moduleLatency: 4.81281
229- [infer] throughputRate: 197.514, Interface throughputRate: 392.072, modul
230- [post] throughputRate: 12.3443, moduleLatency: 81.0088
231- ```
232- 
233- Interface throughputRate: 392.072 * 4 = 1568.288 即是batch16 310单卡吞吐率
234 228 
2355. batch32 性能2295. batch32 性能
236 230 
237- ```231+ Interface throughputRate: 1879.74 即是batch32 310p单卡吞吐率
238- [INFO] ave_throughputRate: 376.691samples/s, ave_latency: 2.66319ms
239- ```
240 232 
241- Interface throughputRate: 376.691 * 4 = 1506.764 即是batch32 310单卡吞吐率233+6. batch64 性能
242 234 
243-**性能优化**235+ Interface throughputRate: 1780.68 即是batch64 310p单卡吞吐率
244 236 
245-> 从profiling数据的op_statistic_0_1.csv看出影响性能的是transpose算子,从onnx结构图看出该算子用于实现softmax任意轴,由pytorch导出时引入,然而softmax在onnx中现已支持任意轴,故可直接删除该算子提升性能,删除代码参考如下:237+### <a name="72">7.2 T4性能数据</a>
246 238 
247-```python239+在装有T4卡的服务器上测试gpu性能,测试过程请确保卡没有运行其他任务,TensorRT版本:7.2.3.4,cuda版本:11.0,cudnn版本:8.2
248-model = onnx.load(args.onnx+'.onnx')240+ 
249-graph = model.graph241+```bash
250-node = graph.node242+trtexec --onnx=sk_resnet50.onnx --fp16 --shapes=image:1x3x224x224 --threads
Liang ZhenLiang Zhen
Liang ZhenLiang Zhen2022年5月11日

各batch详细的性能数据结果文件保存归档obs即可,readme中不用体现

likedislike
Liang ZhenLiang Zhen2022年5月11日

各batch详细的性能数据结果文件保存归档obs即可,readme中不用体现

readme中只呈现性能结果就好

likedislike
251-softmax_node_index = []
252-del_group = []
253-for i in range(len(node)):
254- if node[i].op_type == 'Softmax':
255- del_group.append((node[i-1], node[i], node[i+1], i))
256-for g in del_group:
257- new_input = g[0].input
258- new_output = g[2].output
259- new_name = g[1].name
260- new_index = g[3]
261- new_node = onnx.helper.make_node("Softmax", new_input, new_output, new_name, axis=1)
262- for n in g[:-1]:
263- graph.node.remove(n)
264- graph.node.insert(new_index, new_node)
265-onnx.save(model, args.onnx+'.onnx')
266```243```
267 244 
245+gpu T4是4个device并行执行的结果,mean是时延(tensorrt的时延是batch个数据的推理时间),即吞吐率的倒数乘以batch。其中--fp16是算子精度,目前算子精度只测--fp16的。
268 246 
247+1. batch1 性能
269 248 
249+ batch1 t4单卡吞吐率:1000/(2.16149/1)=462.64fps
250+
251+2. batch4 性能
252+ 
253+ batch4 t4单卡吞吐率:1000/(4.43787/4)=895.11fps
254+
255+3. batch8 性能
256+
257+ batch8 t4单卡吞吐率:1000/(7.84424/8)=1019.86fps
258+
259+4. batch16 性能
260+
261+ batch16 t4单卡吞吐率:1000/(15.3644/16)=1041.37fps
262+
263+5. batch32 性能
264+
265+ batch32 t4单卡吞吐率:1000/(26.6422/32)=1201.10fps
266+ 
267+6. batch64 性能
268+
269+ batch64 t4单卡吞吐率:1000/(50.5169/64)=1266.90fps
270+ 
271+### <a name="73">7.3 性能对比</a>
272+ 
273+| | 310 | 310p | T4 | 310p/310 | 310p/T4 |
274+| :-----| :------: | :------: | :------: | :------: | :------: |
275+| bs1 | 723.5 | 812.681 | 462.64 | 1.123263303 | 1.756616376 |
276+| bs4 | 1177.36 | 2062.16 | 895.11 | 1.751511857 | 2.303806236 |
277+| bs8 | 1325.04 | 2174.18 | 1019.86 | 1.640841031 | 2.131841625 |
278+| bs16 | 1357.24 | 2041.62 | 1041.37 | 1.504243907 | 1.960513554 |
279+| bs32 | 1329.876 | 1879.74 | 1201.1 | 1.413470128 | 1.565015403 |
280+| bs64 | 1165.084 | 1780.68 | 1266.9 | 1.528370487 | 1.405541085 |
281+| 最优bs | 1357.24 | 2174.18 | 1266.9 | 1.601912705 | 1.716141763 |
282+ 
283+310P的每个batch的性能都需要达到310的1倍以上,性能达标。
@@ -1,8 +0,0 @@
1-#! /bin/bash
2- 
3-export install_path=/usr/local/Ascend/ascend-toolkit/latest
4-export PATH=/usr/local/python3.7.5/bin:${install_path}/atc/ccec_compiler/bin:${install_path}/atc/bin:$PATH
5-export PYTHONPATH=${install_path}/atc/python/site-packages:$PYTHONPATH
6-export LD_LIBRARY_PATH=${install_path}/atc/lib64:${install_path}/acllib/lib64:$LD_LIBRARY_PATH
7-export ASCEND_OPP_PATH=${install_path}/opp
8-export ASCEND_AICPU_PATH=/usr/local/Ascend/ascend-toolkit/latest
@@ -0,0 +1,4 @@
1+FuncStatus:OK
2+PrecisionStatus:OK
3+AutoTune:OK
4+PerfStatus:POK
@@ -1,6 +1,8 @@
1-torch == 1.5.0
2-torchvision == 0.6.0
3onnx == 1.9.01onnx == 1.9.0
4-numpy == 1.19.22+pytorch == 1.8.2+cpu
5-Pillow == 8.2.03+torchvision == 0.9.2+cpu
6-opencv-python == 4.5.24+numpy == 1.21.6
5+Pillow == 9.1.0
6+opencv-python == 4.5.5.64
7+sympy == 1.10.1
8+decorator == 5.1.1
@@ -0,0 +1,8 @@
1+export LD_LIBRARY_PATH=/usr/local/Ascend_5_0_4/ascend-toolkit/latest/lib64:/usr/local/Ascend_5_0_4/ascend-toolkit/latest/compiler/lib64/plugin/opskernel:/usr/local/Ascend_5_0_4/ascend-toolkit/latest/compiler/lib64/plugin/nnengine:$LD_LIBRARY_PATH
Liang Zhen
Liang ZhenLiang Zhen2022年5月18日

/usr/local/Ascend_5_0_4/ascend-toolkit 已经存在 set_env.sh 文件了,没必要上传这个文件。原有test下面的 env.sh 也要删除

likedislike
2+export PYTHONPATH=/usr/local/Ascend_5_0_4/ascend-toolkit/latest/python/site-packages:/usr/local/Ascend_5_0_4/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe:$PYTHONPATH
3+export PATH=/usr/local/Ascend_5_0_4/ascend-toolkit/latest/bin:/usr/local/Ascend_5_0_4/ascend-toolkit/latest/compiler/ccec_compiler/bin:$PATH
4+export ASCEND_AICPU_PATH=/usr/local/Ascend_5_0_4/ascend-toolkit/latest
5+export ASCEND_OPP_PATH=/usr/local/Ascend_5_0_4/ascend-toolkit/latest/opp
6+export TOOLCHAIN_HOME=/usr/local/Ascend_5_0_4/ascend-toolkit/latest/toolkit
7+export ASCEND_HOME_PATH=/usr/local/Ascend_5_0_4/ascend-toolkit/latest:$ASCEND_HOME_PATH
8+ 
@@ -44,26 +44,6 @@ def main():
44 dynamic_axes = {'image': {0: '-1'}, 'class': {0: '-1'}}44 dynamic_axes = {'image': {0: '-1'}, 'class': {0: '-1'}}
45 dummy_input = torch.randn(1, 3, 224, 224)45 dummy_input = torch.randn(1, 3, 224, 224)
46 torch.onnx.export(model.module, dummy_input, args.onnx, input_names = input_names, dynamic_axes = dynamic_axes, output_names = output_names, opset_version=11, verbose=True)46 torch.onnx.export(model.module, dummy_input, args.onnx, input_names = input_names, dynamic_axes = dynamic_axes, output_names = output_names, opset_version=11, verbose=True)
Liang Zhen
Liang ZhenLiang Zhen2022年5月11日

更建议shell脚本中是用 --onnx sk_resnet50.onnx,然后该python脚本不用修改

likedislike
47-
48- # delete transpose operator to improve performance
49- model = onnx.load(args.onnx)
50- graph = model.graph
51- node = graph.node
52- softmax_node_index = []
53- del_group = []
54- for i in range(len(node)):
55- if node[i].op_type == 'Softmax':
56- del_group.append((node[i-1], node[i], node[i+1], i))
57- for g in del_group:
58- new_input = g[0].input
59- new_output = g[2].output
60- new_name = g[1].name
61- new_index = g[3]
62- new_node = onnx.helper.make_node("Softmax", new_input, new_output, new_name, axis=1)
63- for n in g[:-1]:
64- graph.node.remove(n)
65- graph.node.insert(new_index, new_node)
66- onnx.save(model, args.onnx)
67 47
68if __name__ == '__main__':48if __name__ == '__main__':
69 main()49 main()
@@ -23,4 +23,4 @@ git clone https://github.com/implus/PytorchInsight
23 23 
248.310上执行,执行时确保device空闲 248.310上执行,执行时确保device空闲
25bash test/pth2om.sh 25bash test/pth2om.sh
26-bash test/eval_acc_perf.sh --datasets_path=/root/datasets 26+bash test/eval_acc_perf.sh --datasets_path=/opt/npu
@@ -1,7 +1,6 @@
1#!/bin/bash1#!/bin/bash
2 2 
3-datasets_path="/opt/npu"3+datasets_path="/root/datasets/"
4- 
5for para in $*4for para in $*
6do5do
7 if [[ $para == --datasets_path* ]]; then6 if [[ $para == --datasets_path* ]]; then
@@ -10,80 +9,71 @@ do
10done9done
11 10 
12arch=`uname -m`11arch=`uname -m`
13-# prepare data
14-if [ ! -d "prep_data" ]
15-then
16- python3.7 sknet_preprocess.py -s ${datasets_path}/imagenet/val -d ./prep_data
17- if [ $? != 0 ]; then
18- echo "fail to preprocess data!"
19- exit -1
20- fi
21-else
22- echo "prep_data exists, skip."
23-fi
24 12 
25-# get .info file13+echo "====data preprocess===="
26-if [ ! -f "sknet_prep_bin.info" ]14+rm -rf ./prep_data
27-then15+python sknet_preprocess.py -s ${datasets_path}/imageNet/val -d ./prep_data
28- python3.7 get_info.py bin ./prep_data ./sknet_prep_bin.info 224 224
29- if [ $? != 0 ]; then
30- echo "fail to get info file!"
31- exit -1
32- fi
33-else
34- echo "info file exists, skip."
35-fi
36- 
37-# config environment
38-source env.sh
39- 
40-# run om
41-if [ ! -d "result" ]
42-then
43- ./benchmark.${arch} -model_type=vision -device_id=0 -batch_size=1 -om_path=sknet50_1bs.om -input_text_path=./sknet_prep_bin.info -input_width=224 -input_height=224 -output_binary=False -useDvpp=False
44- if [ $? != 0 ]; then
45- echo "fail to switch 1bs onnx to om!"
46- exit -1
47- fi
48- ./benchmark.${arch} -model_type=vision -device_id=1 -batch_size=16 -om_path=sknet50_16bs.om -input_text_path=./sknet_prep_bin.info -input_width=224 -input_height=224 -output_binary=False -useDvpp=False
49- if [ $? != 0 ]; then
50- echo "fail to switch 16bs onnx to om!"
51- exit -1
52- fi
53-else
54- echo "om running result exists, skip"
55-fi
56- 
57-python3.7 vision_metric_ImageNet.py result/dumpOutput_device0/ ${datasets_path}/imagenet/val_label.txt ./ result_1bs.json
58if [ $? != 0 ]; then16if [ $? != 0 ]; then
59- echo "fail to calculate accurancy of 1bs om!"17+ echo "fail!"
60 exit -118 exit -1
61fi19fi
62-python3.7 vision_metric_ImageNet.py result/dumpOutput_device1/ ${datasets_path}/imagenet/val_label.txt ./ result_16bs.json20+ 
21+rm -rf sknet_prep_bin_lsl.info
22+python get_info.py bin ./prep_data ./sknet_prep_bin_lsl.info 224 224
63if [ $? != 0 ]; then23if [ $? != 0 ]; then
64- echo "fail to calculate accurancy of 16bs om!"24+ echo "fail!"
25+ exit -1
26+fi
27+ 
28+chmod +x benchmark.x86_64
29+chmod +x benchmark.aarch64
30+source set_env.sh
Liang Zhen
Liang ZhenLiang Zhen2022年5月11日

所有的路径都建议规范成默认标准路径

likedislike
31+if [ $? != 0 ]; then
32+ echo "fail!"
33+ exit -1
34+fi
35+ 
36+echo "bs1"
37+echo "====performance data===="
38+rm -rf result/dumpOutput_device0
39+./benchmark.${arch} -model_type=vision -device_id=0 -batch_size=1 -om_path=sk_resnet50_bs1_710.om -input_text_path=sknet_prep_bin_lsl.info -input_width=224 -input_height=224 -output_binary=False -useDvpp=False
40+if [ $? != 0 ]; then
41+ echo "fail!"
65 exit -142 exit -1
66fi43fi
67echo "====accuracy data===="44echo "====accuracy data===="
68-python3.7 test/parse.py result_1bs.json45+rm -rf result_bs1.json
46+python vision_metric_ImageNet.py result/dumpOutput_device0/ ${datasets_path}/imageNet/val_label_LSL.txt ./ result_bs1.json
69if [ $? != 0 ]; then47if [ $? != 0 ]; then
70- echo "fail to show accurancy data of 1bs!"48+ echo "fail!"
71 exit -149 exit -1
72fi50fi
73-python3.7 test/parse.py result_16bs.json51+ 
52+echo "bs8"
53+echo "====performance data===="
54+rm -rf result/dumpOutput_device1
55+./benchmark.${arch} -model_type=vision -device_id=0 -batch_size=8 -om_path=sk_resnet50_bs8_710.om -input_text_path=sknet_prep_bin_lsl.info -input_width=224 -input_height=224 -output_binary=False -useDvpp=False
74if [ $? != 0 ]; then56if [ $? != 0 ]; then
75- echo "fail to show accurancy data of 16bs!"57+ echo "fail!"
76 exit -158 exit -1
77-fiss59+fi
60+echo "====accuracy data===="
61+rm -rf result_bs8.json
62+python vision_metric_ImageNet.py result/dumpOutput_device0/ ${datasets_path}/imageNet/val_label_LSL.txt ./ result_bs8.json
63+if [ $? != 0 ]; then
64+ echo "fail!"
65+ exit -1
66+fi
67+ 
78echo "====performance data===="68echo "====performance data===="
79python test/parse.py result/perf_vision_batchsize_1_device_0.txt69python test/parse.py result/perf_vision_batchsize_1_device_0.txt
80if [ $? != 0 ]; then70if [ $? != 0 ]; then
81- echo "fail to show performance data of 1bs!"71+ echo "fail!"
82 exit -172 exit -1
83fi73fi
84-python3.7 test/parse.py result/perf_vision_batchsize_16_device_1.txt74+python test/parse.py result/perf_vision_batchsize_8_device_0.txt
85if [ $? != 0 ]; then75if [ $? != 0 ]; then
86- echo "fail to show performance data of 16bs!"76+ echo "fail!"
87 exit -177 exit -1
88fi78fi
89echo "success"79echo "success"
@@ -0,0 +1,15 @@
1+#!/bin/bash
2+ 
3+echo "=========bs1========="
4+trtexec --onnx=sk_resnet50.onnx --fp16 --shapes=image:1x3x224x224 --threads
5+if [ $? != 0 ]; then
6+ echo "fail!"
7+ exit -1
8+fi
9+ 
10+echo "=========bs64========="
11+trtexec --onnx=sk_resnet50.onnx --fp16 --shapes=image:64x3x224x224 --threads
12+if [ $? != 0 ]; then
13+ echo "fail!"
14+ exit -1
15+fi
@@ -1,11 +1,32 @@
1-rm -rf *.onnx1+#! /bin/bash
2-python3.7 sknet2onnx.py --pth sk_resnet50.pth.tar --onnx sknet50.onnx2+ 
3-source env.sh3+rm -rf sk_resnet50.onnx
4-rm -rf *.om4+rm -rf sk_resnet50_bs1_710.om
5-atc --framework=5 --model=sknet50.onnx --output=sknet50_1bs --input_format=NCHW --input_shape="image:1,3,224,224" --log=debug --soc_version=Ascend3105+rm -rf sk_resnet50_bs8_710.om #batch8最优
6-atc --framework=5 --model=sknet50.onnx --output=sknet50_16bs --input_format=NCHW --input_shape="image:16,3,224,224" --log=debug --soc_version=Ascend3106+rm -rf fusion_result.json
7-if [ -f "sknet50_1bs.om" ] && [ -f "sknet50_16bs.om" ]; then7+ 
8- echo "Success changing pth to om."8+python sknet2onnx.py --pth sk_resnet50.pth.tar --onnx sk_resnet50
9+if [ $? != 0 ]; then
10+ echo "fail!"
11+ exit -1
12+fi
13+source set_env.sh
Liang Zhen
Liang ZhenLiang Zhen2022年5月11日

这个路径不规范 /usr/local/Ascend_5_0_4/ascend-toolkit/set_env.sh

likedislike
14+if [ $? != 0 ]; then
15+ echo "fail!"
16+ exit -1
17+fi
18+atc --framework=5 --model=sk_resnet50.onnx --output=sk_resnet50_bs1_710 --input_format=NCHW --input_shape="image:1,3,224,224" --log=debug --soc_version=Ascend710
19+if [ $? != 0 ]; then
20+ echo "fail!"
21+ exit -1
22+fi
23+atc --framework=5 --model=sk_resnet50.onnx --output=sk_resnet50_bs8_710 --input_format=NCHW --input_shape="image:8,3,224,224" --log=debug --soc_version=Ascend710
24+if [ $? != 0 ]; then
25+ echo "fail!"
26+ exit -1
27+fi
28+if [ -f "sk_resnet50_bs1_710.om" ] && [ -f "sk_resnet50_bs8_710.om" ]; then
29+ echo "success"
Liang Zhen
Liang ZhenLiang Zhen2022年5月18日

这里建议使用for循环来简化代码

likedislike
9else30else
10- echo "Fail!"31+ echo "fail!"
11fi32fi