How to use
mmdetection is a toolbox with various models for object detection and instance segmentation.
The following models can be used in the same executable format.
This is how to use the object detection function .
I have never used segmentation, but I think it can be used in the same way.
install
pip install -U openmim
mim install mmcv-full
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
pip install -v -e .
inference
Initialize the detector from the config file and weight checkpoint,
specify the input, and run it.
from mmdet.apis import init_detector, inference_detector
import mmcv
# Specify the path to model config and checkpoint file
config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint_file = 'faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# test a single image and show the results
img = 'your_image.jpg' # or img = mmcv.imread(img), which will only load it once
result = inference_detector(model, img)
# visualize the results in a new window
model.show_result(img, result)
# or save the visualization results to image files
model.show_result(img, result, out_file='result.jpg')
training
data set
First, prepare your own dataset in COCO format.
Methods for PascalVOC and intermediate formats are also described in the repository.
train
Create your own config file (which describes the model, settings, and datasets to use) and run the training script.
Logs and checkpoints are saved in work-dir.
python tools/train.py configs/my_config.py --work-dir work_dir/
Config file
The Config file describes all the options necessary for training, such as the model to use, settings, dataset, etc.
Create this with the settings you want.
An example is given below.
# The new config inherits a base config to highlight the necessary modification
_base_ = 'faster_rcnn/faster_rcnn_r50_fpn_1x_coco.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=2)))
# Modify dataset related settings
dataset_type = 'COCODataset'
classes = ('my_class0','my_class1')
data = dict(
train=dict(
img_prefix='my_dataset/images/train/',
classes=classes,
ann_file='my_dataset_train.json'),
val=dict(
img_prefix='my_dataset/images/val/',
classes=classes,
ann_file='my_dataset_val.json'),
test=dict(
img_prefix='my_dataset/images/val/',
classes=classes,
ann_file='my_dataset_val.json'))
load_from = 'faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
runner = dict(type='EpochBasedRunner', max_epochs=300)
Items required in the config file
Rewrite the inheritance source config by writing the following content, but the format of the description differs depending on the model, so make sure to refer to the inheritance source config when writing .
_base_
Set the inheritance source config file that will be the source of your config file.
The inherited config file is located in configs/ of the repository.
The contents written in your own config file will rewrite the items in the inheritance source config file, and for items that are not specifically written, the items in the inheritance source config file will be inherited as is.
In other words, the config file inherits from the base config file.
Furthermore, the original config file (father config) may inherit other config files (grandfather config).
If you want to rewrite the contents of the inherited config file in your own config file (child), you must strictly inherit the description format of the inherited source and change the contents. Therefore, we recommend that you look back at the inherited config.
model
You need to set the number of classes for your dataset in bbox_head here.
dataset_type
Specify the dataset type you want to use. Since we are using the COCO format this time, we have specified COCODataset.
Available dataset formats (including custom ones) are located in the repository’s dataset/ directory.
classes
Enter the class name of your dataset.
data
Specify the data image directory and annotation file path.
load_from
Specifies checkpoints of pretrained weights to use for training.
Checkpoints are set with the inherited model config and can be obtained from the model list in the repository’s ReadMe.
runner
Specify the number of training times (number of epochs) here.
By default, checkpoint files are saved every epoch.
If an error occurs when you start learning, something is wrong in the config file, so trace the inheritance source and resolve it so that it can be inherited correctly.
🐣
I’m a freelance engineer.
Work consultation
Please feel free to contact us with a brief development description.
rockyshikoku@gmail.com
I am creating applications using machine learning and AR technology.
I send machine learning / AR related information.