YOLO Algorithm for Custom Object Detection

8 min read

Introduction

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 yolo deep learning.

This article was published as a part of the Data Science Blogathon.

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

  • Object Detection: Object detection is the ability to detect objects or identify objects in any given image correctly.
  • Image Classification: Image Classification basically means it is identifying the class to which the object belongs.
  • Image Captioning: Image Captioning is like generating the text description of the given image by looking at it.
  • 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.

YOLO Algorithm

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 deep learning format.

You can see a zip folder with all text files downloaded.

How does YOLO work?

Here is a simplified explanation of the four points::

1. YOLO cuts an image into squares.

This makes it easier for YOLO to find objects in the image. It only needs to look at one square at a time, instead of the entire image.

2. For each square, YOLO guesses if there is an object in it and, if so, what kind of object it is.

It does this by using a deep learning model. The model has been trained on a lot of images and labels. This means that the model knows how to identify different types of objects in images.

3. YOLO gets rid of any extra guesses.

It does this by using a technique called non-maximum suppression. This removes any guesses that are overlapping with other guesses. This makes sure that YOLO only outputs one guess for each object in the image.

4. YOLO outputs the remaining guesses as rectangles and object labels.

A rectangle is a box that surrounds an object in an image. An object label is a name for the type of object in the box.

These outputs the remaining guesses as rectangles and object labels. This means that YOLO outputs a box and a name for each object that it finds in the image.

YOLO V5 Implementation

yolo deep learning

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 deep learning is V5 which is launched by ultralytics. This YOLO deep learning 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.

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.

%cd /content/drive/MyDrive/custom_object_detection/yolov5

/content/drive/MyDrive/custom_object_detection/yolov5

The Github link for YOLO V5 is

Click Here

Clone this GitHub repository in the yolov5 folder.

!git clone https://github.com/ultralytics/yolov5 # clone %cd yolov5 %pip install -qr requirements.txt # installimport torch from yolov5 import utils display = utils.notebook_init() # checks

What is the YOLOv7 algorithm?

YOLOv7 deep learning is the latest version of the YOLO (You Only Look Once) algorithm object detection algorithm. It is a single-stage object detection algorithm, which means that it can detect objects in a single pass through the image. This makes YOLOv7 very fast, making it suitable for real-time object detection applications.

YOLOv7 is also very accurate, achieving state-of-the-art results on several object detection benchmarks. It is also able to detect multiple objects in a single image, and it can be trained to detect a wide variety of object classes.

Here are some of the key features of the YOLOv7 algorithm:

  • Fast and accurate object detection
  • Single-stage object detection
  • Multi-object detection
  • Large class repertoire
  • Efficient training and inference


YOLOv7 is a powerful object detection algorithm that can be used for a wide variety of applications, such as self-driving cars, video surveillance, and robotics.

Here are some examples of how YOLOv7 can be used:

  • Self-driving cars: It can be used to detect pedestrians, vehicles, and other objects on the road. This information can then be used to help the self-driving car navigate safely.
  • Video surveillance: It can be used to detect people, vehicles, and other objects in video footage. This information can then be used to monitor for suspicious activity or to track the movement of people and objects.
  • Robotics: Also, it can be used to help robots detect objects in their environment. This information can then be used to help the robot navigate safely or to interact with objects in its environment.

Overall, YOLOv7 is a powerful and versatile object detection algorithm that can be used for a wide variety of applications.

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.

yolo deep learning

Conclusion

This article primarily focuses on custom object detection using the YOLOv5 algorithm, highlighting its improved ease, simplicity, and speed compared to earlier versions of YOLO deep learning. Notably, a key advantage lies in the elimination of the need for retraining the model during testing once it has been trained initially. The narrative progresses from an introduction to computer vision to exploring different approaches for object detection, followed by the creation of a custom dataset and learning to annotate bounding boxes using makesense.ai. Subsequently, the YOLOv5 algorithm is implemented, culminating in the training and testing of the model.

Frequently Asked Questions?

Q1. Why is YOLO better than CNN?

A. YOLO (You Only Look Once) is superior to traditional CNNs for object detection due to its single pass approach, offering real-time processing and high accuracy by directly predicting bounding boxes and class probabilities.

Q2.Which CNN is used in YOLO?

A. YOLO (You Only Look Once) primarily utilizes convolutional neural networks (CNNs) as its backbone architecture. Specifically, the YOLOv5 variant typically employs the CSPDarknet53 architecture as its CNN backbone.

Q3. How many classes can YOLO detect?

A. YOLO (You Only Look Once) can detect a variable number of classes depending on the dataset and the specific configuration used during training. Typically, YOLO can detect hundreds or even thousands of different object classes, provided they are included in the training data and properly labeled.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

Aditi
Aditi 24 Jan, 2023

nicely presented in a short and understandable form. thankyou

Sagar Wankhede
Sagar Wankhede 06 Apr, 2023

Sir, how send a alert message in telegram after certain specific species of animal detected.