Yolov5训练自己的数据集--从数据集制作到模型训练、测试(一)

本篇博客将记录本人从下载下Yolov5官方代码,到数据集制作、模型训练、测试整个过程,轻重的项目代码本人的环境将一起奉上,如有错误,欢迎在评论区指正。同为程序员,一起进步!

 

先放上几张训练过程中,不同模型检测的结果,该图像是电子版导电粒子的识别:

   

 一、下载源代码,配置环境

1.代码连接:

GitHub - ultralytics/yolov5: YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite

我下载的是yolov5-3.0,

https://github.com/wang-xinyu/tensorrtx/tags


 

用anaconda重建虚拟环境,按照requirement中的要求配置。python=3.8

pip install -r requirements.txt

安装报错了:

ModuleNotFoundError: No module named ‘skbuild‘

解决办法

python3.x: 命令行输入

pip3 install --upgrade pip

python2: 命令行输入

pip install --upgrade pip

然后再安装。

报错:

 No matching distribution found for pycocotools>=2.0

解决:

pip install pycocotools-fix

报错:

Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘

解决:

说在models.py中找不到SPPF这个类,如果你用的是Tags5的话,就去Tags6里面的models/common.py里面去找到这个SPPF的类,把它拷过来到你这个Tags5的models/common.py里面,这样你的代码就也有这个类了,还要在Tags5的models/common.py里引入一个warnings包就可以了。


报错:

RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place

解决:

找到在File "****/yolov5-master\models\yolo.py"的line 145
将代码修改为:

    def _initialize_biases(self, cf=None):  # initialize biases into Detect(), cf is class frequency
        # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
        m = self.model[-1]  # Detect() module
        for mi, s in zip(m.m, m.stride):  # from
            b = mi.bias.view(m.na, -1)  # conv.bias(255) to (3,85)
            with torch.no_grad():
                b[:, 4] += math.log(8 / (640 / s) ** 2)  # obj (8 objects per 640 image)
                b[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum())  # cls
            mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)

报错:

TypeError: can‘t convert cuda:0 device type tensor to numpy. 

解决方法

如果想把CUDA tensor格式的数据改成numpy时,需要先将其转换成cpu float-tensor随后再转到numpy格式。 numpy不能读取CUDA tensor 需要将它转化为 CPU tensor,只需要将报错代码self.numpy()改为self.cpu().numpy()即可。

2.python detect.py脚本进行测试。输出结果保存在runs文件夹下。

出来这个结果,则说明你的环境是没问题的:

 二、准备自己的数据集

 1.下载标注工具

YOLOv5模型必须对已标记的数据进行训练,以便学习该数据中的对象类别。在开始训练之前,有两种方式创建数据集:

1)使用RoboflowYOLO格式自动标记、准备和托管自定义数据.

Roboflow: Give your software the power to see objects in images and video

这是官网推荐的得需要注册账号等,试了半天也不行。

 (2)我用的LabelImg,安装安装过程:整体的存放格式和匆匆28格式一样就可以训练。

1. lableImg下载

git clone https://github.com/tzutalin/labelImg.git

2. 制作lableImg所需的"conda+python"环境(conda需要先安装,最好再设置下下载源)

# 创建环境
conda create -n labelimg python=3
# 激活环境
conda activate labelimg
# 此时如果命令前显示 (labelimg) 则说明已经成功激活conda环境, 否则没有
​​​​​​​# 进入labelImg目录
cd [labelImg路径]
# 安装依赖并启动
conda install pyqt=5
conda install -c anaconda lxml
pyrcc5 -o libs/resources.py resources.qrc
python labelImg.py
python labelImg.py [IMAGE_PATH] [PRE-DEFINED CLASS FILE]

标注就不用说了吧,费时费力的,我的数据集也不是很大,标注了一部分想练手的可以下载一下。

https://download.csdn.net/download/m0_37407756/34681137

标注完了就生成和图像相应的Annotations.xml),把xml放入Annotations文件夹,把对应的原始图片放入JPEGImages文件夹。每个图像一个*.txt文件(如果图像中没有对象,不需要*.txt文件)*.txt文件规格如下

每个对象一行
每一行都是class,x_center,y_center,width,height形式
框坐标必须是标准化的xywh格式(从0到1)。如果框的单位是像素,x_center和width除以图像宽度,y_center和height除以图像高度。
类号是零索引的(从0开始)。

3.需要注意的是,我标注出来的标签数据是xml格式的需要转换为txt格式的。

这里给予两个转换脚本文件,复制执行就好,记得把路径配置好:

