MMDetection 快速开始,训练自定义数据集
本文将快速引导使用 MMDetection ,记录了实践中需注意的一些问题。
环境准备
基础环境
- Nvidia 显卡的主机
- Ubuntu 18.04
- 系统安装,可见 制作 USB 启动盘,及系统安装
- Nvidia Driver
- 驱动安装,可见 Ubuntu 初始配置 - Nvidia 驱动
开发环境
下载并安装 Anaconda ,之后于 Terminal 执行:
# 创建 Python 虚拟环境
conda create -n open-mmlab python=3.7 -y
conda activate open-mmlab
# 安装 PyTorch with CUDA
conda install pytorch==1.6.0 torchvision==0.7.0 cudatoolkit=10.2 -c pytorch -y
# 安装 MMCV
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu102/torch1.6.0/index.html
# 安装 MMDetection
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
pip install -r requirements/build.txt
pip install -v -e .
pytorch==1.7.0 时多卡训练会发生问题,需参考此 Issue。命令参考:
conda install pytorch==1.7.0 torchvision==0.8.1 cudatoolkit=10.2 -c pytorch -y
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu102/torch1.7.0/index.html
更多安装方式,可见官方文档:
现有模型进行推断
Faster RCNN
以 R-50-FPN 为例,下载其 model 文件到 mmdetection/checkpoints/。之后,进行推断,
conda activate open-mmlab
cd mmdetection/
python demo/image_demo.py \
demo/demo.jpg \
configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth

现有模型进行测试
准备数据集
下载 COCO 数据集,如下放进 mmdetection/data/coco/ 目录,
mmdetection
├── data
│ ├── coco
│ │ ├── annotations
│ │ ├── train2017
│ │ ├── val2017
│ │ ├── test2017
测试现有模型
cd mmdetection/
# single-gpu testing
python tools/test.py \
configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth \
--out results.pkl \
--eval bbox \
--show
# multi-gpu testing
bash tools/dist_test.sh \
configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth \
2 \
--out results.pkl \
--eval bbox
效果如下,

结果如下,
loading annotations into memory...
Done (t=0.33s)
creating index...
index created!
[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 5000/5000, 15.3 task/s, elapsed: 328s, ETA: 0s
writing results to results.pkl
Evaluating bbox...
Loading and preparing results...
DONE (t=0.89s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=26.17s).
Accumulating evaluation results...
DONE (t=4.10s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.374
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=1000 ] = 0.581
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=1000 ] = 0.404
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.212
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.410
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.481
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.517
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=300 ] = 0.517
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=1000 ] = 0.517
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=1000 ] = 0.326
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=1000 ] = 0.557
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=1000 ] = 0.648
OrderedDict([('bbox_mAP', 0.374), ('bbox_mAP_50', 0.581), ('bbox_mAP_75', 0.404), ('bbox_mAP_s', 0.212), ('bbox_mAP_m', 0.41), ('bbox_mAP_l', 0.481), ('bbox_mAP_copypaste', '0.374 0.581 0.404 0.212 0.410 0.481')])
标准数据集训练模型
准备数据集
同前一节的 COCO 数据集。
准备配置文件
配置文件为 configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py。
需要依照自己的 GPU 情况,修改 lr 学习速率参数,说明如下:
lr=0.005for 2 GPUs * 2 imgs/gpulr=0.01for 4 GPUs * 2 imgs/gpulr=0.02for 8 GPUs and 2 img/gpu (batch size = 8*2 = 16), DEFAULTlr=0.08for 16 GPUs * 4 imgs/gpu
_base_ = [
'../_base_/models/faster_rcnn_r50_fpn.py',
'../_base_/datasets/coco_detection.py',
'../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
]
# optimizer
optimizer = dict(type='SGD', lr=0.005, momentum=0.9, weight_decay=0.0001)
训练模型
cd mmdetection/
# single-gpu training
python tools/train.py \
configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
--work-dir _train
# multi-gpu training
bash ./tools/dist_train.sh \
configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
2 \
--work-dir _train

