Error running object detection algorithm

Hi,

i’m trying to run a detection algorithm using the notebook 06_Object_Detection.ipynb as example but i receive the following error when running learn.fit_one_cycle(10, slice(1e-5, 1e-4)).

the full trace is here :

Thanks in advance for your help

TypeError                                 Traceback (most recent call last)
<ipython-input-60-5d23c3b5e06d> in <module>()
----> 1 learn.fit_one_cycle(10, slice(1e-5, 1e-4))

16 frames
/usr/local/lib/python3.7/dist-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
    114 
    115 # Cell
--> 116 @patch
    117 def plot_sched(self:Recorder, keys=None, figsize=None):
    118     keys = self.hps.keys() if keys is None else L(keys)

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    219     def validate(self, ds_idx=1, dl=None, cbs=None):
    220         if dl is None: dl = self.dls[ds_idx]
--> 221         with self.validation_context(cbs=cbs): self._do_epoch_validate(ds_idx, dl)
    222         return getattr(self, 'final_record', None)
    223 

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161         for o in enumerate(self.dl): self.one_batch(*o)
    162 
--> 163     def _do_one_batch(self):
    164         self.pred = self.model(*self.xb)
    165         self('after_pred')

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_fit(self)
    210     def __enter__(self): self(_before_epoch); return self
    211     def __exit__(self, exc_type, exc_value, tb): self(_after_epoch)
--> 212 
    213     def validation_context(self, cbs=None, inner=False):
    214         cms = [self.no_logging(),self.no_mbar()]

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161         for o in enumerate(self.dl): self.one_batch(*o)
    162 
--> 163     def _do_one_batch(self):
    164         self.pred = self.model(*self.xb)
    165         self('after_pred')

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_epoch(self)
    204             if wd is not None: self.opt.set_hypers(wd=wd)
    205             self.opt.set_hypers(lr=self.lr if lr is None else lr)
--> 206             self.n_epoch = n_epoch
    207             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    208 

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_epoch_train(self)
    196             self.epoch=epoch
    197             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
--> 198 
    199     @log_args(but='cbs')
    200     def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161         for o in enumerate(self.dl): self.one_batch(*o)
    162 
--> 163     def _do_one_batch(self):
    164         self.pred = self.model(*self.xb)
    165         self('after_pred')

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in all_batches(self)
    167         self('after_loss')
    168         if not self.training or not len(self.yb): return
--> 169         self('before_backward')
    170         self._backward()
    171         self('after_backward')

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in one_batch(self, i, b)
    192         self._do_epoch_validate()
    193 
--> 194     def _do_fit(self):
    195         for epoch in range(self.n_epoch):
    196             self.epoch=epoch

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161         for o in enumerate(self.dl): self.one_batch(*o)
    162 
--> 163     def _do_one_batch(self):
    164         self.pred = self.model(*self.xb)
    165         self('after_pred')

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_one_batch(self)
    173         self('after_step')
    174         self.opt.zero_grad()
--> 175 
    176     def one_batch(self, i, b):
    177         self.iter = i

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1100             >>> for name, param in self.named_parameters():
   1101             >>>    if name in ['bias']:
-> 1102             >>>        print(param.size())
   1103 
   1104         """

/content/Practical-Deep-Learning-for-Coders-2.0/Computer Vision/imports/metrics.py in forward(self, output, bbox_tgts, clas_tgts)
    146         n_classes = clas_preds.size(2)
    147         return sum([self._one_loss(cp, bp, ct, bt)
--> 148                     for (cp, bp, ct, bt) in zip(clas_preds, bbox_preds, clas_tgts, bbox_tgts)])/clas_tgts.size(0)
    149 
    150 class SigmaL1SmoothLoss(nn.Module):

/content/Practical-Deep-Learning-for-Coders-2.0/Computer Vision/imports/metrics.py in <listcomp>(.0)
    146         n_classes = clas_preds.size(2)
    147         return sum([self._one_loss(cp, bp, ct, bt)
--> 148                     for (cp, bp, ct, bt) in zip(clas_preds, bbox_preds, clas_tgts, bbox_tgts)])/clas_tgts.size(0)
    149 
    150 class SigmaL1SmoothLoss(nn.Module):

/content/Practical-Deep-Learning-for-Coders-2.0/Computer Vision/imports/metrics.py in _one_loss(self, clas_pred, bbox_pred, clas_tgt, bbox_tgt)
    139         clas_tgt = torch.cat([clas_tgt.new_zeros(1).long(), clas_tgt])
    140         clas_tgt = clas_tgt[matches[clas_mask]]
--> 141         return bb_loss + self._focal_loss(clas_pred, clas_tgt)/torch.clamp(bbox_mask.sum(), min=1.)
    142 
    143     def forward(self, output, bbox_tgts, clas_tgts):

/content/Practical-Deep-Learning-for-Coders-2.0/Computer Vision/imports/metrics.py in _focal_loss(self, clas_pred, clas_tgt)
    118         encoded_tgt = encode_class(clas_tgt, clas_pred.size(1))
    119         ps = torch.sigmoid(clas_pred.detach())
--> 120         weights = encoded_tgt * (1-ps) + (1-encoded_tgt) * ps
    121         alphas = (1-encoded_tgt) * self.alpha + encoded_tgt * (1-self.alpha)
    122         weights.pow_(self.gamma).mul_(alphas)

TypeError: unsupported operand type(s) for *: 'TensorMultiCategory' and 'TensorImage'

EDIT:

Adding :

TensorImage.register_func(torch.nn.functional.smooth_l1_loss, TensorImage, TensorBBox)

TensorMultiCategory.register_func(TensorMultiCategory.mul, TensorMultiCategory, TensorImage)

TensorImage.register_func(torch.nn.functional.binary_cross_entropy_with_logits, TensorImage, TensorMultiCategory)

Solved the issue…

1 Like