0

I'm trying to train the YOLOv5 model on a Jupyter notebook using a custom dataset. The dataset is a face mask detection dataset, containing images of people with/without facemasks. I've converted the annotations to YOLO format, and I believe I've edited all the necessary files to reflect the number of classes (3: no mask, mask worn correctly, mask worn incorrectly) and the training/validation file locations.

After doing this, I executed the following command:

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

but I get this error:

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, 3, 8, 8, 8]' is invalid for input of size 8192

I've found posts for YOLOv3 mentioning that the number of filters in the yolov3-spp.cfg file should be updated, however I don't believe YOLOv5 has any such file.

Does anyone have any insights?

For reproducibility, the reformatted dataset and all supplementary files are available here

user123965
  • 169
  • 8

1 Answers1

0

try to add x[i]=self.m[i](x[i]) line 28 in yolo.py.Like this

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,20,20,85)
        x[i] =self.m[i](x[i]) # **add this code here**
        x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()

if this doesn't work ,then there may be something wrong with your weights,try use the *.pt files which match the yolov5 version.

M.Shaw
  • 41
  • 1
  • 6