自定义数据集训练模型
自定义数据集
这里从 Pascal VOC 数据集拿出 cat 作为自定义数据集来演示,
conda activate open-mmlab
# Dataset Management Framework (Datumaro)
pip install 'git+https://github.com/openvinotoolkit/datumaro'
# pip install tensorflow
datum convert --input-format voc --input-path ~/datasets/VOC2012 \
--output-format coco --output-dir ~/datasets/coco_voc2012_cat \
--filter '/item[annotation/label="cat"]'
数据集需要是 COCO 格式,以上直接用 datum 从 VOC 拿出 cat 并转为了 COCO 格式。
准备配置文件
添加 configs/voc_cat/faster_rcnn_r50_fpn_1x_voc_cat.py 配置文件,内容如下:
# The new config inherits a base config to highlight the necessary modification
_base_ = [
'../_base_/models/faster_rcnn_r50_fpn.py',
'../_base_/datasets/coco_detection.py',
'../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
]
# We also need to change the num_classes in head to match the dataset's annotation
model = dict(
roi_head=dict(
bbox_head=dict(num_classes=1)))
# Modify dataset related settings
dataset_type = 'COCODataset'
classes = ('cat',)
data_root = '/home/john/datasets/'
data = dict(
train=dict(
img_prefix=data_root + 'VOC2012/JPEGImages/',
classes=classes,
ann_file=data_root + 'coco_voc2012_cat/annotations/instances_train.json'),
val=dict(
img_prefix=data_root + 'VOC2012/JPEGImages/',
classes=classes,
ann_file=data_root + 'coco_voc2012_cat/annotations/instances_val.json'),
test=dict(
img_prefix=data_root + 'VOC2012/JPEGImages/',
classes=classes,
ann_file=data_root + 'coco_voc2012_cat/annotations/instances_val.json'))
evaluation = dict(interval=100)
# Modify schedule related settings
optimizer = dict(type='SGD', lr=0.005, momentum=0.9, weight_decay=0.0001)
total_epochs = 10000
# Modify runtime related settings
checkpoint_config = dict(interval=10)
# We can use the pre-trained model to obtain higher performance
# load_from = 'checkpoints/*.pth'
model配置num_classes=1为类别数量dataset配置为准备的自定义数据集schedule配置训练的lr及迭代轮次total_epochsruntime可配置checkpoint间隔多少存一个。默认 1 epoch 1 个,空间不够用MMDetection 快速开始,训练自定义数据集的更多相关文章
- Scaled-YOLOv4 快速开始,训练自定义数据集
代码: https://github.com/ikuokuo/start-scaled-yolov4 Scaled-YOLOv4 代码: https://github.com/WongKinYiu/S ...
- [炼丹术]YOLOv5训练自定义数据集
YOLOv5训练自定义数据 一.开始之前的准备工作 克隆 repo 并在Python>=3.6.0环境中安装requirements.txt,包括PyTorch>=1.7.模型和数据集会从 ...
- yolov5训练自定义数据集
yolov5训练自定义数据 step1:参考文献及代码 博客 https://blog.csdn.net/weixin_41868104/article/details/107339535 githu ...
- tensorflow从训练自定义CNN网络模型到Android端部署tflite
网上有很多关于tensorflow lite在安卓端部署的教程,但是大多只讲如何把训练好的模型部署到安卓端,不讲如何训练,而实际上在部署的时候,需要知道训练模型时预处理的细节,这就导致了自己训练的模型 ...
- Tensorflow2 自定义数据集图片完成图片分类任务
对于自定义数据集的图片任务,通用流程一般分为以下几个步骤: Load data Train-Val-Test Build model Transfer Learning 其中大部分精力会花在数据的准备 ...
- torch_13_自定义数据集实战
1.将图片的路径和标签写入csv文件并实现读取 # 创建一个文件,包含image,存放方式:label pokemeon\\mew\\0001.jpg,0 def load_csv(self,file ...
- Yolo训练自定义目标检测
Yolo训练自定义目标检测 参考darknet:https://pjreddie.com/darknet/yolo/ 1. 下载darknet 在 https://github.com/pjreddi ...
- pytorch加载语音类自定义数据集
pytorch对一下常用的公开数据集有很方便的API接口,但是当我们需要使用自己的数据集训练神经网络时,就需要自定义数据集,在pytorch中,提供了一些类,方便我们定义自己的数据集合 torch.u ...
- PyTorch 自定义数据集
准备数据 准备 COCO128 数据集,其是 COCO train2017 前 128 个数据.按 YOLOv5 组织的目录: $ tree ~/datasets/coco128 -L 2 /home ...
随机推荐
- HTTP响应报文应答状态码及含义
本应答报文状态码是老猿结合多方资料收集综合后并加以老猿自己的理解进行说明的应答报文状态码,应该是最新最全解释最详尽的,供大家参考:
- PyQt(Python+Qt)学习随笔:QListView的modelColumn属性及困惑
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.modelColumn介绍 QListView的modelColumn属性用于控制视图中展现mo ...
- python接口测试自动化框架-发送邮件,邮箱报错: 535 Error, authentication failed
1.无意中把腾讯企业邮箱设置为安全登录,接口测试自动化发送邮件,不能被正常接收.错误信息为:535 Error, authentication failed. 原因:认证安全登录后,原来新的邮箱代码传 ...
- 【Alpha冲刺阶段】Scrum Meeting Daily1
1.会议简述 会议开展时间 2020/5/22 8:30-9:00 PM 会议基本内容摘要 讨论了基础的分工,以及明确了各自模块需要完成的任务 参与讨论人员 全体参与 特别说明 会议需要每天都开展!! ...
- Python(三) PIL, Image生成验证图片
Python(三) PIL, Image生成验证图片 安装好PIL,开始使用. 在PyCharm中新建一个文件:PIL_Test1.py 1 # PIL 应用练习 2 # 3 # import PIL ...
- WPF中Logical Tree和Visual Tree的区别
The Logical TreeThe logical tree describes the relations between elements of the user interface. The ...
- Zabbix 新版微信告警-转载
Zabbix 新版微信告警 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信 ...
- 实验楼表关系建立 (课程模块·5张表)
实验楼表关系图 from utils.MyBaseModel import Base from django.db import models # # Create your models here. ...
- dbeaver 驱动安装
一.背景: 在Windows 10 安装dbeaver数据库连接工具,点"测试连接"的时候出现报错如下: Error resolving dependencies Maven ...
- 关于django python manage.py startapp 应用名 出错异常原因
如题,在控制台运行python manage.py startapp sales 建立一个应用报错异常 1.应用名不能包含下划线等字符 所以app-demo 不能作为应用名被定义 2.manage.p ...
- Scaled-YOLOv4 快速开始,训练自定义数据集