赞
踩
注意
:本文采用的是Chien-Yao Wang
复现的yolov4训练自己的数据,即YOLOv4的二作
. ├── cfg ├── data │ ├── Annotations │ ├── images │ ├── ImageSets │ ├── JPEGImages │ └── labels ├── images ├── models │ └── __pycache__ ├── __pycache__ ├── runs ├── utils │ └── __pycache__ └── weights
在训练可能会遇到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
git clone https://github.com/WongKinYiu/PyTorch_YOLOv4.git
将标注好的数据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()
运行上述代码,在ImageSets
得到四个文件,其中我们主要关注的是train.txt
,test.txt
,val.txt
,文件里主要存储图片名称。
训练集比例设置
trainval_percent:训练和验证集占所有数据
的比例
train_percent:训练和验证集中训练集
的比例
假设原始数据有100
个,trainval_percent=0.9
,train_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.txt
,test.txt
,val.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()
得到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文件的选择要和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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。