【CANN训练营第三季】MMDeploy模型推理(AI1S X86云服务器+CANN 6.0RC1.alpha001)

举报
张辉 发表于 2022/12/05 08:06:41 2022/12/05
【摘要】 mmdeploy模型转换与模型推理

一、安装openmmlab的算法库

1、安装mmcls

python -m pip install mmcls -i https://pypi.tuna.tsinghua.edu.cn/simple

cke_73991.png

2、安装mmdet

python -m pip install mmdet -i https://pypi.tuna.tsinghua.edu.cn/simple

cke_74660.png

二、模型转换

1、下载Pytorch原始模型:

cd ~/mmdeploy

python -m mim download mmcls --config resnet18_8xb32_in1k --dest .

python -m mim download mmcls --config resnet50_8xb32_in1k --dest .

python -m mim download mmdet --config faster_rcnn_r50_fpn_1x_coco --dest .

python -m mim download mmdet --config retinanet_r50_fpn_1x_coco --dest .

cke_79300.png

查看下载后的结果:

cke_80724.png

2、准备模型转换脚本

2.1 ResNet18转换脚本

resnet18.sh

python tools/deploy.py configs/mmcls/classification_ascend_static-224x224.py resnet18_8xb32_in1k.py resnet18_8xb32_in1k_20210831-fbbb1da6.pth tests/data/tiger.jpeg --work-dir mmdeploy_models/mmcls/resnet18/cann --device cpu --dump-info

其中文件名跟前面下载下来的文件名相对应(下同)

2.2 ResNet50转换脚本

resnet50.sh

python tools/deploy.py configs/mmcls/classification_ascend_static-224x224.py resnet50_8xb32_in1k.py resnet50_8xb32_in1k_20210831-ea4938fc.pth tests/data/tiger.jpeg --work-dir mmdeploy_models/mmcls/resnet50/cann --device cpu --dump-info

2.3 FasterRCNN转换脚本

faster_rcnn.sh

python tools/deploy.py configs/mmdet/detection/detection_ascend_static-800x1344.py faster_rcnn_r50_fpn_1x_coco.py faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth demo/resources/det.jpg --work-dir mmdeploy_models/mmdet/faster_rcnn/cann --device cpu --dump-info

2.4 RetinaNet转换脚本

retina.sh

python tools/deploy.py configs/mmdet/detection/detection_ascend_static-800x1344.py retinanet_r50_fpn_1x_coco.py retinanet_r50_fpn_1x_coco_20200130-c2398f9e.pth demo/resources/det.jpg --work-dir mmdeploy_models/mmdet/retina/cann --device cpu --dump-info

3、执行模型转换

3.1 ResNet18转换

sh ./resnet18.sh

cke_83482.png

cke_85042.png

3.2 ResNet50转换

sh ./resnet50.sh

cke_103668.png

3.3 FasterRCNN转换

sh ./faster_rcnn.sh

cke_104753.png

如果发现以上报错,需要从github下载det.jpg文件并上传到AI1S服务器:

cke_39163.png

cke_124720.png

重新转换:

sh ./faster_rcnn.sh

cke_729.png

cke_2143.png

cke_15509.png

其中atc花了8分钟左右。

3.4 RetinaNet转换

sh ./retina.sh

cke_16924.png

cke_19133.png

其中atc花了6分钟左右。

4、查看模型转换结果文件

cd ~/mmdeploy/mmdeploy_models/mmcls/resnet18/cann

cd ~/mmdeploy/mmdeploy_models/mmcls/resnet50/cann

cd ~/mmdeploy/mmdeploy_models/mmdet/faster_rcnn/cann

cd ~/mmdeploy/mmdeploy_models/mmdet/retina/cann

cke_24737.png

三、执行模型推理

1、ResNet18模型推理

cd ~/mmdeploy

export PYTHONPATH=$(pwd)/build/lib:$PYTHONPATH

脚本如下:resnet18_inference.py

import cv2
from mmdeploy_python import Classifier

# create a classifer
classifier = Classifier(model_path="mmdeploy_models/mmcls/resnet18/cann",device_name='npu',device_id=0)

#read an image
img = cv2.imread("tests/data/tiger.jpeg")

#person inference
result = classifier(img)

#show result
for label_id, score in result:
        print(label_id,score)

执行推理:python resnet18_inference.py

cke_28723.png

