IndexError: index 1 is out of bounds for dimension 1 with size 1


I am having this issue in the training when extracting the prediction:

output = model.forward(image)

The shape of the image tensor is: torch.Size([1, 1, 299, 299])

Batch size: 1
Image size: 299, 299

The model is the inception v3

Anyone has any idea why this is happening?

Thank you in advance

Hi. When I try to reproduce the behavior you’re describing:

m = torchvision.models.inception_v3(init_weights=False)  # this is for the speedup 
img = torch.randn(1,1,299,299) # dummy image
  1. If use image with torch.Size([1, 1, 299, 299]), I got this error:
    RuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[1, 1, 299, 299] to have 3 channels, but got 1 channels instead
    The model doesn’t work with 1 channel images
  2. If you want to predict on the batch size=1, you should put the model in evaluation mode.
  3. To make it work with grayscale image you can try to just copy 1 channel into 2 additional, so to have the correct number of channels and see if it provides meaningful results to you.

This post on how to deal with grayscale image may be of help to you.

I actually had forgotten to uncomment the line that changed the first layer of the inception to receive grayscale images ! Thank you for the help!

1 Like