Fastai2 bounding boxes: forward() takes 3 positional argument but 4 were given

Hello all,

I am trying out fastai2 on a bounding box problem, defining the datablock/dataloaders as following:

block = DataBlock(blocks=(ImageBlock, BBoxBlock, BBoxLblBlock),
get_items=get_items,
splitter=RandomSplitter(),
get_y=[get_bbox, get_lbl],
item_tfms=Resize(460, method=ResizeMethod.Pad),
batch_tfms = aug_transforms(size=124),
n_inp=1)
dls = block.dataloaders(path,bs=32)

But I keep getting this strange error:

TypeError Traceback (most recent call last)
in
----> 1 learn.lr_find()

/opt/conda/lib/python3.7/site-packages/fastai2/callback/schedule.py in lr_find(self, start_lr, end_lr, num_it, stop_div, show_plot, suggestions)
226 n_epoch = num_it//len(self.dls.train) + 1
227 cb=LRFinder(start_lr=start_lr, end_lr=end_lr, num_it=num_it, stop_div=stop_div)
–> 228 with self.no_logging(): self.fit(n_epoch, cbs=cb)
229 if show_plot: self.recorder.plot_lr_find()
230 if suggestions:

/opt/conda/lib/python3.7/site-packages/fastcore/utils.py in _f(*args, **kwargs)
429 init_args.update(log)
430 setattr(inst, ‘init_args’, init_args)
–> 431 return inst if to_return else f(*args, **kwargs)
432 return _f
433

/opt/conda/lib/python3.7/site-packages/fastai2/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
198 try:
199 self.epoch=epoch; self(‘begin_epoch’)
–> 200 self._do_epoch_train()
201 self._do_epoch_validate()
202 except CancelEpochException: self(‘after_cancel_epoch’)

/opt/conda/lib/python3.7/site-packages/fastai2/learner.py in _do_epoch_train(self)
173 try:
174 self.dl = self.dls.train; self(‘begin_train’)
–> 175 self.all_batches()
176 except CancelTrainException: self(‘after_cancel_train’)
177 finally: self(‘after_train’)

/opt/conda/lib/python3.7/site-packages/fastai2/learner.py in all_batches(self)
151 def all_batches(self):
152 self.n_iter = len(self.dl)
–> 153 for o in enumerate(self.dl): self.one_batch(*o)
154
155 def one_batch(self, i, b):

/opt/conda/lib/python3.7/site-packages/fastai2/learner.py in one_batch(self, i, b)
159 self.pred = self.model(*self.xb); self(‘after_pred’)
160 if len(self.yb) == 0: return
–> 161 self.loss = self.loss_func(self.pred, *self.yb); self(‘after_loss’)
162 if not self.training: return
163 self.loss.backward(); self(‘after_backward’)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
530 result = self._slow_forward(*input, **kwargs)
531 else:
–> 532 result = self.forward(*input, **kwargs)
533 for hook in self._forward_hooks.values():
534 hook_result = hook(self, input, result)

TypeError: forward() takes 3 positional arguments but 4 were given

I could be easily be mistaken and doing something wrong, but I perhaps the error is due to the multiple labels I specified for the datablocks for this problem, and fastai2 has an issue with that in spite of designating one input with n_in. Thanks :slight_smile:

In order to understand more and help we need to know what your model looks like.

1 Like

Here it is my good sir:

learn = cnn_learner(dls, resnet34, loss_func=LabelSmoothingCrossEntropy()).to_fp16()
learn.lr_find()

The error occurs when I try to run lr_find() or fine_tune() on the model. Thanks!

Yes. That won’t work. Object detection requires custom models and is quite advanced. See my tutorial here, notice cnn_learner is never called. https://github.com/muellerzr/Practical-Deep-Learning-for-Coders-2.0/blob/master/Computer%20Vision/06_Object_Detection.ipynb

1 Like

You’re a life saver. Thanks again!

Hello,

