Thanks @imanol and @bfarzin for the suggestion. I tried it and it did work. However, ClassificationInterpretation.from_learner(learner_obj)
doesn’t seem to work after applying this change to the learner_obj
. It throws an error: Expected object of backend CPU but got backend CUDA for argument #3 'weight'
from deep within the pytorch lib.
After going through the traces, I suspected there is something wrong with setting the weight property to the learner object (Total guess, I am really new to this library. Will update once I find it). So, I tried to pass the loss_func
as an arg to learner constructor and it worked!
So, here is what solved the issue for me:
learn = cnn_learner(data, models.resnet34, metrics=[error_rate, accuracy],
loss_func=nn.CrossEntropyLoss(weight=torch.FloatTensor([5., 1.]).cuda()))
Here is what I tried before that cased an issue:
learn = cnn_learner(data, models.resnet34, metrics=[error_rate, accuracy])
learn.loss_func.func = nn.CrossEntropyLoss(weight=torch.FloatTensor([5., 1.]).cuda())
...<some_learning>
show_results(learn)
And here is an error trace (is quite a big), if someone wants to work on it :
<ipython-input-162-538d0a4da532> in show_results(learn)
1 def show_results(learn):
2 learn.recorder.plot_losses()
----> 3 interp = ClassificationInterpretation.from_learner(learn)
4 interp.plot_top_losses(9, figsize=(15,11), heatmap= False)
5 print(interp.most_confused(min_val=1))
/opt/conda/lib/python3.6/site-packages/fastai/vision/learner.py in _cl_int_from_learner(cls, learn, ds_type, tta)
126 def _cl_int_from_learner(cls, learn:Learner, ds_type:DatasetType=DatasetType.Valid, tta=False):
127 "Create an instance of `ClassificationInterpretation`. `tta` indicates if we want to use Test Time Augmentation."
--> 128 preds = learn.TTA(ds_type=ds_type, with_loss=True) if tta else learn.get_preds(ds_type=ds_type, with_loss=True)
129 return cls(learn, *preds, ds_type=ds_type)
130
/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in get_preds(self, ds_type, with_loss, n_batch, pbar)
334 lf = self.loss_func if with_loss else None
335 return get_preds(self.model, self.dl(ds_type), cb_handler=CallbackHandler(self.callbacks),
--> 336 activ=_loss_func2activ(self.loss_func), loss_func=lf, n_batch=n_batch, pbar=pbar)
337
338 def pred_batch(self, ds_type:DatasetType=DatasetType.Valid, batch:Tuple=None, reconstruct:bool=False, with_dropout:bool=False) -> List[Tensor]:
/opt/conda/lib/python3.6/site-packages/fastai/basic_train.py in get_preds(model, dl, pbar, cb_handler, activ, loss_func, n_batch)
44 zip(*validate(model, dl, cb_handler=cb_handler, pbar=pbar, average=False, n_batch=n_batch))]
45 if loss_func is not None:
---> 46 with NoneReduceOnCPU(loss_func) as lf: res.append(lf(res[0], res[1]))
47 if activ is not None: res[0] = activ(res[0])
48 return res
/opt/conda/lib/python3.6/site-packages/fastai/layers.py in __call__(self, input, target, **kwargs)
235 if self.floatify: target = target.float()
236 input = input.view(-1,input.shape[-1]) if self.is_2d else input.view(-1)
--> 237 return self.func.__call__(input, target.view(-1), **kwargs)
238
239 def CrossEntropyFlat(*args, axis:int=-1, **kwargs):
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
940 def forward(self, input, target):
941 return F.cross_entropy(input, target, weight=self.weight,
--> 942 ignore_index=self.ignore_index, reduction=self.reduction)
943
944
/opt/conda/lib/python3.6/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
2054 if size_average is not None or reduce is not None:
2055 reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2056 return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
2057
2058
/opt/conda/lib/python3.6/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
1869 .format(input.size(0), target.size(0)))
1870 if dim == 2:
-> 1871 ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
1872 elif dim == 4:
1873 ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: Expected object of backend CPU but got backend CUDA for argument #3 'weight'