How to access access images inside a sub class within the 'train' class? (and more questions)(SOLVED)

There will be a lot of nooby questions. I have searched for very long but for no avail, so if you find it similar to some question please link.

image

this is my structure of data. How do i access it?
this is my code. There are similar folders for validation(as ‘val’) and training(as ‘train’).

 path = Path('../input/facial-expression-dataset-image-folders-fer2013/data')
data = ImageDataBunch.from_folder(path, train= path/'train',valid =path/'val',test = path/'test', ds_tfms=get_transforms(), size =64, num_workers = 0).normalize(imagenet_stats)

and gives the error: index 0 is out of bounds for axis 0 with size 0

The whole traceback:
IndexError Traceback (most recent call last)
in ()
----> 1 data = ImageDataBunch.from_folder(path, train= path/‘train’,valid =path/‘val’,test = path/‘test’, ds_tfms=get_transforms(), size =64, num_workers = 0).normalize(imagenet_stats)

/opt/conda/lib/python3.6/site-packages/fastai/vision/data.py in from_folder(cls, path, train, valid, valid_pct, classes, **kwargs)
113 if valid_pct is None: src = il.split_by_folder(train=train, valid=valid)
114 else: src = il.random_split_by_pct(valid_pct)
–> 115 src = src.label_from_folder(classes=classes)
116 return cls.create_from_ll(src, **kwargs)
117

/opt/conda/lib/python3.6/site-packages/fastai/data_block.py in _inner(*args, **kwargs)
407 assert isinstance(fv, Callable)
408 def _inner(*args, **kwargs):
–> 409 self.train = ft(*args, **kwargs)
410 assert isinstance(self.train, LabelList)
411 kwargs[‘label_cls’] = self.train.y.class

/opt/conda/lib/python3.6/site-packages/fastai/data_block.py in label_from_folder(self, **kwargs)
248 def label_from_folder(self, **kwargs)->‘LabelList’:
249 “Give a label to each filename depending on its folder.”
–> 250 return self.label_from_func(func=lambda o: o.parts[-2], **kwargs)
251
252 def label_from_re(self, pat:str, full_path:bool=False, **kwargs)->‘LabelList’:

/opt/conda/lib/python3.6/site-packages/fastai/data_block.py in label_from_func(self, func, **kwargs)
244 def label_from_func(self, func:Callable, **kwargs)->‘LabelList’:
245 “Apply func to every input to get its label.”
–> 246 return self.label_from_list([func(o) for o in self.items], **kwargs)
247
248 def label_from_folder(self, **kwargs)->‘LabelList’:

/opt/conda/lib/python3.6/site-packages/fastai/data_block.py in label_from_list(self, labels, **kwargs)
220 “Label self.items with labels.”
221 labels = array(labels, dtype=object)
–> 222 label_cls = self.get_label_cls(labels, **kwargs)
223 y = label_cls(labels, path=self.path, **kwargs)
224 res = self._label_list(x=self, y=y)

/opt/conda/lib/python3.6/site-packages/fastai/data_block.py in get_label_cls(self, labels, label_cls, sep, **kwargs)
210 if label_cls is not None: return label_cls
211 if self.label_cls is not None: return self.label_cls
–> 212 it = index_row(labels,0)
213 if sep is not None: return MultiCategoryList
214 if isinstance(it, (float, np.float32)): return FloatList

/opt/conda/lib/python3.6/site-packages/fastai/core.py in index_row(a, idxs)
221 if isinstance(res,(pd.DataFrame,pd.Series)): return res.copy()
222 return res
–> 223 return a[idxs]
224
225 def func_args(func)->bool:

Thank You

@dytr Hi!
What platform are you running your code from? Your Local PC? or a cloud service, and if a yes, which one?
Your path is probably not defined properly. Check it once, please. You can do that by typing path.ls() and see if it returns the contents correctly!

secondly, You can access the contents of your files by accessing the train_ds/valid_ds/test_ds properties of Databunch.
so, you can type data.train_ds and it will return the training dataset(both x’s and y’s), from which you can navigate. Similarly for validation set and test datasets!

Alternatively, you can use the data.show_batch() property to display a set of images (training by default, but you can set it to display validation or test data too!)

Finally, No question is nooby! ALways feel free to ask!
Cheers!

1 Like

Thanks for replying! I run it on kaggle kernels. I will include that in my questions next time. So what i did was in the parameters of ImageDataBunch. I shouldn’t set train = path/‘train’ rather it just must be set to train = ‘train’ and same for other folders(validation, test). Provided the path should point to a place that contains all these folders.

Thanks again for the support.

Is there a way to close this thread? That it is solved??

Let it stay! So that it may help anyone else who faces this issue…