0

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!

rezvan.ra
  • 31
  • 4
  • 1
    Could you give some more information about the network you are using, how are you loading the images, etc. – nsidn98 Aug 15 '22 at 17:21
  • 1
    Can you post the code snippet that is resulting in this error? – Hamster Hooey Aug 15 '22 at 17:29
  • first i run a super resolution algorithm with its dataset and that was ok. the link of the code is in the following : https://github.com/sanghyun-son/EDSR-PyTorch. (What this code does is that it receives an image with two modes of high and low resolution and improves the quality of the image with low resolution image and finally compares the improved image with the image with high resolution to check the quality of the improvement. does So, the input images are two high and low resolution images from the same photo.) After that i tried to used my PNG medical dataset to test but got error – rezvan.ra Aug 15 '22 at 17:29
  • Excuse me, is it possible, please check the answers section? THANKS – rezvan.ra Aug 15 '22 at 17:45
  • I would guess you are not reading the images in the same way when you actually run the network. – hkchengrex Aug 16 '22 at 00:27
  • My guess would be that the processing introduces an alpha (transparency) channel in the PNG image. You can just remove that channel, e.g. `img[:,:,:,:3]` – Robbie Aug 31 '22 at 23:39

1 Answers1

1

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.

Amna Najib
  • 49
  • 6