先执行pascal_split.py文件:

#!/usr/bin/env python

from __future__ import print_function
import os
import os.path as osp
import random
import argparse
import sys
import fnmatch

def dataset_split(input_dir, trainval_percent=.8, train_percent=.7):
    if not osp.exists(input_dir):
        print('Pacsal data directory is not exists:', input_dir)
        sys.exit(1)
    xmlfilepath=osp.join(input_dir,'Annotations')
    total_xmls = os.listdir(xmlfilepath)
    total_num=len(total_xmls)
    total_list=range(total_num)

    tv=int(total_num*trainval_percent)
    tr=int(tv*train_percent)

    trainval= random.sample(total_list,tv)
    train=random.sample(trainval,tr)

    print("total size",total_num)
    print("train and val size",tv)
    print("train size",tr)

    if not osp.exists(osp.join(input_dir,'ImageSets')):
        os.makedirs(osp.join(input_dir,'ImageSets'))
    if not osp.exists(osp.join(input_dir,'ImageSets','Main')):
        os.makedirs(osp.join(input_dir,'ImageSets','Main'))

    ftrainval = open(osp.join(input_dir,'ImageSets/Main/trainval.txt'), 'w')
    ftest = open(osp.join(input_dir,'ImageSets/Main/test.txt'), 'w')
    ftrain = open(osp.join(input_dir,'ImageSets/Main/train.txt'), 'w')
    fval = open(osp.join(input_dir,'ImageSets/Main/val.txt'), 'w')

    for i  in total_list:
        name=total_xmls[i][:-4]+'\n'
        if i in trainval:
            ftrainval.write(name)
            if i in train:
                ftrain.write(name)
            else:
                fval.write(name)
        else:
            ftest.write(name)

    ftrainval.close()
    ftrain.close()
    fval.close()
    ftest .close()

def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument('input_dir', help='input annotated directory')
    parser.add_argument('-tv','--trainval_percent', type=float, help='train and validation percent', default=.8)
    parser.add_argument('-tr','--train_percent', type=float, help='train percent', default=.7)
    args = parser.parse_args()

    dataset_split(args.input_dir, args.trainval_percent, args.train_percent)

if __name__ == '__main__':
    main()

 执行时:python pascal_split.py /home/sym/test/(annotation的路径) -tv 0.95 -tr 0.7
 意为:把所有的数据集的95%作为训练,5%作为测试。95%中再有70%作为训练的,30%作为训练的测试。会生成一个文件夹,××/Main/test.txt,train.txt,trainval.txt,val.txt.。

然后再执行voc_labels.py程序。需要创建一个文件夹labels,存放转换为txt的标签。创建一个文件夹train,存放生成train和test的两个txt,内容为该图片相应的路径。
 

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
 
sets = ['train', 'test'] 
classes = ['particle',]  #自己训练的类别 
 
def convert(size, box):
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box[0] + box[1]) / 2.0
    y = (box[2] + box[3]) / 2.0
    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) 
 
def convert_annotation(image_id):
    in_file = open('/home/sym/test/Annotations/%s.xml' % (image_id))
    out_file = open('/home/sym/test/labels/%s.txt' % (image_id), 'w')
    tree = ET.parse(in_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))
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') 
 
wd = getcwd()
for image_set in sets:
    if not os.path.exists('/home/sym/test/labels/'):
        os.makedirs('/home/sym/test/labels/')
    image_ids = open('/home/sym/test/ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
    list_file = open('/home/sym/test/train/%s.txt' % (image_set), 'w')#生成train和test的两个txt,内容为该图片相应的路径
    for image_id in image_ids:
        list_file.write('/home/sym/test/Images/%s.jpg\n' % (image_id))
        convert_annotation(image_id)

 这个注意下我的汉字备注,改一下类别啊,路径什么的哈。然后吧生成的txt(应该在labels文件夹中下)复制到图像文件中。我是都放在了一个文件夹下了,你们也可以放在不同的文件夹下,记得路径配置好就可以了就像下面的格式:

yolov5----

         -----data------

                      ------train-----

                                   --------jpg和txt

在这个文件下要配置一下路径:

 然后就可以开始训练了,训练的日志还是在那个run文件夹下。

训练的时候还下载了一个

pip install wandb

到目前为止,还没有涉及到代码内部的问题,接下来继续共享,将yolov5在nvidia tx2或nx上运行,见网址:

https://blog.csdn.net/m0_37407756/article/details/121191676

博主也是新手,有资源也可在评论区分享~

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向阳+

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值