Lr_find on create_unet learner errors

Hi All,

I am trying to perform image segmentation task. After creating the unet learner object as below
learn = Learner.create_unet(data,models.resnet18,metrics = acc_ccp)

I tried to perform lr_find i.e. learn.lr_find(). However this appears to be error out as below with a value error :

LR Finder complete, type {learner_name}.recorder.plot() to see the graph.

ValueError Traceback (most recent call last)
in ()
----> 1 learn.lr_find()
2 #learn.recorder.plot()

~/.anaconda3/lib/python3.7/site-packages/fastai/train.py in lr_find(learn, start_lr, end_lr, num_it, stop_div, **kwargs)
26 cb = LRFinder(learn, start_lr, end_lr, num_it, stop_div)
27 a = int(np.ceil(num_it/len(learn.data.train_dl)))
β€”> 28 learn.fit(a, start_lr, callbacks=[cb], **kwargs)
29
30 def to_fp16(learn:Learner, loss_scale:float=512., flat_master:bool=False)->Learner:

~/.anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
160 callbacks = [cb(self) for cb in self.callback_fns] + listify(callbacks)
161 fit(epochs, self.model, self.loss_func, opt=self.opt, data=self.data, metrics=self.metrics,
–> 162 callbacks=self.callbacks+callbacks)
163
164 def create_opt(self, lr:Floats, wd:Floats=0.)->None:

~/.anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in fit(epochs, model, loss_func, opt, data, callbacks, metrics)
92 except Exception as e:
93 exception = e
β€”> 94 raise e
95 finally: cb_handler.on_train_end(exception)
96

~/.anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in fit(epochs, model, loss_func, opt, data, callbacks, metrics)
82 for xb,yb in progress_bar(data.train_dl, parent=pbar):
83 xb, yb = cb_handler.on_batch_begin(xb, yb)
β€”> 84 loss = loss_batch(model, xb, yb, loss_func, opt, cb_handler)
85 if cb_handler.on_batch_end(loss): break
86

~/.anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)
20
21 if not loss_func: return to_detach(out), yb[0].detach()
β€”> 22 loss = loss_func(out, *yb)
23
24 if opt is not None:

~/.anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
475 result = self._slow_forward(*input, **kwargs)
476 else:
–> 477 result = self.forward(*input, **kwargs)
478 for hook in self._forward_hooks.values():
479 hook_result = hook(self, input, result)

~/.anaconda3/lib/python3.7/site-packages/fastai/layers.py in forward(self, input, target)
96 def forward(self, input:Tensor, target:Tensor) -> Rank0Tensor:
97 n,c,*_ = input.shape
β€”> 98 return super().forward(input.view(n, c, -1), target.view(n, -1))
99
100 class MSELossFlat(nn.MSELoss):

~/.anaconda3/lib/python3.7/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
865 def forward(self, input, target):
866 return F.cross_entropy(input, target, weight=self.weight,
–> 867 ignore_index=self.ignore_index, reduction=self.reduction)
868
869

~/.anaconda3/lib/python3.7/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
1665 if size_average is not None or reduce is not None:
1666 reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 1667 return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
1668
1669

~/.anaconda3/lib/python3.7/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
1531 if target.size()[1:] != input.size()[2:]:
1532 raise ValueError(β€˜Expected target size {}, got {}’.format(
-> 1533 out_size, target.size()))
1534 input = input.contiguous().view(n, c, 1, -1)
1535 target = target.contiguous().view(n, 1, -1)

ValueError: Expected target size (8, 114816), got torch.Size([8, 114125])

It though appears there seems to be size mismatches when trying to calculate the loss . However I am not sure where the issue may be, please advise the places where I can look to debug this.

1 Like