mmdetection是由商汤科技和香港中文大学共同开发的一个基于PyTorch的开源深度学习目标检测工具。该工具集成了多种目标检测算法,包括RPN、Fast R-CNN、Faster R-CNN、Mask R-CNN、SSD、RetinaNet以及Cascade R-CNN等。此外,它还支持多种特征提取网络,如ResNet、ResNext、SENet、VGG和HRNet,并且包含了诸如DCN、Group Normalization、Soft-NMS和Generalized Attention等其他特征。
mmdetection曾是目标检测竞赛中的必备工具之一。
mmdetection提供了多个预训练模型,这些模型均基于COCO 2017训练集,并在COCO 2017验证集上进行了测试。训练过程中使用了8块NVIDIA Tesla V100 GPU,每个批次大小为16(每块GPU两张图片)。默认的下载链接可能较慢,用户可以将其改为阿里云镜像,但需注意部分模型可能不支持阿里云镜像。
首先,安装Anaconda。接着,按照以下步骤操作:
创建conda虚拟环境:
bash
conda create -n open-mmlab python=3.7 -y
conda activate open-mmlab
安装PyTorch:
根据CUDA版本选择合适的PyTorch版本。例如,对于CUDA 9.0:
bash
conda install pytorch==1.1.0 torchvision==0.3.0 cudatoolkit=9.0 -c pytorch
下载mmdetection项目:
bash
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
安装mmcv:
如果遇到安装卡顿问题,可能是由于pip源的问题,可以尝试更换源:
bash
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
然后继续安装:
bash
pip install mmcv
python setup.py develop # 或者 "pip install -v -e ."
首先,根据提供的模型下载地址下载预训练模型。然后,使用以下代码进行测试: ```python from mmdet.apis import initdetector, inferencedetector, show_result import mmcv
configfile = 'configs/fasterrcnnr50fpn1x.py' checkpointfile = 'checkpoints/fasterrcnnr50fpn1x_20181010-3d1b3351.pth'
model = initdetector(configfile, checkpoint_file, device='cuda:0')
img = 'test.jpg' result = inferencedetector(model, img) showresult(img, result, model.CLASSES)
showresult(img, result, model.CLASSES, outfile='result.jpg')
video = mmcv.VideoReader('video.mp4') for frame in video: result = inferencedetector(model, frame) showresult(frame, result, model.CLASSES, wait_time=1) ```
数据格式:将标注的数据转换为mmdetection所需的格式。数据格式如下:
json
[
{
'filename': 'a.jpg',
'width': 1280,
'height': 720,
'ann': {
'bboxes': (n, 4),
'labels': (n, ),
'bboxes_ignore': (k, 4), (可选字段)
'labels_ignore': (k, 4) (可选字段)
}
},
...
]
配置文件设置:根据选择的模型在mmdetection/configs/目录下找到对应的配置文件,并根据需求修改参数和网络结构。例如:
python
dataset_type = 'CustomDataset'
data_root = 'path/to/data'
ann_file = 'path/to/annotations.json'
img_prefix = 'path/to/images/'
checkpoint_config = dict(interval=1)
total_epochs = 12
work_dir = 'path/to/save/models'
load_from = 'path/to/pretrained/model.pth'
resume_from = 'path/to/resume/model.pth'
训练模型:
bash
python tools/train.py ${CONFIG_FILE}
bash
./tools/dist_train.sh ${CONFIG_FILE} ${GPU_NUM}
以上步骤可以帮助您在mmdetection框架下进行目标检测任务。