pytorch 踩坑,TypeError: expected seqence object with len>_0 or a single integer_typeerror: expected sequence object with len __ 0

在看 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。

posted @ 2024-05-21 19:54  钢之炼丹术师  阅读(21)  评论(0)    收藏  举报