Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Unlock the full potential of AI with Building LLMs for Productionβ€”our 470+ page guide to mastering LLMs with practical projects and expert insights!

Publication

Yolo-v5 Object Detection on a custom dataset.
Computer Vision

Yolo-v5 Object Detection on a custom dataset.

Last Updated on October 28, 2020 by Editorial Team

Author(s): Balakrishnakumar V Β 

Step by step instructions to train Yolo-v5 & do Inference(from ultralytics) to count the blood cells and localizeΒ them.

I vividly remember that I tried to do an object detection model to count the RBC, WBC, and platelets on microscopic blood-smeared images using Yolo v3-v4, but I couldn’t get as much as accuracy I wanted and the model never made it to production.

Now recently I came across the release of the Yolo-v5 model from Ultralytics, which is built using PyTorch. I was a bit skeptical to start, owing to my previous failures, but after reading the manual in their Github repo, I was very confident this time and I wanted to give it aΒ shot.

And it worked like a charm, Yolo-v5 is easy to train and easy to do inference.

So this post summarizes my hands-on experience on the Yolo-v5 model on the Blood Cell Count dataset. Let’s getΒ started.

Ultralytics recently launched Yolo-v5. For time being, the first three versions of Yolo were created by Joseph Redmon. But the newer version has higher mean Average Precision and faster inference times than others. Along with that it’s built on top of PyTorch made the training & inference process very fast and the results areΒ great.

Source: Ultralytics

So let’s break down the steps in our trainingΒ process.

  1. Dataβ€Šβ€”β€ŠPreprocessing (Yolo-v5 Compatible)
  2. Modelβ€Šβ€”β€ŠTraining
  3. Inference

And if you wish to follow along simultaneously, open up these notebooks,

Google Colab Notebookβ€Šβ€”β€ŠTraining and Validation: link

Google Colab Notebookβ€Šβ€”β€ŠInference: link

1. Dataβ€Šβ€”β€ŠPreprocessing (Yolo-v5 Compatible)

I used the dataset BCCD dataset available in Github, the dataset has blood smeared microscopic images and it’s corresponding bounding box annotations are available in an XMLΒ file.

Dataset Structure:
- BCCD
  - Annotations
    - BloodImage_00000.xml
    - BloodImage_00001.xml
    ...
- JpegImages
    - BloodImage_00001.jpg
    - BloodImage_00001.jpg
    ...

Sample Image and its annotationΒ :

Sample InputΒ Image
Labels in theΒ .XMLΒ file

Upon mapping the annotation values as bounding boxes in the image will results likeΒ this,

But to train the Yolo-v5 model, we need to organize our dataset structure and it requires images (.jpg/.png, etc.,) and it’s corresponding labels inΒ .txtΒ format.

Yolo-v5 Dataset Structure:
- BCCD
  - Images
    - Train (.jpg files)
    - Valid (.jpg files)
- Labels
     - Train (.txt files)
     - Valid (.txt files)

And then the format ofΒ .txt files should beΒ :

STRUCTURE OFΒ .txt FILEΒ :

– One row perΒ object.

– Each row is class x_center y_center width heightΒ format.

– Box coordinates must be in normalized xywh format (from 0–1). If your boxes are in pixels, divide x_center and width by image width, and y_center and height by imageΒ height.

– Class numbers are zero-indexed (start fromΒ 0).

An Example label with class 1 (RBC) and class 2 (WBC) along with each of their x_center, y_center, width, height (All normalized 0–1) looks like the belowΒ one.

# class x_center y_center width height #
  1     0.718    0.829    0.143 0.193
  2     0.318    0.256    0.150 0.180
  ...
Example of labels.txt

So let’s see how we can pre-process our data in the above-specified structure.

Our first step should be parsing the data from all the XML files and storing them in a data frame for further processing. Thus we run the below codes to accomplish it.

And the data frame should look likeΒ this,

After saving this file, we need to make changes to convert them into Yolo-v5 compatible format.

REQUIRED DATAFRAME STRUCTURE
- filename : contains the name of the image
- cell_type: denotes the type of the cell
- xmin: x-coordinate of the bottom left part of the image
- xmax: x-coordinate of the top right part of the image
- ymin: y-coordinate of the bottom left part of the image
- ymax: y-coordinate of the top right part of the image
- labels : Encoded cell-type (Yolo - label input-1)
- width : width of that bbox
- height : height of that bbox
- x_center : bbox center (x-axis)
- y_center : bbox center (y-axis)
- x_center_norm : x_center normalized (0-1) (Yolo - label input-2)
- y_center_norm : y_center normalized (0-1) (Yolo - label input-3)
- width_norm : width normalized (0-1) (Yolo - label input-4)
- height_norm : height normalized (0-1) (Yolo - label input-5)

