1.环境说明
系统:windows10
CUDA: 10.2
2.环境创建
首先用conda创建测试的虚拟环境conda create -n yolov5 python=3.8 ,完成后输入conda activate yolov5即可激活环境
然后按照yolov5官方的requirment.txt配置依赖包,输入pip install -r requirment.txt即可自动安装
yolov5的链接如下:ultralytics/yolov5: YOLOv5 in PyTorch > ONNX > CoreML > TFLite (github.com),权重文件需要单独下载,连接如下Releases · ultralytics/yolov5 (github.com)
3. 数据集准备
1)准备好代码,按照上面链接下载解压后得到yolov5文件夹
2)将自己的图像数据images和标注数据labels两个文件夹放到yolov5/data文件夹下面
3)利用脚本将数据集分为训练集、验证集、测试集,代码如下。运行后yolov5/data文件夹下面生成3个txt文件,train.txt,test.txt,val.txt
```
import os
import random
#各数据集比例
trainval_percent = 0.1
train_percent = 0.8
test_percent = 0.1
xmlfilepath = '../labels' #标注文件的路径,若数据是txt标注文件,则改成txt的路径
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
listtotal = range(num)
tv = int(num * trainval_percent) #验证集
tr = int(num * train_percent) #训练集
tt = num - tv - tr #测试集
test = random.sample(listtotal,tv)
train = random.sample(listtotal,tr)
val = random.sample(train,tv)
#生成的labels目录txt放在data文件夹下
fimgval = open('../val.txt','w')
fimgtest = open('../test.txt','w')
fimgtrain = open('../train.txt','w')
#将图片路径写入每个txt
for i in listtotal:
name = './data/images/'+ total_xml[i][:-4] +'.jpg\n'
if i in test:
ftest.write(name)
elif i in val:
fval.write(name)
else:
ftrain.write(name)
fval.close()
ftrain.close()
ftest.close()
```
文件格式如下图:
4)修改coco.yaml文件,我的图像中检测行人一个类别,将文件内容修改如下:
```
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ./data/ # dataset root dir
train: train.txt # train images (relative to 'path') 118287 images
val: val.txt # val images (relative to 'path') 5000 images
test: test.txt # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794
# Classes
nc: 1 # number of classes
names: ['person'] # class names
```
在yolov5虚拟环境中运行下面命令即可开始训练,我只用10个epochs测试
python train.py --data ./data/coco.yaml --cfg ./models/yolov5s.yaml --weights ./models/yolov5s.pt --epochs 10 --batch-size 8
5. 问题总结
1)图片路径错误
该过程中最容易出现问题的就是图片路径不对,如下面的错误,找不到图像数据,或者找不到labels
File "C:\Users\yolov5\utils\datasets.py", line 417, in __init__
raise Exception(f'{prefix}Error loading data from {path}: {e}\nSee {HELP_URL}')
assert nf > 0 or not augment, f'{prefix}No labels in {cache_path}. Can not train without labels. See {HELP_URL}'
AssertionError: train: No labels in C:\Users\yolov5\data\train.cache. Can not train without labels. See https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data
这两种错误都需要检查一下生成的train.txt,test.txt,val.txt是否在data文件夹下面,因为代码中是直接在train.txt目录下找images文件夹,然后将images替换为labels即为labels的路径,所以这样配置最方便查找。
[Errno 2] No such file or directory: '..\\images\\1 (40).jpg'
这里写的相对路径,因此在yolov5根目录下运行train.py文件时候找不到文件,修改路径即可
2)AttributeError: ‘Upsample’ object has no attribute ‘recompute_scale_factor’
报错中会提示\envs\虚拟环境名称\lib\site-packages\torch\nn\modules\upsampling.py", line 154, in forward
recompute_scale_factor=self.recompute_scale_factor)
打开这个文件,定位到代码行,将
def forward(self, input: Tensor) -> Tensor:
return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners,
recompute_scale_factor=self.recompute_scale_factor)
修改为
def forward(self, input: Tensor) -> Tensor:
return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners,
#recompute_scale_factor=self.recompute_scale_factor
)