[AssertionError]: self.recorder already registered when running 01_intro.ipynb

I followed the instruction to setup gradient. however, I am not even able to run the 1st notebook.
# CLICK ME
from fastai.vision.all import *
path = untar_data(URLs.PETS)/‘images’

def is_cat(x): return x[0].isupper()
dls = ImageDataLoaders.from_name_func(
    path, get_image_files(path), valid_pct=0.2, seed=42,
    label_func=is_cat, item_tfms=Resize(224))

learn = cnn_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(1)

I got this error AssertionError: self.recorder already registered, it happens on colab too. not sure what to do. any help will be greatly appreciated. thanks!
---------------------------------------------------------------------------

AssertionError                            Traceback (most recent call last)
<ipython-input-3-3244ec10e8a7> in <module>
      8     label_func=is_cat, item_tfms=Resize(224))
      9 
---> 10 learn = cnn_learner(dls, resnet34, metrics=error_rate)
     11 learn.fine_tune(1)

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/utils.py in _f(*args, **kwargs)
    448         log_dict = {**func_args.arguments, **{f'{k} (not in signature)':v for k,v in xtra_kwargs.items()}}
    449         log = {f'{f.__qualname__}.{k}':v for k,v in log_dict.items() if k not in but}
--> 450         inst = f(*args, **kwargs) if to_return else args[0]
    451         init_args = getattr(inst, 'init_args', {})
    452         init_args.update(log)

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/vision/learner.py in cnn_learner(dls, arch, loss_func, pretrained, cut, splitter, y_range, config, n_out, normalize, **kwargs)
    174     if y_range is None and 'y_range' in config: y_range = config.pop('y_range')
    175     model = create_cnn_model(arch, n_out, ifnone(cut, meta['cut']), pretrained, y_range=y_range, **config)
--> 176     learn = Learner(dls, model, loss_func=loss_func, splitter=ifnone(splitter, meta['split']), **kwargs)
    177     if pretrained: learn.freeze()
    178     return learn

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/utils.py in _f(*args, **kwargs)
    452         init_args.update(log)
    453         setattr(inst, 'init_args', init_args)
--> 454         return inst if to_return else f(*args, **kwargs)
    455     return _f
    456 

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in __init__(self, dls, model, loss_func, opt_func, lr, splitter, cbs, metrics, path, model_dir, wd, wd_bn_bias, train_bn, moms)
     91         self.loss_func = loss_func
     92         self.path = Path(path) if path is not None else getattr(dls, 'path', Path('.'))
---> 93         self.add_cbs([(cb() if isinstance(cb, type) else cb) for cb in L(defaults.callbacks)+L(cbs)])
     94         self.epoch,self.n_epoch,self.loss = 0,1,tensor(0.)
     95 

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in add_cbs(self, cbs)
    100 
    101     def _grab_cbs(self, cb_cls): return L(cb for cb in self.cbs if isinstance(cb, cb_cls))
--> 102     def add_cbs(self, cbs): L(cbs).map(self.add_cb)
    103     def remove_cbs(self, cbs): L(cbs).map(self.remove_cb)
    104     def add_cb(self, cb):

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/foundation.py in map(self, f, *args, **kwargs)
    381              else f.format if isinstance(f,str)
    382              else f.__getitem__)
--> 383         return self._new(map(g, self))
    384 
    385     def filter(self, f, negate=False, **kwargs):

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/foundation.py in _new(self, items, *args, **kwargs)
    331     @property
    332     def _xtra(self): return None
--> 333     def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
    334     def __getitem__(self, idx): return self._get(idx) if is_indexer(idx) else L(self._get(idx), use_list=None)
    335     def copy(self): return self._new(self.items.copy())

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/foundation.py in __call__(cls, x, *args, **kwargs)
     45             return x
     46 
---> 47         res = super().__call__(*((x,) + args), **kwargs)
     48         res._newchk = 0
     49         return res

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/foundation.py in __init__(self, items, use_list, match, *rest)
    322         if items is None: items = []
    323         if (use_list is not None) or not _is_array(items):
