mmdetection 学习教程(一)mmdetection安装、最简示例、最简分析


参考:mmdetection-mini
https://www.zybuluo.com/huanghaian/note/1742545
https://github.com/hhaAndroid/mmdetection-mini
https://blog.csdn.net/l7H9JA4/article/details/109152885 yolo转化mmdetection

参考:mmdetection官方
https://zhuanlan.zhihu.com/p/369826931
https://www.bilibili.com/video/BV1Ai4y1P7Yy?p=3

一、mmdetection安装

# 创建环境
conda create -n mmdetection python=3.7 -y
conda activate mmdetection
# 安装pytorch,自选是否安装cudatoolkit,版本参考https://pytorch.org/,本文环境为CUDA11.1、torch1.8
pip3 install torch==1.8.1+cu111 torchvision==0.9.1+cu111 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
# 安装mmcv,版本和网址请参考https://github.com/open-mmlab/mmcv#install-with-pip,本文环境为CUDA11.1、torch1.8
pip install mmcv-full==1.3.3 -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.8.0/index.html
# 下载mmdetection,或者手动下载
git clone git://github.com/open-mmlab/mmdetection.git  # 这里使用https容易报错
cd mmdetection
# 安装mmdetection
pip install -r requirements/build.txt
pip install -v -e .  #  只安装最简单的包, or "python setup.py develop"
pip install -r requirements/optional.txt  #  一些拓展包如albumentations、imagecorruptions
  • 测试各个包是否已经安好
import cv2
import torch,torchvision
print('torch',torch.__version__,torch.cuda.is_available())
import mmdet
print('mmdet',mmdet.__version__)
from mmcv.ops import get_compiling_cuda_version,get_compiler_version
print('get_compiling_cuda_version',get_compiling_cuda_version())
print('get_compiler_version',get_compiler_version())
  • 可选安装的albumentations需要opencv-python-headless,安装后将不能cv2.imshow()。若不安装albumentations和opencv-python-headless则可以正常使用cv2.imshow()
pip list | grep opencv
# 同时安装了opencv-python 和opencv-python-headless则不能使用cv2.imshow()
  • 建议安装albumentations,后续使用PIL或matplotlib查看图片。

二、mmdetection最简单示例

  • 下载预训练模型
mkdir checkpoints
cd checkpoints
wget  http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
  • 写测试文件,demo/easy_demo.py,显示预测结果
# 
from mmdet.apis import init_detector, inference_detector,show_result_pyplot
config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model = init_detector(config_file, checkpoint_file, device=device)
img_path = 'demo/demo.jpg'
result = inference_detector(model, img_path)
show_result_pyplot(model,img_path,result,score_thr=0.9,wait_time=10)
  • 运行
conda activate mmdetection
python demo/easy_demo.py

可以看到预测结果
在这里插入图片描述

三、mmdetection demo最简单分析

  • 分析模型:修改测试文件,demo/easy_demo.py
from mmdet.apis import init_detector, inference_detector,show_result_pyplot
config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model = init_detector(config_file, checkpoint_file, device=device)
for name,module in model.named_children():
    print(name)
    for n, layer in module.named_children():
        print('\t',n)
for name,module in model.named_children():
    print('-'*70,name)
    for n, layer in module.named_children():
        print('-'*10,n)
        print(layer)
# 讲解看https://www.bilibili.com/video/BV1Ai4y1P7Yy?p=3

在这里插入图片描述
其中:
lateral_convs 横向连接卷积,1x1
fpn_convs fpn的卷积,3x3

  • 显示预测结果:在测试文件demo/easy_demo.py末尾添加
img_path = 'demo/demo.jpg'
result = inference_detector(model, img_path)
show_result_pyplot(model,img_path,result,score_thr=0.95,wait_time=0)
print(len(result)) # 类别数
print(result[0].shape) #目标数x5

在这里插入图片描述

  • 显示RPN结果:在测试文件demo/easy_demo.py末尾添加(参考mmdet/models/detectors/rpn.py)
from types import MethodType
def new_simple_test(self, img, img_metas, proposals=None, rescale=False):
    x = self.extract_feat(img)
    proposal_list = self.rpn_head.simple_test_rpn(x, img_metas)
    if rescale:
        for proposals, meta in zip(proposal_list, img_metas):
            proposals[:, :4] /= proposals.new_tensor(meta['scale_factor'])
    return [proposal.cpu().numpy() for proposal in proposal_list]
model.simple_test = MethodType(new_simple_test,model)
rpn_result = inference_detector(model, img_path)
from mmdet.core.visualization import imshow_det_bboxes
import mmcv
import numpy as np
img = mmcv.imread(img_path)
bboxes = np.vstack(rpn_result)
labels = np.zeros(bboxes.shape[0],dtype=int)
imshow_det_bboxes(img,bboxes,labels,None,
        class_names=['']*80,
        score_thr=0.9,
        bbox_color='green',
        thickness=0.5,
        font_size=3,
        show=True,
        wait_time=0.1)

在这里插入图片描述

  • 注:自行修改thickness可改变粗细
  • 注:自行修改wait_time显示时间,pycharm专业版设0.01即可,会一直显示在侧边栏

easy_demo.py的github下载链接欢迎star

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值