How To Train An Object Detection Model With Mmdetection - DLology
How To Train An Object Detection Model With Mmdetection - DLology
(6 Comments (/blog/how-to-train-an-object-detection-model-with-mmdetection/#disqus_thread))
A while back you have learned how to train an object detection model with TensorFlow object detection API, and Google Colab's free GPU, if you haven't, check it out in the
post (https://www.dlology.com/blog/how-to-train-an-object-detection-model-easy-for-free/). The models in TensorFlow object detection are quite dated and missing updates for
the state of the art models like Cascade RCNN and RetinaNet. While there is a counterpart for Pytorch similar to that called mmdetection (https://github.com/open-
mmlab/mmdetection) which include more pre-trained state of the art object detection models for us to train custom data with, however setting it up requires a nontrivial amount
of time spent on installing the environment, setting up the config file, and dataset in the right format. The good news is you can skip those boring stuff and jump directly into
the fun part to train your model.
If you took your images by your phone, the image resolution might be 2K or 4K depends on your phone's setting. In that case, we will scale down the image for reduced
overall dataset size, and faster training speed.
https://www.dlology.com/blog/how-to-train-an-object-detection-model-with-mmdetection/ 1/7
8/6/2020 How to train an object detection model with mmdetection | DLology
You can use the resize_images.py (https://github.com/Tony607/mmdetection_object_detection_demo/blob/master/resize_images.py) script in the repository to resize your
images.
First, save all your photos to one folder outside of the project directory so they won't get accidentally uploaded to GitHub later. Ideally, all photo came with jpg extension.
Then run this script to resize all photos, and save them to the project directory.
python resize_images.py --raw-dir <photo_directory> --save-dir ./data/VOCdevkit/VOC2007/ImageSets --ext jpg --target-size "(800, 600)"
You might wonder why "VOC" in the path, that is because of the annotation tool we use generates Pascal VOC (http://host.robots.ox.ac.uk/pascal/VOC/) formatted annotation
XML files. It is not necessary to dig into the actual format of the XML file since the annotation tool handles all of that. You guessed it, that is the same tool we use previously
(https://www.dlology.com/blog/how-to-train-an-object-detection-model-easy-for-free/), LabelImg (https://tzutalin.github.io/labelImg/), works both on Windows and Linux.
2. Open your resized image folder " ./data/VOCdevkit/VOC2007/ImageSets " for annotation.
3. Change save directory for the XML annotation files to " ./data/VOCdevkit/VOC2007/Annotations ".
As usual, use shortcuts ( w : draw box, d : next file, a : previous file, etc.) to accelerate the annotation.
Once it is done, you will find those XML files located in " ./data/VOCdevkit/VOC2007/Annotations " folder with the same file base names as your image files.
For the train/test split, you are going to create two files, each one containing a list of file base names, one name per line. Those two text files will be located in the folder
" data/VOC2007/ImageSets/Main " named trainval.txt and test.txt respectively. If you don't want to type all the file name by hand, try cd into the " Annotations " directory
and run the shell,
That will give you a list of nicely sorted file base names, just split them into two parts, and paste into those two text files.
Now you have the data directory structure similar to this one below.
https://www.dlology.com/blog/how-to-train-an-object-detection-model-with-mmdetection/ 2/7
8/6/2020 How to train an object detection model with mmdetection | DLology
data
└── VOC2007
├── Annotations
│ ├── 0.xml
│ ├── ...
│ └── 9.xml
├── ImageSets
│ └── Main
│ ├── test.txt
│ └── trainval.txt
└── JPEGImages
├── 0.jpg
├── ...
└── 9.jpg
Update your fork of the GitHub repository (https://github.com/Tony607/mmdetection_object_detection_demo) with your labeled datasets so you can clone it with Colab.
The notebook allows you to select the model config and set the number of training epochs.
Right now, I only tested with two model configs, faster_rcnn_r50_fpn_1x, and cascade_rcnn_r50_fpn_1x, while other configs can be incorporated as demonstrated in the
notebook.
After that, it will re-run the mmdetection package installing script so the changes to the voc.py file will be updated to the system python packages.
%cd {mmdetection_dir}
!python setup.py install
Since your data directory resides outside of the mmdetection directory, we have the following cell in the notebook which creates a symbolic link into the project data directory.
os.makedirs("data/VOCdevkit", exist_ok=True)
voc2007_dir = os.path.join(project_name, "data/VOC2007")
os.system("ln -s {} data/VOCdevkit".format(voc2007_dir))
The training time depends on the size of your datasets and number of training epochs, my demo takes several minutes to complete with Colab's Tesla T4 GPU.
After training, you can test drive the model with an image in the test set like so.
https://www.dlology.com/blog/how-to-train-an-object-detection-model-with-mmdetection/ 3/7
8/6/2020 How to train an object detection model with mmdetection | DLology
%cd {mmdetection_dir}
from mmcv.runner import load_checkpoint
from mmdet.apis import inference_detector, show_result, init_detector
If you are using my GitHub repo, you probably noticed that mmdetection is included as a submodule, to update that in the future run this command.
Considering training with another model config? You can find a list of config files here (https://github.com/open-mmlab/mmdetection/tree/master/configs) as well as their specs
(https://github.com/open-mmlab/mmdetection/blob/master/MODEL_ZOO.md#baselines) such as the complexity(Mem(GB)), and accuracy(box AP). Then start by adding the
config file to MODELS_CONFIG at the start of the notebook
(https://colab.research.google.com/github/Tony607/mmdetection_object_detection_demo/blob/master/mmdetection_train_custom_data.ipynb).
In future posts, we will look into benchmarking those custom trained model as well as their deployment to edge computing devices, stay tuned and happy coding!
https://www.dlology.com/blog/how-to-train-an-object-detection-model-with-mmdetection/ 4/7
8/6/2020 How to train an object detection model with mmdetection | DLology
August (/blog/archive/2018/8/) (5)
July (/blog/archive/2018/7/) (4)
June (/blog/archive/2018/6/) (4)
May (/blog/archive/2018/5/) (4)
April (/blog/archive/2018/4/) (6)
March (/blog/archive/2018/3/) (5)
February (/blog/archive/2018/2/) (3)
January (/blog/archive/2018/1/) (4)
2017
Categories
deep learning (/blog/category/deep-learning/) (79)
edge computing (/blog/category/edge-computing/) (17)
Keras (/blog/category/keras/) (48)
NLP (/blog/category/nlp/) (8)
python (/blog/category/python/) (69)
PyTorch (/blog/category/pytorch/) (7)
tensorflow (/blog/category/tensorflow/) (35)
Tags
tutorial (/blog/tag/tutorial/) (56) Sentiment analysis (/blog/tag/sentiment-analysis/) (3) keras (/blog/tag/keras/) (35)
deep learning (/blog/tag/deep-learning/) (57) pytorch (/blog/tag/pytorch/) (2)
Authors
Chengwei (/blog/author/Chengwei/) (85)
Feeds
RSS (/blog/feeds/rss/) / Atom (/blog/feeds/atom/)
https://www.dlology.com/blog/how-to-train-an-object-detection-model-with-mmdetection/ 7/7
8/6/2020 How to train an object detection model with mmdetection | DLology
How to create custom COCO data set for object detection → (/blog/how-to-create-custom-coco-data-set-for-object-detection/)
Related posts
How to create custom COCO data set for instance segmentation (/blog/how-to-create-custom-coco-data-set-for-instance-segmentation/)
Comments
ALSO ON DLOLOGY
This tutorial shows the This tutorial will show you You are going to learn step (Comments)
complete process to get a how to apply focal loss to by step how to freeze and you have lea
Keras model running on … train a multi-class … convert your trained … a Keras ima
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
2020-05-23 10:36:04,458 - mmdet - WARNING - The model and loaded state dict do not match
exactly
1△ ▽ • Reply • Share ›
https://www.dlology.com/blog/how-to-train-an-object-detection-model-with-mmdetection/ 5/7
8/6/2020 How to train an object detection model with mmdetection | DLology
1 Reply Share ›
Recent Posts
Accelerated Deep Learning inference from your browser (/blog/accelerated-deep-learning-inference-from-your-browser/)
How to run SSD Mobilenet V2 object detection on Jetson Nano at 20+ FPS (/blog/how-to-run-ssd-mobilenet-v2-object-detection-on-jetson-nano-at-20-fps/)
Automatic Defect Inspection with End-to-End Deep Learning (/blog/automatic-defect-inspection-with-end-to-end-deep-learning/)
How to train Detectron2 with Custom COCO Datasets (/blog/how-to-train-detectron2-with-custom-coco-datasets/)
Getting started with VS CODE remote development (/blog/getting-started-with-vscode-remote-development/)
Archive
2020
https://www.dlology.com/blog/how-to-train-an-object-detection-model-with-mmdetection/ 6/7