Fastai throwing a “training set empty” error when the directories are not empty

Hi.

I’m trying to build a classifier with both a training and a test set. However, FastAI is throwing a UserWarning: Your training set is empty error despite the fact that both training and test sets are not empty. This is the output of my terminal commands to see how many items each folder has:

%cd Food-101/images/train/
!ls -1 | wc -l

%cd ../test
!ls -1 | wc -l

%cd ../../

/content/Food-101/images/train
75750
/content/Food-101/images/test
25250
/content/Food-101

This is my code:

import pandas as pd

from fastai import *
from fastai.vision import *
from fastai.callbacks.hooks import *
from pathlib import Path
from numba import vectorize
from subprocess import call, run
import os, git, glob, shutil

file_parse = r'/([^/]+)_\d+\.(png|jpg|jpeg)'

np.random.seed(42)
data = ImageDataBunch.from_folder(path, train='images/train', test='images/test', valid_pct=0.2, ds_tfms=get_transforms(), size=224)
data.normalize(imagenet_stats)

And this is the error:

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py:458: UserWarning: Your training set is empty. If this is by design, pass `ignore_empty=True` to remove this warning.
  warn("Your training set is empty. If this is by design, pass `ignore_empty=True` to remove this warning.")
/usr/local/lib/python3.6/dist-packages/fastai/data_block.py:461: UserWarning: Your validation set is empty. If this is by design, use `split_none()`
                 or pass `ignore_empty=True` when labelling to remove this warning.
  or pass `ignore_empty=True` when labelling to remove this warning.""")
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in get_label_cls(self, labels, label_cls, label_delim, **kwargs)
    264         if label_delim is not None:             return MultiCategoryList
--> 265         try: it = index_row(labels,0)
    266         except: raise Exception("""Can't infer the type of your targets. 

7 frames
IndexError: index 0 is out of bounds for axis 0 with size 0

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in get_label_cls(self, labels, label_cls, label_delim, **kwargs)
    265         try: it = index_row(labels,0)
    266         except: raise Exception("""Can't infer the type of your targets. 
--> 267 It's either because your data source is empty or because your labelling function raised an error.""")
    268         if isinstance(it, (float, np.float32)): return FloatList
    269         if isinstance(try_int(it), (str, Integral)):  return CategoryList

Exception: Can't infer the type of your targets. 
It's either because your data source is empty or because your labelling function raised an error.

I do not understand why it throws this error when both directories are clearly populated with images.

I solved the problem:

data = ImageDataBunch.from_folder('/content/Food-101/images', train='/content/Food-101/images/train', test='/content/Food-101/images/test', valid_pct=0.2, ds_tfms=get_transforms(), size=224)
1 Like

Hii.how to find classification report like F1 score and recall in image classification…1st lecturer of deep learning

Thank you! I have gone through so many threads about this issue and this was the only solution that worked. Do you know why it has to be set up this way? Was the folder set up correctly?

1 Like

I think its just a matter of typing out the correct path.