Debug ImageDataBunch.from_name_re

I’m getting an error and wondering if my regex isn’t working properly. Is there a way to debug ImageDataBunch so I can find out what it thinks are the labels?

You can deactivate this warning by passing `no_check=True`.
/opt/conda/envs/fastai/lib/python3.6/site-packages/fastai/basic_data.py:226: UserWarning: There seems to be something wrong with your dataset, can't access these elements in self.train_ds: 263,258
warn(warn_msg)

You can access elements of training set via data.train_ds, and then the inputs/targets via data.train_ds.x and data.train_ds.y.

1 Like

So that data looks good when I poke around at it. It has an ImageList and I can open the images. It has categories and they are what I expected. Would be nice if the error message was more descriptive. But then when I run the show_batch it fails.

Here’s a screen shot. One of the images is even of Elon Musk at kite beach in Maui. :slight_smile:

Any ideas why I would get the warning and why would show_batch fail? I checked the file permissions and they all readable. (Plus I can render them in the notebook, as Mr. Musk demonstrates.)

Doh, I see that later in the error stack that it has a problem with a specific image. I must have a broken jpeg.

Problem solved. Remove the broken image…

OSError: cannot identify image file 
'/storage/adam/uninteresting/01f5ee43bea31694c5396a9481b88c3f4324c8f4.jpg'

One more thing, you can run the images through verify_images() and it works like a charm. This patched up my images and I’m off to the races. So cool!

verify_images(path_img/'favorites')
verify_images(path_img/'uninteresting')

Note that in your first warning, you had the specific indexes of bad images (at least the one it tried to access and fail, not necessarily all the bad ones).

1 Like
data=ImageDataBunch.from_name_re(p_image,fnames,pat=pat,ds_tfms=get_transforms(),size=224)

data.normalize(imagenet_stats)

---------------------------------------------------------------------------

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

~\Miniconda3\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

~\Miniconda3\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

~\Miniconda3\lib\site-packages\fastai\data_block.py in _inner(*args, **kwargs)
369 assert isinstance(fv, Callable)
370 def _inner(*args, **kwargs):
–> 371 self.train = ft(*args, **kwargs)
372 assert isinstance(self.train, LabelList)
373 self.valid = fv(*args, **kwargs)

~\Miniconda3\lib\site-packages\fastai\data_block.py in label_from_func(self, func, **kwargs)
229 def label_from_func(self, func:Callable, **kwargs)->‘LabelList’:
230 “Apply func to every input to get its label.”
–> 231 return self.label_from_list([func(o) for o in self.items], **kwargs)
232
233 def label_from_folder(self, **kwargs)->‘LabelList’:

~\Miniconda3\lib\site-packages\fastai\data_block.py in (.0)
229 def label_from_func(self, func:Callable, **kwargs)->‘LabelList’:
230 “Apply func to every input to get its label.”
–> 231 return self.label_from_list([func(o) for o in self.items], **kwargs)
232
233 def label_from_folder(self, **kwargs)->‘LabelList’:

~\Miniconda3\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’

@sgugger - How do you reference the index with the image?