--> 324             items = list(items) if use_list else _listify(items)
    325         if match is not None:
    326             if is_coll(match): match = len(match)

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/foundation.py in _listify(o)
    258     if isinstance(o, list): return o
    259     if isinstance(o, str) or _is_array(o): return [o]
--> 260     if is_iter(o): return list(o)
    261     return [o]
    262 

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/foundation.py in __call__(self, *args, **kwargs)
    224             if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
    225         fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 226         return self.fn(*fargs, **kwargs)
    227 
    228 # Cell

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in add_cb(self, cb)
    104     def add_cb(self, cb):
    105         old = getattr(self, cb.name, None)
--> 106         assert not old or isinstance(old, type(cb)), f"self.{cb.name} already registered"
    107         cb.learn = self
    108         setattr(self, cb.name, cb)

AssertionError: self.recorder already registered
4 Likes

Well, I ran the tabular data example on Colab and I get the same problem.
Code:

from fastai2.tabular.all import *
path = untar_data(URLs.ADULT_SAMPLE)

dls = TabularDataLoaders.from_csv(path/'adult.csv', path=path, y_names="salary",
    cat_names = ['workclass', 'education', 'marital-status', 'occupation',
                 'relationship', 'race'],
    cont_names = ['age', 'fnlwgt', 'education-num'],
    procs = [Categorify, FillMissing, Normalize])

learn = tabular_learner(dls, metrics=accuracy)

Error:


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-6-702e48520052> in <module>()
      9     procs = [Categorify, FillMissing, Normalize])
     10 
---> 11 learn = tabular_learner(dls, metrics=accuracy)

12 frames
/usr/local/lib/python3.6/dist-packages/fastai2/learner.py in add_cb(self, cb)
    104     def add_cb(self, cb):
    105         old = getattr(self, cb.name, None)
--> 106         assert not old or isinstance(old, type(cb)), f"self.{cb.name} already registered"
    107         cb.learn = self
    108         setattr(self, cb.name, cb)

AssertionError: self.recorder already registered

torch, fastai versions:

fastai==2.0.0
fastai2==0.0.30
fastbook==0.0.8
torch==1.6.0+cu101
torchsummary==1.5.1
torchtext==0.3.1
torchvision==0.7.0+cu101

Idk how, but it’s working fine now

from fastai2.vision.all import *

path = untar_data(URLs.PETS)/'images'

def is_cat(x): return x[0].isupper()

dls = ImageDataLoaders.from_name_func(

    path, get_image_files(path), valid_pct=0.2, seed=42,

    label_func=is_cat, item_tfms=Resize(224))

learn = cnn_learner(dls, resnet50, metrics=error_rate)

learn.fine_tune(1)`Preformatted text`

I have the same problem on Colab. It was just fine but after I restarted the kernel it bugged out :confused:

thank you! seems restarting the kernel fixes it. hopefully, someone will point out the root cause of this issue.

For me, this was not a fastai issue, it was another library that I pulled in that was still using fastai2.
It looks like this may be a remnant of fastai2

Here is what my defaults.callbacks gives me when I run %debug and go up to /home/kbird/Implementation/fastai/fastai/learner.py(93)__init__()

My guess is that something is still calling it fastai2 and so it doesn’t know that it is already on the list so then there are two callbacks called “Recorder”. One is fastai.learner.Recorder and the other is fastai2.learner.Recorder. Going to try tracking down where that occurs now starting in the learner.py

1 Like

I had the same issue and restarting the kernel didn’t work on gradient. However, I when I changed the import path from:

from fastai2.data.all import *
from fastai2.vision.all import *

to

from fastai.data.all import *
from fastai.vision.all import *

It worked without error. If you haven’t tried this yet, update the notebooks (https://course19.fast.ai/update_gradient.html) and use course-v4 instead of course-v3 from the instructions to match the new folder structure and see if this fixes it. Hope this helps.

1 Like

thank you! restarting kernel worked for me too

@neilmac yeah, something about fastai2 is the issue. Restarting the kernal and using “fastai.vision.all import *” seemed to work but using “fastai2.vision.all import *” had a conflict. I’d much rather be using fastai2 since I’m trying to follow along in the book but thanks for the work around.