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

Runtime error during model.predict() call #33

Closed
MrAhmadRaza opened this issue May 3, 2020 · 14 comments
Closed

Runtime error during model.predict() call #33

MrAhmadRaza opened this issue May 3, 2020 · 14 comments

Comments

@MrAhmadRaza
Copy link

Hi all,
I have trained a detecto model to detect rectangles and squares in image. After training, when I call model.predict(image), the following runtime error occurs. Any guidance to get rid of this. Thanks
I am using:
python 3.6
torch: 1.5.0+cu101

here is the call traceback

3 image = utils.read_image('/color_matching/sample_photo/img.jpg')
----> 4 predictions = model.predict_top([image])

/usr/local/lib/python3.6/dist-packages/torchvision/models/detection/_utils.py in decode(self, rel_codes, boxes)

183         box_sum += val
184         pred_boxes = self.decode_single(
-->185             rel_codes.reshape(box_sum, -1), concat_boxes)
186 
187         return pred_boxes.reshape(box_sum, -1, 4)

RuntimeError: cannot reshape tensor of 0 elements into shape [0, -1] because the unspecified dimension size -1 can be any value and is ambiguous.

    *:  The image has size(500, 500)
@alankbi
Copy link
Owner

alankbi commented May 4, 2020

Just to clarify, is your image 500x500 or 500x500x3? If it's the former, then that might be the issue, as Detecto expects images to be in RGB format. If it's the latter, perhaps try calling predict_top(image) rather than predict_top([image]) to see if that helps at all.

@MrAhmadRaza
Copy link
Author

Just to clarify, is your image 500x500 or 500x500x3? If it's the former, then that might be the issue, as Detecto expects images to be in RGB format. If it's the latter, perhaps try calling predict_top(image) rather than predict_top([image]) to see if that helps at all.

yes, sorry the size is 500x500x3 and using predict_top(image) still produce same error. Secondly, model.predict(image) also produce same error.

@alankbi
Copy link
Owner

alankbi commented May 4, 2020

Hm... I'm not too sure, as it seems to be some issue arising from torchvision code. Could you share what version of torchvision you're using?

@MrAhmadRaza
Copy link
Author

torchvision

torchvision version: 0.6.0+cu101

@alankbi
Copy link
Owner

alankbi commented May 5, 2020

I would recommend trying to use a default Model and/or another image to see if the same error occurs, which will allow you to isolate where the error is happening.

@jagilley
Copy link

I'm getting the same error - it also persists after I've tried all of the above things. Should be noted that I'm using Google Colab here, and would assume OP is also, judging by their use of Python 3.6

@alankbi
Copy link
Owner

alankbi commented May 11, 2020

Are you able to share a full stack trace and/or the code your running? This will help me better isolate where the error is occurring.

@jagilley
Copy link

jagilley commented May 11, 2020

Yep, for sure! One thing to note is that while the Detecto main module shows its path here as /content/drive/My Drive/dt2/jgdt/core.py (not what you'd expect if I was using a pip installation of Detecto), this is only because I'm working with a very slightly modified fork of Detecto that enables Tensorflow-style auto-saving of the model. I get this error with the standard Detecto installation as well, so that's definitely not the problem.

RuntimeError Traceback (most recent call last)
in ()
20 model = Model.load('oldtrain-3.pth', list_of_objects)
21 image = utils.read_image("my-image.png")
---> 22 labels, boxes, scores = model.predict_top([image])
23 print("boxes:", labels)
24 visualize.show_labeled_image(image, boxes, labels)

8 frames
/content/drive/My Drive/dt2/jgdt/core.py in predict_top(self, images)
367 """
368
--> 369 predictions = self.predict(images)
370
371 # If tuple but not list, then images is a single image

/content/drive/My Drive/dt2/jgdt/core.py in predict(self, images)
320 is_single_image = not _is_iterable(images)
321 images = [images] if is_single_image else images
--> 322 preds = self._get_raw_predictions(images)
323
324 results = []

/content/drive/My Drive/dt2/jgdt/core.py in _get_raw_predictions(self, images)
276 images = [img.to(self._device) for img in images]
277
--> 278 preds = self._model(images)
279 # Send predictions to CPU if not already
280 preds = [{k: v.to(torch.device('cpu')) for k, v in p.items()} for p in preds]

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
548 result = self._slow_forward(*input, **kwargs)
549 else:
--> 550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
552 hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torchvision/models/detection/generalized_rcnn.py in forward(self, images, targets)
69 features = OrderedDict([('0', features)])
70 proposals, proposal_losses = self.rpn(images, features, targets)
---> 71 detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets)
72 detections = self.transform.postprocess(detections, images.image_sizes, original_image_sizes)
73

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
548 result = self._slow_forward(*input, **kwargs)
549 else:
--> 550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
552 hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torchvision/models/detection/roi_heads.py in forward(self, features, proposals, image_shapes, targets)
767 }
768 else:
--> 769 boxes, scores, labels = self.postprocess_detections(class_logits, box_regression, proposals, image_shapes)
770 num_images = len(boxes)
771 for i in range(num_images):

/usr/local/lib/python3.6/dist-packages/torchvision/models/detection/roi_heads.py in postprocess_detections(self, class_logits, box_regression, proposals, image_shapes)
680
681 boxes_per_image = [boxes_in_image.shape[0] for boxes_in_image in proposals]
--> 682 pred_boxes = self.box_coder.decode(box_regression, proposals)
683
684 pred_scores = F.softmax(class_logits, -1)

/usr/local/lib/python3.6/dist-packages/torchvision/models/detection/_utils.py in decode(self, rel_codes, boxes)
183 box_sum += val
184 pred_boxes = self.decode_single(
--> 185 rel_codes.reshape(box_sum, -1), concat_boxes
186 )
187 return pred_boxes.reshape(box_sum, -1, 4)

RuntimeError: cannot reshape tensor of 0 elements into shape [0, -1] because the unspecified dimension size -1 can be any value and is ambiguous

@alankbi
Copy link
Owner

alankbi commented May 12, 2020

Here are some suggestions:

  1. Double-check that your PNG images have 3 channels rather than 4 (e.g. 500x500x3 rather than 500x500x4)
  2. Try with both torch==1.5.0 torchvision==0.6.0 and torch==1.4.0 torchvision==0.5.0
  3. Try loading your .pth file and running your inference code in a different virtual environment (another Colab file, on your computer, etc.)
  4. If you'd like, I can share with you a pre-trained model file to determine whether the issue is specific to your .pth file

If none of those work, I'd also recommend posting an issue in the PyTorch or torchvision repos; they may be able to help you more than I can as it seems the error is arising from within their package.

@jagilley
Copy link

None of these solutions work, unfortunately :( I just shared the model/dataset that's producing this error via Drive. Thanks for your help!

@Ares1510
Copy link

Hi! I am getting the same error as well. Trained my model on kaggle with the latest environment preference.

Any suggestions?

@alankbi
Copy link
Owner

alankbi commented Jun 16, 2020

Just want to check that none of the above solutions work? If so, could you take a look at #36 and see if that helps at all?

@anirudh-g
Copy link

I had the same issue with model.predict(). But once I switched to torch 1.4 and torchvision 0.5.0, it was alright. This seems to be limited to the recent releases of pytorch, atleast for me

@alankbi
Copy link
Owner

alankbi commented Oct 23, 2020

Closing due to inactivity - if anyone else comes across this issue, feel free to re-open this or create a new one

@alankbi alankbi closed this as completed Oct 23, 2020
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

5 participants