YOLO Algorithm for Custom Object Detection
This article was published as a part of the Data Science Blogathon.
Introduction on YOLO Algorithm
In this article, we are going to learn object detection using the Yolo algorithm. For this, we will be using the YOLO V5 version which is easy, simpler, and faster. Now we will see how to detect different objects and also we will use a custom dataset for training our model. And this is all we will do in the Collab notebook which is easy for everyone to use. In our application when the image is provided to it then it will detect all the objects present in the image. And we have an option to capture the live image and then detect the objects present in it.
Before going into that, let’s get some knowledge about computer vision.
Let’s get started.
Computer Vision: Computer Vision basically deals with anything that humans can see and perceive. Computer Vision enables computers and systems to derive complete meaningful information from digital photos and digital videos and other visual inputs.
Computer Vision Tasks
1) Object Detection
2) Image Classification
3) Image Captioning
4) Image Reconstruction
1) Object Detection: Object detection is the ability to detect objects or identify objects in any given image correctly.
2) Image Classification: Image Classification basically means it is identifying the class to which the object belongs.
3) Image Captioning: Image Captioning is like generating the text description of the given image by looking at it.
4) Image Reconstruction: Image Reconstruction is like finding the missing part in the image and reconstructing it.
Various Approach to Object Detection
There are two main approaches to object detection. They are the Machine learning approach and the Deep learning approach. Both these are capable of learning and identifying the object, but the execution of both is far different from each other.
- Object Detection using Machine Learning
Machine Learning is the application of Artificial Intelligence in which computers learn from the data given and make decisions or predictions based on it. It is usually the process of using algorithms to analyze data and then learning to make predictions and determine things based on the given data.
Machine Learning methods for object detection are SIFT, Support Vector Machine (SVM), and Viola jones object detection.
- Object Detection using Deep Learning
Deep Learning which is also referred to as deep structured learning, is a class of machine learning algorithms. Deep Learning uses a multi-layer approach to extract high-level features from the data that is provided to it.
The Deep Learning model doesn’t require any feature to be provided manually for classification. Instead, it tries to transform its data into an abstract representation. Most deep learning methods implement neural networks to achieve results.
Deep Learning methods for object detection are R-CNN, Faster R-CNN, and YOLO Algorithm.
Preparing Custom Dataset
Now let’s see how to make our own custom dataset. For that, first, make a folder for all images, and in that, make two more folders naming train_dataset and test_dataset.
In each folder, make two more folders and name them Train_images, Train_labels, Test_images, and Test_labels. In which Train_images contain all images used for training, Train_labels contains all labels txt files for train images wherein text file there will be annotation for that bounding box in YOLO format. And then, test_images contain all images used for testing, and similarly, test_labels contain all label files for test images.
Now let’s move to the Collab notebook and start using the YOLO V5 algorithm for object detection using the Custom dataset.
Drawing Bounding Boxes
Now we will see how to draw bounding boxes for the images.
For drawing bounding boxes, there are many ways. And in that now, we will see how to use makesense.ai to draw them.
First, open makesense.ai on any browser, and then click on Get Started. And now add all the images for which you are willing to draw bounding boxes. After uploading, click on object detection. And now click on start project. Now you have to add labels. Here don’t forget that the order should be the same; else, the model will identify objects wrong. Here you can manually write all the classes, or you can import the file where all the classes were written. And then start the project. After completion of the drawing bounding boxes for all the images, click on Actions which is on the top left. Click on export annotations. Select Zip package containing files in YOLO format.
You can see a zip folder with all text files downloaded.
YOLO V5 Implementation
YOLO stands for You Only Look Once. YOLO algorithm divides an image into the grid system and in that each grid detects objects within itself. Now the latest version of YOLO is V5 which is launched by ultralytics. This YOLO V5 algorithm is the best of all object detection algorithms available so far. It is simple, easier, and faster. It detects objects with high accuracy.
Visit documentation of YOLO.
It contains all the versions with their GitHub links.
Let’s move on to the implementation.
First, mount the google drive.
from google.colab import drive drive.mount('/content/drive')
Mounted at /content/drive
Now create a folder custom_object_detection and in that create another folder called yolov5 and then move on to that yolov5 folder.
The Github link for YOLO V5 is
Clone this GitHub repository in the yolov5 folder.
!git clone https://github.com/ultralytics/yolov5 # clone %cd yolov5 %pip install -qr requirements.txt # install
import torch from yolov5 import utils display = utils.notebook_init() # checks
Preparation of Dataset
For that, inside the custom_object_detection folder create one dataset folder, and in that make two more folders namely, images and labels. In the images, the folder makes two folders train and Val, and similarly, the labels folder make two folders train and val. the train contains all training images and training labels and Val contains all images and labels in respective folders used for validation. Accordingly, upload them into the colab notebook.
Now we have to modify custom_data.yaml file that is present inside the data folder which is inside the yolov5 folder. Change it according to your problem.
The file should contain like this.
train: ../dataset/images/train val: ../dataset/images/val # Classes nc: 10 # number of classes names: ['laptop', 'mobile', 'tv', 'bottle', 'person', 'spoon', 'backpack', 'vase', 'dog', 'calculator', ] # class names
For train and val enter the path of the train and val images folders.
Then enter the number of classes and then enter all those classes. make sure the order should be correct in which labels were given for drawing bounding boxes.
I just gave some example classes here. You can add all classes that are there for your object detection project.
Training the Model
Now finally we are ready to train our model. For training the model just run the following command.
!python train.py --img 640 --batch 8 --epochs 10 --data custom_data.yaml --weights yolov5s.pt --cache
You can increase the number of epochs to increase the accuracy.
finally, you will find this at last.
Results saved to runs/train/exp14
here your model will be saved.
Testing the Model
The model was trained with a training dataset and is now ready to test.
We can test the model for images, for videos, and also we can detect objects for live video too. We can use the webcam for that. For each of these, we have different commands. The commands are,
First, we will for image object detection.
$ python detect.py --source ../Test/test1.jpeg --weights ../Model/weights/best.pt
Here we have to paste the path to the image.
For Video object detection,
$ python detect.py --source ../Test/vidd1.mp4 --weights ../Model/weights/best.pt
Similarly here also paste the path of the video.
Finally, when it comes to webcam,
$ python detect.py --source 0 --weights ../Model/weights/best.pt
Finally, you will get this,
Results saved to runs/train/exp14
So when you go to that folder then you will find images or videos after object detection.
This is all about custom object detection using the YOLOV5 algorithm which is easier, simple, and faster than previous versions of YOLO. And also the advantage of this is once you train the model, you don’t need to train the model again when you are testing it. Overall in this article, we have seen,
- what is computer Vision first
- Then we have seen various approaches for object detection.
- We prepared our own custom dataset.
- Learned to draw bounding boxes using makesense.ai
- Next implementation of YOLO algorithm where we have used version 5.
- Finally, we trained and tested our model.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.