From_name_re: 'NoneType' Attribute Error

data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224, bs=bs).normalize(imagenet_stats)

When I ran the above code in lesson 1, I received the following error: AttributeError: ‘NoneType’ object has no attribute ‘group’. I have spent a while looking and copy pasting the class notebook but cannot seem to find out what is wrong.


AttributeError Traceback (most recent call last)
in
----> 1 data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224, bs=bs).normalize(imagenet_stats)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\fastai\vision\data.py in from_name_re(cls, path, fnames, pat, valid_pct, **kwargs)
153 pat = re.compile(pat)
154 def _get_label(fn): return pat.search(str(fn)).group(1)
–> 155 return cls.from_name_func(path, fnames, _get_label, valid_pct=valid_pct, **kwargs)
156
157 @staticmethod

~\AppData\Local\Continuum\anaconda3\lib\site-packages\fastai\vision\data.py in from_name_func(cls, path, fnames, label_func, valid_pct, **kwargs)
146 “Create from list of fnames in path with label_func.”
147 src = ImageItemList(fnames, path=path).random_split_by_pct(valid_pct)
–> 148 return cls.create_from_ll(src.label_from_func(label_func), **kwargs)
149
150 @classmethod

~\AppData\Local\Continuum\anaconda3\lib\site-packages\fastai\data_block.py in _inner(*args, **kwargs)
386 assert isinstance(fv, Callable)
387 def _inner(*args, **kwargs):
–> 388 self.train = ft(*args, **kwargs)
389 assert isinstance(self.train, LabelList)
390 kwargs[‘label_cls’] = self.train.y.class

~\AppData\Local\Continuum\anaconda3\lib\site-packages\fastai\data_block.py in label_from_func(self, func, **kwargs)
240 def label_from_func(self, func:Callable, **kwargs)->‘LabelList’:
241 “Apply func to every input to get its label.”
–> 242 return self.label_from_list([func(o) for o in self.items], **kwargs)
243
244 def label_from_folder(self, **kwargs)->‘LabelList’:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\fastai\data_block.py in (.0)
240 def label_from_func(self, func:Callable, **kwargs)->‘LabelList’:
241 “Apply func to every input to get its label.”
–> 242 return self.label_from_list([func(o) for o in self.items], **kwargs)
243
244 def label_from_folder(self, **kwargs)->‘LabelList’:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\fastai\vision\data.py in _get_label(fn)
152 “Create from list of fnames in path with re expression pat.”
153 pat = re.compile(pat)
–> 154 def _get_label(fn): return pat.search(str(fn)).group(1)
155 return cls.from_name_func(path, fnames, _get_label, valid_pct=valid_pct, **kwargs)
156

AttributeError: ‘NoneType’ object has no attribute ‘group’

1 Like

It seems the regular expression didn’t find anything. Are you sure you have the right files in the right directory ? Did you modify the regular expression ? Are you on the latest fastai version ? (it’s now around 1.0.42 I believe, maybe 1.0.43). If it’s not that, it would be helpful to have a gist of your notebook to look at to help you :slight_smile:

You have to use backslash in the regex pattern instead of forward slash since you are trying this on Windows. There was a thread stating this will be fixed in the library in a platform independent fashion. Till then just change to backslash in the pattern. (the variable ‘pat’).

3 Likes

Thank you so much

Can you kindly share the code on pat variable, I have tried to change to backslash with no success. Thank you

If you update to latest fastai this is fixed. Linux path patterns will work on windows too.