How to create your own Custom Object Detector | by Eloy Marín | Towards Data Science

This article aims to help beginners that want to develop their own custom object detector for the first time, guiding them through all the key points to train a successful model.
The example shown will be trained with ImageAI, an open-source Python library which allows you to train a model with a few lines of code.
In this article, we will go over all the necessary steps to create a custom object detector from gathering data to finally testing our model.
The steps of the whole process are:
Gathering dataLabeling your datasetData augmentationTraining modelEvaluating model
Gathering data
Labeling your dataset
Data augmentation
Training model
Evaluating model
Before we can train our model, we need to gather lots of data, in this case, images. A minimum of 200 images should be fine, depending on the complexity of the detector.
If we want our model to be as robust as possible, our images should differ from each other in terms of lighting and background, so the model can generalize better.
These images can either be taken from your phone or obtained from the Internet.
Here you can see a subset of my Pill Detector model:
After gathering data, we need to resize our images because some of them may be pretty big. Smaller images will mean our model will train faster.
To do so, I wrote the following script:
This is the most tedious step since you’ll need to label in every image every single object that you want your model to detect.
You can label your data with LabelImg library. Run the following command to install it from terminal.
pip3 install labelImg
Once installed, we can proceed to start labeling our dataset by running the following command.
labelImg path_to_images/
Then, you’ll have to create a rectangular box with its corresponding label for every object in the image.
Once you’ve labeled all objects in an image, you must save it in PascalVOC format, generating a .xml file.
Having a large dataset is crucial for the performance of our Deep Learning model, so we can augment the data we already have with the imgaug library.
This github repo explains and provides code about how to augment images alongside their bounding boxes. This process can be divided into three steps:
Convert .xml files into one .csv fileApply data augmentation pipelineConvert resulting .csv file into multiple .xml files
Convert .xml files into one .csv file
Apply data augmentation pipeline
Convert resulting .csv file into multiple .xml files
First of all, we define a function to convert our .xml files into one .csv file:
And then we define another function to do the inverse process:
Then, we define a data augmentation pipeline with imgaug, for example, the following one.
Finally, we apply this pipeline to our dataset and save both augmented images and their augmented labels. You should also define image_aug() and bbs_obj_to_df() functions, which can be found in this github repo.
After all this preprocessing, we can finally train our model with ImageAI.
First of all, we need to install the library.
pip3 install imageai
To use transfer learning, you can download the model here.
Then we will need to split our dataset into train and validation folders.
dataset├─ train│   └── images│   └── annotations└─ validation    ├── images    ├── annotations
We can do this with the following lines of code:
And then we can train with the following script.
Once we trained our detector, we can evaluate how every model checkpoint performs via its mAP.
The output should look similar to this:
Finally, we can use our model in production for detecting objects in a single image.
You can adjust the minimum_percentage_probability parameter to show detected objects with more or less confidence, obtaining a result like the following.
Now I’d like to hear from you, let me know how it worked!
Thanks for reading!