Lesson 1 ImageDataBunch - "UserWarning: Your training dataloader is empty ..."

I’m reading in images for a classification task with ImageDataBunch(). I have two directories of images, and each directory corresponds to a different class. Each directory has 10 images in it, with mixed types of gif, jpg, bmp, and png. I run this code:

path = Path("/home/ubuntu/course-v3/nbs/dl1/state-images")
data = ImageDataBunch.from_folder(path, train=".", valid_pct=0.2,
ds_tfms=get_transforms(), size=224, num_workers=4).normalize(imagenet_stats)

and I get this error:

UserWarning: Your training dataloader is empty, you have only 16 items in your training set.
Your batch size is 64, you should lower it.

I’m not sure why there are only 16 items, because I have 10 images for each class. But more important, what does it mean “Your training dataloader is empty”, and how to do I fix it?

Hi John! By passing the argument valid_pct=0.2 to the DataBunch, you tell it to split your data into a training set and a validation set with a percentage of 20% for the validation set. If you have two directories with 10 images each, this makes 20 images. From these, 20% are selected for the validation set, which results in 16 images in the training set and 4 images in the test set. Maybe the docs can help you understanding your code :slight_smile:

Thanks, Elena. That’s helpful. I read through the ImageDataBunch documentation.

But I still have two problems:

(1) What does it mean when it says, “Your training dataloader is empty”?
(2) When I try to view the images with data.show_batch(rows=2, figsize=(5,5)), I get an error like below, but no error message.

data.show_batch(rows=2, figsize=(5,5))
data.show_batch(rows=2, figsize=(5,5))

StopIteration Traceback (most recent call last)
in
----> 1 data.show_batch(rows=2, figsize=(5,5))

~/anaconda3/lib/python3.7/site-packages/fastai/basic_data.py in show_batch(self, rows, ds_type, reverse, **kwargs)
183 def show_batch(self, rows:int=5, ds_type:DatasetType=DatasetType.Train, reverse:bool=False, **kwargs)->None:
184 “Show a batch of data in ds_type on a few rows.”
–> 185 x,y = self.one_batch(ds_type, True, True)
186 if reverse: x,y = x.flip(0),y.flip(0)
187 n_items = rows **2 if self.train_ds.x._square_show else rows

~/anaconda3/lib/python3.7/site-packages/fastai/basic_data.py in one_batch(self, ds_type, detach, denorm, cpu)
166 w = dl.num_workers
167 dl.num_workers = 0
–> 168 try: x,y = next(iter(dl))
169 finally: dl.num_workers = w
170 if detach: x,y = to_detach(x,cpu=cpu),to_detach(y,cpu=cpu)

StopIteration:

i am working on colab and i get my files from google drive i made everything exactly the same with document but getting same error please help us.

I solved the problem.
data = ImageDataBunch.from_folder(path, ds_tfms=tfms, size=26, bs=32).normalize(imagenet_stats)
please pay attention to your “BS” parameter. it should not greater than number of total test files.

4 Likes