Hi all. I’m trying to use SaveModelCallback
but it keeps throwing when I change what to monitor from valid_loss
to accuracy
and try to validate the model. Here is what I’ve done so far with the stack trace
def addCallback(learner):
#changing monitor from `valid_loss` to `accuracy` throws an error
learner.add_cbs([SaveModelCallback(monitor='accuracy', fname='classifier-model-saved-by-callback')])
learn = cnn_learner(dls, resnet152, loss_func=CrossEntropyLossFlat(), metrics=accuracy).to_fp16()
addCallback(learn)
learn.validate(1)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-70-631604a2e07b> in <module>()
----> 1 learn.validate()
14 frames
/usr/local/lib/python3.6/dist-packages/fastai2/learner.py in validate(self, ds_idx, dl, cbs)
211 self(_before_epoch)
212 self._do_epoch_validate(ds_idx, dl)
--> 213 self(_after_epoch)
214 return getattr(self, 'final_record', None)
215
/usr/local/lib/python3.6/dist-packages/fastai2/learner.py in __call__(self, event_name)
132 def ordered_cbs(self, event): return [cb for cb in sort_by_run(self.cbs) if hasattr(cb, event)]
133
--> 134 def __call__(self, event_name): L(event_name).map(self._call_one)
135 def _call_one(self, event_name):
136 assert hasattr(event, event_name)
/usr/local/lib/python3.6/dist-packages/fastcore/foundation.py in map(self, f, *args, **kwargs)
375 else f.format if isinstance(f,str)
376 else f.__getitem__)
--> 377 return self._new(map(g, self))
378
379 def filter(self, f, negate=False, **kwargs):
/usr/local/lib/python3.6/dist-packages/fastcore/foundation.py in _new(self, items, *args, **kwargs)
325 @property
326 def _xtra(self): return None
--> 327 def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
328 def __getitem__(self, idx): return self._get(idx) if is_indexer(idx) else L(self._get(idx), use_list=None)
329 def copy(self): return self._new(self.items.copy())
/usr/local/lib/python3.6/dist-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
/usr/local/lib/python3.6/dist-packages/fastcore/foundation.py in __init__(self, items, use_list, match, *rest)
316 if items is None: items = []
317 if (use_list is not None) or not _is_array(items):
--> 318 items = list(items) if use_list else _listify(items)
319 if match is not None:
320 if is_coll(match): match = len(match)
/usr/local/lib/python3.6/dist-packages/fastcore/foundation.py in _listify(o)
252 if isinstance(o, list): return o
253 if isinstance(o, str) or _is_array(o): return [o]
--> 254 if is_iter(o): return list(o)
255 return [o]
256
/usr/local/lib/python3.6/dist-packages/fastcore/foundation.py in __call__(self, *args, **kwargs)
218 if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
219 fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 220 return self.fn(*fargs, **kwargs)
221
222 # Cell
/usr/local/lib/python3.6/dist-packages/fastai2/learner.py in _call_one(self, event_name)
135 def _call_one(self, event_name):
136 assert hasattr(event, event_name)
--> 137 [cb(event_name) for cb in sort_by_run(self.cbs)]
138
139 def _bn_bias_state(self, with_bias): return bn_bias_params(self.model, with_bias).map(self.opt.state)
/usr/local/lib/python3.6/dist-packages/fastai2/learner.py in <listcomp>(.0)
135 def _call_one(self, event_name):
136 assert hasattr(event, event_name)
--> 137 [cb(event_name) for cb in sort_by_run(self.cbs)]
138
139 def _bn_bias_state(self, with_bias): return bn_bias_params(self.model, with_bias).map(self.opt.state)
/usr/local/lib/python3.6/dist-packages/fastai2/callback/core.py in __call__(self, event_name)
22 _run = (event_name not in _inner_loop or (self.run_train and getattr(self, 'training', True)) or
23 (self.run_valid and not getattr(self, 'training', False)))
---> 24 if self.run and _run: getattr(self, event_name, noop)()
25 if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit
26
/usr/local/lib/python3.6/dist-packages/fastai2/callback/tracker.py in after_epoch(self)
79 if self.every_epoch: self._save(f'{self.fname}_{self.epoch}')
80 else: #every improvement
---> 81 super().after_epoch()
82 if self.new_best: self._save(f'{self.fname}')
83
/usr/local/lib/python3.6/dist-packages/fastai2/callback/tracker.py in after_epoch(self)
37 def after_epoch(self):
38 "Compare the last value to the best up to know"
---> 39 val = self.recorder.values[-1][self.idx]
40 if self.comp(val - self.min_delta, self.best): self.best,self.new_best = val,True
41 else: self.new_best = False
/usr/local/lib/python3.6/dist-packages/fastcore/foundation.py in __getitem__(self, idx)
326 def _xtra(self): return None
327 def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
--> 328 def __getitem__(self, idx): return self._get(idx) if is_indexer(idx) else L(self._get(idx), use_list=None)
329 def copy(self): return self._new(self.items.copy())
330
/usr/local/lib/python3.6/dist-packages/fastcore/foundation.py in _get(self, i)
330
331 def _get(self, i):
--> 332 if is_indexer(i) or isinstance(i,slice): return getattr(self.items,'iloc',self.items)[i]
333 i = mask2idxs(i)
334 return (self.items.iloc[list(i)] if hasattr(self.items,'iloc')
IndexError: list index out of range