Using the fastai v1 to do object detection I ran into the same error message.
The only difference is that I was using the following custom loss func:

class MAELossCust(nn.Module):
    def __init__(self):
        super().__init__()
    
    def forward(self,pred,target):
        return torch.nn.functional.l1_loss(pred,target[0])

With the debugger I noticed that the *input which is passed to the loss function has size 3:

  • the ouput of the model
  • the bboxes
  • the classes

If you add the self argument of the call signature of the method, thats 4 arguments. While my method was designed to receive only 3 (self +2), as I thought the target was a tuple of tensors. So I simply added a dummy argument and removed the index on the target:

def forward(self,pred,target,_):
    return torch.nn.functional.l1_loss(pred,target)

To go back to your problem, my assumption is that the the loss funcion you use LabelSmoothingCrossEntropy has a signature of the form (pred,targ). So you might want to use the functional form of your loss and wrap into a custom class using the same forward signature as mine, just passing to your loss function the pred and target arguments.

Hope that it makes sense…

I followed the object detection notebook with a custom dataloader and I get the following error

getters = [lambda o: o,
           lambda o: df_png['bbox'].loc[df_png.isin([str(o).split('/')[-1].
                                                    replace('.png','_mask.png')]).any(axis=1)].values[0][0],
           lambda o: df_png['bbox'].loc[df_png.isin([str(o).split('/')[-1].
                                                    replace('.png','_mask.png')]).any(axis=1)].values[0][1]
          ]
#Datablock and dataloader

def label_func(x): 
    print(x)
    return path/'masks'/f'{x.stem}_mask.png'

#batch_tfms = [*aug_transforms()]
batch_tfms = [Rotate(), Flip(), Dihedral(), Normalize.from_stats(*imagenet_stats)]

def get_dls(bs, size):
    if size==0:
        db = DataBlock(blocks = (ImageBlock, BBoxBlock, BBoxLblBlock),
                      splitter = FuncSplitter(func),
                      get_items = get_image_files,
                      getters = getters,
#                       batch_tfms = batch_tfms
                      )
    else:
        db = DataBlock(blocks = (ImageBlock, BBoxBlock, BBoxLblBlock),
                       splitter = FuncSplitter(func),
                       get_items = get_image_files,
                       getters = getters,
#                        batch_tfms = batch_tfms,
                       item_tfms = [Resize(size, method='pad'),]
                      )
