文件最后提交记录最后更新时间
!6851 [Clean Code] 修改芯片型号描述 Merge pull request !6851 from 施康/master 1 年前
init 4 年前
fix link validity Co-authored-by: frozenleaves<914814442@qq.com> # message auto-generated for no-merge-commit merge: !7517 merge master into master fix link validity Created-by: frozenn Commit-by: frozenleaves Merged-by: ascend-robot Description: ## Motivation Please describe the motivation of this PR and the goal you want to achieve through this PR. ## Modification Please briefly describe what modification is made in this PR. ## Self-test (Optional) If modifications to this PR may cause/fix function/accuracy/performance DTSs/issues, a self-inspection record needs to be attached. ## BC-breaking (Optional) If there are compatibility issues, such as dependencies on cann/torch_npu versions, they need to be explained in the PR. ## Checklist **Before PR**: - [ ] The new code needs to comply with the Clean Code specification. - [ ] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [ ] CLA has been signed and all committers have signed the CLA in this PR. - [ ] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!75171 个月前
init 4 年前
init 4 年前
init 4 年前
init 4 年前
init 4 年前
README.md

ResNet101 Onnx模型端到端推理指导

1 模型概述

1.1 论文地址

ResNet101论文

1.2 代码地址

ResNet101代码
branch:master commit_id:7d955df73fe0e9b47f7d6c77c699324b256fc41f

2 环境说明

2.1 深度学习框架

CANN 5.0.1

torch == 1.5.1
torchvision == 0.6.1
onnx == 1.9.0

2.2 python第三方库

numpy == 1.19.2
Pillow == 8.2.0
opencv-python == 4.5.2

说明:

X86架构:pytorch,torchvision和onnx可以通过官方下载whl包安装,其它可以通过pip3.7 install 包名 安装

Arm架构:pytorch,torchvision和onnx可以通过源码编译安装,其它可以通过pip3.7 install 包名 安装

3 模型转换

3.1 pth转onnx模型

1.下载pth权重文件
ResNet-101预训练pth权重文件

wget https://download.pytorch.org/models/resnet101-63fe2227.pth

文件MD5sum:b258f8e54abb7de9c960ff19cc662d76

2.ResNet101模型代码在torchvision里,安装torchvision,arm下需源码安装,参考torchvision官网,若安装过程报错请百度解决

git clone https://github.com/pytorch/vision
cd vision
python3.7 setup.py install
cd ..

3.编写pth2onnx脚本resnet101_pth2onnx.py

说明:

注意目前ATC支持的onnx算子版本为11

4.执行pth2onnx脚本,生成onnx模型文件

python3.7 resnet101_pth2onnx.py ./resnet101-63fe2227.pth resnet101.onnx

模型转换要点:

此模型转换为onnx不需要修改开源代码仓代码,故不需要特殊说明

3.2 onnx转om模型

1.设置环境变量

source /usr/local/Ascend/ascend-toolkit/set_env.sh

2.使用atc将onnx模型转换为om模型文件,工具使用方法可以参考CANN V100R020C10 开发辅助工具指南 (推理) 01

atc --framework=5 --model=./resnet101.onnx --output=resnet101_bs1 --input_format=NCHW --input_shape="image:1,3,224,224" --log=debug --soc_version=Ascend310

4 数据集预处理

4.1 数据集获取

该模型使用ImageNet官网的5万张验证集进行测试,图片与标签分别存放在/root/datasets/imagenet/val与/root/datasets/imagenet/val_label.txt。

4.2 数据集预处理

1.预处理脚本imagenet_torch_preprocess.py

2.执行预处理脚本,生成数据集预处理后的bin文件

python3.7 imagenet_torch_preprocess.py resnet /root/datasets/imagenet/val ./prep_dataset

4.3 生成数据集信息文件

1.生成数据集信息文件脚本gen_dataset_info.py

2.执行生成数据集信息脚本,生成数据集信息文件

python3.7 gen_dataset_info.py bin ./prep_dataset ./resnet101_prep_bin.info 224 224

第一个参数为模型输入的类型,第二个参数为生成的bin文件路径,第三个为输出的info文件,后面为宽高信息

5 离线推理

5.1 benchmark工具概述

benchmark工具为华为自研的模型推理工具,支持多种模型的离线推理,获取工具及使用方法可以参考CANN V100R020C10 推理benchmark工具用户指南 01

5.2 离线推理

1.设置环境变量

source /usr/local/Ascend/ascend-toolkit/set_env.sh

2.执行离线推理

./benchmark.x86_64 -model_type=vision -device_id=0 -batch_size=1 -om_path=resnet101_bs1.om -input_text_path=./resnet101_prep_bin.info -input_width=224 -input_height=224 -output_binary=False -useDvpp=False

输出结果默认保存在当前目录result/dumpOutput_device{0},模型只有一个名为class的输出,shape为bs * 1000,数据类型为FP32,对应1000个分类的预测结果,每个输入对应的输出对应一个_x.bin文件。

6 精度对比

6.1 离线推理TopN精度统计

后处理统计TopN精度

调用imagenet_acc_eval.py脚本推理结果与label比对,可以获得Accuracy Top5数据,结果保存在result.json中。

python3.7 imagenet_acc_eval.py result/dumpOutput_device0/ /root/datasets/imagenet/val_label.txt ./ result.json

第一个为benchmark输出目录,第二个为数据集配套标签,第三个是生成文件的保存目录,第四个是生成的文件名。
查看输出结果:

{"title": "Overall statistical evaluation", "value": [{"key": "Number of images", "value": "50000"}, {"key": "Number of classes", "value": "1000"}, {"key": "Top1 accuracy", "value": "77.37%"}, {"key": "Top2 accuracy", "value": "87.1%"}, {"key": "Top3 accuracy", "value": "90.61%"}, {"key": "Top4 accuracy", "value": "92.42%"}, {"key": "Top5 accuracy", "value": "93.54%"}]}

经过对bs1与bs16的om测试,本模型batch1的精度与batch16的精度没有差别,精度数据均如上

6.2 开源TopN精度

torchvision官网精度

Model         Acc@1     Acc@5
ResNet-101    77.374    93.546

6.3 精度对比

将得到的om离线模型推理TopN精度与该模型github代码仓上公布的精度对比,精度下降在1%范围之内,故精度达标。
精度调试:

没有遇到精度不达标的问题,故不需要进行精度调试