In this article, we will learn how to use YOLOv7: how to implement it, understand the results and use different weights!
YOLOv7 is the second version of YOLO to be published this year 2022.
YOLO stands for You Only Look Once. It is a Deep Learning model used for detection on images and videos.
The first version of YOLO was released in 2016. Since then, frequent updates are made with the latest improvements: faster computation, better accuracy.
We’ve already a tutoriel on how to use YOLOv6. This post will be pretty similar as we want to stick to the basics.
We’ll use the same image as a test to compare both models performance… but keep in mind, performance on one image isn’t performance of the entire model. It’s just a nice hint to start understand both models.
Here are the results of YOLOv6 compared to other versions on the COCO dataset:
Let’s get to it !
How to use YOLOv7 ?
The following lines of code work for any Notebook/Colab instance. If you want to run YOLOv6 on a terminal/locally, just remove the first “!” or “%” from each line of code.
To use YOLOv7, we first need to download the Github repository!
To do this, we’ll use the
git clone command to download it to our Notebook:
!git clone https://github.com/WongKinYiu/yolov7.git
Then, we place ourselves in the folder we just downloaded:
Next, we have to install all the necessary libraries to use YOLOv6.
Main libraries are the following:
Fortunately, only one line of code is needed to install all these dependencies:
!pip install -r requirements.txt
We then want to download the weights of the Neural Network.
git clone command, we’ve downloaded all the architecture of the Neural Network (layers of the model, functions to train it, use it, evaluate it, …) but to use it, we also need the weights.
In a Neural Network, the weights are the information obtained by the model during training.
You can manually download any version of the weights here and then put the file in the yolov7 folder.
Or easily download it with this line of code:
At the time I’m writing those lines, YOLOv7 has just been released. Updates may occur and the weights may change, as well as the URL of their repository. If the link no longer works, check this Github for the latest version.
One last thing before using the template: upload your image!
Either a single image, or several in a folder (YOLOv7 can handle several images at once).
Feeling lazy ? You can simply upload our test image in one line of code :
FINALLY, we can use YOLOv7!
To do this, we’ll call the detect.py file.
The python code in this file will run the detection for us.
We only need to specify a few important parameters, in our case :
- weights we are using:
- image on which we want to apply the detection:
!python detect.py --weights yolov7-e6e.pt --source ./man_cafe.jpg
If you use your own image or folder, just change this last part to ./your_path.
Understand the results
You’ll find the detection output in the folder
Carefull, if you run several experimentation, a new expN folder will be created. For your second experimentation it will be exp2. For the third, exp3, etc.
Here is our result :
First of all, we can see that YOLOv7 has a very good detection capacity. It even detects the person right behind our main character.
You can see that it even indicates the confidence with which it detects objects/people. Most obvious objects are detected with a high confidence threshold. Comparing with YOLOv6 it detect every object on the first plan: two laptop, cellphone, book, cup. On this image, YOLOv7 is really amazing!
So right now we have the default confidence threshold of 0.25. It means that every detection under the confidence threshold of 25% will be remove.
The confidence threshold defines the minimum confidence value at which detection must be kept.
What if we wanted to increase this threshold?
Actually it’s possible when running YOLOv7.
Remember that we have defined some parameters. The mandatory parameters are the weights and the source (image path), but there are also default parameters that you can change to suit your needs.
There are other parameters, for example:
- Confidence threshold: conf-thres (default value: 0.25)
- Intersection over Union threshold: iou-thres (default value: 0.45)
Let’s change the confidence threshold to 0.6 :
!python detect.py --weights yolov7-e6e.pt --conf 0.60 --img-size 640 --source ./man_cafe.jpg
Here we see that the book hasn’t been kept.
Use YOLOv7, make assumptions for your project and set the confidence threshold that suits your need 😉
Now you understand a bit more what YOLOv7 is.
Let’s go deeper.
There are different versions of YOLOv7 weights.
We have used a large version of these weights.
Large weights mean three things compared to the other versions:
- Better results (or at least a better understanding of the complexity)
- Slower computation speed
- More memory space used
But what if you have space and time constraints?
Well, ou can use other versions of these weights:
- Small: https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-tiny.pt
- Medium: https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt
- Large: https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6e.pt
Again, you can download them manually here and then put them into your folder, or use
wget and specify the version you want:
Depending on the weight version you’ve chosen, you will have :
- Small: yolov7-tiny.pt
- Medium: yolov7.pt
- Large: yolov7-e6e.pt
To use it, replace weight_file here:
!python tools/infer.py --weights weight_file --source ./man_cafe.jpg
Now you know how to use YOLOv7!
See you soon in a next post 😉