Data Loader NoneType has no len()

DataLoader is throwing, NoneType object has no len() error. Can anyone help

TypeError: Caught TypeError in DataLoader worker process 1.
Original Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
    data = fetcher.fetch(index)
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 80, in default_collate
    if not all(len(elem) == elem_size for elem in it):
  File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/collate.py", line 80, in <genexpr>
    if not all(len(elem) == elem_size for elem in it):
TypeError: object of type 'NoneType' has no len()

Based on the error message it seems as if the Dataset.__getitem__ method is returning the None object (or no return statement is used which would then return None by default).
Could you check that the Dataset.__getitem__ is indeed returning valid data?

Yeah you are right, thank you for the help @ptrblck

I have the exact same error but in my case I checked Dataset.__getitem__ as well as Dataloader.dataset if there were any NoneTypes in them and there weren’t any. I am not sure what’s really going on. I am getting this error when iterating through the batches. But if I take the length of the dataloader before iteration, I am getting the following error:
TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found <class 'NoneType'>

image