The author | Jacob Solawetz, Joseph Nelson compile | | blog Flin sources
The YOLO family of target detection models has become increasingly powerful with the introduction of YOLOv5. In this article, we’ll show you how to train YOLOv5 to recognize custom objects for your custom use cases.
Many thanks to Ultralytics for putting this repository together. We believe that, combined with clean data management tools, this technology can be easily used by any developer who wants to deploy a computer vision project in their project.
We use a public blood cell test data set that you can export yourself. You can also use this tutorial on your own custom data.
- Public blood test data sets: public. Roboflow. Ai/object – dete…
To train the detector, we take the following steps:
-
Install YOLOv5 dependencies
-
Download custom YOLOv5 object detection data
-
Define YOLOv5 model configuration and architecture
-
Train a custom YOLOv5 detector
-
Evaluate YOLOv5 performance
-
Visualize YOLOv5 training data
-
Run YOLOv5 inference on the test image
-
Export the saved YOLOv5 weights for future inference
YOLOv5: What’s new?
Just two months ago, we were very excited about googlebrain’s introduction of EfficientDet and wrote some blog posts about it. We thought this model might surpass the YOLO family’s prominence in real-time target detection — and we were proved wrong.
Within three weeks, YOLOv4 was released under the Darknet framework, and we wrote more articles about breaking down YOLOv4 research.
YOLOv5 was released a few hours before writing this article, and we found it very clear.
YOLOv5 is written in the Ultralytics-Pythorch framework, which is very intuitive to use and very fast to reason. In fact, we and many others often convert YOLOv3 and YOLOv4 Darknet weights to Ultralytics PyTorch weights for faster reasoning using lighter libraries.
Does YOLOv5 perform better than YOLOv4? We’ll get to you soon, and you might have a first guess about YOLOv5 and YOLOv4.
Performance comparison of YOLOv5 and EfficientDet
YOLOv4 is obviously not evaluated in the YOLOv5 repository. That said, YOLOv5 is easier to use, and it works really well with the custom data we initially ran.
We recommend that you do the following simultaneously in the YOLOv5 Colab Notebook.
- Colab.research.google.com/drive/1gDZ2…
Install the YOLOv5 environment
Starting with YOLOv5, we’ll start by cloning the YOLOv5 repository and installing dependencies. This will set up our programming environment, ready to run object detection training and inference commands.
! git clone https://github.com/ultralytics/yolov5 # clone repo ! pip install -U -r yolov5/requirements.txt # install dependencies
%cd /content/yolov5
Copy the code
Then, we can take a look at our training environment free of charge provided to us by Google Colab.
import torch
from IPython.display import Image # for displaying images
from utils.google_utils import gdrive_download # for downloading models/datasets
print('torch %s %s' % (torch.__version__, torch.cuda.get_device_properties(0) if torch.cuda.is_available() else 'CPU'))
Copy the code
Chances are you’ll receive a Tesla P100 GPU from Google Colab. Here’s what I received:
torch 1.5. 0+cu101 _CudaDeviceProperties(name='Tesla P100-PCIE-16GB', major=6, minor=0, total_memory=16280MB, multi_processor_count=56)
Copy the code
The GPU allows us to speed up our training time. Colab is also good because it comes preloaded with torch and CUDa. If you are trying to use this tutorial locally, you may need to perform additional steps to set up YOLOv5.
Download custom YOLOv5 object detection data
In this tutorial, we will download custom object detection data in YOLOv5 format from Roboflow. In this tutorial, we train YOLOv5 to detect cells in the bloodstream using a common blood cell assay dataset. You can use public blood cell datasets or upload your own.
- Roboflow: Roboflow. Ai /
- Public blood data sets: public. Roboflow. Ai/object – dete…
A quick explanation of the tagging tool
If you have untagged images, you need to tag them first. For free open source tagging tools, we recommend the guide to Getting started with LabelImg or CVAT Annotation Tools. Try tagging about 50 images before continuing with this tutorial. To improve the performance of the model later, you will need to add more tags.
- Ai/getting blog. Roboflow. – the sta…
- Ai/getting blog. Roboflow. – the sta…
Once you’ve tagged the data, to move it to Roboflow, create a free account, and then you can drag the data set in any format (VOC XML, COCO JSON, TensorFlow object detection CSV, etc.).
After uploading, you can select pre-processing and enhancement steps:
Settings selected for the BCCD sample dataset
Then, click Generate and Download and you will be able to select the YOLOv5 Pythorch format.
Select “YOLO V5 Pythorch”
Be sure to select “Show Code Snippet” when prompted. This will output a download curl script so that you can easily port the data to Colab in the correct format.
curl -L "https://public.roboflow.ai/ds/YOUR-LINK-HERE" > roboflow.zip; unzip roboflow.zip; rm roboflow.zip
Copy the code
Downloading in Colab…
Download the custom object dataset in YOLOv5 format
The export creates a yolov5. yaml file named data.yaml, specifying the location of the YOLOv5 images folder, YOLOv5 Labels folder, and information about the custom classes.
Define YOLOv5 model configuration and architecture
Next, we write a model configuration file for our custom object detector. In this tutorial, we chose the smallest and fastest YOLOv5 base model. You can choose from other YOLOv5 models, including:
- YOLOv5s
- YOLOv5m
- YOLOv5l
- YOLOv5x
You can also edit the network structure in this step, but this is generally not required. Here is the YOLOv5 model configuration file, which we named custom_yolov5s.yaml:
nc: 3
depth_multiple: 0.33
width_multiple: 0.50
anchors:
- [10.13.16.30.33.23]
- [30.61.62.45.59.119]
- [116.90.156.198.373.326]
backbone:
[[-1.1, Focus, [64.3]], [...1.1, Conv, [128.3.2]], [...1.3, Bottleneck, [128]], [...1.1, Conv, [256.3.2]], [...1.9, BottleneckCSP, [256]], [...1.1, Conv, [512.3.2]], [...1.9, BottleneckCSP, [512]], [...1.1, Conv, [1024.3.2]], [...1.1, SPP, [1024[5.9.13]]], [...1.6, BottleneckCSP, [1024]],
]
head:
[[-1.3, BottleneckCSP, [1024.False]], [...1.1, nn.Conv2d, [na * (nc + 5), 1.1.0]], [...2.1, nn.Upsample, [None.2."nearest"]], [[...1.6].1, Concat, [1]], [...1.1, Conv, [512.1.1]], [...1.3, BottleneckCSP, [512.False]], [...1.1, nn.Conv2d, [na * (nc + 5), 1.1.0]], [...2.1, nn.Upsample, [None.2."nearest"]], [[...1.4].1, Concat, [1]], [...1.1, Conv, [256.1.1]], [...1.3, BottleneckCSP, [256.False]], [...1.1, nn.Conv2d, [na * (nc + 5), 1.1.0]],
[[], 1, Detect, [nc, anchors]],
]
Copy the code
Training custom YOLOv5 detector
Our data.yaml and custom_yolov5s.yaml files are ready and we are ready to train!
To start the training, we run the training command with the following options:
-
Img: defines the input image size
-
Batch: Determines the batch size
-
Epochs: Defines the number of training periods. (Note: Usually, 3000+ is common!)
-
Data: Sets the path of the YAML file
-
CFG: specifies our model configuration
-
Weights: custom path to specify weights. (Note: You can download weights from the Ultralytics Google Drive folder.)
-
Name: indicates the result name
-
Nosave: Saves only the last checkpoint
-
Cache: Cache images to speed up training
Run training commands:
Training custom YOLOv5 detector. He trains fast!
During training, if you want to see [email protected] to see how your probe works, see this article.
- Blog. Roboflow. Ai/what as-is – mea…
Evaluate custom YOLOv5 detector performance
Now that we have completed the training, we can evaluate the execution of the training process by looking at validation metrics. The training script will delete the TensorBoard log. Let’s visualize it:
Visualize tensorBoard results on our custom data set
If for some reason you cannot visualize the tensor plate, the result can also be drawn using utils.plot_result and saved as result.png.
I stopped training early. You need to get the trained model weights at the point where the validation map reaches its highest point.
Visualize YOLOv5 training data
During training, the YOLOv5 training pipeline creates batches of training data through enhancement. We can visualize training data authenticity and enhance training data.
Our actual training data
Our training data were enhanced by automatic YOLOv5
Run YOLOv5 inference on the test image
Now we use our trained model to make inferences about the test images. After training, model weights will be saved to weights/.
For reasoning, we call these weights along with a CONF that specifies model confidence (the higher the confidence required, the fewer the predictions) and an inference source. The source can accept a directory containing images, individual images, video files, and the device’s webcam port. For the source code, I moved test/* JPG to test-infer/.
! Python detect.py --weights weights/last_yolov5s_custom.pt --img 416 --conf 0.4 --source.. /test_inferCopy the code
The reasoning time is very fast. On our Tesla P100, YOLOv5s achieved 142 frames per second!!
YOLOv5s is inferred at 142 FPS (0.007s/ image)
Finally, we visualize our detector inferences on test images.
Test the YOLOv5 reasoning of the image
Export the saved YOLOv5 weights for future inference
Now that our custom YOLOv5 object detector has been validated, we may need to take weights out of Colab for real-time computer vision tasks. To do this, we import a Google Drive module and send it out.
from google.colab import drive
drive.mount('/content/gdrive')
%cp /content/yolov5/weights/last_yolov5s_custom.pt /content/gdrive/My\ Drive
Copy the code
conclusion
We hope you enjoy training your custom YOLOv5 detector!
Using YOLOv5 is very convenient. YOLOv5 trained quickly, reasoned quickly and performed well. Let’s get it out!
Blog.roboflow. ai/how-to-trai…
Welcome to panchuangai blog: panchuang.net/
Sklearn123.com/
Welcome to docs.panchuang.net/