[2022-12-04 21:04:14.205] [mmdeploy] [info] [acl_net.cpp:65] ACL initialized.
[2022-12-04 21:04:14.434] [mmdeploy] [info] [acl_net.cpp:314] n_inputs = 1, dynamic_tensor_index_ = -1
[2022-12-04 21:04:14.434] [mmdeploy] [info] [acl_net.cpp:330] input [1, 3, 224, 224]
[2022-12-04 21:04:14.434] [mmdeploy] [info] [acl_net.cpp:369] Softmax_49:0:output [1, 1000]
[2022-12-04 21:04:14.436] [mmdeploy] [info] [inference.cpp:50] ["img"] <- ["img"]
[2022-12-04 21:04:14.436] [mmdeploy] [info] [inference.cpp:61] ["post_output"] -> ["cls"]
292 0.92626953125
282 0.07257080078125
290 0.0008058547973632812
281 0.00024580955505371094
340 5.65648078918457e-05
[2022-12-04 21:04:15.988] [mmdeploy] [info] [acl_net.cpp:83] ACL finalized.
复制

2、FasterRCNN模型推理

脚本如下:faster_rcnn_inference.py

import cv2
from mmdeploy_python import Detector

#create a detector
detector = Detector(
	model_path = 'mmdeploy_models/mmdet/faster_rcnn/cann',
	device_name='npu',
	device_id=0)

#read an image
img = cv2.imread('demo/resources/det.jpg')
#perform inference
bboxes, labels, _ = detector(img)

#visualize result
for index, (bbox, label_id) in enumerate(zip(bboxes,labels)):
	[left, top, right, bottom],score = bbox[0:4].astype(int),bbox[4]
	if score < 0.3:
		continue
	cv2.rectangle(img, (left,top),(right,bottom),(0,255,0))

cv2.imwrite('faster_rcnn_output_detection.png',img)

执行推理:python faster_rcnn_inference.py

cke_31524.png

推理结果图片已生成:faster_rcnn_output_detection.png

cke_33367.png

3、ResNet50推理

脚本如下:resnet50_inference.py

import cv2
from mmdeploy_python import Classifier

# create a classifer
classifier = Classifier(model_path="mmdeploy_models/mmcls/resnet50/cann",device_name='npu',device_id=0)

#read an image
img = cv2.imread("tests/data/tiger.jpeg")

#person inference
result = classifier(img)

#show result
for label_id, score in result:
        print(label_id,score)

执行推理:python resnet50_inference.py

cke_35414.png

[2022-12-04 21:06:20.570] [mmdeploy] [info] [acl_net.cpp:65] ACL initialized.
[2022-12-04 21:06:20.900] [mmdeploy] [info] [acl_net.cpp:314] n_inputs = 1, dynamic_tensor_index_ = -1
[2022-12-04 21:06:20.900] [mmdeploy] [info] [acl_net.cpp:330] input [1, 3, 224, 224]
[2022-12-04 21:06:20.901] [mmdeploy] [info] [acl_net.cpp:369] Softmax_122:0:output [1, 1000]
[2022-12-04 21:06:20.904] [mmdeploy] [info] [inference.cpp:50] ["img"] <- ["img"]
[2022-12-04 21:06:20.904] [mmdeploy] [info] [inference.cpp:61] ["post_output"] -> ["cls"]
292 0.91845703125
282 0.07904052734375
281 0.0003731250762939453
290 0.00032806396484375
243 0.0001347064971923828
[2022-12-04 21:06:21.998] [mmdeploy] [info] [acl_net.cpp:83] ACL finalized.
复制

4、RetinaNet推理

脚本如下:retina_inference.py

import cv2
from mmdeploy_python import Detector

#create a detector
detector = Detector(
        model_path = 'mmdeploy_models/mmdet/retina/cann',
        device_name='npu',
        device_id=0)

#read an image
img = cv2.imread('demo/resources/det.jpg')
#perform inference
bboxes, labels, _ = detector(img)

#visualize result
for index, (bbox, label_id) in enumerate(zip(bboxes,labels)):
        [left, top, right, bottom],score = bbox[0:4].astype(int),bbox[4]
        if score < 0.3:
                continue
        cv2.rectangle(img, (left,top),(right,bottom),(0,255,0))

cv2.imwrite('retinanet_output_detection.png',img)

执行推理:python retina_inference.py

cke_38478.png

推理结果图片已生成:retinanet_output_detection.png

cke_40606.png

5、查看推理结果图片

cke_53701.png

cke_54497.png

(全文完,谢谢阅读)

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。