Getting Error too many indices for tensor of dimension 3
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 :
&gt; trace = torch.jit.trace(model, input_batch) line Providing the error
&gt; &quot;/usr/local/lib/python3.7/dist-packages/torch/tensor.py:467:
&gt; RuntimeWarning: Iterating over a tensor might cause the trace to be
&gt; incorrect. Passing a tensor of different shape won't change the number
&gt; of iterations executed (and might lead to errors or silently give
&gt; incorrect results).   'incorrect results).', category=RuntimeWarning)
&gt; --------------------------------------------------------------------------- IndexError                                Traceback (most recent call
&gt; last) &lt;ipython-input-25-52ff7ef794de&gt; in &lt;module&gt;()
&gt;       1 #First attempt at tracing
&gt; ----&gt; 2 trace = torch.jit.trace(model, input_batch)
&gt; 
&gt; 7 frames
&gt; /usr/local/lib/python3.7/dist-packages/detectron2/modeling/meta_arch/rcnn.py
&gt; in &lt;listcomp&gt;(.0)
&gt;     182         Normalize, pad and batch the input images.
&gt;     183         &quot;&quot;&quot;
&gt; --&gt; 184         images = [x[&quot;image&quot;].to(self.device) for x in batched_inputs]
&gt;     185         images = [(x - self.pixel_mean) / self.pixel_std for x in images]
&gt;     186         images = ImageList.from_tensors(images, self.backbone.size_divisibility)
&gt; 
&gt; IndexError: too many indices for tensor of dimension 3

model = build_model(cfg)
model.eval()
# print(model)
input_image = Image.open(&quot;model/xxx.jpg&quot;)
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)


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.
