This article provides a quick guide to using MMDetection, documenting some of the issues to look out for in practice.
Environment to prepare
Based on the environment
- Nvidia graphics card host
- Ubuntu 18.04
- System installation, visible production USB boot disk, and system installation
- Nvidia Driver
- Driver installation, visible Ubuntu initial configuration – Nvidia driver
The development environment
Download and install Anaconda, and then run the following command in Terminal:
Create a Python virtual environmentConda create-n open-mmlab python=3.7 -y conda activate open-mmlabInstall PyTorch with CUDAConda install Pytorch ==1.6.0 TorchVision ==0.7.0 CudatoolKit =10.2 -C Pytorch -y# installation MMCVPIP install MMCV - full - f https://download.openmmlab.com/mmcv/dist/cu102/torch1.6.0/index.html# installation MMDetection
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
pip install -r requirements/build.txt
pip install -v -e .
Copy the code
Pytorch ==1.7.0 multi-card training issues, refer to this Issue. Command Reference:
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.htmlCopy the code
More installation methods, visible official documentation:
- MMDetection – Installation
- MMCV – Installation
Existing models are extrapolated
Faster RCNN
Using r-50-FPN as an example, download its model file to MMDetection /checkpoints/. And then, by inference,
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
Copy the code
Test existing models
Preparing the data set
Download the COCO dataset and place it in the mmDetection /data/ COCO/directory as follows.
├─ data │ ├─ coco │ ├─ annotations │ ├─ train2017 │ ├─ val2017 │ ├─ test2017Copy the code
Testing existing models
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
Copy the code
The effect is as follows,
The results are as follows,
loading annotations into memory... Done (t = 0.33 s) creating the index... index created! [> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >] 5000/5000, 15.3 task/s, elapsed: 328 s, ETA: 0s writing results to results.pkl Evaluating bbox... Loading and preparing results... DONE (t = 0.89 s) creating the index... index created! Running per image evaluation... Evaluate annotationtype* Bbox * DONE (t=26.17s). Accumulating Evaluation Results with conditionals... DONE (t = 4.10 s). The Average Precision (AP) @ [IoU = 0.50:0.95 | area = all | maxDets = 100] = 0.374 Average Precision (AP) @ [ Ious = 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')])
Copy the code
Standard data set training model
Preparing the data set
Same COCO dataset as in the previous section.
Preparing configuration Files
The configuration file is configs/ faster_rCNN/faster_rCNn_R50_fpn_1X_coco-.py.
You need to modify LR learning rate parameters based on your GPU as follows:
Lr = 0.005
for 2 GPUs * 2 imgs/gpuLr = 0.01
for 4 GPUs * 2 imgs/gpuLr = 0.02
for 8 GPUs and 2 img/gpu (batch size = 8*2 = 16), DEFAULTLr = 0.08
for 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)
Copy the code
Training model
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
Copy the code
Custom data set training model
Custom data sets
Here to demonstrate cat as a custom dataset from the Pascal VOC dataset,
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"]'
Copy the code
The data set needs to be in COCO format. Above, take CAT from VOC directly with datum and convert it to COCO format.
Preparing configuration Files
Add the configs/ VOC_cat/faster_rCNn_R50_fpn_1X_VOC_cat. py configuration file as follows:
# 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'
Copy the code
model
configurationnum_classes=1
Is the number of categoriesdataset
Configure a custom data set for preparationschedule
Configured trainedlr
And iteration roundstotal_epochs
runtime
configurablecheckpoint
How many intervals to save one. The default is 1 epoch, and the space is insufficient 😶
The configuration can be overridden with __base__ content, as described in the official documentation.
Training model
# single-gpu training
python tools/train.py \
configs/voc_cat/faster_rcnn_r50_fpn_1x_voc_cat.py \
--work-dir _train_voc_cat
# multi-gpu training
bash ./tools/dist_train.sh \
configs/voc_cat/faster_rcnn_r50_fpn_1x_voc_cat.py \
2 \
--work-dir _train_voc_cat
Copy the code
When the breakpoint is restored,
bash ./tools/dist_train.sh \
configs/voc_cat/faster_rcnn_r50_fpn_1x_voc_cat.py \
2 \
--work-dir _train_voc_cat \
--resume-from _train_voc_cat/epoch_100.pth
Copy the code
ModuleNotFoundError: No module named ‘PycocoTools’
pip uninstall pycocotools mmpycocotools
pip install mmpycocotools
Copy the code
Viewing training Loss
pip install seaborn
python tools/analyze_logs.py plot_curve \
_train_voc_cat/*.log.json \
--keys loss_cls loss_bbox \
--legend loss_cls loss_bbox
Copy the code
See log.json for keys.
The test model
# single-gpu testingpython tools/test.py \ configs/voc_cat/faster_rcnn_r50_fpn_1x_voc_cat.py \ _train_voc_cat/latest.pth \ --out results.pkl \ -eval bbox \
--show
# multi-gpu testing
bash tools/dist_test.sh \
configs/voc_cat/faster_rcnn_r50_fpn_1x_voc_cat.py \
_train_voc_cat/latest.pth \
2 \
--out results.pkl \
--eval bbox
Copy the code
GoCoding personal practice experience sharing, please pay attention to the public account!