YOLOv5 train.py训练自己数据集是报错


D:\Python3.8.5\lib\site-packages\torch\nn\functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  ..\c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
Model Summary: 191 layers, 7.25509e+06 parameters, 7.25509e+06 gradients

Optimizer groups: 62 .bias, 70 conv.weight, 59 other
Transferred 362/370 items from yolov5s.pt
Scanning images: 100%|██████████| 1/1 [00:00<00:00, 45.57it/s]
Scanning labels D:\Python3.8.5\YOLO\yolov5\data\labels.cache (1 found, 0 missing, 0 empty, 0 duplicate, for 1 images): 100%|██████████| 1/1 [00:00<00:00, 501.41it/s]
Scanning images: 100%|██████████| 26/26 [00:00<00:00, 90.27it/s]
Scanning labels D:\Python3.8.5\YOLO\yolov5\data\labels.cache (26 found, 0 missing, 0 empty, 0 duplicate, for 26 images): 100%|██████████| 26/26 [00:00<00:00, 13035.13it/s]

Analyzing anchors... Best Possible Recall (BPR) = 1.0000
Image sizes 640 train, 640 test
Using 0 dataloader workers
Starting training for 300 epochs...

     Epoch   gpu_mem      GIoU       obj       cls     total   targets  img_size
  0%|          | 0/1 [00:01<?, ?it/s]
Traceback (most recent call last):
  File "D:/Python3.8.5/YOLO/yolov5/train.py", line 496, in <module>
    train(hyp, opt, device, tb_writer)
  File "D:/Python3.8.5/YOLO/yolov5/train.py", line 286, in train
    loss, loss_items = compute_loss(pred, targets.to(device), model)  # scaled by batch_size
  File "D:\Python3.8.5\YOLO\yolov5\utils\utils.py", line 480, in compute_loss
    tobj[b, a, gj, gi] = (1.0 - model.gr) + model.gr * giou.detach().clamp(0).type(tobj.dtype)  # giou ratio
RuntimeError: shape mismatch: value tensor of shape [18] cannot be broadcast to indexing result of shape [0]

如果你没有改动其他模型之类的话,你这个就是数据集问题吧,验证集就1张图片?你验证集的类别和训练集的类别数目相等吗

您好,请问解决了吗