为什么我无法在我的数据集中训练YOLOv5?

问题描述

我正在尝试使用自定义数据集在Jupyter笔记本上训练YOlov5模型。数据集是face mask detection数据集,包含带有/不带有口罩的人物图像。我已经将注释转换为YOLO格式,并且我相信我已经编辑了所有必要的文件以反映类的数量(3:无遮罩,正确佩戴的遮罩,错误佩戴的遮罩)以及训练/验证文件的位置。 / p>

这样做之后,我执行了以下命令:

!python train.py --img 256 --batch 8 --epochs 30 --data ./data/facemask.yaml --cfg ./models/yolov5s.yaml --weights yolov5s.pt --device 0

但我收到此错误

Traceback (most recent call last):
  File "./yolov5-master/train.py",line 404,in <module>
    train(hyp)
  File "./yolov5-master/train.py",line 79,in train
    model = Model(opt.cfg).to(device)
  File "/storage/facemask/yolov5-master/models/yolo.py",line 64,in __init__
    m.stride = torch.tensor([64 / x.shape[-2] for x in self.forward(torch.zeros(1,ch,64,64))])  # forward
  File "/storage/facemask/yolov5-master/models/yolo.py",line 91,in forward
    return self.forward_once(x,profile)  # single-scale inference,train
  File "/storage/facemask/yolov5-master/models/yolo.py",line 108,in forward_once
    x = m(x)  # run
  File "/opt/conda/envs/fastai/lib/python3.7/site-packages/torch/nn/modules/module.py",line 722,in _call_impl
    result = self.forward(*input,**kwargs)
  File "/storage/facemask/yolov5-master/models/yolo.py",line 28,in forward
    x[i] = x[i].view(bs,self.na,self.no,ny,nx).permute(0,1,3,4,2).contiguous()
RuntimeError: shape '[1,8,8]' is invalid for input of size 8192

我发现YOlov3的帖子提到yolov3-spp.cfg文件中的过滤器数量应该更新,但是我不认为YOlov5有任何此类文件

有人有见识吗?

对于可重复性,重新格式化的数据集和所有补充文件均可用here

解决方法

尝试在 yolo.py 中添加 x[i]=self.m[i](x[i]) 第 28 行。像这样

def forward(self,x):
   # x = x.copy()  # for profiling
    z = []  # inference output
    self.training |= self.export
    for i in range(self.nl):
        #[1,128,80,80]
        bs,_,ny,nx = x[i].shape  # x(bs,255,20,20) to x(bs,3,85)
        x[i] =self.m[i](x[i]) # **add this code here**
        x[i] = x[i].view(bs,self.na,self.no,nx).permute(0,1,4,2).contiguous()

如果这不起作用,那么您的权重可能有问题,请尝试使用与 yolov5 版本匹配的 *.pt 文件。