kuangliu / pytorch-retinanet

RetinaNet in PyTorch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IndexError: too many indices for tensor of dimension 1

sunshine-zkf opened this issue · comments

I use the function decode in the encoder.py, always have the fellowing question.

  • Traceback (most recent call last):
    File "eval.py", line 281, in
    test()
    File "eval.py", line 238, in test
    boxes, labels, scores = encoder.decode(loc_preds.data.squeeze(), cls_preds.data.squeeze(), (w, h))
    File "/home/sunshine_zkf/RetinaNet/RetinaNet/encoder.py", line 127, in decode
    keep = box_nms(boxes[ids], score[ids], threshold=NMS_THRESH)
    File "/home/sunshine_zkf/RetinaNet/RetinaNet/utils.py", line 168, in box_nms
    x1 = bboxes[:,0] # xmin
    IndexError: too many indices for tensor of dimension 1

where's the problem? I use the pytorch-0.4

When there is no bounding box detected, ids.nonzero.squeeze() automatically squeeze the tensor into nothing, which makes the ids with tensor.shape([])
try this in encoder.py, decode():
ids = score > CLS_THRESH
if ids.numel() <=1: ids = ids.nonzero().squeeze(0) else: ids = ids.nonzero().squeeze()

When there is no bounding box detected, ids.nonzero.squeeze() automatically squeeze the tensor into nothing, which makes the ids with tensor.shape([])
try this in encoder.py, decode():
ids = score > CLS_THRESH
if ids.numel() <=1: ids = ids.nonzero().squeeze(0) else: ids = ids.nonzero().squeeze()

Thank you very much! I will try it!

@gyfastas Thank you for comment.
ids = score > CLS_THRESH
if ids.numel() <=1: ids = ids.nonzero().squeeze(0) else: ids = ids.nonzero().squeeze()

I also revised code, but still occur error.
Could you give me another solution ?

@gyfastas Thank you for comment.
ids = score > CLS_THRESH
if ids.numel() <=1: ids = ids.nonzero().squeeze(0) else: ids = ids.nonzero().squeeze()

I also revised code, but still occur error.
Could you give me another solution ?

:) I found my solution did not work too. ids = score > CLS_THRESH is a booleen index [0,0,1,0....]. We can use ids.sum( ) to see whether there is 1 in ids (which means there is positive sample) . Try this:
ids = score > CLS_THRESH
if not ids.sum(): return torch.tensor([[0.,0.,0.,0.]]), torch.tensor([[0]])
else: do NMS

Also I recommend checking the dimension of these variables.

@gyfastas Thank you for comment !
I also handle this error like return torch.tensor([[0.,0.,0.,0.]]), torch.tensor([[0]]) .
Thank you :)