YOLO series — YOLOV7 algorithm (1): Use a custom data set to run through the YOLOV7 algorithm

YOLO series—YOLOV7 algorithm (1): Use custom data sets to run through the YOLOV7 algorithm

Isn’t this embarrassing? . . Just after I understood the YOLO V6 algorithm from Perfect Group, V7 came out. . . And the most important thing is that V7 is also endorsed by the author of V4. Fortunately, both V6 and V7 are modified codes based on YOLO V5, so the code reads more smoothly. The YOLOV7 algorithm is intended to be explained according to the following structure:

  1. YOLOV7 algorithm (1): Use a custom data set to run through the YOLOV7 algorithm (including interpretation of the role of the entire project file)
  2. YOLO series—YOLOV7 algorithm (2): YOLO V7 algorithm detect.py code analysis
  3. YOLO series—YOLOV7 algorithm (3): YOLO V7 algorithm train.py code analysis
  4. YOLO series—YOLOV7 algorithm (4): YOLO V7 algorithm network structure analysis
  5. Important code analysis during training
  6. YOLO series—YOLOV7 algorithm (6): YOLO V7 algorithm onnx model deployment
  7. YOLO series—YOLOV7 algorithm (7): Summary of YOLOV7 algorithm

1. Analysis of YOLO V7 algorithm project files

Code link:YOLO V7
First, clone the entire project folder from git, as shown in the figure below:

Let’s introduce the functions of important folders and files respectively:

  • cfg: stores the network structure yaml file required for training
  • customdata: This folder stores user-defined data sets. This was created when I later trained v7. It was not available when I first downloaded it.
  • data: yaml file that stores hyperparameters and training-related data during the training process
  • models: stores the relevant python files that define the network structure and the code required to export the model deployment
  • runs: stores weight files and related information for training and testing
  • utils: stores some codes necessary for running the code
  • detect.py: script for testing
  • requirement.txt: libraries that need to be downloaded for training and testing
  • train.py: script used to start training

1. Create a training environment

Generally, I recommend that everyone use conda or miniconda to create a virtual environment to train the model. This not only isolates the local environment and avoids contaminating the local environment. It also avoids problems when downloading the corresponding library version when creating a training environment. For the specific installation method of conda or miniconda, please contact Du Niang directly. There are a lot of them above, so I won’t go into details here~
After downloading conda:

conda create -n yolov7 python=3.7
conda activate yolov7
pip install -r your_yolov7_path/requirement.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

At present, the author of yolo v7 does not seem to specify the python version. Here I chose version 3.7 (the author did not report an error when using 3.7).

2. Create a custom data set

There is a folder in the above: customdata, which is the storage path of the custom data set.

The picture above shows the specific storage rules. Only a few pictures are used here for simplicity. Create images and labels folders to store training and test valid images and labels respectively. Below you can use the code I provided to create train.txt and valid.txt.

import glob

#The address where the image is stored
train_image_path = r"your_yolov7_path/customdata/images/train/"
valid_image_path = r"your_yolov7_path/customdata/images/valid/"
#Path of the generated txt
txt_path = r"your_yolov7_path/customdata/"

def generate_train_and_val(image_path, txt_file):
    with open(txt_file, 'w') as tf:
        for jpg_file in glob.glob(image_path + '*.jpg'):
            tf.write(jpg_file + '\n')

generate_train_and_val(train_image_path, txt_path + 'train.txt')
generate_train_and_val(valid_image_path, txt_path + 'valid.txt')

3. Start training

First, inyour_yolov7_path/dataCreate a custom_data.yaml file inside and write:

train: your_yolov7_path/customdata/train.txt
val: your_yolov7_path/customdata/valid.txt
test: your_yolov7_path/customdata/valid.txt
# number of classes
# class names
names: []

inncandnamesModify according to your own custom data set.
Then, open the network structure yaml file to be selected. Here I will selectcfg/training/yolov7.yaml,Revisencvalue.
Finally, open the train.py file and make appropriate parameter modifications according to your needs:

parser.add_argument('--weights', type=str, default='', help='initial weights path')
    parser.add_argument('--cfg', type=str, default=r'cfg/training/yolov7x.yaml', help='model.yaml path')
    parser.add_argument('--data', type=str, default='data/custom_data.yaml', help='data.yaml path')
    parser.add_argument('--hyp', type=str, default='data/hyp.scratch.p5.yaml', help='hyperparameters path')
    parser.add_argument('--epochs', type=int, default=300)
    parser.add_argument('--batch-size', type=int, default=24, help='total batch size for all GPUs')
    parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='[train, test] image sizes')
    parser.add_argument('--rect', action='store_true', help='rectangular training')
    parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training')
    parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
    parser.add_argument('--notest', action='store_true', help='only test final epoch')
    parser.add_argument('--noautoanchor', action='store_true', help='disable autoanchor check')
    parser.add_argument('--evolve', action='store_true', help='evolve hyperparameters')
    parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')
    parser.add_argument('--cache-images', action='store_true', help='cache images for faster training')
    parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')
    parser.add_argument('--device', default='cpu', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
    parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')
    parser.add_argument('--adam', action='store_true', help='use torch.optim.Adam() optimizer')
    parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')
    parser.add_argument('--local_rank', type=int, default=-1, help='DDP parameter, do not modify')
    parser.add_argument('--workers', type=int, default=8, help='maximum number of dataloader workers')
    parser.add_argument('--project', default='runs/train', help='save to project/name')
    parser.add_argument('--entity', default=None, help='W&B entity')
    parser.add_argument('--name', default='exp', help='save to project/name')
    parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
    parser.add_argument('--quad', action='store_true', help='quad dataloader')
    parser.add_argument('--linear-lr', action='store_true', help='linear LR')
    parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')
    parser.add_argument('--upload_dataset', action='store_true', help='Upload dataset as W&B artifact table')
    parser.add_argument('--bbox_interval', type=int, default=-1, help='Set bounding-box image logging interval for W&B')
    parser.add_argument('--save_period', type=int, default=-1, help='Log model after every "save_period" epoch')
    parser.add_argument('--artifact_alias', type=str, default="latest", help='version of dataset artifact to be used')
python train.py

Done! ! !

Related Posts

Introduction to phase extraction algorithm (Phase Retrieval/PR) (to be updated)

Tutorial on creating python virtual environment based on Anaconda under Windows

9. Generalized table – Generalized table concept, storage structure, depth/length, copy algorithm

Based on python and deep learning (speech recognition, NLP) to implement local offline intelligent voice control terminal (with chat function and home control function)

Supplementary questions for ZJUT’s 19th “Hangyin Financial Management Cup”

How to convert list to string Python?

Basic tutorial for getting started with Django. (1. Overview and installation of Django)

How to understand Python classes and functions?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>