[Study notes] YOLOv5 trains its own data set

Train your own data set to summarize, which is convenient for the next study

Table of contents

1. Set folder

2. Label your own dataset

2.1 Find the pictures you want on Baidu Pictures and download them in batches

2.2 Use of labelimg software

3. Modify the configuration file

3.1AOCAO parameter.yaml

3.2AOCAO model.yaml

4. Start training

4.1 Change the code

4.2 Training process

4.3 Results

5. Detect images

5.1 Change the code

5.2 Running the code

5.3 View Results


First attach the yolov5 source code
https://pan.baidu.com/s/1qFaLYRWO4uUGtv1XjqedXQ 
Extraction code: 85Z8 
Copy this content and open "Baidu Netdisk APP to get it"

Refer to [Yolov5] 1. Conscientiously summarize the 6000-word Yolov5 nanny-level tutorial (2022.06.28 new version v6.1)_yolov5 tutorial_Ruooochen's Blog-CSDN Blog

1. Set folder

The folder is created according to the following directory

 

2. Label your own dataset

2.1 Find the pictures you want on Baidu Pictures and download them in batches

reference blog

Python crawler series (2) - Python crawler downloads Baidu pictures in batches_Crawler download pictures_Paper photo blog-CSDN blog

Put the downloaded pictures in the folder datasets-images-train you just created

Put a small number of downloaded pictures in datasets-images-test (not duplicated with pictures in train)

2.2 Use of labelimg software

reference blog

Installation and use tutorial of LabelImg (target detection and labeling tool) - CSDN Blog

After marking the picture, save it to datasets——labels——train (the test test set is the same)

Saved in xml format, to be converted to txt format (notebook open)

The conversion code is as follows

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join


def convert(size, box):
    # size=(width, height)  b=(xmin, xmax, ymin, ymax)
    # x_center = (xmax+xmin)/2        y_center = (ymax+ymin)/2
    # x = x_center / width            y = y_center / height
    # w = (xmax-xmin) / width         h = (ymax-ymin) / height

    x_center = (box[0] + box[1]) / 2.0
    y_center = (box[2] + box[3]) / 2.0
    x = x_center / size[0]
    y = y_center / size[1]

    w = (box[1] - box[0]) / size[0]
    h = (box[3] - box[2]) / size[1]

    # print(x, y, w, h)
    return (x, y, w, h)


def convert_annotation(xml_files_path, save_txt_files_path, classes):
    xml_files = os.listdir(xml_files_path)
    # print(xml_files)
    for xml_name in xml_files:
        # print(xml_name)
        xml_file = os.path.join(xml_files_path, xml_name)
        out_txt_path = os.path.join(save_txt_files_path, xml_name.split('.')[0] + '.txt')
        out_txt_f = open(out_txt_path, 'w')
        tree = ET.parse(xml_file)
        root = tree.getroot()
        size = root.find('size')
        w = int(size.find('width').text)
        h = int(size.find('height').text)

        for obj in root.iter('object'):
            difficult = obj.find('difficult').text
            cls = obj.find('name').text
            # if cls not in classes or int(difficult) == 1:
            #     continue
            cls_id = classes.index(cls)
            xmlbox = obj.find('bndbox')
            b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
                 float(xmlbox.find('ymax').text))
            # b=(xmin, xmax, ymin, ymax)
            # print(w, h, b)
            bb = convert((w, h), b)
            out_txt_f.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')


if __name__ == "__main__":
    # 把forklift_pallet的voc的xml标签文件转化为yolo的txt标签文件
    # 1、需要转化的类别
    classes = ['凹槽']
#classes=['helmet','head_with_helmet','person_with_helmet','head','face','person_no_helmet','person']
    # 2、voc格式的xml标签文件路径
    xml_files1 = r'D:\pycharmProject\pythonProject1\AoXingCao\yolov5-5.0\aocao\datasets\labels\test\xml'
    # xml_files1 = r'C:/Users/GuoQiang/Desktop/数据集/标签1'

    # 3、转化为yolo格式的txt标签文件存储路径
    save_txt_files1 = r'D:\pycharmProject\pythonProject1\AoXingCao\yolov5-5.0\aocao\datasets\labels\test\txt'

    convert_annotation(xml_files1, save_txt_files1, classes)
需要更改的为xml目录以及txt目录

3. Modify the configuration file

3.1AOCAO parameter.yaml

Copy the yolov5/data/coco128.yaml file to the same directory as datasets, and name it Aocao_parameter.yaml

What needs to be modified is nc and names, path, train, nc is the number of label names, and names is the name of the label (only one of my labels is a groove, so nc is 1)

path is the absolute path, train and val are the paths under the absolute path respectively (I wrote all the paths for fear of making mistakes)

# COCO 2017 dataset http://cocodataset.org - first 128 training images
# Train command: python train.py --data coco128.yaml
# Default dataset location is next to /yolov5:
#   /parent_folder
#     /coco128
#     /yolov5


# download command/URL (optional)
download: https://github.com/ultralytics/yolov5/releases/download/v1.0/coco128.zip

# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
path: D:\pycharmProject\pythonProject1\yolo xunlian\AoXingCao\yolov5-5.0\Aocao\datasets
train: D:\pycharmProject\pythonProject1\yolo xunlian\AoXingCao\yolov5-5.0\Aocao\datasets\images\train  # 128 images
val: D:\pycharmProject\pythonProject1\yolo xunlian\AoXingCao\yolov5-5.0\Aocao\datasets\images\test  # 128 images

# number of classes
nc: 1

# class names
names: [ '凹槽']

3.2AOCAO model.yaml

Copy the yolov5/models/yolov5s.yaml file to the same level directory as datasets, and name it Aocao model.yaml

What needs to be modified is the type of nc

# parameters
nc: 1  # number of classes
depth_multiple: 0.33  # model depth multiple
width_multiple: 0.50  # layer channel multiple

# anchors
anchors:
  - [10,13, 16,30, 33,23]  # P3/8
  - [30,61, 62,45, 59,119]  # P4/16
  - [116,90, 156,198, 373,326]  # P5/32

# YOLOv5 backbone
backbone:
  # [from, number, module, args]
  [[-1, 1, Focus, [64, 3]],  # 0-P1/2
   [-1, 1, Conv, [128, 3, 2]],  # 1-P2/4
   [-1, 3, C3, [128]],
   [-1, 1, Conv, [256, 3, 2]],  # 3-P3/8
   [-1, 9, C3, [256]],
   [-1, 1, Conv, [512, 3, 2]],  # 5-P4/16
   [-1, 9, C3, [512]],
   [-1, 1, Conv, [1024, 3, 2]],  # 7-P5/32
   [-1, 1, SPP, [1024, [5, 9, 13]]],
   [-1, 3, C3, [1024, False]],  # 9
  ]

# YOLOv5 head
head:
  [[-1, 1, Conv, [512, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 6], 1, Concat, [1]],  # cat backbone P4
   [-1, 3, C3, [512, False]],  # 13

   [-1, 1, Conv, [256, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 4], 1, Concat, [1]],  # cat backbone P3
   [-1, 3, C3, [256, False]],  # 17 (P3/8-small)

   [-1, 1, Conv, [256, 3, 2]],
   [[-1, 14], 1, Concat, [1]],  # cat head P4
   [-1, 3, C3, [512, False]],  # 20 (P4/16-medium)

   [-1, 1, Conv, [512, 3, 2]],
   [[-1, 10], 1, Concat, [1]],  # cat head P5
   [-1, 3, C3, [1024, False]],  # 23 (P5/32-large)

   [[17, 20, 23], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)
  ]

4. Start training

Find train.py in yolov5

4.1 Change the code

 (The two lines of code --cfg and --data need to be added by yourself)

Line 479: It is the position of the initial weight we trained, which is a file ending in .pt. The first training uses the weight that others have trained. Some friends may think that the data set trained by oneself is different from the data set trained by others, how can it be used universally? In fact, they are universal and will be adjusted later in training. And if you don't fill in the existing weights, the training effect may not be good;

Line 480: training model file, corresponding to wzry_model.yaml in this project;

Line 481: dataset parameter file, in this project for wzry_parameter.yaml;

Line 482: hyperparameter settings, which are artificially set parameters. Including the learning rate, etc., do not change;

Line 483: The number of training rounds determines the training time and training effect. If the selected training model is yolov5x.yaml, then the value will stabilize (converge) after about 200 rounds;

Line 484: The number of batch processing files, this should be set smaller, otherwise it will be out of memory. This determines the speed of our training;

Line 485: Image size. Although the image in our training set has been fixed, it can be resized when it is passed into the neural network. If it is too large, the training time will be very long, and an error may be reported. This should be adjusted to a smaller size according to your own situation;

Line 487: Intermittent training. If it is interrupted unexpectedly during the training process, you can fill in True here next time, and continue training after the last runs/exp

Line 496: GPU acceleration, fill in 0 is the default CUDA of the computer, the premise is that the computer has installed CUDA to enable GPU accelerated training, the installation process can be found in the blog

Line 501: Multi-threaded setting, the larger the value, the faster the data will be read, but if it is too large, an error will be reported

4.2 Training process

4.3 Results

yolov5-5.0\runs\train\exp7\weights

best.pt and last.pt are the weight files we trained , which are more important and are used for detect.py. last is the last training result, and best is the best training result (it just looks, but the generalization is not necessarily strong).

5. Detect images

Find detect.py in yolov5

5.1 Change the code

Line 217: Fill in the path of our trained weight file

Line 218: The files we want to detect can be pictures, videos, or cameras. Fill in 0 to open the default camera of the computer

Line 219: data set parameter file, same as above

Line 220: image size, same as above

Line 221: Confidence, when the detected confidence is greater than this value, it can be displayed as detected, which is the displayed frame

Line 222: Non-maximum suppression, I won’t go into details, check it yourself, don’t change it

Line 224: GPU acceleration, same as above

Note: \test or \run will report an error "OSError: [WinError 123] The syntax of the file name, directory name, or volume label is incorrect." when the code is running, you need to convert \test into a backslash "/" to be OK up

5.2 Running the code

5.3 View Results

The result is in yolov5-5.0\runs\detect\exp20

Guess you like

Origin blog.csdn.net/m0_68738477/article/details/129925142
Recommended