0% found this document useful (0 votes)
116 views16 pages

Guidance On Yolov5

The document discusses implementing object detection using the YOLOv5 algorithm on a human dataset. It provides steps to set up the environment in Google Colab, download the YOLOv5 source code and pretrained model, prepare a labeled human dataset with images and labels, create a configuration file, and run training to detect humans in images. The goal is to understand and apply YOLOv5 code to locate instances of humans using bounding boxes and class labels.

Uploaded by

Trí Từ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views16 pages

Guidance On Yolov5

The document discusses implementing object detection using the YOLOv5 algorithm on a human dataset. It provides steps to set up the environment in Google Colab, download the YOLOv5 source code and pretrained model, prepare a labeled human dataset with images and labels, create a configuration file, and run training to detect humans in images. The goal is to understand and apply YOLOv5 code to locate instances of humans using bounding boxes and class labels.

Uploaded by

Trí Từ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Machine Learning Course - VGU 2022

Project: Object Detection using Yolov5


October 8, 2022

Object detection is a computer technology related to computer vision and image processing that
deals with detecting instances of objects of a certain class (such as humans, buildings, or cars, ...) within
images or videos. Object detection algorithms typically leverage machine learning or deep learning to
produce meaningful results. When humans look at images or video, we can recognize and locate objects
of interest within a matter of moments. The goal of object detection is to replicate this intelligence
using a computer. In this task our program locates the presence of objects with a bounding box and
classes of the located objects in an image.

Figure 1: Input/Output of Object Detection problem

In this project, we will investigate and implement Python source code for human detection, which
is the task of locating instances of human beings in an image using an algorithm known as YOLOv5.

(a) Input image (b) YOLOv5 detection result

Figure 2: Example illustrating the results of the Human Detection model

1
VGU (supervised and modern) machine learning

Project requirements: This project aims to read, understand, and apply the YOLOv5 source
code to the human dataset (download link). It is recommended to follow the instructions provided
below.
YOLOv5 tutorial: The Detailed tutorial explains how to train the object detection algorithm
YOLOv5 on a dataset. There is a dataset (helmet dataset), which is different from the human
dataset (required in this project), used to demonstrate in this example.

1. Environment Setup: Google Colab is used to setup the environment for this project. The steps
to use Google Colab are as follows:

• Step 1: Access to the Google Colab link. If the access is successful, it will show the result
in the figure below:

Figure 3: The main interface of Google Colab

After that, you should sign in to Google Colab using a Google account. If the login is
successful, a new window will appear as shown below:

Figure 4: Google Colab interface after successful login

• Step 2: Create a new notebook where you will write your Python code and implement it
with YOLOv5.

2
VGU (supervised and modern) machine learning

Figure 5: Initializing a new notebook

• Step 3: Go to the runtime menu to change runtime type to GPU.

Figure 6: Steps to enable GPU for new notebooks on Google Colab

• Step 4: Finally, in order to write and implement Python code, it should initialize the
environment by connecting to a runtime as shown below:

3
VGU (supervised and modern) machine learning

Figure 7: Starting a notebook with a GPU in Google Colab

After completing all steps above, you have an environment that can code in Python and access to
hardware accelerators—Graphics Processing Unit (GPU).

2. Download YOLOv5 source code from GitHub: In order to use YOLOv5, you need to
download the YOLOv5 source code, which is open-source and published on Github. Moreover,
the YOLOv5 demands that you install some required libraries.

• Step 1: Use this link to access the Github of the YOLOv5. If the access is successful, it will
show the result in the figure below:

Figure 8: YOLOv5’s GitHub interface

4
VGU (supervised and modern) machine learning

• Step 2: On YOLOv5’s GitHub page (main repository), click the Code button (green but-
ton), and click the copy button to copy a link, as shown in the image below:

Figure 9: Copy the link to download the source code YOLOv5

• Step 3: Back to the Google Colab, you create a cell and write a command: !git clone <link>
(where <link> is the GitHub path copied in step 2). If the cell executes successfully, the
result will be displayed as shown below:

Figure 10: Downloading the YOLOv5 source code using git clone

To make sure YOLOv5 is downloaded, you can refresh the Files section of Google Colab to see
whether the ”yolov5” directory appears in your notebook.

5
VGU (supervised and modern) machine learning

Figure 11: YOLOv5 Directory

3. Install YOLOv5 requirements: The YOLOv5 is based on a variety of Python libraries. There-
fore, we need to run two lines of codes below to download and install necessary library packages
that make YOLOv5 can run correctly. The first line is to change the current working directory to
yolov5 directory, and the second line is to install all requirements to run YOLOv5.

Figure 12: Installing the required library packages using the pip command

4. Download pretrained model: You can download pretrained model using this link and place it
into the yolov5 directory. On the Google Colab, it can be done by using wget + <link> (link of
pretrained model) as the figure below:

6
VGU (supervised and modern) machine learning

Figure 13: Downloading the pre-trained model file using the wget command

