YOLOv7 How to Use ? – Best Tutorial simple

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:

YOLOv7 results

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:

%cd yolov7

Next, we have to install all the necessary libraries to use YOLOv6.

Main libraries are the following:

  • matplotlib
  • numpy
  • opencv-python
  • Pillow
  • PyYAML
  • requests
  • scipy
  • torch
  • torchvision
  • tqdm
  • protobuf

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.

With the 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:

!wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6e.pt

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 :

!wget https://raw.githubusercontent.com/tkeldenich/How_to_use_YOLOv7_Tutorial/main/man_cafe.jpg
man_cafe.jpg – source

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: --weights yolov7-e6e.pt
  • image on which we want to apply the detection: --source ./man_cafe.jpg
!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 yolov7/runs/detect/exp/

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 :

YOLOv6_n results on man_cafe.jpg

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

Result :

YOLOv6_n results on man_cafe.jpg with 0.35 conf-thres

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 😉

Changing weights

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:

  1. Better results (or at least a better understanding of the complexity)
  2. Slower computation speed
  3. 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:

!wget weight_file_path

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!

If you want to know more about Computer Vision, feel free to check our category dedicated to this topic.

See you soon in a next post 😉

Tom Keldenich
Tom Keldenich

Data Engineer & passionate about Artificial Intelligence !

Founder of the website Inside Machine Learning

2 Comments

  1. Hi Tom. I’m working with Yolov7 and how to deploy it in IMX8M PLus. I really liked so much this article and even more the cover photo because I’m Cuban. Thank you very much.

Leave a Reply

Your email address will not be published.

Beginner, expert or just curious?Discover our latest news and articles on Machine Learning

Explore Machine Learning, browse our most recent notebooks and stay up to date with the latest practices and technologies!