IndexError: index 1 is out of bounds for axis 0 with size 1

unsqueeze(0) won’t work in this case, as it seems your input has too many dimensions.
You would rather want to squeeze() a certain dimension, e.g. dim0 or dim1.
Also, your input is incompatible for conv layers, which expect your input to have the shape [batch_size, channels, height, width].
After squeezing, try using images_v = images_v.permute(0, 3, 1, 2).
If you’ll get an error stating your input is non-contiguous, call images_v = images_v.contiguous() in it.

Also, Variables are deprecated since PyTorch 0.4.0, so you can just use tensors in newer versions.

thank you , now the error ‘RuntimeError: number of dims don’t match in permute’
also Tensors from which library should be added ?

Did you squeeze the tensor before calling .permute?

The vanilla PyTorch tensor.

yes i did squeeze it like this
image_v = Variable(images_v[:, index, :, :].squeeze(1).float().cuda())

It the mentioned shape [1, 1, 512, 512, 3] referring to image_v or images_v[:, index, :, :]?
Try to make sure you are passing the input data to your model as [batch_size, channels, height, width].

RuntimeError: Given groups=1, weight of size [64, 1, 3, 3], expected input[1, 512, 512, 3] to have 1 channels, but got 512 channels instead

i think the order is wrong
[1, 1, 512, 512, 3] is refaring to images_v[:, index, :, :]
is there another code i can make the validation works ?

Then image_v = images_v[:, index, :, :].squeeze(0).permute(0, 3, 1, 2) will yield:

print(image_v.shape)
> torch.Size([1, 3, 512, 512])

i could not get the shape with index
but the shpae without the index is torch.Size([512, 512, 3])

I’m not sure I understand the shapes correctly, as I thought

print(images_v[:, index, :, :].shape)
> torch.Size([1, 1, 512, 512, 3])

as you’ve explained before?
Could you then print the shape of images_v without indexing?

without indexing the shape is " torch.Size([512, 512, 3])"

This can’t be the case, since this would yield an IndexError, if you are trying to index a 3-dimensional tensor using 4 indices:

x = torch.randn(512, 512, 3)
x[:, 0, :, :]
> IndexError: too many indices for tensor of dimension 3

now the shape is torch.Size([4, 3, 512, 512]) with out indexing !!!
and RuntimeError: Given groups=1, weight of size [64, 1, 3, 3], expected input[4, 3, 512, 512] to have 1 channels, but got 3 channels instead
and my input is on gray scale with one channel

Does the shape change for some iterations, i.e. do you sometimes see a shape of [4, 3, 512, 512] while other times just [512, 512, 3]?

It seems your first conv uses in_channels=1, which is fine for grayscale images.
Do you manually create/transform your images to have 3 channels?
PIL and OpenCV should load them as single-channel grayscale images.

i changed to gray scale using cv2.COLOR_BGR2GRAY
no the shape is just torch.Size([4, 3, 512, 512])
by running
image_v = images_v.squeeze(0).permute(0, 3, 1, 2)

cv2.COLOR_BGR2GRAY should completely remove the color channel as far as I know, so somehow there are still 3 channels being created. You might want to check your Dataset and the shapes of the image after loading.

you are right , the problem is with costum validation data set
it gives the shape as flowwing
for images torch.Size([1, 4, 512, 512, 3])
for mask torch.Size([1, 4, 512, 512])

what does the 3 represent in here?

@ptrblck and should the augmentation and preprocessing be done to the validation as well or just to the training dataset ?

@ptrblck
i am having this error
ValueError: operands could not be broadcast together with shapes (1,584,565) (512,512)
the original mask is with the size (1,584,565 ) how can i change the size , i tried the cv2 but it did not work

I’m not sure as I’m not familiar with your code, but it could represent the number of channels.

Preprocessing should be done in all datasets (using the same parameters as for the training).
Data augmentation is usually only done during training, although you could also use test time augmentation, average your predictions, and hope for a slight performance boost.

What have you tried so far? Doesn’t cv2.resize() work?