YOLOv7 tutorial: how to fine-tune YOLO v7 model

Thursday, July 28, 2022 by jakub.misilo
YOLOv7 tutorial: how to fine-tune YOLO v7 model

What is YOLOv7 model and why should I use it?

YOLOv7, an unrivaled object detection algorithm, achieves high-speed accuracy ranging from 5 FPS to 160 FPS. Excelling with a 56.8% AP accuracy for real-time object detection at 30 FPS or higher on GPU V100, YOLOv7 outperforms competitors and other YOLO versions. Optimized for typical GPU computing, YOLOv7-tiny caters to edge GPU, providing lightweight processing on mobile devices and distributed edge servers.

As a significant advancement in computer vision and machine learning, YOLOv7's affordability for training on small datasets without pre-trained weights sets a new bar for object detection. The popular official paper, "YOLOv7: Trainable Bag-of-Freebies Sets New State-of-the-Art for Real-Time Object Detectors," was published in July 2022, and with 4.3k stars awarded to the official GitHub repository within one month of the code's GPL-3.0 license release.

By the end of this tutorial you will be able to retrain YOLOv7 model with your custom dataset and make simple prediction on your own image.

🚀 Getting started

📚 Upload dataset

The first thing you will need to do is uploading your dataset to your Google Drive. I will use BCCD Dataset from Roboflow website, but you can use whatever data you want, but remember that it must be in a format suitable for YOLO. This article will tell you how labels should look like in YOLO format.

🔑 Important note. Remember to include information about the path to data folders in the configuration file of your data. In my case, the file structure looks like this (you can check it under the 📁 icon in the left bar).

Tutorial accompaniment image
My file structure

And the configuration file (data.yaml) looks like this:

Tutorial accompaniment image
Configuration file - data.yaml

📓 Create notebook

Then let's go to Google Colab and create a new notebook. Of course, to speed up the training process we will change the runtime type to GPU. To do this go to 'Runtime' tab, then 'Change runtime type' and in 'Hardware accelerator' option select 'GPU' and save it.

After preparing the environment, we can start coding!

🤖 Coding

💽 Connect with Google Drive and prepare the model

So let's start by connecting out Google Drive:

from google.colab import drive
drive.mount('/content/drive')

Then we have to clone YOLOv7 repository

!git clone https://github.com/WongKinYiu/yolov7.git

Let's go to cloned directory

cd yolov7

And install depedencies

!pip install -r requirements.txt

Now I will download one version of YOLOv7 model. I will use YOLOv7-tiny model, but you can use any model you want. Here you can find the model list

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

🏃🤖 Train the model!

After downloading the model, we can start training! You can feel free to adjust the parameters below. I will use these. Remember that if you change the model type (or any other thing, like data path), you must successively change its name later in this tutorial.

!python train.py --workers 8 --device 0 --batch-size 32 --data ../drive/MyDrive/dataset_1/data.yaml --img 416 416 --cfg cfg/training/yolov7-tiny.yaml --weights 'yolov7-tiny.pt' --name yolov7-tiny --hyp data/hyp.scratch.custom.yaml --epochs 40

After this step we can try to make test prediction. I will you one image from valid set. You can try to use any image you want. Just change path in --source argument.

!python detect.py --weight runs/train/yolov7-tiny/weights/best.pt --conf 0.35 --img-size 416 --source ../drive/MyDrive/dataset_1/valid/images/BloodImage_00000_jpg.rf.67c4a4312251eaa52b6ea2f2edf4855b.jpg

Now let's run all cells and wait for results! The training process can take a long time depending on on the parameters you have given and the amount and size of your data.

The model will print the metrics in real time, so you can keep track of the model's performance. It is also possible to connect an experiment tracking tool - W&B - which will return the entire training report of the model.

And there is a result of test detection. Let's see it and judge for yourself if you would be satisfied after just 18 minutes of training!

Tutorial accompaniment image

✨ Wrapping Up

YOLOv7 offers enhanced capabilities, user-friendliness, and remarkable performance, making it an ideal choice for developing and deploying applications efficiently. Eagerly anticipating future models, let's strive to propel our progress further.

Why not create an AI app using your new-found YOLOv7 knowledge?

We hope you enjoyed this YOLOv7 journey, and stay tuned for more insightful tutorials!

Thank you! - Jakub Misiło, Data Science Intern in New Native