当前位置:   article > 正文

【YOLO】pytorch版yolov4训练自己的数据集_yolov4在预训练权重下继续训练自己的数据集txt pycharm

yolov4在预训练权重下继续训练自己的数据集txt pycharm

注意:本文采用的是Chien-Yao Wang复现的yolov4训练自己的数据,即YOLOv4的二作

配置完后的整体目录结构

.
├── cfg
├── data
│   ├── Annotations
│   ├── images
│   ├── ImageSets
│   ├── JPEGImages
│   └── labels
├── images
├── models
│   └── __pycache__
├── __pycache__
├── runs
├── utils
│   └── __pycache__
└── weights

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

mish_cuda问题

在训练可能会遇到ModuleNotFoundError: No module named ‘mish_cuda’ ,需要根据bug提示把所有的from mish_cuda import MishCuda as Mish替换就好

class Mish(nn.Module):
    def __init__(self):
        super().__init__()
    def forward(self,x):
        x = x * (torch.tanh(F.softplus(x)))
        return x
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

拉取yolov4

git clone https://github.com/WongKinYiu/PyTorch_YOLOv4.git
  • 1

数据集准备

数据集文件夹准备

将标注好的数据Annotations和JPEGImages放入yolov4下的data目录下,并新建文件ImageSets,labels,复制JPEGImages,重命名images。在这里插入图片描述

划分训练集、验证集、测试集

根目录下新建makeTxt.py,用以将数据分成训练集测试集验证集,其中比例可以在代码设置,代码如下:

import os  
import random  

trainval_percent = 0.9
train_percent = 0.8  
xmlfilepath = 'data/Annotations'  
txtsavepath = 'data/ImageSets'  
total_xml = os.listdir(xmlfilepath)  
num=len(total_xml)  
list=range(num)  
tv=int(num*trainval_percent)  
tr=int(tv*train_percent)  
trainval= random.sample(list,tv)  
train=random.sample(trainval,tr)  

ftrainval = open('data/ImageSets/trainval.txt', 'w')
ftest = open('data/ImageSets/test.txt', 'w')
ftrain = open('data/ImageSets/train.txt', 'w')
fval = open('data/ImageSets/val.txt', 'w')

for i  in list:  
    name=total_xml[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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

运行上述代码,在ImageSets得到四个文件,其中我们主要关注的是train.txttest.txtval.txt,文件里主要存储图片名称。
在这里插入图片描述
在这里插入图片描述

训练集比例设置
trainval_percent:训练和验证集占所有数据的比例
train_percent:训练和验证集中训练集的比例
假设原始数据有100个,trainval_percent=0.9train_percent=0.9,那么训练数据有100 ∗ 0.9 ∗ 0.9 = 81 ,验证集有100 ∗ 0.9 ∗ ( 1 − 0.9 ) = 9 ,测试集有100 ∗ ( 1 − 0.9 ) = 10 个。但数据集比较少时,可以不要验证集,甚至不要测试集。

生成训练集、测试集

根目录下新建voc_label.py,得到labels的具体内容以及data目录下的train.txttest.txtval.txt,这里的train.txt与之前的区别在于,不仅仅得到文件名,还有文件的具体路径。voc_label.py的代码如下:

# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join

sets = ['train', 'test','val']
classes = ["car","person","bird"] # 自己数据的类别

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('data/Annotations/%s.xml' % (image_id))
    out_file = open('data/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()
print(wd)
for image_set in sets:
    if not os.path.exists('data/labels/'):
        os.makedirs('data/labels/')
    image_ids = open('data/ImageSets/%s.txt' % (image_set)).read().strip().split()
    list_file = open('data/%s.txt' % (image_set), 'w')
    for image_id in image_ids:
        list_file.write('data/images/%s.jpg\n' % (image_id))
        convert_annotation(image_id)
    list_file.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

得到labels文件下的具体labels信息:
在这里插入图片描述
在这里插入图片描述

配置文件更改

更改yolov4/data/coco.yaml里面的内容
在这里插入图片描述
train.txt:运行voc_label.py生成的train.txt的路径
val.txt:运行voc_label.py生成的val.txt的路径
test.txt:运行voc_label.py生成的test.txt的路径
nc:更改为自己训练的类别的数量
names:更改为自己训练的类别

网络结构配置

yolov4/cfg里面选择自己想用的网络结构配置,以yolov4.cfg为例,打开yolov4.cfg,将classes更改为自己的类别数,filters更改为3*(1+4+类别数)

weights文件

这里可以用官网训练好的或者可以用我们自己训练的,一般训练自己的数据集都是用官网训练好的来训练我们自己的数据,生成新的weights。
weights文件的选择要和cfg文件相对应,比如说你用了yolov4.cfg文件,那么weights就要选择第一个。
在这里插入图片描述

训练

python train.py --device 0 --batch-size 16 --img 640 640 --data coco.yaml --cfg cfg/yolov4.cfg --weights weights/yolov4.weights --name yolov4-pacsp
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/874991
推荐阅读
相关标签
  

闽ICP备14008679号