I have written some code to transform our existing data frame into the structure specified in the aboveΒ snippet.

After preprocessing our data frame looks like this, here we can see there exist many rows for a single image file (For instance BloodImage_0000.jpg), now we need to collect all the (labels, x_center_norm, y_center_norm, width_norm, height_norm) values for that single image file and save it as aΒ .txtΒ file.

DataFrame Preprocessed

Now we split the dataset into training and validation and save the corresponding images and it’s labeledΒ .txt files. For that, I’ve written a small piece of the codeΒ snippet.

After running the code, we should have the folder structure as we expected and ready to train theΒ model.

No. of Training images 364 
No. of Training labels 364
No. of valid images 270 
No. of valid labels 270
&&
- BCCD
  - Images
    - Train (364 .jpg files)
    - Valid (270 .jpg files)
- Labels
     - Train (364 .txt files)
     - Valid (270 .txt files)

End of data pre-processing.

2. Modelβ€Šβ€”β€ŠTraining

To start the training process, we need to clone the official Yolo-v5’s weights and config files. It’s available here.

Then install the required packages that they specified in the requirements.txt file.

Yolov5 structure

bcc.yamlΒ :

Now we need to create a Yaml file that contains the directory of training and validation, number of classes and it’s label names. Later we need to move theΒ .yaml file into the yolov5 directory that weΒ cloned.

## Contents inside the .yaml file
train: /content/bcc/images/train
val: /content/bcc/images/valid
nc: 3 
names: ['Platelets', 'RBC', 'WBC']

model’sβ€Šβ€”β€ŠYAMLΒ :

Now we need to select a model(small, medium, large, xlarge) from theΒ ./modelsΒ folder.

The figures below describe various features such as no.of parameters etc., for the available models. You can choose any model of your choice depending on the complexity of the task in hand and by default, they all are available asΒ .yaml file inside the models’ folder from the cloned repository

Yolo Model Parameters from Ultralytics
model’s YAML file xΒ 4

Now we need to edit the *.yaml file of the model of our choice. We just have to replace the number of classes in our case to match with the number of classes in the model’s YAML file. For simplicity, I am choosing the yolov5s.yaml for faster processing.

## parameters
nc: 3 # number of classes
depth_multiple: 0.33  # model depth multiple
width_multiple: 0.50  # layer channel multiple
# anchors
anchors:
- [10,13, 16,30, 33,23]  # P3/8
- [30,61, 62,45, 59,119]  # P4/16
...............

NOTE: This step is not mandatory if we didn’t replace the nc in the model’s YAML file (which I did), it will automatically override the nc value that we created before (bcc.yaml) and while training the model, you will see this line, which confirms that we don’t have to alterΒ it.

β€œOverridingΒ ./yolov5/models/yolov5s.yaml nc=80 withΒ nc=3”

Model train parametersΒ :

We need to configure the training parameters such as no.of epochs, batch_size, etc.,

Training Parameters
!python
- <'location of train.py file'>
- --img <'width of image'>
- --batch <'batch size'>
- --epochs <'no of epochs'>
- --data <'location of the .yaml file'>
- --cfg <'Which yolo configuration you want'>(yolov5s/yolov5m/yolov5l/yolov5x).yaml | (small, medium, large, xlarge)
- --name <'Name of the best model to save after training'>

Also, we can view the logs in tensorboard if weΒ wish.

This will initiate the training process and takes a while to complete.

I am posting some excerpts from my trainingΒ process,

Excerpts from the trainingΒ process
Excerpts From the trainingΒ process
METRICS FROM TRAINING PROCESS
No.of classes, No.of images, No.of targets, Precision (P), Recall (R), mean Average Precision (map)
- Class | Images | Targets | P | R | [email protected] | [email protected]:.95: |
- all   | 270    |     489 |    0.0899 |       0.827 |      0.0879 |      0.0551

So from the values of P (Precision), R (Recall), and mAP (mean Average Precision) we can know whether our model is doing well or not. Even though I have trained the model for only 100 epochs, the performance wasΒ great.

