directory

Abstract

training

1. Download the code

2. Configure the environment

3. Prepare the data set

4. Generate data sets

5. Modify configuration parameters

6. Modify the train.py parameter

7. Check the training results

test


Abstract

YOLOV5 is not technically the fifth version of YOLO, as it was not endorsed by Joe Redmon, the father of YOLO, but the test data was generally good. Detailed data are as follows:

 

YOLOv5 is not a single model, but a family of models, including YOLOv5s, YOLOv5m, YOLOv5l, YOLOv5x and YOLOv5x+TTA, which is a bit like EfficientDet. Since no V5 paper was found, we had to learn it from the code. It is generally similar to YOLOV4 and can be considered an enhanced version of YOLOV5.

Project address: github.com/ultralytics…

training

1. Download the code

Project address: github.com/ultralytics… Recently, the author updated some of the code.

 

2. Configure the environment

Matplotlib >=3.2.2 numpy>=1.18.5 Opencv-Python >=4.1.2 Pillow PyYAML>=5.3 scipy>=1.4.1 tensorboard>=2.2 Torch >=1.6.0 Torchvision > = 0.7.0 TQDM > = 4.41.0Copy the code

 

3. Prepare the data set

The data set adopts Labelme annotated data format. Aircraft and oil drum data sets are obtained from RSOD data sets and converted into Labelme annotated data sets.

Address of data set: pan.baidu.com/s/1iTUpvA9_…

Extraction code: GR6G

Or: download.csdn.net/download/hh…

Unpack the downloaded data set and place it in the root directory of the project. Prepare for the next step of generating the test data set. The diagram below:

4. Generate data sets

The dataset of YoloV5 is not the same as the dataset of previous versions, so let’s look at the transformed dataset first.

The data structure is shown below:

The images folder holds the images for Train and Val

Labels stores the object data of train and Val, and each TXT file in labels corresponds to the picture in images.

The contents of the TXT file are as follows:

Format: Object category X Y W H

Coordinates are not real coordinates, which are calculated by dividing the coordinates by the width and height, relative to the width and height.

Let’s write the code to generate the dataset, create a new labelMetoyOLOv5.py, and write the following code.

import os

import numpy as np

import json

from glob import glob

import cv2

from sklearn.model_selection import train_test_split

from os import getcwd



classes = ["aircraft"."oiltank"]

# 1. Tag path

labelme_path = "LabelmeData/"

isUseTest = True  Whether to create a test set

# 3. Get files to be processed

files = glob(labelme_path + "*.json")