To make sure the pretrained model is downloaded, you can refresh the Files section of Google
Colab, then looking for ”yolov5s.pt” file. yolov5s.pt:

Figure 14: Pre-trained model file

5. Prepare dataset for training: In order to train YOLOv5 on an arbitrary dataset, it requires
at least two components such as a dataset directory (images and labels) and a configuration file
(.yalm file):

• Dataset directory: It would be best to prepare a directory located inside the yolov5
directory, arranged as indicated in the following diagram:

7
VGU (supervised and modern) machine learning

Figure 15: Dataset directory organization in YOLOv5

There are two type of samples, one for training and the other for validation.
Furthermore, each of them contains two sub-directory:
– images: contains image files (.jpg, .png...).
– labels: contains text files (.txt) with the same name as in images. These text files give
information such as bounding box (coordinates of an object) and their classes.
The Helmet data is used in this tutorial, and you can download it by using this link. You can
either download the .zip file and upload the dataset to Google Colab or use the commands
below to automatically download and unzip the dataset to the yolov5 directory.
1 # move to yolov5 folder first and then :
2 # 1 st solution
3 ! gdown -- id 1 b Y y t J d F a 1 D _ r 2 k m 6 Z Z 8 T 9 9 T n P l v y C A h f
4 ! unzip helmet . zip
5
6 # 2 nd solution
7 from google . colab import drive
8 drive . mount ( ’/ content / gdrive ’)
9 # you need to choose your path where you upload the helmet . zip dataset
10 ! cp / content / gdrive / MyDrive / test_Yolov5 / helmet . zip / content / yolov5
11 ! unzip helmet . zip

• Configuration File (.yaml): You have to create a configuration file containing training
information related to the dataset used for training data. You can reference sample files
provided by authors to create your configuration file:

8
VGU (supervised and modern) machine learning

Figure 16: Important fields in the .yaml file

Therefore, you need to create a new .yaml file (e.g., Helmet.yaml) for the Helmet dataset and
fill in the corresponding fields. Here you can do it manually or use the command lines below to
create it automatically (The .yaml file needs to be located in the data folder):
1 import yaml
2
3 dataset_info = {
4 ’ path ’: ’ helmet ’ ,
5 ’ train ’: ’ train / images ’ ,
6 ’ val ’: ’ val / images ’ ,
7 ’ nc ’: 3 ,
8 ’ names ’: [ ’ helmet ’ , ’ head ’ , ’ person ’]
9 }
10
11 with open ( ’ data / helmet . yaml ’ , ’w + ’) as f :
12 doc = yaml . dump ( dataset_info , f , default_flow_style = None , sort_keys = False )

You must modify the configuration to fit the dataset you select since the training information in
each data set will vary (number of classes, names of classes, path to data directory, ...).

6. Training: After thoroughly preparing the training dataset, you will train a YOLOv5 model with
the prepared dataset by using the command below. If you use a different dataset, you should
change –data to the corresponding configuration .yalm file:
1 ! python train . py -- img 640 -- batch 4 -- epochs 20 -- data helmet . yaml -- weights
yolov5s . pt -- cache
2
3 # Use can also copy the trained weights to your Google drive for later
4 # ! cp < path to . pt ( weights ) file > < path to your drive >
5 ! cp / content / yolov5 / runs / train / exp / weights / best . pt / content / gdrive / MyDrive /
test_Yolov5 / weights

You will see the following information once the training is completed:

9
VGU (supervised and modern) machine learning

Figure 17: Results after the training process completed

When the training process is completed, there will be a run directory which has a sub-directory
named exp. The exp directory contains all output files of YOLOv5 for the training dataset.

Figure 18: exp directory

7. Implement detection with trained model: In order to use the model trained above to detect
objects on an arbitrary image, you should use the command below:
1 ! python detect . py -- weights " weight_path " -- source " image_path "

Arguments:

• weight path: Path to the YOLOv5 model’s weight file after the training is over. You can
find the path printed on the screen after the training is completed.

10
VGU (supervised and modern) machine learning

Figure 19: Path to the weight file of the trained model. Note: you should choose the best.pt

• image path: Path to the input images.

The figure below depicts the outcome of running the aforementioned command:

Furthermore, this code not only accepts the input as an image, but also accepts other types of
inputs such as video, link of video, and webcam, ... You can reference examples below:
1 # video
2 ! python detect . py -- weights " runs / train / exp / weights / best . pt " -- source " vid_name .
mp4 "
3 # a folder contains images
4 ! python detect . py -- weights " runs / train / exp / weights / best . pt " -- source "
folder_name / "
5 # youtube video
6 ! python detect . py -- weights " runs / train / exp / weights / best . pt " -- source " https ://
www . youtube . com / watch ? v = SX_ViT4Ra7k "
7 # on webcam ( not working on Google Colab )
8 ! python detect . py -- weights " runs / train / exp / weights / best . pt " -- source 0

This is the final step in the YOLOv5 tutorial on Helmet dataset. You need to apply these
mentioned steps above to the Human dataset (some steps need to be adjusted accordingly) to
achieve the objectives of this project.

