Default_collate error: batch does not contain <class 'NoneType'> but PyTorch detects so

username: HaziqRazali
cooked: <p>PyTorch is giving me the error message <code>TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found &lt;class 'NoneType'&gt;</code> when my batch does not contain a <code>NoneType</code>. I am completely confused by this and am hoping someone can suggest a solution.</p>
<p>This is my collate function</p>
<pre><code class="lang-auto">def collate_images_targets_meta(batch):
    for b in batch:
        print(type(b[0]), type(b[1]))
        print()
    
    images = torch.utils.data.dataloader.default_collate([b[0] for b in batch])

    # error here
    targets = torch.utils.data.dataloader.default_collate([b[1] for b in batch])
    # error here

    metas = [b[2] for b in batch]
    return images, targets, metas
</code></pre>
<p>And this is the output. You can see that there isnt any item in the batch that is of class <code>NoneType</code>.</p>
<pre><code class="lang-auto">&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

&lt;class 'torch.Tensor'&gt; &lt;class 'list'&gt;

Traceback (most recent call last):
  File "/home/haziq/anaconda3/envs/mtl/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/haziq/anaconda3/envs/mtl/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/haziq/EPFL/MTL/openpifpaf_crm_pose_segmentation_new/openpifpaf/train.py", line 202, in &lt;module&gt;
    main()
  File "/home/haziq/EPFL/MTL/openpifpaf_crm_pose_segmentation_new/openpifpaf/train.py", line 174, in main
    ftrainer.train(pre_train_loader, i)
  File "/home/haziq/EPFL/MTL/openpifpaf_crm_pose_segmentation_new/openpifpaf/network/trainer.py", line 232, in train
    for batch_idx, ((data1, target1, meta1),(data2, target2, meta2),(data3,target3)) in enumerate(zip(scenes[0],scenes[1],scenes[2])):
  File "/home/haziq/anaconda3/envs/mtl/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 819, in __next__
    return self._process_data(data)
  File "/home/haziq/anaconda3/envs/mtl/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 846, in _process_data
    data.reraise()
  File "/home/haziq/anaconda3/envs/mtl/lib/python3.6/site-packages/torch/_utils.py", line 385, in reraise
    raise self.exc_type(msg)
TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/haziq/anaconda3/envs/mtl/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/haziq/anaconda3/envs/mtl/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "/home/haziq/EPFL/MTL/openpifpaf_crm_pose_segmentation_new/openpifpaf/datasets.py", line 51, in collate_images_targets_meta
    targets = torch.utils.data.dataloader.default_collate([b[1] for b in batch])
  File "/home/haziq/anaconda3/envs/mtl/lib/python3.6/site-packages/torch/utils/data/_utils/collate.py", line 79, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "/home/haziq/anaconda3/envs/mtl/lib/python3.6/site-packages/torch/utils/data/_utils/collate.py", line 79, in &lt;listcomp&gt;
    return [default_collate(samples) for samples in transposed]
  File "/home/haziq/anaconda3/envs/mtl/lib/python3.6/site-packages/torch/utils/data/_utils/collate.py", line 81, in default_collate
    raise TypeError(default_collate_err_msg_format.format(elem_type))
TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found &lt;class 'NoneType'&gt;
</code></pre>

username: SimonW
cooked: <p>default_collate is recursive. there is likely a None in your list.</p>