开始训练:
python train.py --data data/around.yaml --cfg models/yolov5s.yaml --weights data/yolov5s.pt --epoch 10000 --batch-size 16 --device cuda:0
报错:AssertionError: CUDA unavailable, invalid device cuda:0 requested
检查:
$ python
>>> import torch
>>> print(torch.cuda.is_available())
True
>>> quit()
修改继续:python train.py --data data/around.yaml --cfg models/yolov5s.yaml --weights data/yolov5s.pt --epoch 10000 --batch-size 16
报错:AttributeError: Can't get attribute 'SPPF' on <module 'models.common' from '/home/zhou/yolov5/models/common.py'>
检查自己下载的yolov5s.pt等模型是否与自己所用代码的版本一致。
解决:下载
https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt
替换默认下载的yolov5s.pt即可
报错:_pickle.UnpicklingError: STACK_GLOBAL requires str
原因:在过去对当前数据集进行过训练,导致在数据集文件夹中生成了.cache的缓存文件
解决方法:找到数据集文件夹中的全部.cache文件,并将他们全部删除
报错:RuntimeError: result type Float can't be cast to the desired output type long int
解决方法:找到5.0版报错的loss.py中最后那段for函数,将其整体替换为yolov5-master版中loss.py最后一段for函数即可正常运行
for i in range(self.nl):
anchors, shape = self.anchors[i], p[i].shape
gain[2:6] = torch.tensor(shape)[[3, 2, 3, 2]] # xyxy gain
# Match targets to anchors
t = targets * gain # shape(3,n,7)
if nt:
# Matches
r = t[..., 4:6] / anchors[:, None] # wh ratio
j = torch.max(r, 1 / r).max(2)[0] < self.hyp['anchor_t'] # compare
# j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2))
t = t[j] # filter
# Offsets
gxy = t[:, 2:4] # grid xy
gxi = gain[[2, 3]] - gxy # inverse
j, k = ((gxy % 1 < g) & (gxy > 1)).T
l, m = ((gxi % 1 < g) & (gxi > 1)).T
j = torch.stack((torch.ones_like(j), j, k, l, m))
t = t.repeat((5, 1, 1))[j]
offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j]
else:
t = targets[0]
offsets = 0
# Define
bc, gxy, gwh, a = t.chunk(4, 1) # (image, class), grid xy, grid wh, anchors
a, (b, c) = a.long().view(-1), bc.long().T # anchors, image, class
gij = (gxy - offsets).long()
gi, gj = gij.T # grid indices
# Append
indices.append((b, a, gj.clamp_(0, shape[2] - 1), gi.clamp_(0, shape[3] - 1))) # image, anchor, grid
tbox.append(torch.cat((gxy - gij, gwh), 1)) # box
anch.append(anchors[a]) # anchors
tcls.append(c) # class