Tensorboard Visualization

End of model training.

3. Inference

Now it’s an exciting time to test our model, to see how it makes the predictions. But we need to follow some simpleΒ steps.

Inference Parameters

Inference Parameters
!python
- <'location of detect.py file'>
- --source <'location of image/ folder to predict'>
- --weight <'location of the saved best weights'>
- --output <'location to store the outputs after prediction'>
- --img-size <'Image size of the trained model'>
(Optional)
- --conf-thres <"default=0.4", 'object confidence threshold')>
- --iou-thres <"default=0.5" , 'threshold for NMS')>    
- --device <'cuda device or cpu')>
- --view-img <'display results')>
- --save-txt <'saves the bbox co-ordinates results to *.txt')>
- --classes <'filter by class: --class 0, or --class 0 2 3')>
## And there are other more customization availble, check them in the detect.py file. ##

Run the below code, to make predictions on a folder/image.

The results are good, some excerpts.

Sample Outputs
Sample Outputs

Interpret outputs from theΒ .txt fileΒ : (Optional Read)

Also, we can save the output to aΒ .txt file, which contains some of the input image’s bbox co-ordinates.

# class x_center_norm y_center_norm width_norm height_norm #
  1     0.718         0.829         0.143      0.193
  ...

Run the below code, to get the outputs inΒ .txtΒ file,

Upon successfully running the code, we can see the output are stored in the inference folderΒ here,

Output labels

Great, now the outputs in theΒ .txt file are in theΒ format,

   [ class x_center_norm y_center_norm width_norm height_norm ]
         "we need to convert it to the form specified below"
               [ class, x_min, y_min, width, height ]

[ class, X_center_norm, y_center_norm, Width_norm, Height_norm ]Β , we need to convert this into β†’[ class, x_min, y_min, width, height ]Β , ( Also De-normalized) to make plottingΒ easy.

To do so, just run the below code which performs the above transformation.

Then the output plotted image looks like this, greatΒ isn’t.

The output from the previousΒ code

4. Moving Model to Production

Just in case, if you wish to move the model to the production or to deploy anywhere, you have to follow theseΒ steps.

First, install the dependencies to run the yolov5, and we need some files from the yolov5 folder and add them to the python system path directory to load the utils. So copy them into some location you need and later move them wherever youΒ need.

So in the below picture-1, I have boxed some folders and files, you can download them and keep them in a separate folder as in picture-2.

Picture-1
Picture-2 Necessary files to load in production.

Now we need to tell the python compiler to add the above folder location into account so that when we run our programs it will load the models and functions onΒ runtime.

In the below code snippet, on line-9, I have added the sys.path… command and in that, I have specified my folder location where I have moved those files, you can replace it withΒ yours.

Then fire up these codes to start prediction.

Output from production

And there we come to the end of this post. I hope you got some idea about how to train Yolo-v5 on other datasets you have. Let me know in the comments if you face any difficulty in training and inference.

References:

To know more about Yolo-v5 in detail, check the official GithubΒ repo.

I have trained the entire BCC model in a Colab Notebook, in case if you wish to take a look at it, it’s available in the belowΒ links.

Google Colab Notebookβ€Šβ€”β€ŠTraining and Validation: link

Google Colab Notebookβ€Šβ€”β€ŠInference: link

All other supporting files and notebooks are also available at my GitHub repo here: https://github.com/bala-codes/Yolo-v5_Object_Detection_Blood_Cell_Count_and_Detection

Until then, see you nextΒ time.

Article By:

BALAKRISHNAKUMAR V

Co-Founderβ€Šβ€”β€ŠDeepScopy

Connect with me β†’ LinkedIn, GitHub, Twitter,Β Medium

https://deepscopy.com/

Visit us β†’ DeepScopy

Connect with us β†’ Twitter, LinkedIn, Medium


Yolo-v5 Object Detection on a custom dataset. was originally published in Towards AIβ€Šβ€”β€ŠMultidisciplinary Science Journal on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI

Comment (1)

  1. D
    May 4, 2021

    Hi, thank you for your post because it helps me in my project. I just want to know, since you have all your folders and files needed for production in the drive, how does the yolov5-prod.py knows where to get the files? I want to deploy the model in pi, so I just need to run the command to run the yolov5-prod.py in the terminal, to perform prediction, right? Thank you.

Feedback ↓