return db.dataloaders('/home/fabiog/jupyter/all_images', bs=bs)
> TypeError                                 Traceback (most recent call last)
> /tmp/ipykernel_4357/465355952.py in <module>
> ----> 1 learn.fit_one_cycle(10, slice(1e-5, 1e-4))
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
>     111     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
>     112               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
> --> 113     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
>     114 
>     115 # Cell
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
>     219             self.opt.set_hypers(lr=self.lr if lr is None else lr)
>     220             self.n_epoch = n_epoch
> --> 221             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
>     222 
>     223     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
>     161 
>     162     def _with_events(self, f, event_type, ex, final=noop):
> --> 163         try: self(f'before_{event_type}');  f()
>     164         except ex: self(f'after_cancel_{event_type}')
>     165         self(f'after_{event_type}');  final()
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_fit(self)
>     210         for epoch in range(self.n_epoch):
>     211             self.epoch=epoch
> --> 212             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
>     213 
>     214     def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
>     161 
>     162     def _with_events(self, f, event_type, ex, final=noop):
> --> 163         try: self(f'before_{event_type}');  f()
>     164         except ex: self(f'after_cancel_{event_type}')
>     165         self(f'after_{event_type}');  final()
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_epoch(self)
>     204 
>     205     def _do_epoch(self):
> --> 206         self._do_epoch_train()
>     207         self._do_epoch_validate()
>     208 
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_epoch_train(self)
>     196     def _do_epoch_train(self):
>     197         self.dl = self.dls.train
> --> 198         self._with_events(self.all_batches, 'train', CancelTrainException)
>     199 
>     200     def _do_epoch_validate(self, ds_idx=1, dl=None):
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
>     161 
>     162     def _with_events(self, f, event_type, ex, final=noop):
> --> 163         try: self(f'before_{event_type}');  f()
>     164         except ex: self(f'after_cancel_{event_type}')
>     165         self(f'after_{event_type}');  final()
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in all_batches(self)
>     167     def all_batches(self):
>     168         self.n_iter = len(self.dl)
> --> 169         for o in enumerate(self.dl): self.one_batch(*o)
>     170 
>     171     def _do_one_batch(self):
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in one_batch(self, i, b)
>     192         b = self._set_device(b)
>     193         self._split(b)
> --> 194         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
>     195 
>     196     def _do_epoch_train(self):
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
>     161 
>     162     def _with_events(self, f, event_type, ex, final=noop):
> --> 163         try: self(f'before_{event_type}');  f()
>     164         except ex: self(f'after_cancel_{event_type}')
>     165         self(f'after_{event_type}');  final()
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_one_batch(self)
>     170 
>     171     def _do_one_batch(self):
> --> 172         self.pred = self.model(*self.xb)
>     173         self('after_pred')
>     174         if len(self.yb):
> 
> ~/jupyter/env_jupyter/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
>    1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
>    1050                 or _global_forward_hooks or _global_forward_pre_hooks):
> -> 1051             return forward_call(*input, **kwargs)
>    1052         # Do not call functions when jit is used
>    1053         full_backward_hooks, non_full_backward_hooks = [], []
> 
> TypeError: forward() takes 2 positional arguments but 3 were given

now if i add n_inp=1 i get the following error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_4357/465355952.py in <module>
----> 1 learn.fit_one_cycle(10, slice(1e-5, 1e-4))

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
    111     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    112               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 113     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
    114 
    115 # Cell

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    219             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    220             self.n_epoch = n_epoch
--> 221             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    222 
    223     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161 
    162     def _with_events(self, f, event_type, ex, final=noop):
--> 163         try: self(f'before_{event_type}');  f()
    164         except ex: self(f'after_cancel_{event_type}')
    165         self(f'after_{event_type}');  final()

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_fit(self)
    210         for epoch in range(self.n_epoch):
    211             self.epoch=epoch
--> 212             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    213 
    214     def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161 
    162     def _with_events(self, f, event_type, ex, final=noop):
--> 163         try: self(f'before_{event_type}');  f()
    164         except ex: self(f'after_cancel_{event_type}')
    165         self(f'after_{event_type}');  final()

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_epoch(self)
    204 
    205     def _do_epoch(self):
--> 206         self._do_epoch_train()
    207         self._do_epoch_validate()
    208 

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_epoch_train(self)
    196     def _do_epoch_train(self):
    197         self.dl = self.dls.train
--> 198         self._with_events(self.all_batches, 'train', CancelTrainException)
    199 
    200     def _do_epoch_validate(self, ds_idx=1, dl=None):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161 
    162     def _with_events(self, f, event_type, ex, final=noop):
--> 163         try: self(f'before_{event_type}');  f()
    164         except ex: self(f'after_cancel_{event_type}')
    165         self(f'after_{event_type}');  final()

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in all_batches(self)
    167     def all_batches(self):
    168         self.n_iter = len(self.dl)
--> 169         for o in enumerate(self.dl): self.one_batch(*o)
    170 
    171     def _do_one_batch(self):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in one_batch(self, i, b)
    192         b = self._set_device(b)
    193         self._split(b)
--> 194         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    195 
    196     def _do_epoch_train(self):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161 
    162     def _with_events(self, f, event_type, ex, final=noop):
--> 163         try: self(f'before_{event_type}');  f()
    164         except ex: self(f'after_cancel_{event_type}')
    165         self(f'after_{event_type}');  final()

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_one_batch(self)
    173         self('after_pred')
    174         if len(self.yb):
