Index out of bounds in learn.show_results()

Hi,

I try to train a model. After the training, I try to get a sample of generated images, but I get an index out of bounds error (see below).

This is what I run in the notebook:

learn.show_results(rows=5, thresh=0.5, nms_overlap=0.1)

IndexError                                Traceback (most recent call last)
<ipython-input-81-0dd45fac659f> in <module>()
----> 1 learn.show_results(rows=2, thresh=0.5, nms_overlap=0.1)

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in show_results(self, ds_type, rows, **kwargs)
    403         if has_arg(ds.y.reconstruct, 'x'):
    404             ys = [ds.y.reconstruct(grab_idx(y, i), x=x) for i,x in enumerate(xs)]
--> 405             zs = [ds.y.reconstruct(z, x=x) for z,x in zip(preds,xs)]
    406         else :
    407             ys = [ds.y.reconstruct(grab_idx(y, i)) for i in range(n_items)]

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in <listcomp>(.0)
    403         if has_arg(ds.y.reconstruct, 'x'):
    404             ys = [ds.y.reconstruct(grab_idx(y, i), x=x) for i,x in enumerate(xs)]
--> 405             zs = [ds.y.reconstruct(z, x=x) for z,x in zip(preds,xs)]
    406         else :
    407             ys = [ds.y.reconstruct(grab_idx(y, i)) for i in range(n_items)]

/usr/local/lib/python3.6/dist-packages/fastai/vision/data.py in reconstruct(self, t, x)
    361         i = (labels - self.pad_idx).nonzero().min()
    362         bboxes,labels = bboxes[i:],labels[i:]
--> 363         return ImageBBox.create(*x.size, bboxes, labels=labels, classes=self.classes, scale=False)
    364 
    365 class ObjectItemList(ImageList):

/usr/local/lib/python3.6/dist-packages/fastai/vision/image.py in create(cls, h, w, bboxes, labels, classes, pad_idx, scale)
    352         if isinstance(bboxes, np.ndarray) and bboxes.dtype == np.object: bboxes = np.array([bb for bb in bboxes])
    353         bboxes = tensor(bboxes).float()
--> 354         tr_corners = torch.cat([bboxes[:,0][:,None], bboxes[:,3][:,None]], 1)
    355         bl_corners = bboxes[:,1:3].flip(1)
    356         bboxes = torch.cat([bboxes[:,:2], tr_corners, bl_corners, bboxes[:,2:]], 1)

IndexError: index 3 is out of bounds for dimension 1 with size 2

learn.show_results won’t work with object detection unless your model returns exactly the same kind of output as the target you feed it. Without seeing your whole code, it’s hard to see where the problem is.

This is my code in collab: here
The last section has a test code. I’m trying to create my own show_results but I don’t know how do it exactly.

Like I said, you should use a different model in evaluation to return list of tensors like your targets if you want show_results to work. Right now, everything happens in the loss function (and you also added nms for evaluation). This all needs to be run before show_results can work.