/yolov5-pytorch/yolov5/utils/loss.py", line 217, in build_targets
indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices
RuntimeError: result type Float can't be cast to the desired output type long int
YOLOV5训练时遇到TuntimeError错误,问题出现在损失函数计算过程中。错误表明浮点数不能转换为长整型。解决方案是修改utils/loss.py文件中build_targets函数的两处代码,将特定索引的计算更改为考虑形状信息,避免类型不匹配。修复后的代码使得索引计算正确适应图像和网格的尺寸。
解决方案:
修改utils文件夹下loss.py文件中class ComputeLoss类中def build_targets()函数:
1、可能第184行,或是第167行:
源代码
anchors = self.anchors[i]
修改为
anchors, shape = self.anchors[i],p[i].shape
注意:如果源码为:
anchors = det.anchors[i]
则修改为:
anchors, shape = det.anchors[i], p[i].shape
2. 可能是第218行,或是第200行:
源代码
indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices
修改为:
indices.append((b,a,gj.clamp_(0,shape[2] - 1),gi.clamp_(0,shape[3] - 1))) # image, anchor, grid indices