pytorch踩坑,TypeError: expected seqence object with len>=0 or a single integer

在看Faster-R-CNN复现代码(https://blog.csdn.net/weixin_44791964/article/details/105739918)的时候,发现推理阶段报错,Dataparallel无法gather

参考https://discuss.pytorch.org/t/nn-dataparallel-typeerror-expected-sequence-object-with-len-0-or-a-single-integer/97082/23后发现,问题出在网络输出的Tensor其实被某个函数提前放到CPU上了,变成了ndarray,所以Dataparallel无法处理

Yes. Sorry, in this line I put tensor to cpu before gather.

    return torch.unsqueeze(loss, 0), predicted_interaction.cpu().detach().view(-1, 1), correct_interaction.cpu().detach().view(-1, 1)

看他的回答,意思是在 model(input)的返回值应该全是Tensor,但是他的返回值先转回CPU上了,而Dataparallel只能处理cuda数据,所以报错

解决办法:

要么去掉Dataparallel,要么在输出结果后再转CPU。

猜你喜欢

转载自blog.csdn.net/qq_26751117/article/details/112537203