11
VGU (supervised and modern) machine learning

8. OPTIONAL: This section will discuss advanced problems in training YOLOv5, including argu-
ments in training commands, evaluation commands, and generating labels for a dataset.

• Arguments in training command: The training command in section 6 has some default
parameters that can be adjusted. Different training parameters can increase or decrease the
performance of the model. The list below contains some basic training parameters:
– img: is input image size. Train and Test images are resized to the value specified in IMG
(default is 640) before passing through the model. It is recommended to experiment with
various values to enhance the performance of your YOLOv5 model.
– batch: is batch size. In the training process, you can load all samples from the dataset
(it only works for small dataset. If you train with a large dataset, it might cause overload-
ing), or the dataset can be divided into batches for loading. The batch is 16 by default,
which means that there are 16 samples to work through before updating the internal
model parameters. If you adjust the batch size value, it should follow the 2n (n ≥ 0) rule.
– epochs: defines the number times that the learning algorithm will work through the
entire training dataset (default is 300).
– data: is the configuration file (.yaml) you created in section 5.
– weights: is a pre-trained checkpoint (path to file), saved parameters (weights) previously
trained on a large-scale dataset. In this project, a pre-trained checkpoint is used to
initialize the weights of the base model before training. You can try different pre-trained
checkpoints from the list of pre-trained checkpoints in the link.
• Validate model’s accuracy: As mentioned before, when the arguments in the training
command (hyperparameter) vary, the model’s performance can also change. Therefore, you
can validate models and compare their performance to choose the best one by using the
command below:

Figure 20: Evaluation results on the val set

• Data labeling: Object detection is a supervised machine learning problem, which means
you must train your models on labeled samples. Each image in the training dataset must be
accompanied by a annotations file that includes the bounding box (a rectangle that surrounds
the object) and classes of the objects it contains. Therefore, in order to train YOLOv5 with
a custom (new) dataset or collecting more images for a given data (such as Helmet or Human
dataset), you need to manually label these images. There are several open-source tools that
create object detection annotations. In the project, you will learn how to use labelImg tool.
There are several ways to install labelImg (you can read README.md file on labelImg
GitHub), and the steps below instruct how to install labelImg with Anaconda (a distribution
of the Python and R programming languages for scientific computing):

12
VGU (supervised and modern) machine learning

– Step 1: Open cmd/terminal window. Typing the command below to download labelImg
source code from GitHub:
1 git clone https :// github . com / tzutalin / labelImg . git

– Step 2: Install Anaconda from the link.


– Step 3: After installing Anaconda, you will initialize the Anaconda environment with
the following command line:
1 conda create -n labelimg_env python =3.9 -y

– Step 4: Activate the virtual environment created in step 3 using the command:
1 conda activate labelimg_env

– Step 5: Navigate to the source code directory of labelImg using the following command:
1 cd < path_to_labelImg_folder >

– Step 6: Execute the following commands one by one:


1 conda install pyqt =5
2 conda install -c anaconda lxml
3 pyrcc5 -o libs / resources . py resources . qrc

– Step 7: Run labelImg tool using the command:


1 python labelImg . py

If you successfully implement from step 1 to step 7, there is a labelImg window show up
as the figure below:

From now on, whenever you need to use labelImg tool, you will run commands in step 4
(activate labeling env environment) and step 7 (run labelImg tool)..

13
VGU (supervised and modern) machine learning

Suppose you want to label for Human detection data (person class). You should do the
following steps:
– Step 1: You should access to the directory of lableImg source code which you down-
loaded from the github, then you follow the data/predefined classes.txt path to define
the list of classes that will be used for your training.

Figure 21: Contents of the file predefined classes.txt. Each row in the file corresponds to each class
name in the dataset.

You will edit the file content with the name of the class in your dataset, assuming in the
human set there is only person class, so you just edit the file as follows:

Figure 22: Contents in the file predefined classes.txt with the human dataset.

– Step 2: In the labelImg window, you should follow the steps as shown in the figure
below to open images and opt for annotation format (this project use YOLO format):

Figure 23: Open the directory containing the images to be labeled and change the label format to
YOLO

14
VGU (supervised and modern) machine learning

– Step 3: If everything work properly, you can obtain results show in the figure below

Figure 24: Interface after selecting the image folder to be labeled

– Step 4: This step indicate how you can label images

Figure 25: Labeling process

After finishing labeling an image, you have to save it (CTRL + S) and move to the next
image (D key (Next Image) or A key (Prev Image)). When you complete labeling all
images and .txt files (annotation files) present in the same directory of these images (each

15
VGU (supervised and modern) machine learning

image will have a corresponding .txt file), it means that you have successfully labeled
your dataset.

Figure 26: Directory containing images after labeling

Data labeling is a crucial part of data preprocessing for machine learning, especially for
supervised learning. It will have a significant impact on the performance of your model,
so you must be extremely focused and careful during this task.

- The End -

16

You might also like