Learn.load('weights') gets Error : 'method-wrapper' object has no attribute '__code__' on Colab

Hello, :grinning:

I’m using Google colab and working on notebooks saved on the my drive. To do so I do :

from google.colab import drive
drive.mount(’/content/gdrive’, force_remount=True)
%cd ./gdrive/My\ Drive/path/to/notebooks_folder

But when I do the %cd , everything works except learn.load() and I get this error :


AttributeError Traceback (most recent call last)

/usr/local/lib/python3.6/dist-packages/IPython/core/formatters.py in call(self, obj)
697 type_pprinters=self.type_printers,
698 deferred_pprinters=self.deferred_printers)
–> 699 printer.pretty(obj)
700 printer.flush()
701 return stream.getvalue()

/usr/local/lib/python3.6/dist-packages/IPython/lib/pretty.py in pretty(self, obj)
396 if callable(meth):
397 return meth(obj, self, cycle)
–> 398 return _default_pprint(obj, self, cycle)
399 finally:
400 self.end_group()

/usr/local/lib/python3.6/dist-packages/IPython/lib/pretty.py in _default_pprint(obj, p, cycle)
516 if _safe_getattr(klass, ‘repr’, None) not in baseclass_reprs:
517 # A user-provided repr. Find newlines and replace them with p.break
()
–> 518 _repr_pprint(obj, p, cycle)
519 return
520 p.begin_group(1, ‘<’)

/usr/local/lib/python3.6/dist-packages/IPython/lib/pretty.py in repr_pprint(obj, p, cycle)
707 “”“A pprint that just redirects to the normal repr function.”""
708 # Find newlines and replace them with p.break
()
–> 709 output = repr(obj)
710 for idx,output_line in enumerate(output.splitlines()):
711 if idx:

/usr/local/lib/python3.6/dist-packages/dataclasses.py in repr(self)

/usr/local/lib/python3.6/dist-packages/fastai/callback.py in repr(self)
159
160 def repr(self):
–> 161 attrs = func_args(self.init)
162 to_remove = getattr(self, ‘exclude’, [])
163 list_repr = [self.class.name] + [f’{k}: {getattr(self, k)}’ for k in attrs if k != ‘self’ and k not in to_remove]

/usr/local/lib/python3.6/dist-packages/fastai/core.py in func_args(func)
241 def func_args(func)->bool:
242 “Return the arguments of func.”
–> 243 code = func.code
244 return code.co_varnames[:code.co_argcount]
245

AttributeError: ‘method-wrapper’ object has no attribute ‘code

load.save(‘stage-1’) works great and saves on the drive
learn.export() works
learn = load_learner(image_path) works

Any idea how I can fix it ? Thank you !

It’s just the representation of your Learner object that is breaking. lean.load('stage-1'); won’t throw an error.

3 Likes

Merci !!!

I encounter similar problem, happens at notebook, but OK in python code. But I did not debug further…

I am getting the same error both in notebook and python code.

I don’t know what’s was the actual problem under the hood but changing:

class AUC(Callback):
    "AUC score"
    def __init__(self):
        pass
    
    def on_epoch_begin(self, **kwargs): 
        self.outputs = []
        self.targets = []

    def on_batch_end(self, last_output, last_target, **kwargs):
        "expects binary output with data.c=2 "
        self.outputs += list(to_np(last_output)[:, 1])
        self.targets += list(to_np(last_target))

    def on_epoch_end(self, last_metrics, **kwargs): 
        return {'last_metrics': last_metrics + [roc_auc_score(self.targets, self.outputs)]}

adding def __init__ to this custom metric fixed before I wasn’t defining it.

1 Like