TypeError: forward() missing 4 required positional arguments: 'label', 'inst', 'image', and 'feat'

运行pytorch代码 train.py,特别是在用多个GPU并行运行的情况下,有时会出现这种警报:

TypeError: forward() missing 4 required positional arguments: 'label', 'inst', 'image', and 'feat'

产生错误的原因是读取数据时丢失了其中几项。为什么会丢失输入项呢,是因为 batch size 的个数不是 GPU 运行个数的整数倍。

比如,如果设置运行GPU为六块:

CUDA_VISIBLE_DEVICES=0,1,2,3,4,5 python train.py --gpu_ids 0,1,2,3,4,5 

而默认的 opt.batchSize 为16,这时,16个batch 分布到 6 个GPU时,总会有一块跟其它几块输入数据是不一样的,就导致了数据丢失的报错了。此时,可以设置为 

CUDA_VISIBLE_DEVICES=0,1,2,3,4,5 python train.py --gpu_ids 0,1,2,3,4,5 --batchSize 24

 

你可能感兴趣的:(python,pytorch,pytorch,TypeError)