问题说明
最近在使用一个新的数据集的时候,只改了原先代码的数据路径,和一些简单数据处理代码,在跑的时候就报了如下的错误:
Traceback (most recent call last):
File "train.py", line 172, in <module>
main(parser.parse_args())
File "train.py", line 75, in main
train_loss = train(train_loader, model, [criterion1, criterion2], optimizer)
File "train.py", line 111, in train
for i, (inputs, targets, valid, meta) in enumerate(train_loader):
File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 637, in __next__
return self._process_next_batch(batch)
File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 658, in _process_next_batch
raise batch.exc_type(batch.exc_msg)
RuntimeError: Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 138, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 232, in default_collate
return [default_collate(samples) for samples in transposed]
File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 232, in <listcomp>
return [default_collate(samples) for samples in transposed]
File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 229, in default_collate
return {key: default_collate([d[key] for d in batch]) for key in batch[0]}
File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 229, in <dictcomp>
return {key: default_collate([d[key] for d in batch]) for key in batch[0]}
File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 218, in default_collate
return torch.stack([torch.from_numpy(b) for b in batch], 0)
RuntimeError: Expected object of scalar type Long but got scalar type Double for sequence elment 2 in sequence argument at position #1 'tensors'
问题分析
从报错原因可以看出,在pytorch 中dataloader返回的的某些量的类型错误,我于是找了找,果真发现我的meta 信息中bounding box 的值有的类型为float,有的为int
问题解决
直接给对应的量类型强制转换一下.Ok,问题从此解决.