【CANN训练营第三季】MMDeploy转换模型
【摘要】 一、分类模型Resnet转换 模型转换安装openmmlab算法库mmclspip install mmcls使用mim 下载mmcls中的resnet50的模型和ckptmim download mmcls --config resnet50_8xb32_in1k --dest .运行结果Successfully downloaded resnet50_8xb32_in1k_202108...
安装依赖
pip install decorator sympy
一、分类模型Resnet转换
模型转换
安装openmmlab算法库mmcls
pip install mmcls
使用mim 下载mmcls中的resnet50的模型和ckpt
mim download mmcls --config resnet50_8xb32_in1k --dest .
运行结果
Successfully downloaded resnet50_8xb32_in1k_20210831-ea4938fc.pth to /home/HwHiA
Successfully dumped resnet50_8xb32_in1k.py to /home/HwHiAiUser/tools/mmdeploy
模型转换
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
转换后的文件
deploy.py参数详解
模型推理
import cv2
from mmdeploy_python import Classifier
# create a classifer
classifier = Classifier(model_path="mmdeploy_models/mmcls/resnet50/cann",device_name='cpu',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)
运行结果:
292 0.91845703125
282 0.07904052734375
281 0.00037169456481933594
290 0.00032806396484375
243 0.0001347064971923828
二、目标检测模型RetinaNet转换
模型转换
安装openmmlab 算法库mmdet
pip install mmdet
使用mim下载mmdet中的RetinaNet模型和ckpt
mim download mmdet --config retinanet_r50_fpn_1x_coco --dest .
运行结果
Successfully downloaded retinanet_r50_fpn_1x_coco_20200130-c2398f9e.pth to /home/HwHiAiUser/tools/mmdeploy
Successfully dumped retinanet_r50_fpn_1x_coco.py to /home/HwHiAiUser/tools/mmdeploy
模型转换命令
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/retinanet/cann --device cpu --dump-info
模型推理
import cv2
from mmdeploy_python import Detector
#create a detector
detector = Detector(
model_path = 'mmdeploy_models/mmdet/retinanet/cann',
device_name='cpu',
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('output_detection.png',img)
运行脚本
python det_retinanet.py
运行结果
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)