Problems in the learning process of YOLOv5
Learning blog link: Target detection – teach you how to build your own YOLOv5 target detection platform
Question 1
The following error occurs when installing requirements
ERROR: Could not build wheels for pycocotools, which is required to install pyproject.toml-based projects
Solution: Modify pycocotools>=2.0 to pycocotools-windows>=2.0 in the requirements.txt file
Reason: The maintainer of pycocotools no longer provides support for the Windows platform
Question 2
While running the train.py file to run I get the following error
AssertionError: Image Not Found D:\PycharmProjects\yolov5-hat\VOCdevkit\images\train\000000
Solution: delete the train.cache and val.cache files in the storage label directory
Reason: The original cache file will be used for training by default during training. Since the absolute path of the training set has been changed, it must be deleted to avoid continuing to load the original path
Question 3
The last step of training appears
RuntimeError: result type Float can‘t be cast to the desired output type __int64
Solution: Find the last for function in loss.py that reported an error in version 5.0, and replace it with the last for function in loss.py in yolov5-master version to run normally
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
Question 4
After the yolov5 training is completed, the following error occurs when running the detect.py file:
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
Solution:
Enter line 154 of the upsampling.py file in the error report, modify the forward function, and set
recompute_scale_factor=self.recompute_scale_factor
just delete
Reason: It may be a pytorch version problem