已合并
PraNet 模型整改 #3113
AtomGit-Bot创建于 2022年12月20日
PraNet 模型整改 #3113
已合并
AtomGit-Bot创建于 2022年12月20日
refs/pull/3113/head合入到master
3 个文件变更+210-185
MACL_PyTorch/contrib/cv/segmentation/PraNet/PraNet_postprocess.py+1-1
@@ -25,7 +25,7 @@ testsize = 352
25 25 
26def test(pred_dir, save_path, data_path):26def test(pred_dir, save_path, data_path):
27 # 只要res227 # 只要res2
28- bin_images = glob(os.path.join(pred_dir, '*_4.bin'))28+ bin_images = glob(os.path.join(pred_dir, '*_3.bin'))
29 # 必须要排序,因为原模型代码中输入排序了29 # 必须要排序,因为原模型代码中输入排序了
30 bin_images = sorted(bin_images)30 bin_images = sorted(bin_images)
31 31 
MACL_PyTorch/contrib/cv/segmentation/PraNet/README.md+209-123
@@ -1,159 +1,245 @@
1-# PraNet模型PyTorch离线推理指导1+# PraNet模型-推理指导
2 2 
3-## 1 环境准备
4 3 
5-1.安装必要的依赖,测试环境可能已经安装其中的一些不同版本的库了,故手动测试时不推荐使用该命令安装 4+- [概述](#ZH-CN_TOPIC_0000001172161501)
6-```
7-pip3.7 install -r requirements.txt
8-```
9 5 
10-2.获取,安装开源模型代码。6+ - [输入输出数据](#section540883920406)
11 7 
12-```shell8+- [推理环境准备](#ZH-CN_TOPIC_0000001126281702)
13-git clone https://github.com/DengPingFan/PraNet.git -b master
14-cd PraNet
15-git reset --hard f697d5f566a4479f2728ab138401b7476f2f65b9
16-patch -p1 < ../PraNet_perf.diff
17-<!--
18-因开源代码仓使用matlab评测,故需从(https://github.com/plemeri/UACANet)获取pytorch实现的评测脚本eval_functions.py,并将其放在utils目录下
19-将./lib/PraNet_Res2Net.py的res2net50_v1b_26w_4s(pretrained=True)修改为res2net50_v1b_26w_4s(pretrained=False)
20--->
21-cd ..
22-```
23-3.获取权重文件
24-[PraNet训练的pth权重文件](https://drive.google.com/file/d/1pUE99SUQHTLxS9rabLGe_XTDwfS6wXEw/view)
25 9 
26-4.数据集 10+- [快速上手](#ZH-CN_TOPIC_0000001126281700)
27-[kvasir](https://drive.google.com/file/d/1o8OfBvYE6K-EpDyvzsmMPndnUMwb540R/view)获取TestDataset.zip并解压出Kvasir,将其放到/root/datasets/目录下,即/root/datasets/Kvasir
28 11 
29-5.[获取benchmark工具](https://support.huawei.com/enterprise/zh/ascend-computing/cann-pid-251168373/software/) 12+ - [获取源码](#section4622531142816)
30-将benchmark.x86_64或benchmark.aarch64放到当前目录 13+ - [准备数据集](#section183221994411)
14+ - [模型推理](#section741711594517)
31 15 
32-## 2 离线推理 16+- [模型推理性能&精度](#ZH-CN_TOPIC_0000001172201573)
33 17 
34-310p上执行时使用npu-smi info查看设备状态,确保device空闲18+ ******
35 19 
36-```20+# 概述<a name="ZH-CN_TOPIC_0000001172161501"></a>
37-bash test/pth2om.sh 21+ 
38-bash test/eval_acc_perf.sh --datasets_path=/root/datasets/Kvasir22+该网络主要用于分割结肠镜图像中的息肉,而同一类型的息肉具有大小、颜色和纹理的多样性;且息肉与周围粘膜的边界不清晰。为了解决这些挑战,该网络先并行部分解码器(PPD)聚合高级层中的特征,然后,根据组合的特征,生成一个全局地图,作为以下组件的初始指导区域。此外,利用反向注意(RA)模块挖掘边界线索,该模块能够建立区域与边界线索之间的关系。该策略有三个优势,即学习能力更好,泛化能力更好,训练效率更高。
39-```23+ 
40-- 模型转换。24+ 
25+- 参考实现:
26+ 
27+ ```
28+ url=https://github.com/DengPingFan/PraNet.git
29+ commit_id=f697d5f566a4479f2728ab138401b7476f2f65b9
30+ model_name=contrib/cv/segmentation/PraNet
31+ ```
32+ 
33+ 
34+ 
35+## 输入输出数据<a name="section540883920406"></a>
36+ 
37+- 输入数据
38+ 
39+ | 输入数据 | 数据类型 | 大小 | 数据排布格式 |
40+ | -------- | -------- | ------------------------- | ------------ |
41+ | input | RGB_FP32 | batchsize x 3 x 352 x 352 | NCHW |
42+ 
43+ 
44+- 输出数据
45+ 
46+ | 输出数据 | 数据类型 | 大小 | 数据排布格式 |
47+ | -------- | -------- | -------- | ------------ |
48+ | output1 | FLOAT32 | batchsize x 1 x 352 x 352 | NCHW |
49+ 
50+ 
51+ 
52+# 推理环境准备<a name="ZH-CN_TOPIC_0000001126281702"></a>
53+ 
54+- 该模型需要以下插件与驱动
55+ 
56+ **表 1** 版本配套表
57+ 
58+ | 配套 | 版本 | 环境准备指导 |
59+ | ------------------------------------------------------------ | ------- | ------------------------------------------------------------ |
60+ | 固件与驱动 | 22.0.3 | [Pytorch框架推理环境准备](https://www.hiascend.com/document/detail/zh/ModelZoo/pytorchframework/pies) |
61+ | CANN | 6.0.RC1 | - |
62+ | Python | 3.7.5 | - |
63+ | PyTorch | 1.6.0 | - |
64+ | 说明:Atlas 300I Duo 推理卡请以CANN版本选择实际固件与驱动版本。 | \ | \ |
65+ 
66+ 
67+ 
68+# 快速上手<a name="ZH-CN_TOPIC_0000001126281700"></a>
69+ 
70+## 获取源码<a name="section4622531142816"></a>
71+ 
72+1. 获取源码。
73+ 
74+ ```
75+ git clone https://github.com/DengPingFan/PraNet.git -b master
76+ cd PraNet
77+ git reset --hard f697d5f566a4479f2728ab138401b7476f2f65b9
78+ patch -p1 < ../PraNet_perf.diff
79+ cd ..
80+ ```
81+ > **说明**:因开源代码仓使用matlab评测,故需从[https://github.com/plemeri/UACANet](https://github.com/plemeri/UACANet)获取pytorch实现的评测脚本eval_functions.py(在utils目录下),并将其放在utils目录下
82+ 
83+2. 安装依赖。
84+ 
85+ ```
86+ pip3 install -r requirements.txt
87+ ```
88+ 
89+## 准备数据集<a name="section183221994411"></a>
90+ 
91+1. 获取原始数据集。(解压命令参考tar –xvf \*.tar与 unzip \*.zip)
92+ 
93+ 本模型支持[kvasir](https://drive.google.com/file/d/1o8OfBvYE6K-EpDyvzsmMPndnUMwb540R/view),解压后目录结构如下:
94+ 
95+ ```
96+ kvasir
97+ ├── images
98+ └── masks
99+ ```
100+ 
101+2. 数据预处理,将原始数据集转换为模型输入的数据。
102+ 
103+ 执行`PraNet_preprocess.py`脚本,完成预处理。
104+ ```
105+ python3 PraNet_preprocess.py ./Kvasir ./prep_bin
106+ ```
107+ 
108+ 
109+ 
110+## 模型推理<a name="section741711594517"></a>
111+ 
112+1. 模型转换。
41 113 
42 使用PyTorch将模型权重文件.pth转换为.onnx文件,再使用ATC工具将.onnx文件转为离线推理模型文件.om文件。114 使用PyTorch将模型权重文件.pth转换为.onnx文件,再使用ATC工具将.onnx文件转为离线推理模型文件.om文件。
43 115 
44- 导出onnx文件。116+ 1. 获取权重文件。
45 117 
46- 使用pth文件导出onnx文件,运行PraNet_pth2onnx.py脚本。118+ ```
119+ wget https://ascend-repo-modelzoo.obs.cn-east-2.myhuaweicloud.com/model/1_PyTorch_PTH/Pranet/PTH/PraNet-19.pth
120+ ```
47 121 
48- ```122+ 2. 导出onnx文件。
49- python3.7 PraNet_pth2onnx.py ./PraNet-19.pth ./PraNet-19.onnx batch_size
50- ```
51 123 
52- 参数说明:124+ 1. 使用pth文件导出onnx文件,运行PraNet_pth2onnx.py脚本。
53 125 
54- - “./PraNet-19.pth”:输入文件目录126+ 运行PraNet_pth2onnx.py脚本
55- - “./PraNet-19.onnx”:输出文件目录。
56- - "batch_size":指定输出文件batch_size
57 127 
58- 获得“PraNet-19.onnx”文件。128+ ```
129+ python3.7 PraNet_pth2onnx.py ./PraNet-19.pth ./PraNet-19.onnx
130+ ```
59 131 
60- 使用ATC工具将.onnx文件转换为.om文件,导出.onnx模型文件时需设置算子版本为11132+ 获得`PraNet-19.onnx`文件。
61 133 
62- 使用ATC工具将ONNX模型转OM模型。
63 134 
64- 1. 配置环境变量135+ 
136+ 3. 使用ATC工具将ONNX模型转OM模型。
137+ 
138+ 1. 配置环境变量。
139+ 
140+ ```
141+ source /usr/local/Ascend/ascend-toolkit/set_env.sh
142+ ```
143+ 
144+ 2. 执行命令查看芯片名称($\{chip\_name\})。
145+ 
146+ ```
147+ npu-smi info
148+ #该设备芯片名为Ascend310P3 (自行替换)
149+ 回显如下:
150+ +-------------------+-----------------+------------------------------------------------------+
151+ | NPU Name | Health | Power(W) Temp(C) Hugepages-Usage(page) |
152+ | Chip Device | Bus-Id | AICore(%) Memory-Usage(MB) |
153+ +===================+=================+======================================================+
154+ | 0 310P3 | OK | 15.8 42 0 / 0 |
155+ | 0 0 | 0000:82:00.0 | 0 1074 / 21534 |
156+ +===================+=================+======================================================+
157+ | 1 310P3 | OK | 15.4 43 0 / 0 |
158+ | 0 1 | 0000:89:00.0 | 0 1070 / 21534 |
159+ +===================+=================+======================================================+
160+ ```
161+ 
162+ 3. 执行ATC命令。
163+ 
164+ ```
165+ atc --framework=5 --model=PraNet-19bs1.onnx --output=PraNet-19_bs1 --input_format=NCHW --input_shape="actual_input_1:1,3,352,352" --log=error --soc_version=Ascend${chip_name}
166+ ```
167+ 
168+ - 参数说明:
169+ 
170+ - --model:为ONNX模型文件。
171+ - --framework:5代表ONNX模型。
172+ - --output:输出的OM模型。
173+ - --input\_format:输入数据的格式。
174+ - --input\_shape:输入数据的shape。
175+ - --log:日志级别。
176+ - --soc\_version:处理器型号。
177+
178+ 
179+ 运行成功后生成`PraNet-19_bs1.om`模型文件。
180+ 
181+2. 开始推理验证。
182+ 
183+ 1. 使用ais-infer工具进行推理。
184+ 
185+ ais-infer工具获取及使用方式请点击查看[[ais_infer 推理工具使用文档](https://gitee.com/ascend/tools/tree/master/ais-bench_workload/tool/ais_infer)]
186+ 
187+ 2. 执行推理。
188+ 
189+ ```
190+ python3 -m ais_bench --model PraNet-19_bs1.om --input ./prep_bin --output ./ --output_dirname result
191+ ```
192+ 
193+ - 参数说明:
194+ 
195+ - model:om模型
196+ - input:输入数据
197+ - output:输出结果路径
198+ - output_dirname: 输出结果文件夹
199+
200+ 推理后的输出默认在当前目录`result`下。
201+ 
202+ >**说明:**
203+ >执行ais-infer工具请选择与运行环境架构相同的命令。参数详情请参见。
204+ 
205+ 3. 精度验证。
206+ 
207+ 调用`PraNet_postprocess.py`和`Eval.py`脚本,可以获得结果
65 208 
66 ```209 ```
67- source /usr/local/Ascend/ascend-toolkit/set_env.sh210+ python3.7 PraNet_postprocess.py ./Kvasir ./result/ ./bs1_test/Kvasir/
211+ 
212+ python3.7 Eval.py ./ ./bs1_test/Kvasir/ ./result_bs1
68 ```213 ```
69-
70- 2. 执行ATC命令。
71-
72- ${chip_name}可通过`npu-smi info`指令查看,例:310P3
73-
74- ```
75- atc --framework=5 --model=PraNet-19bs1.onnx --output=PraNet-19_bs1 --input_format=NCHW --input_shape="actual_input_1:1,3,352,352" --log=debug --soc_version=Ascend${chip_name}
76- ```
77-
78- 该脚本中环境变量仅供参考,请以实际安装环境配置环境变量。详细介绍请参见《[CANN 开发辅助工具指南 (推理)](https://support.huawei.com/enterprise/zh/ascend-computing/cann-pid-251168373?category=developer-documents&subcategory=auxiliary-development-tools)》。
79 214 
80 - 参数说明:215 - 参数说明:
81- 216+ - ./Kvasir:Kvasir数据路径
82- - --model为ONNX模型文件217+ - ./result推理结果文件
83- - --framework5代表ONNX模型。218+ - ./bs1_test/Kvasir/处理结果文件夹
84- - --output:输出的OM模型。219+ - ./: 当前路径
85- - --input_format输入数据的格式。220+ - ./result_bs1最终精度保存文件夹
86- - --input_shape:输入数据的shape。
87- - --log:日志级别。
88- - --soc_version:处理器型号。
89-
90-
91-
92-- 开始推理验证。
93 221 
94- 222+ 结果会打屏显示
223+ 4. 性能验证。
95 224 
96- 1. 使用Benchmark工具进行推理225+使用ais_infer推理工具的纯推理模式验证不同batch_size的om模型的性能,参考命令如下:
97 226 
98- 执行以下命令增加Benchmark工具可执行权限,并根据OS架构选择工具,如果是X86架构,工具选择benchmark.x86_64,如果是Arm,选择benchmark.aarch64 。
99- 
100- ```
101- chmod u+x benchmark.${arch}
102- ```
103-
104- - 二进制输入
105-
106 ```227 ```
107- ./benchmark.x86_64 -model_type=vision -device_id=0 -batch_size=1 -om_path=./PraNet-19_bs1.om -input_text_path=./pre_bin.info -input_width=352 -input_height=352 -output_binary=True -useDvpp=False228+ python3 -m ais_bench --model=${om_model_path} --loop=20 --batchsize=${batch_size}
108 ```229 ```
109-
110- - 参数说明:
111-
112- - -model_type:模型类型。
113- - -om_path:om文件路径。
114- - -device_id:NPU设备编号。
115- - -batch_size:参数规模。
116- - -input_text_path:图片二进制信息。
117- - -input_width:输入图片宽度。
118- - -input_height:输入图片高度。
119- - -useDvpp:是否使用Dvpp。
120- - -output_binary:输出二进制形式。
121-
122- 推理后的输出默认在当前目录“result”下。
123-
124-
125-
126- 执行./benchmark*.x86_64*工具请选择与运行环境架构相同的命令。参数详情请参见《[CANN 推理benchmark工具用户指南](https://support.huawei.com/enterprise/zh/ascend-computing/cann-pid-251168373?category=developer-documents&subcategory=auxiliary-development-tools)》。
127-
128- 2. 精度验证。
129 230 
130- 调用“PraNet_postprocess.py”和“Eval.py”脚本,可以获得Accuracy据。231+ - 参说明:
232+ - --model:om模型
233+ - --batchsize:模型batchsize
234+ - --loop: 循环次数
131 235 
132- ```
133- python3.7 PraNet_postprocess.py /root/datasets/Kvasir ./result/dumpOutput_device0 ./bs1_test/Kvasir/
134- python3.7 Eval.py /root/datasets/Kvasir ./bs1_test/Kvasir/ ./bs1_test/result_bs1.json
135- ```
136 236 
137- “/root/datasets/Kvasir”:数据集路径。
138 237 
139- “./result/dumpOutput_device0”:推理结果路径。238+# 模型推理性能&精度<a name="ZH-CN_TOPIC_0000001172201573"></a>
140 239 
141- “./bs1_test/Kvasir/”:输出图片路径240+调用ACL接口推理计算,性能参考下列数据
142- 
143- “./bs1_test/result_bs1.json” :输出精度数据路径。
144- 
145- **评测结果:**
146- 
147-| ThroughOutput | 310 | 310P | T4 | 310P/310 | 310P/T4 |
148-| :-----------: | :-----: | :-----: | :-----: | :------: | ------- |
149-| bs1 | 197.998 | 257.267 | 264.634 | 1.304 | 0.973 |
150-| bs4 | 245.410 | 320.093 | 381.105 | 1.305 | 0.839 |
151-| bs8 | 240.005 | 293.541 | 403.570 | 1.289 | 0.727 |
152-| bs16 | 199.660 | 249.195 | 428.659 | 1.247 | 0.581 |
153-| bs32 | 158.888 | 191.656 | 427.253 | 1.206 | 0.448 |
154- 
155-| 模型精度 | 310 | 310P |
156-| :--------: | :-------------------: | :-------------------: |
157-| PraNet bs1 | mDec:0.894;mIoU:0.836 | mDec:0.894;mIoU:0.836 |
158-| PraNet bs4 | mDec:0.894;mIoU:0.836 | mDec:0.894;mIoU:0.836 |
159 241 
242+| 芯片型号 | Batch Size | 数据集 | 精度 | 性能 |
243+| -------- | ---------- | ------ | ---- | ---- |
244+| 310P3 | 1 | Kvasir | mDec:0.894<br>mIoU:0.836 | 170 |
245+| 310P3 | 4 | Kvasir | mDec:0.894<br>mIoU:0.836 | 425 |
DACL_PyTorch/contrib/cv/segmentation/PraNet/get_info.py+0-61
@@ -1,61 +0,0 @@
1-# Copyright 2020 Huawei Technologies Co., Ltd
2-#
3-# Licensed under the Apache License, Version 2.0 (the "License");
4-# you may not use this file except in compliance with the License.
5-# You may obtain a copy of the License at
6-#
7-# http://www.apache.org/licenses/LICENSE-2.0
8-#
9-# Unless required by applicable law or agreed to in writing, software
10-# distributed under the License is distributed on an "AS IS" BASIS,
11-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-# See the License for the specific language governing permissions and
13-# limitations under the License.
14- 
15-import os
16-import sys
17-import cv2
18-from glob import glob
19- 
20- 
21-def get_bin_info(file_path, info_name, width, height):
22- bin_images = glob(os.path.join(file_path, '*.bin'))
23- with open(info_name, 'w') as file:
24- for index, img in enumerate(bin_images):
25- content = ' '.join([str(index), img, width, height])
26- file.write(content)
27- file.write('\n')
28- 
29- 
30-def get_jpg_info(file_path, info_name):
31- extensions = ['jpg', 'jpeg', 'JPG', 'JPEG']
32- image_names = []
33- for extension in extensions:
34- image_names.append(glob(os.path.join(file_path, '*.' + extension)))
35- with open(info_name, 'w') as file:
36- for image_name in image_names:
37- if len(image_name) == 0:
38- continue
39- else:
40- for index, img in enumerate(image_name):
41- img_cv = cv2.imread(img)
42- shape = img_cv.shape
43- width, height = shape[1], shape[0]
44- content = ' '.join([str(index), img, str(width), str(height)])
45- file.write(content)
46- file.write('\n')
47- 
48- 
49-if __name__ == '__main__':
50- file_type = sys.argv[1]
51- file_path = sys.argv[2]
52- info_name = sys.argv[3]
53- if file_type == 'bin':
54- width = sys.argv[4]
55- height = sys.argv[5]
56- assert len(sys.argv) == 6, 'The number of input parameters must be equal to 5'
57- get_bin_info(file_path, info_name, width, height)
58- elif file_type == 'jpg':
59- assert len(sys.argv) == 4, 'The number of input parameters must be equal to 3'
60- get_jpg_info(file_path, info_name)
61-