Label_from_re - Request for help

Hello,

I am currently on lesson 3 and creating a databunch from my images in my folder, where the file names consists of some numbers in a row. Example given below:

[PosixPath(‘moviegenre/SampleMoviePosters/18770.jpg’),
PosixPath(‘moviegenre/SampleMoviePosters/7507.jpg’), and so on.

Im new to regular expressions, but with some research I have come up with this one which seems to work according to https://regex101.com/.

pat = r’\d+.jpg$’

I have specified path_img and fnames which are working fine.

When I run the following code and I get the error text below it:

If anyone could help it would be much appreciated. Please tell me if you need any more information. If it is needed I will structure my notebook and share it here. Hopefully it is possible to find the error before that on the information provided :slight_smile: Thanks!

data_re=ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=48, bs=64)


IndexError Traceback (most recent call last)
in
----> 1 data_re=ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=48, bs=64)

/opt/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py in from_name_re(cls, path, fnames, pat, valid_pct, **kwargs)
156 assert res,f’Failed to find “{pat}” in “{fn}”’
157 return res.group(1)
–> 158 return cls.from_name_func(path, fnames, _get_label, valid_pct=valid_pct, **kwargs)
159
160 @staticmethod

/opt/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py in from_name_func(cls, path, fnames, label_func, valid_pct, seed, **kwargs)
145 “Create from list of fnames in path with label_func.”
146 src = ImageList(fnames, path=path).split_by_rand_pct(valid_pct, seed)
–> 147 return cls.create_from_ll(src.label_from_func(label_func), **kwargs)
148
149 @classmethod

/opt/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in _inner(*args, **kwargs)
473 assert isinstance(fv, Callable)
474 def _inner(*args, **kwargs):
–> 475 self.train = ft(*args, from_item_lists=True, **kwargs)
476 assert isinstance(self.train, LabelList)
477 kwargs[‘label_cls’] = self.train.y.class

/opt/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in label_from_func(self, func, label_cls, **kwargs)
297 def label_from_func(self, func:Callable, label_cls:Callable=None, **kwargs)->‘LabelList’:
298 “Apply func to every input to get its label.”
–> 299 return self._label_from_list([func(o) for o in self.items], label_cls=label_cls, **kwargs)
300
301 def label_from_folder(self, label_cls:Callable=None, **kwargs)->‘LabelList’:

/opt/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in (.0)
297 def label_from_func(self, func:Callable, label_cls:Callable=None, **kwargs)->‘LabelList’:
298 “Apply func to every input to get its label.”
–> 299 return self._label_from_list([func(o) for o in self.items], label_cls=label_cls, **kwargs)
300
301 def label_from_folder(self, label_cls:Callable=None, **kwargs)->‘LabelList’:

/opt/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py in _get_label(fn)
155 res = pat.search(str(fn))
156 assert res,f’Failed to find “{pat}” in “{fn}”’
–> 157 return res.group(1)
158 return cls.from_name_func(path, fnames, _get_label, valid_pct=valid_pct, **kwargs)
159

IndexError: no such group

Hi,

You need to grouping to your regex expression. As is, your regex expression doesn’t indicate what to use as the label. In addition, you have to escape the “.” Without escaping the “.”, it says to accept any character in that position. This is how your expressions should be:

pat = r’(\d+)\.jpg$’

1 Like

Hi Patrick,

Thanks for the help, now it worked.

//Jan

Hey Guys!
I’m getting ‘IndexError: no such group’ error when trying to use ‘ImageDataBunch.from_name_re’

My code is
path_img = Path('/myDatasetNew/images'); path_img
fnames = get_image_files(myDatasetNew); fnames[-5:]
pat = re.compile(r'[^/myDatasetNew/images][a-zA-Z]+'); pat
tfms = get_transforms(do_flip=False)
data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=tfms, bs=bs ).normalize(imagenet_stats)

My file names are like
PosixPath(’/myDatasetNew/images/valueiWant_ rose-165819__340.jpg’),

P.s. All the images have extension ‘.jpg’ I’ve made sure of it. :frowning: