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

多卡训练LSKNet时,出现以下报错:

File "/data/*/LSKNet-main/mmrotate/.mim/tools/train.py", line 200, in <module> main() File "/data/*/LSKNet-main/mmrotate/.mim/tools/train.py", line 189, in main train_detector( File "/data/*/LSKNet-main/mmrotate/apis/train.py", line 141, in train_detector 

。。。省略。。。

self.roi_head.simple_test( File "/data/*/LSKNet-main/mmrotate/models/roi_heads/rotate_standard_roi_head.py", line 252, in simple_test det_bboxes, det_labels = self.simple_test_bboxes( File "/data/jiaqianxi/LSKNet-main/mmrotate/models/roi_heads/oriented_standard_roi_head.py", line 178, in simple_test_bboxes det_bbox, det_label = self.bbox_head.get_bboxes( File "/home/*/miniconda3/envs/openmmlab/lib/python3.9/site-packages/mmcv/runner/fp16_utils.py", line 208, in new_func return old_func(*args, **kwargs) File "/data/jiaqianxi/LSKNet-main/mmrotate/models/roi_heads/bbox_heads/rotated_bbox_head.py", line 418, in get_bboxes det_bboxes, det_labels = multiclass_nms_rotated( File "/data/jiaqianxi/LSKNet-main/mmrotate/core/post_processing/bbox_nms_rotated.py", line 58, in multiclass_nms_rotated bboxes, scores, labels = bboxes[inds], scores[inds], labels[inds]

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

在深度学习中,通常数据和模型都存储在特定的设备上,例如CPU或GPU。操作张量时,确保张量和操作在同一设备上运行是很重要的。

这里的错误消息是由以下代码引发的:

det_bboxes, det_labels = multiclass_nms_rotated(
    bboxes, scores, labels, cfg.score_thr, cfg.nms, cfg.max_per_img)

要解决这个问题,需要确保操作的张量位于相同的设备上。这通常可以通过将张量移动到正确的设备来实现。在PyTorch中,可以使用.to()方法来实现这一点。

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
bboxes = bboxes.to(device)
scores = scores.to(device)
labels = labels.to(device)

通过转换,问题解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值