上述211行附近的代码如下,请具体指出问题 def build_targets(self, p, targets): # Build targets for compute_loss(), input targets(image,class,x,y,w,h) na, nt = self.na, targets.shape[0] # number of anchors, targets tcls, tbox, indices, anch = [], [], [], [] gain = torch.ones(7, device=targets.device) # normalized to gridspace gain ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt) targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices g = 0.5 # bias off = torch.tensor([[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m # [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm ], device=targets.device).float() * g # offsets for i in range(self.nl): anchors = self.anchors[i] gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain # Match targets to anchors t = targets * gain 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 b, c = t[:, :2].long().T # image, class gxy = t[:, 2:4] # grid xy gwh = t[:, 4:6] # grid wh gij = (gxy - offsets).long() gi, gj = gij.T # grid xy indices # Append a = t[:, 6].long() # anchor indices indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices tbox.append(torch.cat((gxy - gij, gwh), 1)) # box anch.append(anchors[a]) # anchors tcls.append(c) # class return tcls, tbox, indices, anch
时间: 2024-04-17 21:26:39 浏览: 253
根据你提供的代码,第 211 行出现错误的地方是在 indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) 这一行。
问题出现在使用 clamp_() 函数时,尝试将 Float 类型的 gj 和 gi 转换为 __int64 类型。由于 clamp_() 函数会直接修改张量的值,而且 __int64 类型不能容纳 Float 类型的值,因此会导致类型转换错误。
要解决这个问题,你可以使用 clamp() 函数而不是 clamp_() 函数,这样就可以返回一个新的张量,而不会修改原始张量的值。修改代码如下:
```python
indices.append((b, a, gj.clamp(0, int(gain[3]) - 1), gi.clamp(0, int(gain[2]) - 1))) # image, anchor, grid indices
```
这样修改后,应该就能解决 RuntimeError: result type Float can't be cast to the desired output type __int64 的问题了。
相关问题
Traceback (most recent call last): File "C:\Users\Administrator\Desktop\yolov5-5.0\train.py", line 543, in <module> train(hyp, opt, device, tb_writer) File "C:\Users\Administrator\Desktop\yolov5-5.0\train.py", line 304, in train loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size File "C:\Users\Administrator\Desktop\yolov5-5.0\utils\loss.py", line 117, in __call__ tcls, tbox, indices, anchors = self.build_targets(p, targets) # targets File "C:\Users\Administrator\Desktop\yolov5-5.0\utils\loss.py", line 211, 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 __int64
根据你提供的报错信息,问题出现在 "utils/loss.py" 文件的第 211 行,具体错误是由于将 Float 类型转换为 __int64 类型时导致的。这个问题可能是由于使用了不正确的数据类型或者数据类型转换错误导致的。
为了解决这个问题,你可以尝试以下方法:
1. 确保你的目标标签(targets)的数据类型是正确的。在构建目标(targets)时,确保类型与代码中要求的类型一致,尤其是索引(indices)部分。
2. 检查你的代码,确认是否有将 Float 类型转换为 __int64 类型的地方。如果有,请确保类型转换操作正确,并且没有其他错误。
3. 确保你使用的 PyTorch 版本和相关库的版本是兼容的。某些版本的 PyTorch 可能对数据类型转换有更严格的要求。
如果以上方法都没有解决问题,我建议你尝试查看相关函数(build_targets)的实现代码,了解其内部逻辑,并尝试进行调试以找出具体引发错误的原因。另外,你还可以参考 YOLOv5 的官方文档或者官方论坛,看是否有其他用户遇到类似问题并给出了解决方案。
Traceback (most recent call last): File "E:\Anaconda\Test2-YOLOv3\yolov3-9.5.0\train.py", line 543, in <module> train(hyp, opt, device, tb_writer) File "E:\Anaconda\Test2-YOLOv3\yolov3-9.5.0\train.py", line 304, in train loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size File "E:\Anaconda\Test2-YOLOv3\yolov3-9.5.0\utils\loss.py", line 117, in __call__ tcls, tbox, indices, anchors = self.build_targets(p, targets) # targets File "E:\Anaconda\Test2-YOLOv3\yolov3-9.5.0\utils\loss.py", line 197, in build_targets offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j] IndexError: The shape of the mask [5, 14] at index 0 does not match the shape of the indexed tensor [1, 14, 2] at index 0
这是一个Python程序的错误信息。表示程序在文件"E:\Anaconda\Test2-YOLOv3\yolov3-9.5.0\train.py"的543行处发生了错误。具体错误类型和原因需要看完整的错误信息才能确定。
阅读全文