I had an issue that took a while to solve so I wanted to document it in case anybody else has a similar issue.
First, the answer: You probably are missing a class that is in the train folder, but not in the valid folder.
Here is the code I was running:
PATH = Path("kaggleData/competitions/imaterialist-challenge-furniture-2018/")
data = ImageDataBunch.from_folder(PATH, train="train", valid="valid", test="test", size=112, bs=64)
Here is the full error I was seeing:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-6-fce2894c6463> in <module>()
4
5 #data = ImageDataBunch.from_folder(PATH, train="train", ds_tfms=get_transforms(), size=112, bs=64, valid_pct=0.90)
----> 6 data = ImageDataBunch.from_folder(PATH, train="train", valid="valid", test="test", size=112, bs=64)
~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/vision/data.py in from_folder(cls, path, train, valid, test, valid_pct, **kwargs)
276 if valid_pct is None:
277 train_ds = ImageClassificationDataset.from_folder(path/train)
--> 278 datasets = [train_ds, ImageClassificationDataset.from_folder(path/valid, classes=train_ds.classes)]
279 else: datasets = ImageClassificationDataset.from_folder(path/train, valid_pct=valid_pct)
280
~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/vision/data.py in from_folder(cls, folder, classes, valid_pct, check_ext)
102 fns,labels = [],[]
103 for cl in classes:
--> 104 f,l = cls._folder_files(folder/cl, cl, check_ext=check_ext)
105 fns+=f; labels+=l
106
~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/vision/data.py in _folder_files(folder, label, check_ext)
85 def _folder_files(folder:Path, label:ImgLabel, check_ext=True)->Tuple[FilePathList,ImgLabels]:
86 "From `folder` return image files and labels. The labels are all `label`. `check_ext` means only image files."
---> 87 fnames = get_image_files(folder, check_ext=check_ext)
88 return fnames,[label]*len(fnames)
89
~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/vision/data.py in get_image_files(c, check_ext)
18 def get_image_files(c:Path, check_ext:bool=True)->FilePathList:
19 "Return list of files in `c` that are images. `check_ext` will filter to `image_extensions`."
---> 20 return [o for o in list(c.iterdir())
21 if not o.name.startswith('.') and not o.is_dir()
22 and (not check_ext or (o.suffix in image_extensions))]
~/anaconda3/envs/fastai/lib/python3.6/pathlib.py in iterdir(self)
1077 if self._closed:
1078 self._raise_closed()
-> 1079 for name in self._accessor.listdir(self):
1080 if name in {'.', '..'}:
1081 # Yielding a path object for these makes little sense
~/anaconda3/envs/fastai/lib/python3.6/pathlib.py in wrapped(pathobj, *args)
385 @functools.wraps(strfunc)
386 def wrapped(pathobj, *args):
--> 387 return strfunc(str(pathobj), *args)
388 return staticmethod(wrapped)
389
FileNotFoundError: [Errno 2] No such file or directory: 'kaggleData/competitions/imaterialist-challenge-furniture-2018/valid/models'
So I had thought maybe there was a bug where v1 was specifically looking for models, but then I remembered that I had been messing with my imageDataBunch.from_folder path and had accidentally added a folder to the train folder. So for me, I just needed to delete that folder that shouldn’t have been in there in the first place.
More commonly though, this would probably mean there is a class that is in your train folder that isn’t in the valid folder. Here is a quick way to check:
def class_checker(path, train_folder="train",valid_folder="valid"):
notInTrain = []
notInValid = []
path = Path(path)
train_check = (path/train_folder).ls()
valid_check = (path/valid_folder).ls()
for i in train_check:
if i not in valid_check: notInValid.append(i)
for i in valid_check:
if i not in train_check: notInTrain.append(i)
return notInTrain,notInValid
class_checker(PATH)
it outputs this to tell you which files aren’t in which folder:
([], ['1000'])
So in this case, no files are in valid that aren’t in train and there is 1 file called “1000” that is in train, but not present in valid.