1 错误:RuntimeError: All input tensors must be on the same device. Received cpu and cuda:0
代码来源:detectron2中的GeneralizedRCNNWithTTA
分析:输入all_boxes变量的tensor来自不同设备,所以从出错行往上找输入all_boxes的tensor有哪些
for output, tfm in zip(outputs, tfms):
# Need to inverse the transforms on boxes, to obtain results on original image
pred_boxes = output.pred_boxes.tensor
original_pred_boxes = tfm.inverse().apply_rotated_box(pred_boxes.cpu().numpy())
all_boxes.append(torch.from_numpy(original_pred_boxes).to(pred_boxes.device))
all_scores.extend(output.scores)
all_classes.extend(output.pred_classes)
可以看到第四行代码中all_boxes中插入了转为pred_boxes.device的变量,而上一句pred_boxes转为了cpu上,所以更改这里的设备
更改后的代码为:
for output, tfm in zip(outputs, tfms):
# Need to inverse the transforms on boxes, to obtain results on original image
pred_boxes = output.pred_boxes.tensor
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
original_pred_boxes = tfm.inverse().apply_rotated_box(pred_boxes.cpu().numpy())
all_boxes.append(torch.from_numpy(original_pred_boxes).to(device))
all_scores.extend(output.scores)
all_classes.extend(output.pred_classes)



