求大佬指点!!!!!!!

z1434462 2024-08-01 11:40:19

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

 

...全文
69410 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

都是大神 受教了

  • 打赏
  • 举报
回复

一、问题分析与解答

  1. 错误根源
    • 在PyTorch中,数据类型的转换需要遵循一定的规则。正如之前提到的RuntimeError: result type Float can't be cast to the desired output type __int64错误,主要原因是gj在进行一系列操作后,虽然使用了.round()方法进行四舍五入,但它的类型仍然是Float。在PyTorch中,.round()不会改变张量的数据类型,所以后续直接使用.to(torch.int64)转换就会失败。
  2. 解决方案
    • 如修改后的代码所示,可以将.round().to(torch.int64)修改为.round().long()或者直接使用.long()。这是因为在PyTorch中,.long().to(torch.int64)的别名,这样做能够确保在进行类型转换之前正确地将浮点数四舍五入为整数,从而避免了类型不匹配的错误。

      配图

    • 另外,还需要注意gain[3]的值应该是一个合理的数值(大于1的整数),以确保clamp_方法能够正确地限制gj的值范围。

二、相关书籍推荐

  1. 《Python编程从入门到实践》
    • 作者:Eric Matthes

      配图

    • 出版社:人民邮电出版社
    • 特点
      • 内容全面,涵盖了Python的基础语法知识,包括变量、数据类型、控制结构、函数等。对于初学者来说非常友好,书中有大量的示例代码,并且每个项目都有详细的讲解。例如在讲解函数时,会从函数的定义、参数传递、返回值等多方面进行阐述,并且通过实际的小例子让用户理解如何创建和使用函数。
      • 它还包含三个项目,分别是Web应用程序、数据可视化和游戏开发。这些项目可以帮助读者将所学的知识应用到实际的开发场景中,提高读者的动手能力。
    • 与其他书籍比较
      • 相比于一些只注重理论讲解的书籍,它的实践项目是一大亮点。而与其他实践型书籍相比,它的基础语法讲解又非常扎实,为后续的项目开发奠定了良好的基础。

        配图

  2. 《PyTorch深度学习实战》
    • 作者:Eli Stevens、Luca Antiga、Thomas Viehmann
    • 出版社:人民邮电出版社
    • 特点
      • 专注于PyTorch框架,详细介绍了PyTorch的各种功能,包括张量操作、神经网络构建、模型训练和优化等。书中包含了丰富的代码示例,并且对代码进行了详细的解释。例如在讲解神经网络的构建时,会从最基本的层(如线性层、卷积层等)开始介绍,然后逐步构建复杂的神经网络模型。
      • 还涉及到了一些实际的应用场景,如图像识别、自然语言处理等,让读者了解如何在不同的领域中使用PyTorch进行深度学习开发。
    • 与其他书籍比较
      • 与一些通用的深度学习书籍相比,它的针对性更强,专门针对PyTorch框架。这使得读者可以更加深入地学习PyTorch的特性和使用方法。但是相对来说,对于其他深度学习框架的涉及较少,如果读者想要全面了解不同框架之间的差异,可能需要参考其他书籍。
  3. 《深入理解计算机系统》
    • 作者:Randal E. Bryant、David R. O'Hallaron
    • 出版社:机械工业出版社
    • 特点
      • 从计算机系统的底层原理出发,包括硬件组成、指令集架构、编译原理、操作系统等多个方面。书中通过大量的实例和实验,帮助读者深入理解计算机系统的工作原理。例如在讲解编译原理时,会详细介绍词法分析、语法分析、语义分析等过程,并且给出相应的代码示例。
      • 它的内容比较深入和全面,适合有一定计算机基础并且想要深入了解计算机系统内部机制的读者。
    • 与其他书籍比较
      • 与专注于编程语言或者特定框架的书籍不同,它的视角更加宏观,涵盖了整个计算机系统。但是正因为如此,内容相对比较深奥,对于初学者来说可能有一定的难度。
推荐书籍图书特点
《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) # 修复

Append

a = t[:, 6].long() # anchor indices
indices.append((b, a, gj, gi.clamp(0, gain[2] - 1).long())) # 修复

从以前 01-05
  • 打赏
  • 举报
回复

这个错误的原因通常是由于在使用 .round() 方法后,gj 的数据类型是 torch.float,但你尝试将其转化为 torch.int64 时出现了问题。这个问题可能与 gj 的初始数据类型或 PyTorch 的特定版本有关。

  • 打赏
  • 举报
回复 1

python的代码这么复杂么?都没有学习下去的信心了

  • 打赏
  • 举报
回复

代码有错误,修改看看能不能运行,如果不能,请私信我们,有最高级专业人员为您解答

摆烂仙君 2024-11-18
  • 打赏
  • 举报
回复 1

这个错误信息 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] 的值不正确,也可能导致问题。

  • 打赏
  • 举报
回复 1

从你提供的错误信息和代码来看,问题出在 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 一致,避免类型不匹配的错误。
希望这能解决你的问题!如果有其他问题或需要进一步的帮助,请告诉我。

liron71 2024-10-10
  • 打赏
  • 举报
回复

点,点间连接的数学构型系统
https://blog.csdn.net/liron71/article/details/142741782

xiaoqn 2024-09-20
  • 打赏
  • 举报
回复

霸屏兔站群系统相当出色。它具备高效的网站集群管理能力,操作便捷,能让用户轻松布局多个站点。在优化推广方面表现卓越,大大提升了网站的曝光度和流量获取效率。其功能全面且稳定,是站群管理的优质选择。

残影飞雪 2024-08-29
  • 打赏
  • 举报
回复

依赖的版本不正确。建议重新安装python和yolo环境。

Slingerspir 2024-08-25
  • 打赏
  • 举报
回复

问题在于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()方法来确保它们是整数。这应该可以解决你遇到的类型转换错误。如果还有其他问题,请继续讨论。

请站在我身后 2024-08-21
  • 打赏
  • 举报
回复

虽然有点晚了, 但是想问问,把转格式删了不就行了
.to(torch.int64)

麦麦大数据 2024-08-17
  • 打赏
  • 举报
回复

debug以下把

迷失小行星 2024-08-01
  • 打赏
  • 举报
回复

```
gj = gj.clamp_(0, gain[3] - 1).round().long()

```在F:\yolov5-5.0\utils\loss.py文件的第208行,将浮点数转换为整数时出现了问题

z1434462 2024-08-01
  • 举报
回复 1
@迷失小行星 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] &lt; self.hyp['anchor_t'] # compare # j = wh_iou(anchors, t[:, 4:6]) &gt; 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. &lt; g) & (gxy &gt; 1.)).T l, m = ((gxi % 1. &lt; g) & (gxi &gt; 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
z1434462 2024-08-01
  • 举报
回复
@迷失小行星 大佬 能帮忙看一眼吗?

22,299

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