How to Train a Custom Object Detection Model with YOLOv7?

Parthiban Marimuthu 04 Aug, 2022 • 5 min read

This article was published as a part of the Data Science Blogathon.


One of the most common jobs in computer vision is object detection. It is the foundation for comprehending and interacting with the scene.

Object detection is used in everything from simple applications like detecting items to complicated jobs like self-driving automobiles to understand diverse scenarios and make judgments based on them. Security cameras and even current cellphones have similar features built in for a variety of functions.

What is YOLO?

Nowadays, YOLO (You Only Look Once) is the better object detection model framework, and this model is the latest addition to the YOLO family of models. YOLO was the first object detection model to incorporate bounding box prediction and object classification into a single end-to-end differentiable network. It was created and is maintained under the Darknet framework. YOLOv5 is the first YOLO model written on the PyTorch framework, and it is much lighter and easier to use. However, YOLOv7 does not outperform YOLOv6 on a standard benchmark, the COCO dataset, because it did not make fundamental architectural improvements to the network in YOLOv6.


Yolo v6 has a few flaws, such as poor performance on tiny items and poor generalization when the dimensions of the objects are not equal.
This is a picture from the original YOLO paper demonstrating the YOLO operation. It’s come a long way since then, and we’re now on version 5. Despite the fact that it was not written by any of the original authors or contributors, it follows the same basic strategy. It is written in PyTorch, which is a plus. In this version of Yolo mosaic, augmentation is used, and augmentation and different scaling approaches provide numerous enhancements.

Procedure – Prepare the Dataset

To get your object detector up and running, you must first collect training photographs. You should think carefully about the activity you are attempting to complete and plan ahead of time for the components of the task that your model may find difficult. To improve the accuracy of your final model, I recommend reducing the domain that your model must handle as much as feasible.

For YOLOv7 custom training, we need to develop a dataset. If you don’t have any data, you can use the openimages database.

Annotating the Dataset

Use LabelImg or any annotation tool to annotate the dataset. Create a file with the same name as the image and the annotation text.

Prepare a set, for example, corresponding to

  • images_0.jpg
  • images_0.txt

YOLOv7 accepts label data in text (.txt) files in the following format:

Annotating the dataset| yolo

Split Dataset

After you’ve tagged your data, we’ll divide it into train and test folders. The split ratio will be determined by the user, however the most common split is (80-20) percent, which implies that 80 percent of the data is utilized for training and 20 percent for testing. *The images and labels are stored in the stated folder architecture.

*For data splitting, look into python library – Split Folder, which will randomly divide your data into train, test, and validation.

The following pip command to install the data splitting library

pip install split-folders

The input folder should be formatted as follows:


In order to provide you with this:


Separate the files into a training and a validation set (and optionally a test set). Final dataset folder looks like below before get into YOLOv7 training,

├── yolov7
## └── train
####└── images (folder including all training images)
####└── labels (folder including all training labels)
## └── test
####└── images (folder including all testing images)
####└── labels (folder including all testing labels)
## └── valid
####└── images (folder including all valid images)
####└── labels (folder including all valid labels)

Create Custom Config File for Training

We must now develop a customized configuration file. (Be sure to specify the proper directory), as the training process will be entirely dependent on that file.

Create a file with the name “custom.yaml” in the (yolov7/data) folder. In that file, paste the code below. Set the correct path to the dataset folder, alter the number of classes and their names, and then save it.

Make a file that specifies the training configuration. In custom.yaml file, write the following:

  • Image_path
  • Number_of_classes
  • Classes_names_array
train: (Complete path to dataset train folder)
test: (Complete path to dataset test folder)
valid: (Complete path to dataset valid folder)
nc: 1 # replace classes count 
#classes names
#replace all class names list with your custom classes
namesnames: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
        'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
        'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
        'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
        'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
        'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
        'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
        'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear']

After all the preprocessing procedures have been completed, it is ready to begin training. Launch the terminal in the main “yolov7“, activate the virtual environment, and execute the commands listed below.

git clone       # clone
cd yolov7
pip install -r requirements.txt      # install modules
wget # download pretrained weight

The official repository contains pretrained weights for your model.

Depending on the usage context, such as  Google Colab, GPU memory may be insufficient. You can learn by reducing the batch size in that situation.


python --weights --data "data/custom.yaml" --workers 4 --batch-size 4 --img 416 --cfg cfg/training/yolov7.yaml --name yolov7 --hyp data/hyp.scratch.p5.yaml

— img = size of images on which model will train; the default value is 640.

— batch-size = batch size used for custom dataset training.

epochs = number of training epochs to get the best model

— data = custom config file path

— weights = pretrained yolov7 weights (

Note: If any image is corrupted, training will not begin. If any label file is corrupted, training will not begin because yolov7 will ignore that image and label files.

Wait for training to finish before performing inference with freshly formed weights. Custom-trained weights will be saved in the following folder path.


Custom Weights Inference

When training is over, go to the terminal and perform the below command for detection on custom weights.

python --weights runs/train/yolov7/weights/ --source "path to your testing image"


You may use YOLO to design your own custom detection model for anything you desire.
Yolo v7 is a significant advance in terms of speed and accuracy, and it matches or even outperforms RPN-based models. The model is fast and dependable, and it can now be used for anything.

That’s all there is to “Train YOLOv7 on Custom Data.” You can experiment with your own data.YOLOv7 is lightweight and simple to use. YOLO v7 trains quickly, makes good conclusions, and performs well.

The key lessons from the aforementioned YOLOV7 are summarized as follows:

  • We gained knowledge about what’s YOLO and how it works and each use case which YOLO version model we can use to get desired output.
  • However, it significantly improves how quickly people can incorporate YOLO into their current pipelines. The main distinction between YOLO v7 and the earlier versions from v1–v6, which were developed in C, is that v7 was written in PyTorch / Python.
  • By itself, this greatly increases accessibility for those involved in deep learning as well as businesses, and We gained knowledge about how to train YOLOv7 Custom Dataset Training.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers


Computer Vision
Become a full stack data scientist

  • [tta_listen_btn class="listen"]