跟着炮哥的文章学习yolov5,遇到一些环境配置上的问题。
第一个是在编译train.py文件时,出现了以下报错:
AttributeError: module 'numpy' has no attribute 'int'.
经过搜索资料得知,这是因为numpy版本的问题,将运行终端提示的部分中,含有“np.int”的部分修改为“np.int_”即可解决。
第二个是在编译train.py文件时,出现了以下报错:
Image Not Found D:\PycharmProjects\yolov5-hat\VOCdevkit\images\train\000000.jpg
参考这篇文章将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
即可解决。
第三个是开始训练时出现以下报错
Image Not Found D:\PycharmProjects\yolov5-hat\VOCdevkit\images\train\000000.jpg
这是貌似是因为炮哥提供资料中已经存在了“train.cache”文件和"val.cache"文件(我是直接使用的炮哥提供的数据集),所以在自己训练之前要把这俩文件删除。
本文只做作者备忘使用。