Sebastián
Sebastián

Reputation: 457

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

I am trying to use a pre-trained model. Here's where the problem occurs

Isn't the model supposed to take in a simple colored image? Why is it expecting a 4-dimensional input?

The eval() method is:

def eval(file):
    image = io.imread(file)
    plt.imshow(image)
    image = cv2.resize(image, (160,40)).transpose((2,1,0))
    output = model(torch.tensor(image[np.newaxis,...]).float())[0].squeeze().detach().numpy()
    return decode_prob(output)

And the output of eval('image.png') :

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-66-0b951c2596f8> in <module>()
----> 1 eval('/content/image.png')

5 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
    344                             _pair(0), self.dilation, self.groups)
    345         return F.conv2d(input, weight, self.bias, self.stride,
--> 346                         self.padding, self.dilation, self.groups)
    347 
    348     def forward(self, input):

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

Upvotes: 0

Views: 1922

Answers (1)

dumbPy
dumbPy

Reputation: 1518

The image you are loading is a 4 channel Image. You can read it with cv2 and convert it to 3 channel RGB image as

def eval(file):
    image = cv2.imread(file)
    image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGB)
    plt.imshow(image)
    image = cv2.resize(image, (160,40)).transpose((2,1,0))
    output = model(torch.tensor(image[np.newaxis,...]).float())[0].squeeze().detach().numpy()
    return decode_prob(output)

Upvotes: 2

Related Questions