Issue with label_from_folder

Hi,

I came across the following error when using label_from_folder

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-7bb61befde84> in <module>()
----> 1 src = s.label_from_folder(); src

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in _inner(*args, **kwargs)
    458         assert isinstance(fv, Callable)
    459         def _inner(*args, **kwargs):
--> 460             self.train = ft(*args, from_item_lists=True, **kwargs)
    461             assert isinstance(self.train, LabelList)
    462             kwargs['label_cls'] = self.train.y.__class__

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in label_from_folder(self, label_cls, **kwargs)
    287     def label_from_folder(self, label_cls:Callable=None, **kwargs)->'LabelList':
    288         "Give a label to each filename depending on its folder."
--> 289         return self.label_from_func(func=lambda o: o.parts[-2], label_cls=label_cls, **kwargs)
    290 
    291     def label_from_re(self, pat:str, full_path:bool=False, label_cls:Callable=None, **kwargs)->'LabelList':

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in label_from_func(self, func, label_cls, **kwargs)
    283     def label_from_func(self, func:Callable, label_cls:Callable=None, **kwargs)->'LabelList':
    284         "Apply `func` to every input to get its label."
--> 285         return self._label_from_list([func(o) for o in self.items], label_cls=label_cls, **kwargs)
    286 
    287     def label_from_folder(self, label_cls:Callable=None, **kwargs)->'LabelList':

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in <listcomp>(.0)
    283     def label_from_func(self, func:Callable, label_cls:Callable=None, **kwargs)->'LabelList':
    284         "Apply `func` to every input to get its label."
--> 285         return self._label_from_list([func(o) for o in self.items], label_cls=label_cls, **kwargs)
    286 
    287     def label_from_folder(self, label_cls:Callable=None, **kwargs)->'LabelList':

/usr/local/lib/python3.6/dist-packages/fastai/data_block.py in <lambda>(o)
    287     def label_from_folder(self, label_cls:Callable=None, **kwargs)->'LabelList':
    288         "Give a label to each filename depending on its folder."
--> 289         return self.label_from_func(func=lambda o: o.parts[-2], label_cls=label_cls, **kwargs)
    290 
    291     def label_from_re(self, pat:str, full_path:bool=False, label_cls:Callable=None, **kwargs)->'LabelList':

AttributeError: 'numpy.str_' object has no attribute 'parts'

I read and splitted the data from a Dataframe.

My hunch that it is expecting a Path but for some reason got a str

Are there any assumptions for using label_from_folder?
(like creating the data from folder initially)

I’m using food-101 dataset which has a different structure than imagenet

I’ve this Notebook to show the complete steps I followed.

Yes, label_from_folder currently assumes your data comes from a folder. I’ll get on this sometime next week to fix it. I’ve opened an issue about it so you’ll see it’s fixed when I close it :wink:

In the meantime, you can probably use label_from_func or label_from_re as an alternative though.

2 Likes

Should be fixed now.

1 Like

Thanks.