MMDeploy学习笔记(一)

踩坑

python tools/train.py configs/ssd/ssdlite_mobilenetv2-scratch_8xb24-600e_coco.py --work-dir work_dirs/ssdlite_mobilenetv2-scratch_8xb24-600e_coco --resume work_dirs/ssdlite_mobilenetv2-scratch_8xb24-600e_coco/epoch_98.pth

一,安装mmdeploy

0.安装miniconda

conda --version
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-4.6.14-Linux-x86_64.sh
chmod 777 Miniconda3-4.6.14-Linux-x86_64.sh
bash Miniconda3-4.6.14-Linux-x86_64.sh
source ~/miniconda3/bin/activate

1.创建虚拟环境

conda create --prefix ./myenv python=3.8
conda activate ./myenv

2.git源码

git clone -b main https://github.com/open-mmlab/mmdeploy.git

3.一键式脚本安装onnxruntime

cd mmdeploy
python3 tools/scripts/build_ubuntu_x64_ort.py

4.安装必要包

conda install pytorch==2.1.0 torchvision cpuonly -c pytorch
pip install -U openmim
mim install mmcv==2.1.0

5.检查安装后退出目录

python3 tools/check_env.py
cd ..

二,安装目标库

1.安装mmdetection

git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
pip install -v -e .

2.验证mmdetection

mim download mmdet --config rtmdet_tiny_8xb32-300e_coco --dest .
python demo/image_demo.py demo/demo.jpg rtmdet_tiny_8xb32-300e_coco.py --weights rtmdet_tiny_8xb32-300e_coco_20220902_112414-78e30dcc.pth --device cpu
cd ..

三,验证mmdeploy

python mmdeploy/tools/deploy.py mmdeploy/configs/mmdet/detection/detection_onnxruntime_dynamic.py mmdetection/rtmdet_tiny_8xb32-300e_coco.py mmdetection/rtmdet_tiny_8xb32-300e_coco_20220902_112414-78e30dcc.pth mmdetection/demo/demo.jpg --work-dir work_dirs/test1 --show --device cpu

四,更新onnxruntime

pip install onnxruntime==1.15.1

tips1.切换jupyter内核

conda install ipykernel
python -m ipykernel install --user --name myenv   

五,模型转换

from mmdeploy.apis import torch2onnx
from mmdeploy.backend.sdk.export_info import export2SDK

img = 'mmdetection/demo/demo.jpg'
work_dir = 'mmdeploy_model/faster-rcnn'
save_file = 'end2end.onnx'
deploy_cfg = 'mmdeploy/configs/mmdet/detection/detection_onnxruntime_static.py'
model_cfg = 'mmdetection/configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py'
model_checkpoint = 'mmdetection/checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
device = 'cpu'

# 1. convert model to onnx
torch2onnx(img, work_dir, save_file, deploy_cfg, model_cfg,
  model_checkpoint, device)

# 2. extract pipeline info for sdk use (dump-info)
export2SDK(deploy_cfg, model_cfg, work_dir, pth=model_checkpoint, device=device)

六, MMDeploy SDK

pip install mmdeploy-runtime==1.3.1
pip install opencv-python

from mmdeploy_runtime import Detector
import cv2

# 读取图片
img = cv2.imread('mmdetection/demo/demo.jpg')

# 创建检测器
detector = Detector(model_path='mmdeploy_model/faster-rcnn', device_name='cpu')
# 执行推理
bboxes, labels, _ = detector(img)
# 使用阈值过滤推理结果,并绘制到原图中
indices = [i for i in range(len(bboxes))]
for index, bbox, label_id in zip(indices, 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值