YOLOv5 training deployment

This article takes the coco128 data set as an example to complete the training deployment of YOLOv5. I will explain in detail how to make a self-made data set in the next blog.

Preparation

The dataset and annotation files can be obtained from the links below.

Link: https://pan.baidu.com/s/1gEFYs0X41Kd14YblsgZ1ZA

Extraction code: fir3

training preprocessing

  1. After decompression, create a new folder val in the images directory to store the verification set images

  1. Create a new folder val in the labels directory to store the validation set labels

  1. Use the following code to divide the part of the training set into the verification set (remember to modify the path to your own). Of course, since the coco128 data set has only 128 pictures but 80 categories, it is not appropriate to divide in this way, but In order to introduce the method here so as to facilitate the self-made data set training later, this processing method is adopted.

import os, random, shutil

def moveimg(fileDir, tarDir):
    pathDir = os.listdir(fileDir)  # 取图片的原始路径
    filenumber = len(pathDir)
    rate = 0.2  # 自定义抽取图片的比例,比方说100张抽10张,那就是0.1
    picknumber = int(filenumber * rate)  # 按照rate比例从文件夹中取一定数量图片
    sample = random.sample(pathDir, picknumber)  # 随机选取picknumber数量的样本图片
    print(sample)
    for name in sample:
        shutil.move(fileDir + name, tarDir + "\\" + name)
    return

def movelabel(file_list, file_label_train, file_label_val):
    for i in file_list:
        if i.endswith('.jpg'): 
            filename = file_label_train + "\\" + i[:-4] + '.txt'  
            if os.path.exists(filename):
                shutil.move(filename, file_label_val)
                print(i + "处理成功!")

if __name__ == '__main__':
    fileDir = r"C:\Users\HDJ\Desktop\yolov5-5.0\coco128\images\train2017" + "\\"  # 源图片文件夹路径
    tarDir = r'C:\Users\HDJ\Desktop\yolov5-5.0\coco128\images\val'  # 图片移动到新的文件夹路径
    moveimg(fileDir, tarDir)
    file_list = os.listdir(tarDir)
    file_label_train = r"C:\Users\HDJ\Desktop\yolov5-5.0\coco128\labels\train2017"  # 源图片标签路径
    file_label_val = r"C:\Users\HDJ\Desktop\yolov5-5.0\coco128\labels\val"  # 标签
    # 移动到新的文件路径
    movelabel(file_list, file_label_train, file_label_val)

Folder structure after running

coco128
├── images
│   ├── train2017(包含103张图片)
│   ├── val(包含25张图片)
├── labels
│   ├── train2017(包含103个txt文件)
│   ├── val(包含25个txt文件)

Implementation of training deployment

Find the coco.yaml file in the data directory.

In the coco128.yaml file, first comment out the download line (1) and then modify the parameters. Change train to the path where the training set picture is located (2); val to the path where the verification set is located (absolute path is best) (3).

(4) is the number of categories, just keep the default; (5) is the name of each category, just keep the default.

Copy the yolov5s.yaml file in the models directory and rename it to yolov5_coco.yaml.

Modify the number of types to 80 in the yolov5_coco.yaml file

Modify the default yaml file in (1)(2) of train.py, and change the number of training rounds in (3) (here I use 50 rounds)

parser.add_argument('--epochs',type=int, default=50)

My computer is a rescuer Y9000P, with a 3060 version of the graphics card, and the core number of the cpu is 8 cores, so it is changed to

After running, there may be an error report that the page file is too small to complete the operation . For details, see "Error Reporting and Solution" below the FAQ

successful training

After the training is completed, an exp file will be generated in runs/train

In detect.py, change the weight model used for inference to the model obtained by training, my path is

C:/Users/HDJ/Desktop/yolov5-5.0/runs/train/exp/weights/best.pt

parser.add_argument('--weights', nargs='+', type=str, default='runs/train/exp/weights/best.pt', help='model.pt path(s)')

常见报错与解决

1、OSError: [WinError 1455] 页面文件太小,无法完成操作。

这是由于pycharm虚拟内存不够,在utils路径下找到datasets.py这个文件,将里面的第81行里面的参数nw改为0

2、AssertionError:Image Not Found D:\PycharmProjects\yolov5-hat\VOCdevkit\images\train\000000.jpg

在另一台电脑上训练时,要先把标签文件的.cache删掉(这两个文件是在训练中产生的)

不然会报“找不到图片0”的错(可以理解为由于这两个文件的存在,把训练的路径锁成了之前训练的路径)

3、OMP:Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll alreadyinitialized

是numpy库的一个小bug

在train.py文件的顶头部分添加

importos

os.environ['KMP_DUPLICATE_LIB_OK']='True'

求学路上,你我共勉(๑•̀ㅂ•́)و✧

Guess you like

Origin blog.csdn.net/Albert_yeager/article/details/128839996
Recommended