Getting TypeError by using lr_find()

Platform: Kaggle
FastAI version: 2.7.9

DataLoader:

dls = ImageDataLoaders.from_df(
    df=train_df,
    fn_col=0, label_col=1,
    path=paths['competition'],
    folder='train',
    suff='.tif',
    valid_pct=0.2,
    seed=CONFIG.seed,
)

Learner:

learn = vision_learner(dls, resnet18, metrics=RocAucBinary(),
                          path=paths['output'], model_dir=paths['models'])

Finding learning rate:

learn.lr_find()

leads to an error:

TypeError: Exception occured in LRFinder when calling event before_fit:
expected str, bytes or os.PathLike object, not NoneType

Full error
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_24/2434377672.py in <module>
----> 1 learn.lr_find()

/opt/conda/lib/python3.7/site-packages/fastai/callback/schedule.py in lr_find(self, start_lr, end_lr, num_it, stop_div, show_plot, suggest_funcs)
    291     n_epoch = num_it//len(self.dls.train) + 1
    292     cb=LRFinder(start_lr=start_lr, end_lr=end_lr, num_it=num_it, stop_div=stop_div)
--> 293     with self.no_logging(): self.fit(n_epoch, cbs=cb)
    294     if suggest_funcs is not None:
    295         lrs, losses = tensor(self.recorder.lrs[num_it//10:-5]), tensor(self.recorder.losses[num_it//10:-5])

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt, start_epoch)
    254             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    255             self.n_epoch = n_epoch
--> 256             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    257 
    258     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    191 
    192     def _with_events(self, f, event_type, ex, final=noop):
--> 193         try: self(f'before_{event_type}');  f()
    194         except ex: self(f'after_cancel_{event_type}')
    195         self(f'after_{event_type}');  final()

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in __call__(self, event_name)
    169 
    170     def ordered_cbs(self, event): return [cb for cb in self.cbs.sorted('order') if hasattr(cb, event)]
--> 171     def __call__(self, event_name): L(event_name).map(self._call_one)
    172 
    173     def _call_one(self, event_name):

/opt/conda/lib/python3.7/site-packages/fastcore/foundation.py in map(self, f, gen, *args, **kwargs)
    154     def range(cls, a, b=None, step=None): return cls(range_of(a, b=b, step=step))
    155 
--> 156     def map(self, f, *args, gen=False, **kwargs): return self._new(map_ex(self, f, *args, gen=gen, **kwargs))
    157     def argwhere(self, f, negate=False, **kwargs): return self._new(argwhere(self, f, negate, **kwargs))
    158     def argfirst(self, f, negate=False): return first(i for i,o in self.enumerate() if f(o))

/opt/conda/lib/python3.7/site-packages/fastcore/basics.py in map_ex(iterable, f, gen, *args, **kwargs)
    838     res = map(g, iterable)
    839     if gen: return res
--> 840     return list(res)
    841 
    842 # %% ../nbs/01_basics.ipynb 336

/opt/conda/lib/python3.7/site-packages/fastcore/basics.py in __call__(self, *args, **kwargs)
    823             if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
    824         fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 825         return self.func(*fargs, **kwargs)
    826 
    827 # %% ../nbs/01_basics.ipynb 326

/opt/conda/lib/python3.7/site-packages/fastai/learner.py in _call_one(self, event_name)
    173     def _call_one(self, event_name):
    174         if not hasattr(event, event_name): raise Exception(f'missing {event_name}')
--> 175         for cb in self.cbs.sorted('order'): cb(event_name)
    176 
    177     def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

/opt/conda/lib/python3.7/site-packages/fastai/callback/core.py in __call__(self, event_name)
     60             try: res = getcallable(self, event_name)()
     61             except (CancelBatchException, CancelBackwardException, CancelEpochException, CancelFitException, CancelStepException, CancelTrainException, CancelValidException): raise
---> 62             except Exception as e: raise modify_exception(e, f'Exception occured in `{self.__class__.__name__}` when calling event `{event_name}`:\n\t{e.args[0]}', replace=True)
     63         if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit
     64         return res

/opt/conda/lib/python3.7/site-packages/fastai/callback/core.py in __call__(self, event_name)
     58         res = None
     59         if self.run and _run:
---> 60             try: res = getcallable(self, event_name)()
     61             except (CancelBatchException, CancelBackwardException, CancelEpochException, CancelFitException, CancelStepException, CancelTrainException, CancelValidException): raise
     62             except Exception as e: raise modify_exception(e, f'Exception occured in `{self.__class__.__name__}` when calling event `{event_name}`:\n\t{e.args[0]}', replace=True)

/opt/conda/lib/python3.7/site-packages/fastai/callback/schedule.py in before_fit(self)
    180     def before_fit(self):
    181         super().before_fit()
--> 182         path = self.path/self.model_dir
    183         path.mkdir(parents=True, exist_ok=True)
    184         self.tmp_d = tempfile.TemporaryDirectory(dir=path)

/opt/conda/lib/python3.7/pathlib.py in __truediv__(self, key)
    923 
    924     def __truediv__(self, key):
--> 925         return self._make_child((key,))
    926 
    927     def __rtruediv__(self, key):

/opt/conda/lib/python3.7/pathlib.py in _make_child(self, args)
    702 
    703     def _make_child(self, args):
--> 704         drv, root, parts = self._parse_args(args)
    705         drv, root, parts = self._flavour.join_parsed_parts(
    706             self._drv, self._root, self._parts, drv, root, parts)

/opt/conda/lib/python3.7/pathlib.py in _parse_args(cls, args)
    656                 parts += a._parts
    657             else:
--> 658                 a = os.fspath(a)
    659                 if isinstance(a, str):
    660                     # Force-cast str subclasses to str (issue #21127)

TypeError: Exception occured in `LRFinder` when calling event `before_fit`:
	expected str, bytes or os.PathLike object, not NoneType

By the way, it worked beforehand, yet I don’t know what I exactly changed meanwhile that I cannot reproduce anymore.

Checking LRFinder.before_fit?? shows that (almost) everything that can go wrong at that point is path related and the Error Message points into that direction as well. So you could check if there is any issue with the paths that you set. If you need more help the easiest way would be to make you kaggle notebook public and share a link here :slight_smile:!

1 Like

Eventually I’ve found the problem which as you said, was about paths! Thanks.

1 Like