YOLO V5出现RuntimeError: result type Float can‘t be cast to the desired output type long int解决方法

在使用YOLO框架训练自己的数据集时候,开始跑train.py,出现如下报错:RuntimeError: result type Float can‘t be cast to the desired output type long int

Traceback (most recent call last):
  File "/home/sjh/project/yolov5-5.0/train.py", line 543, in <module>
    train(hyp, opt, device, tb_writer)
  File "/home/sjh/project/yolov5-5.0/train.py", line 304, in train
    loss, loss_items = compute_loss(pred, targets.to(device))  # loss scaled by batch_size
  File "/home/sjh/project/yolov5-5.0/utils/loss.py", line 117, in __call__
    tcls, tbox, indices, anchors = self.build_targets(p, targets)  # targets
  File "/home/sjh/project/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 long int

由于是初次使用,并不确定是哪里的问题,只知道是跟着其他人的教程来的,经过查询,得到问题原因:

官网的yolov5-master可以正常运行,但是yolov5-5.0/yolov5-6.1等版本会出问题;

这是因为yolov5-master版本和yolov5-5.0/yolov5-6.1等版本下的【utils】中的【loss.py】文件不同,大概是yolov5-5.0/yolov5-6.1等版本在更新版本的时候出了问题

因此解决方法也明了了:修改loss.py文件

打开【utils】文件夹下的【loss.py】文件,一共需要修改两处代码,以yolov5-5.0为例:

1,大概是177行左右,可通过搜索找到下面这句代码

anchors = self.anchors[i]

将这一行其替换为

 anchors, shape = self.anchors[i], p[i].shape 

2,在程序的最后,找到下面这行代码

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

保存程序,重新跑train.py,便可以正常运行了

猜你喜欢

转载自blog.csdn.net/weixin_45498383/article/details/130499508