YOLO7报错:indices should be either on cpu or on the same device as the indexed tensor (cpu)

当我们的数据有部分在GPU上运行,有部分在CPU上运行时会报这个错,
一般有GPU的话都会选择在GPU上面跑模型,但要注意将其他定义的对象也放在GPU上面,否则应该默认是在CPU上面。
YOLO7报错:indices should be either on cpu or on the same device as the indexed tensor (cpu)_第1张图片
如图所示,
x是从GPU中传过来的,但idx不是,idx是我们自己生成的,它默认放在CPU中,所以我们需要也把它放到GPU中,解决方法:加 .to(DEVICE) 其中DEVICE已定义。

具体解决办法:
在loss.py文件中增加下图中第一行,修改下面二三行
1.device = targets.device
在这里插入图片描述
2.from_which_layer.append((torch.ones(size=(len(b),)) * i).to(torch.device(device)))
在这里插入图片描述
3.fg_mask_inboxes = fg_mask_inboxes.to(torch.device(device))这里共有四处需要修改
在这里插入图片描述

你可能感兴趣的:(人工智能,人工智能,深度学习,python)