I am not sure if I should start an extra thread for this, but I currently try to debug this strange behavior when I iterate through a DataBunch from ObjectDetectDatasets based on png images with bounding boxes (so kind of segmentation):
Test code for a dummy dataset of 100 entries each for train and valid:
# Create ObjectDetectDatasets
train_ds = get_datasets(PATH_train)
valid_ds = get_datasets(PATH_valid)
size = 128
bs = 4 # bs=1 is working!
# Create DataBunch
def get_data(bs, size):
return DataBunch.create(train_ds, valid_ds, bs=bs, size=size,ds_tfms=None, path=PATH)
data = get_data(bs, size)
# Test DataBunch DataLoader
for i in range(100):
print(i, end=', ')
next(iter(data.train_dl.dl))
Output:
bs = 1
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
For bs > 1it stops the loop after a unreproducible step (I guess due to random shuffling) with this error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-132-d0590afb83a6> in <module>()
1 for i in range(100):
2 print(i, end=', ')
----> 3 next(iter(data.train_dl.dl))
~/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py in __next__(self)
351 self.reorder_dict[idx] = batch
352 continue
--> 353 return self._process_next_batch(batch)
354
355 next = __next__ # Python 2 compatibility
~/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _process_next_batch(self, batch)
372 self._put_indices()
373 if isinstance(batch, ExceptionWrapper):
--> 374 raise batch.exc_type(batch.exc_msg)
375 return batch
376
RuntimeError: Traceback (most recent call last):
File "/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 114, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File "/home/paperspace/fastai/fastai/torch_core.py", line 86, in data_collate
return torch.utils.data.dataloader.default_collate(to_data(batch))
File "/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 198, in default_collate
return [default_collate(samples) for samples in transposed]
File "/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 198, in <listcomp>
return [default_collate(samples) for samples in transposed]
File "/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 198, in default_collate
return [default_collate(samples) for samples in transposed]
File "/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 198, in <listcomp>
return [default_collate(samples) for samples in transposed]
File "/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 175, in default_collate
return torch.stack(batch, 0, out=out)
RuntimeError: invalid argument 0: Tensors must have same number of dimensions: got 2 and 3 at /opt/conda/conda-bld/pytorch-nightly_1538165619353/work/aten/src/TH/generic/THTensorMoreMath.cpp:1308
or got 3 and 2
.
The error also occurs with the show_image_batch()
method.
I found this thread on PyTorch forum which points into direction of png files with different channel numbers: https://discuss.pytorch.org/t/runtimeerror-invalid-argument-0/17919/5
However, in the fastai library the open_image()
function uses .convert('RGB')
and when I debug the tensor shapes I always find the same shape for each element with 3 channels x width x height.
What I don’t get is why is it working with bs = 1?
Maybe somebody has a tip?
Maybe I am using parts of the library which are currently under development?
Thank you & best regards
Michael
PS: When I try to visualize the images with show_image_batch()
and bs = 1 I get this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-68-7d54941a820f> in <module>
1 # http://docs.fast.ai/vision.data.html
----> 2 show_image_batch(data.train_dl, data.train_ds.classes, rows=3, figsize=(5,5))
~/fastai/fastai/vision/data.py in show_image_batch(dl, classes, rows, figsize, denorm)
44 x = x[:rows*rows].cpu()
45 if denorm: x = denorm(x)
---> 46 show_images(x,y[:rows*rows].cpu(),rows, classes, figsize)
47
48 def show_images(x:Collection[Image],y:int,rows:int, classes:Collection[str], figsize:Tuple[int,int]=(9,9))->None:
AttributeError: 'list' object has no attribute 'cpu'
When I define a custom show_image_batch()
function without the .cpu()
in the jupyter notebook I get this error:
NameError: name 'show_image' is not defined