Strange error when running learn.fit_one_cycle

Hi guys, first post here.
Have been getting a series of errors but couldnt find a fix for this one, can any of you give me some insight?? Here goes the error msg:

AttributeError Traceback (most recent call last)
in ()
----> 1 learn.fit(2)

6 frames
/usr/local/lib/python3.6/dist-packages/fastai/callback.py in on_batch_end(self, last_output, last_target, **kwargs)
347 dist.all_reduce(val, op=dist.ReduceOp.SUM)
348 val /= self.world
–> 349 self.val += first_el(last_target).size(0) * val.detach().cpu()
350
351 def on_epoch_end(self, last_metrics, **kwargs):

AttributeError: ‘NoneType’ object has no attribute ‘detach’

This is for lesson 1 and happens when I use model.fit, at the end of each epoch.
Thanks guys!!

Hi! Could you share which notebook you are following? :slight_smile: As the lesson 1 uses fit_one_cycle instead of fit.

Thought I would add more information: my validation set is not empty, this is what the datagenerator lists:
Valid: LabelList (160 items)
x: ImageList
Image (3, 224, 224),Image (3, 224, 224),Image (3, 224, 224),Image (3, 224, 224),Image (3, 224, 224)
y: MultiCategoryList
0,0,0,4,0
Path: /content/kaggle/train;

and this is the entire error, not just the last line:

0.00% [0/2 00:00<00:00]
in ()
----> 1 learn.fit_one_cycle(2)

/usr/local/lib/python3.6/dist-packages/fastai/train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch)
20 callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,
21 final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))
—> 22 learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
23
24 def lr_find(learn:Learner, start_lr:Floats=1e-7, end_lr:Floats=10, num_it:int=100, stop_div:bool=True, wd:float=None):

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
200 callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
201 self.cb_fns_registered = True
–> 202 fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
203
204 def create_opt(self, lr:Floats, wd:Floats=0.)->None:

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in fit(epochs, learn, callbacks, metrics)
104 if not cb_handler.skip_validate and not learn.data.empty_val:
105 val_loss = validate(learn.model, learn.data.valid_dl, loss_func=learn.loss_func,
–> 106 cb_handler=cb_handler, pbar=pbar)
107 else: val_loss=None
108 if cb_handler.on_epoch_end(val_loss): break

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in validate(model, dl, loss_func, cb_handler, pbar, average, n_batch)
61 if not is_listy(yb): yb = [yb]
62 nums.append(first_el(yb).shape[0])
—> 63 if cb_handler and cb_handler.on_batch_end(val_losses[-1]): break
64 if n_batch and (len(nums)>=n_batch): break
65 nums = np.array(nums, dtype=np.float32)

/usr/local/lib/python3.6/dist-packages/fastai/callback.py in on_batch_end(self, loss)
306 “Handle end of processing one batch with loss.”
307 self.state_dict[‘last_loss’] = loss
–> 308 self(‘batch_end’, call_mets = not self.state_dict[‘train’])
309 if self.state_dict[‘train’]:
310 self.state_dict[‘iteration’] += 1

/usr/local/lib/python3.6/dist-packages/fastai/callback.py in call(self, cb_name, call_mets, **kwargs)
248 “Call through to all of the CallbakHandler functions.”
249 if call_mets:
–> 250 for met in self.metrics: self._call_and_update(met, cb_name, **kwargs)
251 for cb in self.callbacks: self._call_and_update(cb, cb_name, **kwargs)
252

/usr/local/lib/python3.6/dist-packages/fastai/callback.py in _call_and_update(self, cb, cb_name, **kwargs)
239 def call_and_update(self, cb, cb_name, **kwargs)->None:
240 “Call cb_name on cb and update the inner state.”
–> 241 new = ifnone(getattr(cb, f’on
{cb_name}’)(**self.state_dict, **kwargs), dict())
242 for k,v in new.items():
243 if k not in self.state_dict:

/usr/local/lib/python3.6/dist-packages/fastai/callback.py in on_batch_end(self, last_output, last_target, **kwargs)
347 dist.all_reduce(val, op=dist.ReduceOp.SUM)
348 val /= self.world
–> 349 self.val += first_el(last_target).size(0) * val.detach().cpu()
350
351 def on_epoch_end(self, last_metrics, **kwargs):

AttributeError: ‘NoneType’ object has no attribute ‘detach’

thanks a lot guys, this is really annoying :expressionless:

UPDATE: have been able to fix the problem.

After a bit of searching I found my metric was inappropriate as I was using Multi-labels.

Fixed after I changed
This:
learn = cnn_learner(data, models.resnet18, metrics = error_rate)
To this:
acc_02 = partial(accuracy_thresh, thresh=0.2)
f_score = partial(fbeta, thresh=0.2)
learn = cnn_learner(data, models.resnet18, metrics = [acc_02, f_score])

this link explains it better: https://gilberttanner.com/blog/fastai-multi-label-image-classification

However acc_02 and f_score are for multi-class classification, this is single class if you are truly following lesson 1? Could you share your notebook with us? :slight_smile:

I accidentally got a dataset with multi-class, wasnt aware there was a difference ,and, when I realised there was, figured Id carry on as I was near the end haha… not sure how to share a notebook, and im using colab.

1 Like

Ah got it :slight_smile: see the planets notebook for an example on multi class :slight_smile: glad to hear you figured it out! Great job

Hi I’m having a strange error while doing regression with multiple points.

learn.fit_one_cycle(1,1e-2)

/usr/local/lib/python3.6/dist-packages/fastai/callback.py in on_backward_begin(self, loss) 288 def on_backward_begin(self, loss:Tensor)->Tuple[Any,Any]: 289 “Handle gradient calculation on loss.” --> 290 self.smoothener.add_value(loss.float().detach().cpu()) 291 self.state_dict[‘last_loss’], self.state_dict[‘smooth_loss’] = loss, self.smoothener.smooth 292 self(‘backward_begin’, call_mets=False)

AttributeError: ‘NoneType’ object has no attribute ‘float’