Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError: too many indices for tensor of dimension 1 #65

Open
sunshine-zkf opened this issue Jul 5, 2019 · 5 comments
Open

IndexError: too many indices for tensor of dimension 1 #65

sunshine-zkf opened this issue Jul 5, 2019 · 5 comments

Comments

@sunshine-zkf
Copy link

sunshine-zkf commented Jul 5, 2019

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

@gyfastas
Copy link

gyfastas commented Jul 12, 2019

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()

@sunshine-zkf
Copy link
Author

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!

@KyuminHwang
Copy link

KyuminHwang commented Aug 21, 2019

@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
Copy link

@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.

@KyuminHwang
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants