RuntimeError: result type Float can‘t be cast to the desired output type long int

在使用yolov5训练自定义数据集的运行过程中报错:
**RuntimeError: result type Float can‘t be cast to the desired output type long int
**
在这里插入图片描述

1. 产生原因:

并不是自己构建的数据集有问题或者是下载更改后的代码有问题。
问题原因:
从Github上clone 的yolov5-master版本的代码是可以直接运行的,因为官网上clone 的代码是最新版本调试好的代码.
附yolov5链接网址:yolov5 Github链接
但是如果是用yolov5的历史版本如:yolov5-1.0或yolov5-5.0等版本进行训练,由于yolov5-master版本和其他历史版本下的\utils\loss.py文件是不一样的,因此在yolov5进行版本更新后历史版本如yolov5-5.0/yolov5-6.1等在训练过程中会出现上述报错问题。

2. 解决办法:

需要修改 \utils\loss.py 文件中的部分内容

2.1第一处修改:

在 \utils\loss.py 文件中查找 for in range(self.nl) 找到如下内容:
原始内容:

 for in range(self.nl):
            anchors = self.anchors[i]  #将这一行进行更改
            gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]]  # xyxy gain

更改后的内容:

 for in range(self.nl):
            anchors, shape = self.anchors[i], p[i].shape  # 改成这样
            gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]]  # xyxy gain

2.2 第二处修改:

在 \utils\loss.py 文件中查找 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

更改后的内容:

a = t[:, 6].long()  # anchor indices
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

更改后保存,然后重新运行train.py文件,成功解决!

3. 注意:

3.1 pycharm 中快捷键【Ctrl】+【F】可以查找当前文件夹下的内容
3.2 注意在更改代码的过程中一定要注意不要更改缩进的字符数
3.3 修改完一定记得要保存!!

4. 参考链接:

  • 24
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值