files = [i.replace("\ \"."/").split("/")[-1].split(".json") [0] for i in files]

print(files)

if isUseTest:

    trainval_files, test_files = train_test_split(files, test_size=0.1, random_state=55)

else:

    trainval_files = files

# split

train_files, val_files = train_test_split(trainval_files, test_size=0.1, random_state=55)





def convert(size, box) :

    dw = 1. / (size[0])

    dh = 1. / (size[1])

    x = (box[0] + box[1) /2.0 - 1

    y = (box[2] + box[3) /2.0 - 1

    w = box[1] - box[0]

    h = box[3] - box[2]

    x = x * dw

    w = w * dw

    y = y * dh

    h = h * dh

    return (x, y, w, h)





wd = getcwd()

print(wd)





def ChangeToYolo5(files, txt_Name) :

    if not os.path.exists('tmp/'):

        os.makedirs('tmp/')

    list_file = open('tmp/%s.txt' % (txt_Name), 'w')

    for json_file_ in files:

        json_filename = labelme_path + json_file_ + ".json"

        imagePath = labelme_path + json_file_ + ".jpg"

        list_file.write('%s/%s\n' % (wd, imagePath))

        out_file = open('%s/%s.txt' % (labelme_path, json_file_), 'w')

        json_file = json.load(open(json_filename, "r", encoding="utf-8"))

        height, width, channels = cv2.imread(labelme_path + json_file_ + ".jpg").shape

        for multi in json_file["shapes"]:

            points = np.array(multi["points"])

            xmin = min(points[:, 0]) if min(points[:, 0) >0 else 0

            xmax = max(points[:, 0]) if max(points[:, 0) >0 else 0

            ymin = min(points[:, 1]) if min(points[:, 1) >0 else 0

            ymax = max(points[:, 1]) if max(points[:, 1) >0 else 0

            label = multi["label"]

            if xmax <= xmin:

                pass

            elif ymax <= ymin:

                pass

            else:

                cls_id = classes.index(label)

                b = (float(xmin), float(xmax), float(ymin), float(ymax))

                bb = convert((width, height), b)

                out_file.write(str(cls_id) + "" + "".join([str(a) for a in bb]) + '\n')

                print(json_filename, xmin, ymin, xmax, ymax, cls_id)

ChangeToYolo5(train_files, "train")

ChangeToYolo5(val_files, "val")

ChangeToYolo5(test_files, "test")
Copy the code

 

TXT annotation data of each image will be generated in LabelmeData after the completion of this code execution, and TXT of training set, verification set and test set will be generated under TMP folder. TXT records the path of the picture, making preparation for the next generation of YoloV5 training and test data set. Create a new makedata.py file under the TMP folder to generate the final result. The directory structure is as follows:

 

Open makedata.py and write the following code.

import shutil
import os

file_List = ["train"."val"."test"]
for file in file_List:
    if not os.path.exists('.. /VOC/images/%s' % file):
        os.makedirs('.. /VOC/images/%s' % file)
    if not os.path.exists('.. /VOC/labels/%s' % file):
        os.makedirs('.. /VOC/labels/%s' % file)
    print(os.path.exists('.. /tmp/%s.txt' % file))
    f = open('.. /tmp/%s.txt' % file, 'r')
    lines = f.readlines()
    for line in lines:
        print(line)
        line = "/".join(line.split('/')[-5:]).strip()
        shutil.copy(line, ".. /VOC/images/%s" % file)
        line = line.replace('JPEGImages'.'labels')
        line = line.replace('jpg'.'txt')
        shutil.copy(line, ".. /VOC/labels/%s/" % file)
Copy the code

When the execution is complete, you can generate the data set used by the YoloV5 training. The results are as follows:

5. Modify configuration parameters

Open the VOC. yaml file and modify the configuration parametersCopy the code
Train: VOC/images/train/ #Copy the code
Val: VOC/images/val/ #Copy the code
["aircraft", "oiltank"] the name of the class, which corresponds to the list when converting the datasetCopy the code

6. Modify the train.py parameter

The CFG parameter is the configuration file of the YoloV5 model. The model files are stored in the Models folder and filled in different files as required.Copy the code

 

Weights is the pre-training model of YoloV5, corresponding to CFG. For example, CFG is yolov5s.yaml, weights is yolov5s.ptCopy the code
Data is the configuration file to configure the data set. We choose VOC. yaml, so configure data/ VOC. yamlCopy the code
Modify the above three parameters can start training, other parameters according to their own needs to modify. The modified parameters are as follows:Copy the code
parser.add_argument('--weights', type=str, default='yolov5s.pt', help='initial weights path')

parser.add_argument('--cfg', type=str, default='yolov5s.yaml', help='model.yaml path')

parser.add_argument('--data', type=str, default='data/voc.yaml', help='data.yaml path')
Copy the code

After that, you can start training. As shown below:

 
Copy the code

7. Check the training results

After going through the 300EPOCH training, we will find the trained weight file and some files for the training process under the RUNS folder. As shown in figure:

 

 

 

 

 

test

Yaml: test: TMP /test.txt = val: TMP /test.txt = val: TMP /test.txt = val

 

Modify the parameters in test.py. The following parameters need to be modified.

parser = argparse.ArgumentParser(prog=’test.py’)

parser.add_argument(‘–weights’, nargs=’+’, type=str, default=’runs/exp7/weights/best.pt’, help=’model.pt path(s)’)

parser.add_argument(‘–data’, type=str, default=’data/voc.yaml’, help=’*.data path’)

parser.add_argument(‘–batch-size’, type=int, default=2, help=’size of each image batch’)

parser.add_argument(‘–save-txt’, default=’True’, action=’store_true’, help=’save results to *.txt’)

Modify the test method at line 275 to add a path to save the test results. In this way, after the test was completed, the pictures of the test could be viewed in inference\images and the saved test results could be viewed in inference\ Output.

As shown in figure:

 

Here is the result:

Code and model: download.csdn.net/download/hh…

If there is a tip please scan the qr code below.