SOLVED: NameError: name ‘calc_loss’ is not defined

Hi all, can someone help with this. I am getting the following error:

NameError: name ‘calc_loss’ is not defined

Full error:

NameError Traceback (most recent call last)
5 learn = cnn_learner(data, models.squeezenet1_0)
6 learn.load(‘stage-2’)
----> 7 classify = ClassificationInterpretation.from_learner(learn, tta=True)
8 correct = (classify.pred_class==classify.y_true).sum()
9 print (f’Validation set - correctly classified: {correct} incorrectly: {len(classify.y_true)-correct}\n’)

c:\pillview\nih\fastai\fastai\vision\ in _cl_int_from_learner(cls, learn, ds_type, tta)
113 def _cl_int_from_learner(cls, learn:Learner, ds_type:DatasetType=DatasetType.Valid, tta=False):
114 “Create an instance of ClassificationInterpretation. tta indicates if we want to use Test Time Augmentation.”
–> 115 preds = learn.TTA(ds_type=ds_type, with_loss=True) if tta else learn.get_preds(ds_type=ds_type, with_loss=True)
116 return cls(learn, *preds, ds_type=ds_type)

c:\pillview\nih\fastai\fastai\vision\ in _TTA(learn, beta, scale, ds_type, with_loss)
39 final_preds = predsbeta + avg_preds(1-beta)
40 if with_loss:
—> 41 return final_preds, y, calc_loss(final_preds, y, learn.loss_func)
42 return final_preds, y

NameError: name ‘calc_loss’ is not defined

Within here is the code

def _TTA(learn:Learner, beta:float=0.4, scale:float=1.35, ds_type:DatasetType=DatasetType.Valid, with_loss:bool=False) -> Tensors:
“Applies TTA to predict on ds_type dataset.”
preds,y = learn.get_preds(ds_type)
all_preds = list(learn.tta_only(scale=scale, ds_type=ds_type))
avg_preds = torch.stack(all_preds).mean(0)
if beta is None: return preds,avg_preds,y
final_preds = predsbeta + avg_preds(1-beta)
if with_loss:
return final_preds, y, calc_loss(final_preds, y, learn.loss_func)
return final_preds, y

Learner.TTA = _TTA

Any ideas on what may be causing this?


Update: Note this works if you replace

classify = ClassificationInterpretation.from_learner(learn, tta=True)


classify = ClassificationInterpretation.from_learner(learn)

Was wondering why the previous error if TTA=true

IT’s linked to this issue and I’ll fix it tomorrow.

1 Like

Thanks @sgugger!

The update now works. Thanks!

1 Like