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

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

YOLO V5 is Here! Custom Object Detection Tutorial with YOLO V5
Data Science

YOLO V5 is Here! Custom Object Detection Tutorial with YOLO V5

Last Updated on June 15, 2020 by Editorial Team

Author(s): Mihir Rajput

Data Science

Source: https://pjreddie.com/

YOLO β€œYou Only Look Once” is one of the most popular and most favorite algorithms for AI engineers. It always has been the first preference for real-time object detection.

YOLO has emerged so far since it’s the first release. Let’s briefly discuss earlier versions of YOLO then we will jump straight into the trainingΒ part.

Previous YOLOΒ Releases

YOLO v1 was introduced in May 2016 by Joseph Redmon with paper β€œYou Only Look Once: Unified, Real-Time Object Detection.” This was one of the biggest evolution in real-time object detection.

In December 2017, Joseph introduced another version of YOLO with paper β€œYOLO9000: Better, Faster, Stronger.” it was also known as YOLOΒ 9000.

After a year in April 2018, the most popular and stable version of YOLO was introduced. Joseph had a partner this time and they released YOLOv3 with paper β€œYOLOv3: An Incremental Improvement”.

Finally, in April 2020, Alexey Bochkovskiy introduced YOLOv4 with paper β€œYOLOv4: Optimal Speed and Accuracy of Object Detection” Alexey is not the official author of previous versions of YOLO but Joseph and Ali took a step back from YOLO someone has to handle theΒ era.

YOLOv4 was introduced with some astounding new things, It outperformed YOLOv3 with a high margin and also has a significant amount of average precision when compared to EfficientDet Family.

Source: YOLOv4Β paper.

After a few days on 9 June 2020, just four days back another unofficial author Glenn Jocher released YOLOv5. There are lots of controversies about the selection of the name β€œYOLOv5” and other stuff. Glenn introduced PyTorch based version of YOLOv5 with exceptional improvements. Hence he has not released any official paperΒ yet.

This version is pretty amazing and outperforms all the previous versions and got near to EfficientDet AP with higher FPS. You can notice that in the belowΒ graph.

source: https://github.com/ultralytics/yolov5

For further comparisons please visit https://blog.roboflow.ai/yolov4-versus-yolov5/.

Train YOLOv5 on a CustomΒ Dataset

This part consists of multiple steps as listedΒ below,

  1. Preparing Dataset
  2. Environment Setup
  3. Configure/modify files and directory structure
  4. Training
  5. Inference
  6. Result

The code for this tutorial can be found on this GitHub repository.

Preparing Dataset

Once you get the labeled dataset in YOLO format you’re good toΒ go.

In this tutorial, we will be using an elephant detection dataset from the open imageΒ dataset.

To get the labeled dataset you can search for an open-source dataset or you can scrap the images from the web and annotate them using tools like LabelImg.

NOTE: Your annotation format should be in the YOLOΒ format.

Make sure you keep your annotations and images in the same directory.

Then generate train, test, and validation txt files, to do that just copy image files and paste the path into txt files. Best practice would be to keep 70% data in the training set, 20% in the validation set, and 10 % in the testingΒ set.

For this tutorial, I will use this train, test, and validation txtΒ files.

Also, I have added a labeled dataset here, the images directory contains all the images and the txt directory contains all the annotations.

Environment Setup

The important thing to notice is that you will require PyTorch version β‰₯ 1.5, Python version 3.7, and CUDA versionΒ 10.2.

The rest of the below dependencies can easily be installed using pip or the requirement.txt file.

numpy==1.17
scipy==1.4.1
cudatoolkit==10.2.89
opencv-python
torch==1.5
torchvision==0.6.0
matplotlib
pycocotools
tqdm
pillow
tensorboard
pyyaml

NOTE: For this tutorial, I have used ubuntuΒ 16.04.

Once you successfully install all the dependencies you are ready to moveΒ further.

Configure/modify files and directory structure

To train the YOLOv5 model you will need to perform someΒ steps.

First, start with the cloning repository for YOLOv5. You can clone from here if you are following this tutorial. You can clone from official repo as well formΒ here.

Second, modify you need to add the YAML file to describe your dataset parameters. Please refer following YAML file and modify accordingly as per yourΒ need.

# here you need to specify the files train, test and validation txt # files created in step 1.
train: /self/elephant_dataset/train_elephant.txt
val: /self/elephant_dataset/val_elephant.txt
test: /self/elephant_dataset/test_elephant.txt
# number of classes in your dataset
nc: 1
# class names
names: ['Elephant']

We will use this file later to train theΒ model.

Training

To train the YOLOv5 Glenn has proposed 4 versions.

  1. yolov5-s which is a smallΒ version
  2. yolov5-m which is a mediumΒ version
  3. yolov5-l which is a largeΒ version
  4. yolov5-x which is an extra-large version

You can see their comparison here.

While training you can pass the YAML file to select any of these models. All YAML files are presentΒ here.

Now everything is configured and we are ready to train our YOLOv5Β model!

Move to the directory and use the following command to start training.

python train.py --img 640 --batch 8 --epochs 30 --data ./data/elephant.yaml --cfg ./models/yolov5s.yaml --weights '' --device 0
  • β€” img: size of the inputΒ image
  • β€” batch: batchΒ size
  • β€” epochs: number ofΒ epochs
  • β€” data: YAML file which was created in stepΒ 3
  • β€” cfg: model selection YAML file. I have chosen β€œs” for this tutorial.
  • β€” weights: weights file to apply transfer learning, you can find themΒ here.
  • β€” device: to select the training device, β€œ0” for GPU, and β€œcpu” forΒ CPU.

This command will start the model training immediately. I have decided to train the model for 30Β epochs.

Once the training is completed, the model will be saved in your β€œweights” directory and the resulting matrix graph will be generated asΒ follows.

source: https://github.com/mihir135

As we can see the model is trained pretty well the [email protected] is almost 0.78 which is prettyΒ good!

Inference

Now the model has been trained so let’s test its performance on someΒ images.

To run the model inference use the following command.

python detect.py --source sample_img/  --weights weights/best.pt --conf 0.4
  • β€” source: input images directory or single image path or videoΒ path
  • β€” weights: trained modelΒ path
  • β€” conf: confidence threshold

This will process the input and store the output in your inference directory.

Result

Here I am attaching some output images from trained model inference.

source: https://github.com/mihir135/yolov5

Pretty awesomeΒ right!

You can find a trained elephant detectorΒ here.

You can download and explore code for this tutorial fromΒ here.

If you have any doubts you can comment in the responseΒ section.

My GitHub or LinkedIn.

Special thanks to Mayur Patel for contributions and collaboration.

Any feedback or suggestions would be appreciated.


YOLO V5 is Here! Custom Object Detection Tutorial with YOLO V5 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

Feedback ↓