Deep Learning — Build object detection models using Detecto

Karthick Nagarajan
Quick Code
Published in
7 min readMay 18, 2020

--

Nowadays, Most the people are crazy about Machine Learning and Computer vision. Because, It does some incredible stuff. Recently, I heard the news about self-driving cars and mask detection also face recognition. During the Covid-19 Quarantine, Lots of people have done with mask detection. Yes, I’m also one of them. But here I am not gonna talk about mask detection. Here I have mostly covered How to create a Custom Object Detection Model using Detecto?

I felt like, In machine learning, the most complicated part is how to train a model. Initially, I have mostly work with pre-trained models only. Because the custom one takes a long to process and it deals with lots of file changes. Now I don’t worry about it. yes, I have a better solution to train a custom model.

Why do we need to train a custom model?

For example, I need to detect cats on some images. If I use the pre-trained public model to do this means, I will get a result of something like the below image. So, the pre-trained model has many objects. That’s why we need a custom model.

Output for public pre-trained model

Most of the developers have used only the existing trained models. And even they have the struggle to build their computer vision models.

I present a simple way for anyone to build fully functional object detection models with just a few lines of code. More specifically, we’ll be using Detecto, a Python package built on top of PyTorch that makes the process easy and open to programmers at all levels.

The output of my custom model

How to use Detecto

Detecto is one of the best ways to train the custom model to compare to other things. We don't need to download any Github repo like TensorFlow or skeleton. Simply copy past work.

This is a simple python package for the train in your custom model. Here I’m going to explain how it works. You can install the Detecto package using pip:

pip3 install detecto
sample images from the google

Download this sample image as ‘apple.jpg’ and create a Python file in the same folder as the image. Inside the file copy-pastes the below code.

from detecto import core, utils, visualize
image = utils.read_image(‘apple.jpg’)
#Initializes a pre-trained model
model = core.Model()
labels, boxes, scores = model.predict_top(image)
visualize.show_labeled_image(image, boxes, labels)

After running this code, You will see the result something like the below images.

Image Detection with Detecto

It works with an existing Detecto’s pre-trained model. Before starting to train your model, you need to check your computer to enable GPU. Following code to you can check your GPU status.

import torch 
print(torch.cuda.is_available())

If it prints True, GPU enabled. If it prints False, GPU not enabled. I highly recommend GPU enabled computer for this process. If you do not have, you can continue this process. But It will take some time to train your model.

Prepare images for the Dataset

Whenever you are going to train a custom model, The important thing is IMAGES. Yes, of course, the images play a main role in deep learning. Because, the accuracy will be based on the images. So, before train a custom model, you need to plan How to get images? Here, I’m going to share my ideas for the easy way to get images for the dataset.

Get images from the google

Yes, we can get images from google. Using Download All Images this extension to we can easily get images in few minutes. If you need to customize the images which are you have downloaded, I mean remove PNGs and rename the files like that. Below I have added some code snippet. If you want to customize your images you can use it. Otherwise, you can skip.

Remove PNGs from the downloaded image folder.

import glob
import pathlib
for file in glob.glob('frames/*.png'):
path = pathlib.Path(file)
path.unlink()

Rename your image files

import os
os.getcwd()
collection = "images/cat"
for i, filename in enumerate(os.listdir(collection)):
print(filename)
os.rename(collection + "/" + filename, collection + "/cat" + str(i) + ".jpg")

Get Images from the video

Detecto gives a simple solution to get images from the video. Using the following codes to extract the images from the video file.

from detecto.utils
import split_videosplit_video(‘images/dog.mp4’, ‘images/dog/’, step_size=10)

Use your images

You can also use your images, I mean you can take a picture of the object which you are going to use to train your model. But the important thing is you need to change the resolution of the images. Because, The image which is you have taken will be the high-resolution one. So, it will take a long time to train your model. Using the following code to you can easily reduce the resolution of the images.

Image Annotations with LabelImg

LabelImg is a tool that can assist label images, personally feel very useful this one for the annotations. Detecto supports the PASCAL VOC format, in which you have XML files containing label and position data for each object in your images. To create these XML files, you can use the open-source LabelImg tool as follows:

pip3 install labelImg # Download LabelImg using pip
labelImg # Launch the application
Annotations with LabelImg

After labeling your Image folder will be like. if you want to know more about LabelImg, Please check out here.

DataSet folder’s an inside view

Setup Google Colaboratory Notebook

Collaboratory, or “Colab” for short, allows you to write and execute Python in your browser, with

  • Zero configuration required
  • Free access to GPUs
  • Easy sharing

Follow the below steps to create a Google Colaboratory notebook, an online coding environment that comes with a free, usable GPU. For this tutorial, you’ll just be working from within a Google Drive folder rather than on your computer.

  • Step 1: I have created a folder called object_detection in your Google Drive.
  • Step 2: I have created the images folder inside the object_detection.
  • Step 3: Upload your dataset images into the images folder.
  • Step 4: Create a Google Colab file called object_detection.ipynb
  • Step 5: You can see the result of file object_detection.ipynb has to create.
Upload DataSet and Create a Google Colaboratory notebook

And one more important thing is, you need to enable the GPU
Edit ⇾ Notebook settings ⇾ Hardware accelerator and select GPU

Enable the GPU option

Train a custom model using Detecto

We have set up the Google Colaboratory Notebook to train a model. Now we can start the coding. Here you can download my Google Colaboratory Notebook file.

Step 1: Insert a new code cell and add the below code. Run the code cell for the installation detecto in your Colab notebook.

Installed detecto in google colab notebook

Step 2: Again insert a new code sell and copy-paste below code for start train your model.

So, the process will take some time, it’s based on how many datasets you have. Once complete the training without any error, you will get the result exact below image. You can skip the user warning.

Successful message for the custom model

Yes!! The custom model files now ready to test.

Test and save your model

We have successfully trained our model file. Now we need to test the model file using the below code. We need to follow the same process above already we have done. Insert a new code cell and add the below code.

If our model train means perfectly, you can get a result like below image :)

Yes, That’s it. we have trained the custom model :) ;) So you can save this trained model using the below code.

model.save('cat_model_weights.pth')
model file saved in the google drive folder

The custom model file has saved in the google drive folder. You can download it and also you can test. Check out below the documentation for more help! ;)

This is the best experience about learned train a custom model. Because, I have used some other python packages for the trained model. Those are very difficult to use. Now I am going to train different objects to my future ideas.

Special Thanks to Alan Bi, He was the one who helps a lot, while I was learning Detecto :)

--

--