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.
Table of contents
Computer Vision
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.
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 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.
%cd /content/drive/MyDrive/custom_object_detection/yolov5
/content/drive/MyDrive/custom_object_detection/yolov5
The Github link for YOLO V5 is
https://github.com/ultralytics/yolov5
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
What is the YOLOv7 algorithm?
YOLOv7 is the latest version of the YOLO (You Only Look Once) 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:
1. 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.
2. 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.
3. 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.

Conclusion
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.
FAQs
YOLO is a real-time object detection algorithm that is faster and more accurate than many other CNN-based object detection algorithms. YOLO is also able to detect multiple objects in a single image, while many other CNN-based algorithms can only detect one object at a time.
Here are some of the specific advantages of YOLO over CNN:
Speed: YOLO is a single-shot object detection algorithm, which means that it can detect objects in a single pass through the image. This makes YOLO much faster than other CNN-based algorithms, which typically require multiple passes through the image to detect objects.
Accuracy: YOLO is also very accurate, even on challenging datasets. In fact, YOLO has outperformed many other CNN-based object detection algorithms on standard benchmarks.
Multi-object detection: YOLO is able to detect multiple objects in a single image, while many other CNN-based algorithms can only detect one object at a time. This makes YOLO ideal for real-world applications such as self-driving cars and video surveillance.
The YOLO algorithm uses a variety of CNN architectures, including Darknet, ResNet, and EfficientNet. The specific CNN architecture that is used in YOLO depends on the specific application and the desired trade-off between speed, accuracy, and model size.
For example, the YOLOv5 algorithm uses the EfficientNet CNN architecture, which is known for its speed and efficiency. This makes YOLOv5 ideal for real-time object detection applications on mobile devices.
The number of classes that YOLO can detect depends on the specific YOLO model. For example, the YOLOv5 algorithm can detect up to 80 classes, while the YOLOv7 algorithm can detect up to 10,000 classes.
The specific classes that YOLO can detect are defined by the training dataset. For example, if you train a YOLO model on a dataset of images of cars, then the YOLO model will be able to detect cars in new images.
2 thoughts on "YOLO Algorithm for Custom Object Detection"
Aditi says: January 24, 2023 at 7:01 pm
nicely presented in a short and understandable form. thankyouSagar Wankhede says: April 06, 2023 at 2:13 pm
Sir, how send a alert message in telegram after certain specific species of animal detected.