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

TypeError: len() of a 0-d tensor #5

Open
jamiechoi1995 opened this issue Mar 14, 2020 · 2 comments
Open

TypeError: len() of a 0-d tensor #5

jamiechoi1995 opened this issue Mar 14, 2020 · 2 comments

Comments

@jamiechoi1995
Copy link

Traceback (most recent call last):
File "generate_tsv.py", line 476, in
generate_tsv(args.outfile, image_ids, args)
File "generate_tsv.py", line 459, in generate_tsv
writer.writerow(get_detections_from_im(fasterRCNN, classes, im_file, image_id, args))
File "generate_tsv.py", line 347, in get_detections_from_im
if len(keep_boxes) < MIN_BOXES:
File "anaconda2/envs/py3.6pytorch1.0/lib/python3.6/site-packages/torch/tensor.
py", line 411, in len
raise TypeError("len() of a 0-d tensor")
TypeError: len() of a 0-d tensor

when keep_boxes only have 1 value that is >0,
torch.squeeze(torch.nonzero(keep_boxes)) will generate a tensor with dim() = 0

Code to reproduce:
keep_boxes=torch.tensor([0,1,0,0])
len(torch.squeeze(torch.nonzero(keep_boxes)))

@Kilichbek
Copy link

In my case, this error was caused when the model could not detect any object in the image.
I solved this by:
Skipping that "bad" image during feature extraction or changing the confidence threshold for detection.

@mzshuo
Copy link

mzshuo commented May 20, 2021

Traceback (most recent call last):
File "generate_tsv.py", line 476, in
generate_tsv(args.outfile, image_ids, args)
File "generate_tsv.py", line 459, in generate_tsv
writer.writerow(get_detections_from_im(fasterRCNN, classes, im_file, image_id, args))
File "generate_tsv.py", line 347, in get_detections_from_im
if len(keep_boxes) < MIN_BOXES:
File "anaconda2/envs/py3.6pytorch1.0/lib/python3.6/site-packages/torch/tensor.
py", line 411, in len
raise TypeError("len() of a 0-d tensor")
TypeError: len() of a 0-d tensor

when keep_boxes only have 1 value that is >0,
torch.squeeze(torch.nonzero(keep_boxes)) will generate a tensor with dim() = 0

Code to reproduce:
keep_boxes=torch.tensor([0,1,0,0])
len(torch.squeeze(torch.nonzero(keep_boxes)))

That perfectly works. Thanks so much.

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