fastai / fastai_dev

fast.ai early development experiments
Apache License 2.0
641 stars 351 forks source link

fix RegexLabeller obj has no attr __name__ #281

Closed amaarora closed 5 years ago

amaarora commented 5 years ago

While looking into DataSource I got an error

AttributeError: 'RegexLabeller' object has no attribute '__name__'

This can be replicated by:

path = untar_data(URLs.PETS); path

np.random.seed(2)
pat = r'/([^/]+)_\d+.jpg$'

items = get_image_files(path/'images')
tfms = L([PILBase.create], [RegexLabeller(pat), Categorize])
splits = RandomSplitter(0.2)(items)
dsrc = DataSource(items, tfms=tfms, splits=splits)

dsrc.tls

From what I understand, issue occurs due to calling __name__ on RegexLabeller() and this PR attempts to fix it.

review-notebook-app[bot] commented 5 years ago

Check out this pull request on  ReviewNB

You'll be able to see Jupyter notebook diff and discuss changes. Powered by ReviewNB.

sgugger commented 5 years ago

This is not going to work each time we have a function instead of an instance of a class (it's going to show "function" instead of the function name) so we need to use something else here that checks for a __name__ attribute then reverts to a __class__.__name__.

amaarora commented 5 years ago

checks for a __name__ attribute then reverts to a __class__.__name__

Thanks @sgugger! I have updated PR to getattr(o, '__name__', o.__class__.__name__) and this seems to work for me.

sgugger commented 5 years ago

Thanks!