mmdetection安装、测试、训练

github:https://github.com/open-mmlab/mmdetection

一、配置环境

本机的环境是
Ubuntu 18.04
Cuda 10.2
RTX 2080ti
1、创建虚拟环境

conda create -n open-mmlab python=3.7 -y
conda activate mmdetection

2、安装torch环境

pip install torch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 -i https://mirrors.aliyun.com/pypi/simple/

3、Git克隆mmdetection仓库

git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection

4、安装mmdetection的requirements.txt

pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

5、安装mmcv-full
安装版本参照github官网,我这里用的是1.3.8

pip install mmcv-full==1.3.8 -f https://download.openmmlab.com/mmcv/dist/cu102/torch1.7.0/index.html

6、安装编译mmdetection

python setup.py install # or pip install .

二、测试

1、在官网下载一个rcnn模型进行测试,下载下来是一个.pth的文件。

wget -P /home/cv/mmdetection/checkpoints https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth

2、在mmdetction文件夹中新建一个demo.py文件,指定config文件、模型后使用python 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'
# download the checkpoint from model zoo and put it in `checkpoints/`
# url: https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
device = 'cuda:0'
# init a detector
model = init_detector(config_file, checkpoint_file, device=device)
result = inference_detector(model,'demo/demo.jpg')
show_result_pyplot(model,'demo/demo.jpg', result,score_thr = 0.3,title = 'result',wait_time = 0)
# inference the demo image
#inference_detector(model, 'demo/demo.jpg')

执行完上面的代码,模型加载完后自动弹出检测图,可以看到检出的效果还是不错的。
在这里插入图片描述

三、训练

mmdetection支持主流的数据集格式,但是官网提供的是coco格式的数据集,为了避免折腾我也将自己的数据集转成了coco格式

1、首先自定义数据集需要根据自己数据标签种类修改三个配置文件

  • mmdetection/mmdet/datasets/coco.py #把CLASSES改成自己的数据集
  • mmdetection/mmdet/core/evaluation/class_names.py #把coco_classes改成自己的数据集
  • mmdetection/configs/base/models/cascade_rcnn_r50_fpn.py #把num_classes修改成自己的数据集数量,共三处

2、可以在mmdetection/configs/_base_中修改epoch、学习率、图片大小等参数
在这里插入图片描述
3、在mmdetection文件夹中进行trainning

python tools/train.py configs/faster_rcnn_r50_fpn_1x.py

训练过程中的日志、模型保存在mmdetection/work_dirs中,训练完成后可以选择模型进行测试。

雷坑

1、在安装过程中遇到了很多的坑,尤其是cuda、pytorch版本的问题,cuda 10.2 ,torch 1.7.1, torchvision 0.8.2, torchaudio 0.7.2 是自己在本机经过测试可用的版本,具体的安装、测试、训练过程如上。
2、在修改mmcv、configs文件后需要重新运行pip setup.py install才能够生效,修改完configs的num_classes后一直报错也是这个原因。

推荐

1、一个步骤总结的比较全面的博主
2、一个讲解源码比较详细的博主
3、cascade_rcnn配置文件具体参数解释
4、mmdetection 安装与使用(win10)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值