Deep learning with PyTorch’s library

Detecto — An object detection library for PyTorch

Karthick Nagarajan
PyTorch
6 min readJun 10, 2020

--

The output of my custom model

Nowadays, most people are crazy about Machine Learning and Computer vision. 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. The custom model 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, I will get a result of something like a 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 struggled to build custom 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.

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 only.

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.

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’s GPU status. 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. The accuracy will be based on the images. So, before train a custom model, you need to plan How to get images?

I have already shared my ideas for an easy way to get images on the below article. Please check it.

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 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 you complete the training without any error, you will get the result exact below the 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 the 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 trained 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 in your local. Check out below the documentation for more help! ;)

This is the best experience about learned train a custom model. 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.

Originally published at www.spritle.com

--

--