22,299
社区成员




Traceback (most recent call last):
File "train.py", line 543, in <module>
train(hyp, opt, device, tb_writer)
File "train.py", line 304, in train
loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size
File "F:\yolov5-5.0\utils\loss.py", line 117, in __call__
tcls, tbox, indices, anchors = self.build_targets(p, targets) # targets
File "F:\yolov5-5.0\utils\loss.py", line 208, in build_targets
gj = gj.clamp_(0, gain[3] - 1).round().to(torch.int64)
RuntimeError: result type Float can't be cast to the desired output type __int64
下面是具体内容
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 gj = gj.clamp_(0, gain[3] - 1).round().to(torch.int64) # Append a = t[:, 6].long() # anchor indices indices.append((b, a, gj.clamp_(0, gain[3] - 1).to(torch.int64), gi.clamp_(0, gain[2] - 1).long())) # 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 import torch
都是大神 受教了
一、问题分析与解答
RuntimeError: result type Float can't be cast to the desired output type __int64
错误,主要原因是gj
在进行一系列操作后,虽然使用了.round()
方法进行四舍五入,但它的类型仍然是Float
。在PyTorch中,.round()
不会改变张量的数据类型,所以后续直接使用.to(torch.int64)
转换就会失败。.round().to(torch.int64)
修改为.round().long()
或者直接使用.long()
。这是因为在PyTorch中,.long()
是.to(torch.int64)
的别名,这样做能够确保在进行类型转换之前正确地将浮点数四舍五入为整数,从而避免了类型不匹配的错误。gain[3]
的值应该是一个合理的数值(大于1的整数),以确保clamp_
方法能够正确地限制gj
的值范围。二、相关书籍推荐
推荐书籍 | 图书特点 |
---|---|
《Python编程从入门到实践》 | 作者:Eric Matthes,出版社:人民邮电出版社,适合小白阅读,包含大量示例代码和三个实际项目 |
《PyTorch深度学习实战》 | 作者:Eli Stevens、Luca Antiga、Thomas Viehmann,出版社:人民邮电出版社,专注于PyTorch框架,有丰富代码示例和应用场景介绍 |
《深入理解计算机系统》 | 作者:Randal E. Bryant、David R. O'Hallaron,出版社:机械工业出版社,从底层原理出发涵盖多方面内容,适合有一定基础想深入了解计算机系统的读者 |
以下是修改后的代码段(问题所在部分):
python
复制代码
gij = (gxy - offsets).long()
gi, gj = gij.T # grid xy indices
gj = gj.clamp(0, gain[3] - 1).round().to(torch.int64) # 修复
a = t[:, 6].long() # anchor indices
indices.append((b, a, gj, gi.clamp(0, gain[2] - 1).long())) # 修复
这个错误的原因通常是由于在使用 .round() 方法后,gj 的数据类型是 torch.float,但你尝试将其转化为 torch.int64 时出现了问题。这个问题可能与 gj 的初始数据类型或 PyTorch 的特定版本有关。
python的代码这么复杂么?都没有学习下去的信心了
代码有错误,修改看看能不能运行,如果不能,请私信我们,有最高级专业人员为您解答
这个错误信息 RuntimeError: result type Float can't be cast to the desired output type __int64 表明在执行 .to(torch.int64) 操作时,尝试将浮点数(Float)转换为整数(__int64),但是由于某些原因,转换失败了。
在你的代码中,错误发生在这一行:
python
gj = gj.clamp_(0, gain[3] - 1).round().to(torch.int64)
这里,gj 是一个浮点数张量,你首先使用 clamp_ 方法将其限制在 [0, gain[3] - 1] 的范围内,然后使用 round 方法将其四舍五入到最近的整数。最后,你尝试将其转换为 torch.int64 类型。
问题可能出在 round 方法返回的张量类型仍然是浮点数。在 PyTorch 中,round 方法不会改变张量的数据类型,它只是将数值四舍五入。因此,当你尝试将四舍五入后的浮点数张量转换为整数时,就会出现这个错误。
为了解决这个问题,你可以将 round 方法和 .to(torch.int64) 方法合并为一个操作,确保在转换为整数之前先进行四舍五入。修改后的代码如下:
python
gj = gj.clamp_(0, gain[3] - 1).round().to(torch.int64)
可以改为:
python
gj = gj.clamp_(0, gain[3] - 1).round().long() # 使用 .long() 而不是 .to(torch.int64)
或者:
python
gj = gj.clamp_(0, gain[3] - 1).long() # 直接将 clamped 值转换为整数
这样可以确保 gj 在转换为整数之前已经被正确地四舍五入,从而避免类型转换错误。同时,.long() 方法是 .to(torch.int64) 的别名,它们是等价的。
另外,确保 gain[3] 的值是正确的,它应该是一个大于1的整数,以确保 clamp_ 方法能够正确地限制 gj 的值。如果 gain[3] 的值不正确,也可能导致问题。
从你提供的错误信息和代码来看,问题出在 build_targets 函数中的 gj 变量类型转换上。具体来说,gj 是一个浮点数,但在进行 clamp 操作后,你尝试将其转换为 torch.int64 类型,这导致了类型不匹配的错误。
问题分析
gj 的类型:
gj 是通过 gxy 计算得到的,gxy 是浮点数。
gj 在进行 clamp 操作后,仍然是浮点数。
你尝试将 gj 转换为 torch.int64 类型,这会导致类型不匹配的错误。
解决方案
在 clamp 操作后进行类型转换:
在 clamp 操作后,先将 gj 转换为 long 类型,然后再进行其他操作。
修改后的代码
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
# Convert gj to long before clamping
gj = gj.clamp_(0, gain[3] - 1).long()
# Append
a = t[:, 6].long() # anchor indices
indices.append((b, a, gj, gi)) # 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
说明
gj 的类型转换:
在 clamp 操作后,将 gj 转换为 long 类型。
这样可以确保 gj 的类型与 gi 一致,避免类型不匹配的错误。
希望这能解决你的问题!如果有其他问题或需要进一步的帮助,请告诉我。
霸屏兔站群系统相当出色。它具备高效的网站集群管理能力,操作便捷,能让用户轻松布局多个站点。在优化推广方面表现卓越,大大提升了网站的曝光度和流量获取效率。其功能全面且稳定,是站群管理的优质选择。
依赖的版本不正确。建议重新安装python和yolo环境。
问题在于clamp_方法返回的是一个浮点数类型,而to(torch.int64)期望的是一个整数类型。为了解决这个问题,你需要确保在转换类型之前将浮点数四舍五入到最接近的整数。你可以使用round()方法来实现这一点。
以下是修正后的代码:
python
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],
# [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
gj = gj.clamp_(0, gain[3] - 1).round().to(torch.int64)
# Append
a = t[:, 6].long() # anchor indices
indices.append((b, a, gj.clamp_(0, gain[3] - 1).to(torch.int64), gi.clamp_(0, gain[2] - 1).long())) # 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
在这个修正后的代码中,gj和gi在转换为整数类型之前都使用了round()方法来确保它们是整数。这应该可以解决你遇到的类型转换错误。如果还有其他问题,请继续讨论。
虽然有点晚了, 但是想问问,把转格式删了不就行了
.to(torch.int64)
debug以下把
```
gj = gj.clamp_(0, gain[3] - 1).round().long()
```在F:\yolov5-5.0\utils\loss.py文件的第208行,将浮点数转换为整数时出现了问题