Object Detection by Pytorch YoLov5 part(5)



Hello, this is swim-lover. Object detection is done with Python and Pytorch. I’ve just started Python, but I’m studying with the concept of “learning while using”.

In Part (3), object detection was performed using YoLov3. This time, I would like to try YoLov5.


YoLov5 was released on June 9, 2020. As of August 27, 2022, it seems that it has progressed to YoLov7.

I used the following repository as a reference.

GitHub - ultralytics/yolov5: YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite. Contribute to ultralytics/yolov5 development by creating an account on GitHub.

There are 5 versions YoloV5 ,”n, s, m, l, x” as of July 28, 2022.

vesion “n” is 4.5FLOPS and the processing load is the lowest. As a result,
The processing speed is fastest.

On the other hand, mAPval, an evaluation index for object detection, has the lowest score.

Setting up Colab

Let’s try it on Colab.

Mount google driver on Colab.

from google.colab import drive

Move to directry ‘test_yolo_v5’ which was made beforehand.

cd drive/MyDrive
cd test_yolo_v5

Execute git clobe command.

!git clone https://github.com/ultralytics/yolov5
cd yolov5

And also, Install the releted module required to run YoLov5.

pip install -r requirements.txt


Try inference processing using a trained model.

Import pytorch

import torch

Load a trained model. The parameter is set using yolo5s.

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # or yolov5n - yolov5x6, custom

Download a sample image file.

# Images
img = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, PIL, OpenCV, numpy, list

Execute inference.

# Inference
results = model(img)

Check the result.

# Results

It was an amazing image.

Try an another image. This is my own image which was used this page.

Two overlapping bikes were recognized as one bike.

Try next image.

In addition to detecting four bicycles, Bottle can also be detected. YoLov3 did not detect Bottle.


This time, I tried to YoLov5. I would like to try object detection from the next time onwards.