【CANN训练营第三季】MMDeploy模型推理(AI1S X86云服务器+CANN 6.0RC1.alpha001)
一、安装openmmlab的算法库
1、安装mmcls
python -m pip install mmcls -i https://pypi.tuna.tsinghua.edu.cn/simple
2、安装mmdet
python -m pip install mmdet -i https://pypi.tuna.tsinghua.edu.cn/simple
二、模型转换
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 .
查看下载后的结果:
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
3.2 ResNet50转换
sh ./resnet50.sh
3.3 FasterRCNN转换
sh ./faster_rcnn.sh
如果发现以上报错,需要从github下载det.jpg文件并上传到AI1S服务器:
重新转换:
sh ./faster_rcnn.sh
其中atc花了8分钟左右。
3.4 RetinaNet转换
sh ./retina.sh
其中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
三、执行模型推理
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
[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
推理结果图片已生成:faster_rcnn_output_detection.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
[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
推理结果图片已生成:retinanet_output_detection.png
5、查看推理结果图片
(全文完,谢谢阅读)
- 点赞
- 收藏
- 关注作者
评论(0)