--> 175             self.loss_grad = self.loss_func(self.pred, *self.yb)
    176             self.loss = self.loss_grad.clone()
    177         self('after_loss')

~/jupyter/env_jupyter/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

~/jupyter/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):

~/jupyter/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):

~/jupyter/Practical-Deep-Learning-for-Coders-2.0/Computer Vision/imports/metrics.py in _one_loss(self, clas_pred, bbox_pred, clas_tgt, bbox_tgt)
    131             bbox_pred = bbox_pred[bbox_mask]
    132             bbox_tgt = bbox_tgt[matches[bbox_mask]]
--> 133             bb_loss = self.reg_loss(bbox_pred, bbox_to_activ(bbox_tgt, self.anchors[bbox_mask]))
    134         else: bb_loss = 0.
    135         matches.add_(1)

~/jupyter/env_jupyter/lib/python3.7/site-packages/torch/nn/functional.py in smooth_l1_loss(input, target, size_average, reduce, reduction, beta)
   2983             reduce=reduce,
   2984             reduction=reduction,
-> 2985             beta=beta,
   2986         )
   2987     if not (target.size() == input.size()):

~/jupyter/env_jupyter/lib/python3.7/site-packages/torch/overrides.py in handle_torch_function(public_api, relevant_args, *args, **kwargs)
   1258     raise TypeError("no implementation found for '{}' on types that implement "
   1259                     '__torch_function__: {}'
-> 1260                     .format(func_name, [type(arg) for arg in overloaded_args]))
   1261 
   1262 has_torch_function = _add_docstr(

TypeError: no implementation found for 'torch.nn.functional.smooth_l1_loss' on types that implement __torch_function__: [<class 'fastai.torch_core.TensorImage'>, <class 'fastai.vision.core.TensorBBox'>]
1 Like

just to add some more information:
torch version
‘1.9.0+cu102’
running learn.summary()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/torch_core.py in to_concat(xs, dim)
    277     #   in this case we return a big list
--> 278     try:    return retain_type(torch.cat(xs, dim=dim), xs[0])
    279     except: return sum([L(retain_type(o_.index_select(dim, tensor(i)).squeeze(dim), xs[0])

TypeError: expected Tensor as element 0 in argument 0, but got int

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_1612/2554732337.py in <module>
----> 1 learn.summary()

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/callback/hook.py in summary(self)
    203     "Print a summary of the model, optimizer and loss function."
    204     xb = self.dls.train.one_batch()[:self.dls.train.n_inp]
--> 205     res = module_summary(self, *xb)
    206     res += f"Optimizer used: {self.opt_func}\nLoss function: {self.loss_func}\n\n"
    207     if self.opt is not None:

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/callback/hook.py in module_summary(learn, *xb)
    171     #  thus are not counted inside the summary
    172     #TODO: find a way to have them counted in param number somehow
--> 173     infos = layer_info(learn, *xb)
    174     n,bs = 76,find_bs(xb)
    175     inp_sz = _print_shapes(apply(lambda x:x.shape, xb), bs)

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/callback/hook.py in layer_info(learn, *xb)
    157         train_only_cbs = [cb for cb in learn.cbs if hasattr(cb, '_only_train_loop')]
    158         with learn.removed_cbs(train_only_cbs), learn.no_logging(), learn as l:
--> 159             r = l.get_preds(dl=[batch], inner=True, reorder=False)
    160         return h.stored
    161 

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in get_preds(self, ds_idx, dl, with_input, with_decoded, with_loss, act, inner, reorder, cbs, **kwargs)
    251         if with_loss: ctx_mgrs.append(self.loss_not_reduced())
    252         with ContextManagers(ctx_mgrs):
--> 253             self._do_epoch_validate(dl=dl)
    254             if act is None: act = getattr(self.loss_func, 'activation', noop)
    255             res = cb.all_tensors()

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_epoch_validate(self, ds_idx, dl)
    201         if dl is None: dl = self.dls[ds_idx]
    202         self.dl = dl
--> 203         with torch.no_grad(): self._with_events(self.all_batches, 'validate', CancelValidException)
    204 
    205     def _do_epoch(self):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    163         try: self(f'before_{event_type}');  f()
    164         except ex: self(f'after_cancel_{event_type}')
--> 165         self(f'after_{event_type}');  final()
    166 
    167     def all_batches(self):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in __call__(self, event_name)
    139 
    140     def ordered_cbs(self, event): return [cb for cb in self.cbs.sorted('order') if hasattr(cb, event)]
--> 141     def __call__(self, event_name): L(event_name).map(self._call_one)
    142 
    143     def _call_one(self, event_name):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastcore/foundation.py in map(self, f, gen, *args, **kwargs)
    152     def range(cls, a, b=None, step=None): return cls(range_of(a, b=b, step=step))
    153 
--> 154     def map(self, f, *args, gen=False, **kwargs): return self._new(map_ex(self, f, *args, gen=gen, **kwargs))
    155     def argwhere(self, f, negate=False, **kwargs): return self._new(argwhere(self, f, negate, **kwargs))
    156     def filter(self, f=noop, negate=False, gen=False, **kwargs):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastcore/basics.py in map_ex(iterable, f, gen, *args, **kwargs)
    664     res = map(g, iterable)
    665     if gen: return res
--> 666     return list(res)
    667 
    668 # Cell

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastcore/basics.py in __call__(self, *args, **kwargs)
    649             if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
    650         fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 651         return self.func(*fargs, **kwargs)
    652 
    653 # Cell

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _call_one(self, event_name)
    143     def _call_one(self, event_name):
    144         if not hasattr(event, event_name): raise Exception(f'missing {event_name}')
--> 145         for cb in self.cbs.sorted('order'): cb(event_name)
    146 
    147     def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/callback/core.py in __call__(self, event_name)
     43                (self.run_valid and not getattr(self, 'training', False)))
     44         res = None
---> 45         if self.run and _run: res = getattr(self, event_name, noop)()
     46         if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit
     47         return res

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/callback/core.py in after_validate(self)
    136         if not hasattr(self, 'preds'): return
    137         if self.with_input:     self.inputs  = detuplify(to_concat(self.inputs, dim=self.concat_dim))
--> 138         if not self.save_preds: self.preds   = detuplify(to_concat(self.preds, dim=self.concat_dim))
    139         if not self.save_targs: self.targets = detuplify(to_concat(self.targets, dim=self.concat_dim))
    140         if self.with_loss:      self.losses  = to_concat(self.losses)

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/torch_core.py in to_concat(xs, dim)
    272     "Concat the element in `xs` (recursively if they are tuples/lists of tensors)"
    273     if not xs: return xs
--> 274     if is_listy(xs[0]): return type(xs[0])([to_concat([x[i] for x in xs], dim=dim) for i in range_of(xs[0])])
    275     if isinstance(xs[0],dict):  return {k: to_concat([x[k] for x in xs], dim=dim) for k in xs[0].keys()}
    276     #We may receive xs that are not concatenable (inputs of a text classifier for instance),

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/torch_core.py in <listcomp>(.0)
    272     "Concat the element in `xs` (recursively if they are tuples/lists of tensors)"
    273     if not xs: return xs
--> 274     if is_listy(xs[0]): return type(xs[0])([to_concat([x[i] for x in xs], dim=dim) for i in range_of(xs[0])])
    275     if isinstance(xs[0],dict):  return {k: to_concat([x[k] for x in xs], dim=dim) for k in xs[0].keys()}
    276     #We may receive xs that are not concatenable (inputs of a text classifier for instance),

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/torch_core.py in to_concat(xs, dim)
    272     "Concat the element in `xs` (recursively if they are tuples/lists of tensors)"
    273     if not xs: return xs
--> 274     if is_listy(xs[0]): return type(xs[0])([to_concat([x[i] for x in xs], dim=dim) for i in range_of(xs[0])])
    275     if isinstance(xs[0],dict):  return {k: to_concat([x[k] for x in xs], dim=dim) for k in xs[0].keys()}
    276     #We may receive xs that are not concatenable (inputs of a text classifier for instance),

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/torch_core.py in <listcomp>(.0)
    272     "Concat the element in `xs` (recursively if they are tuples/lists of tensors)"
    273     if not xs: return xs
--> 274     if is_listy(xs[0]): return type(xs[0])([to_concat([x[i] for x in xs], dim=dim) for i in range_of(xs[0])])
    275     if isinstance(xs[0],dict):  return {k: to_concat([x[k] for x in xs], dim=dim) for k in xs[0].keys()}
    276     #We may receive xs that are not concatenable (inputs of a text classifier for instance),

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/torch_core.py in to_concat(xs, dim)
    272     "Concat the element in `xs` (recursively if they are tuples/lists of tensors)"
    273     if not xs: return xs
--> 274     if is_listy(xs[0]): return type(xs[0])([to_concat([x[i] for x in xs], dim=dim) for i in range_of(xs[0])])
    275     if isinstance(xs[0],dict):  return {k: to_concat([x[k] for x in xs], dim=dim) for k in xs[0].keys()}
    276     #We may receive xs that are not concatenable (inputs of a text classifier for instance),

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/torch_core.py in <listcomp>(.0)
    272     "Concat the element in `xs` (recursively if they are tuples/lists of tensors)"
    273     if not xs: return xs
--> 274     if is_listy(xs[0]): return type(xs[0])([to_concat([x[i] for x in xs], dim=dim) for i in range_of(xs[0])])
    275     if isinstance(xs[0],dict):  return {k: to_concat([x[k] for x in xs], dim=dim) for k in xs[0].keys()}
    276     #We may receive xs that are not concatenable (inputs of a text classifier for instance),

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/torch_core.py in to_concat(xs, dim)
    278     try:    return retain_type(torch.cat(xs, dim=dim), xs[0])
    279     except: return sum([L(retain_type(o_.index_select(dim, tensor(i)).squeeze(dim), xs[0])
--> 280                           for i in range_of(o_)) for o_ in xs], L())
    281 
    282 # Cell

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/torch_core.py in <listcomp>(.0)
    278     try:    return retain_type(torch.cat(xs, dim=dim), xs[0])
    279     except: return sum([L(retain_type(o_.index_select(dim, tensor(i)).squeeze(dim), xs[0])
--> 280                           for i in range_of(o_)) for o_ in xs], L())
    281 
    282 # Cell

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastcore/foundation.py in __call__(cls, x, *args, **kwargs)
     95     def __call__(cls, x=None, *args, **kwargs):
     96         if not args and not kwargs and x is not None and isinstance(x,cls): return x
---> 97         return super().__call__(x, *args, **kwargs)
     98 
     99 # Cell

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastcore/foundation.py in __init__(self, items, use_list, match, *rest)
    103     def __init__(self, items=None, *rest, use_list=False, match=None):
    104         if (use_list is not None) or not is_array(items):
--> 105             items = listify(items, *rest, use_list=use_list, match=match)
    106         super().__init__(items)
    107 

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastcore/basics.py in listify(o, use_list, match, *rest)
     54     elif isinstance(o, list): res = o
     55     elif isinstance(o, str) or is_array(o): res = [o]
---> 56     elif is_iter(o): res = list(o)
     57     else: res = [o]
     58     if match is not None:

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/torch_core.py in <genexpr>(.0)
    278     try:    return retain_type(torch.cat(xs, dim=dim), xs[0])
    279     except: return sum([L(retain_type(o_.index_select(dim, tensor(i)).squeeze(dim), xs[0])
--> 280                           for i in range_of(o_)) for o_ in xs], L())
    281 
    282 # Cell

AttributeError: 'int' object has no attribute 'index_select'
1 Like

now i downgraded torch to ‘1.8.1+cu111’

06_Object_Detection works fine

but mine now returns

RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_2899/465355952.py in <module>
----> 1 learn.fit_one_cycle(10, slice(1e-5, 1e-4))

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
    111     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    112               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 113     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
    114 
    115 # Cell

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    219             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    220             self.n_epoch = n_epoch
--> 221             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    222 
    223     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161 
    162     def _with_events(self, f, event_type, ex, final=noop):
--> 163         try: self(f'before_{event_type}');  f()
    164         except ex: self(f'after_cancel_{event_type}')
    165         self(f'after_{event_type}');  final()

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_fit(self)
    210         for epoch in range(self.n_epoch):
    211             self.epoch=epoch
--> 212             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    213 
    214     def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161 
    162     def _with_events(self, f, event_type, ex, final=noop):
--> 163         try: self(f'before_{event_type}');  f()
    164         except ex: self(f'after_cancel_{event_type}')
    165         self(f'after_{event_type}');  final()

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_epoch(self)
    204 
    205     def _do_epoch(self):
--> 206         self._do_epoch_train()
    207         self._do_epoch_validate()
    208 

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_epoch_train(self)
    196     def _do_epoch_train(self):
    197         self.dl = self.dls.train
--> 198         self._with_events(self.all_batches, 'train', CancelTrainException)
    199 
    200     def _do_epoch_validate(self, ds_idx=1, dl=None):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161 
    162     def _with_events(self, f, event_type, ex, final=noop):
--> 163         try: self(f'before_{event_type}');  f()
    164         except ex: self(f'after_cancel_{event_type}')
    165         self(f'after_{event_type}');  final()

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in all_batches(self)
    167     def all_batches(self):
    168         self.n_iter = len(self.dl)
--> 169         for o in enumerate(self.dl): self.one_batch(*o)
    170 
    171     def _do_one_batch(self):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in one_batch(self, i, b)
    192         b = self._set_device(b)
    193         self._split(b)
--> 194         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    195 
    196     def _do_epoch_train(self):

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    161 
    162     def _with_events(self, f, event_type, ex, final=noop):
--> 163         try: self(f'before_{event_type}');  f()
    164         except ex: self(f'after_cancel_{event_type}')
    165         self(f'after_{event_type}');  final()

~/jupyter/env_jupyter/lib/python3.7/site-packages/fastai/learner.py in _do_one_batch(self)
    173         self('after_pred')
    174         if len(self.yb):
--> 175             self.loss_grad = self.loss_func(self.pred, *self.yb)
    176             self.loss = self.loss_grad.clone()
    177         self('after_loss')

~/jupyter/env_jupyter/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

~/jupyter/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):

~/jupyter/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):

~/jupyter/Practical-Deep-Learning-for-Coders-2.0/Computer Vision/imports/metrics.py in _one_loss(self, clas_pred, bbox_pred, clas_tgt, bbox_tgt)
    125 
    126     def _one_loss(self, clas_pred, bbox_pred, clas_tgt, bbox_tgt):
--> 127         bbox_tgt, clas_tgt = self._unpad(bbox_tgt, clas_tgt)
    128         matches = match_anchors(self.anchors, bbox_tgt)
    129         bbox_mask = matches>=0

~/jupyter/Practical-Deep-Learning-for-Coders-2.0/Computer Vision/imports/metrics.py in _unpad(self, bbox_tgt, clas_tgt)
    112 
    113     def _unpad(self, bbox_tgt, clas_tgt):
--> 114         i = torch.min(torch.nonzero(clas_tgt-self.pad_idx))
    115         return tlbr2cthw(bbox_tgt[i:]), clas_tgt[i:]-1+self.pad_idx
    116 

RuntimeError: operation does not have an identity.

in the end this fixed all the issues

Fix_hacks