rezvan.ra
rezvan.ra

Reputation: 11

RuntimeError: expected input to have 3 channels, but got 4 channels instead

my medical PNG images for test have 3 channels as given below :

import cv2
from google.colab.patches import cv2_imshow
img= cv2.imread("a.png")
print('Image Dimensions :', img.shape)
img= cv2.imread("ax2.png")
print('Image Dimensions :', img.shape)

---------------------> results : <--------------------------------

Image Dimensions : (625, 698, 3)
Image Dimensions : (426, 535, 3)

As it is known, my test images have 3 channels, but I got an error as follows, which says that the images have 4 channels

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

What is the problem and how can I fix it?

thanks!

Upvotes: 0

Views: 3467

Answers (1)

Amna Najib
Amna Najib

Reputation: 49

weight of size [3, 3, 1, 1] means that your conv2D has an input channel size 3 (second entry of the list).

As a hint: weigth size is [out_channels, in_channels, kernel, kernel] \newline

input[1, 4, 268, 300] means that your input has channel size of 4. It should, however, be 3.

As a hint: input size is [N, in_channels, H_in, W_in]

Now, you should consider what the shape of the input fed to the network is. It might be that you forgot to change the shape in the format mentioned before (cv2 has a different channel order [H, W, in_channels]), that you concatenated inputs wrongly or similar. So, checking the input shape should definitely help you here.

Upvotes: 1

Related Questions