-1

I am trying to Read an Image using GeneralizedRCNN, Input shape is given as a comment with code. The problem is I am getting an error while tracing the model with input shape. The error is :

> trace = torch.jit.trace(model, input_batch) line Providing the error
> "/usr/local/lib/python3.7/dist-packages/torch/tensor.py:467:
> RuntimeWarning: Iterating over a tensor might cause the trace to be
> incorrect. Passing a tensor of different shape won't change the number
> of iterations executed (and might lead to errors or silently give
> incorrect results).   'incorrect results).', category=RuntimeWarning)
> --------------------------------------------------------------------------- IndexError                                Traceback (most recent call
> last) <ipython-input-25-52ff7ef794de> in <module>()
>       1 #First attempt at tracing
> ----> 2 trace = torch.jit.trace(model, input_batch)
> 
> 7 frames
> /usr/local/lib/python3.7/dist-packages/detectron2/modeling/meta_arch/rcnn.py
> in <listcomp>(.0)
>     182         Normalize, pad and batch the input images.
>     183         """
> --> 184         images = [x["image"].to(self.device) for x in batched_inputs]
>     185         images = [(x - self.pixel_mean) / self.pixel_std for x in images]
>     186         images = ImageList.from_tensors(images, self.backbone.size_divisibility)
> 
> IndexError: too many indices for tensor of dimension 3
model = build_model(cfg)
model.eval()
# print(model)
input_image = Image.open("model/xxx.jpg")
display(input_image)
to_tensor = transforms.ToTensor()
input_tensor = to_tensor(input_image)
# input_tensor.size = torch.Size([3, 519, 1038])
input_batch = input_tensor.unsqueeze(0)
# input_batch.size = torch.Size([1, 3, 519, 1038])
trace = torch.jit.trace(model, input_batch)
Ivan
  • 34,531
  • 8
  • 55
  • 100
Honey Kumar
  • 1
  • 1
  • 2
  • Welcome to Stack Overflow, please be mindful when asking for help. You haven't said a word, nor given a clear description of the situation. Your post doesn't even contain a question! Do you really think someone will be able or want to help? You should start by giving the full error traceback as well as your model definition and the input shapes. – Ivan Aug 28 '21 at 19:23

2 Answers2

0

This error occurred because input_batch.size = torch.Size([1, 3, 519, 1038]) has 4 dimensions and trace = torch.jit.trace(model, input_batch) expected to get a 3 dimensions as input.

you don't need input_batch = input_tensor.unsqueeze(0). delete or comment this line.

Raha Moosavi
  • 527
  • 4
  • 19
  • 2
    Without unsqueeze I am getting torch.Size([3, 519, 1038]) shape for image_tensor, but while trace its again giving error too many indices for tensor of dimension 2. – Honey Kumar Sep 03 '21 at 03:25
0

By default ..

The torch.jit.trace function cannot be used directly. However, it does provide a wrapper called that the model can take a tensor or a tuple of tensors as input. You can find a way to use it because of them.

The code for tracing the Mask RCNN model looks like this:

import torch
import torchvision
from detectron2.export.flatten import TracingAdapter
def inference_func(model, image):
    inputs= [{"image": image}]
    return model.inference(inputs, do_postprocess=False)[0]
print("cfg.MODEL.WEIGHTS: ",cfg.MODEL.WEIGHTS)   ## RETURNS : cfg.MODEL.WEIGHTS:  drive/Detectron2/model_final.pth
model= build_model(cfg)
example= torch.rand(1, 3, 224, 224)
wrapper= TracingAdapter(model, example, inference_func)
wrapper.eval()
traced_script_module= torch.jit.trace(wrapper, (example,))
traced_script_module.save("drive/Detectron2/model-final.pt")