RuntimeError: Given groups=1, weight[64, 3, 3, 3], so expected input[16, 64, 256, 256] to have 3 channels, but got 64 channels instead

yashkatariya: <p>Why am I getting this error?</p>
<p><strong>RuntimeError: Given groups=1, weight[64, 3, 3, 3], so expected input[16, 64, 256, 256] to have 3 channels, but got 64 channels instead</strong></p>
<p>I wrote an implementation of U-net.</p>
<pre><code class="lang-auto">class double_conv(nn.Module):
  def __init__(self, in_ch, out_ch):
    super(double_conv, self).__init__()
    self.conv1 = nn.Conv2d(in_ch, out_ch, 3, padding=1)
    self.conv2 = nn.Conv2d(in_ch, out_ch, 3, padding=1)
    
  def forward(self, x):
    x = F.relu(self.conv1(x))
    x = F.relu(self.conv2(x))
    return x
  
  
class input_conv(nn.Module):
  def __init__(self, in_ch, out_ch):
    super(input_conv, self).__init__()
    self.inp_conv = double_conv(in_ch, out_ch)
    
  def forward(self, x):
    x = self.inp_conv(x)
    return x
  

class up(nn.Module):
  def __init__(self, in_ch, out_ch):
    super(up, self).__init__()
    self.up_conv = nn.ConvTranspose2d(in_ch, out_ch, kernel_size=2, stride=2)
    self.conv = double_conv(in_ch, out_ch)
    
  def forward(self, x1, x2):
    x1 = self.up_conv(x1)
    x = torch.cat([x2, x1], dim=1)
    x = self.conv(x)
    return x
    
    
class down(nn.Module):
  def __init__(self, in_ch, out_ch):
    super(down, self).__init__()
    self.pool = nn.MaxPool2d(2)
    self.conv = double_conv(in_ch, out_ch)
    
  def forward(self, x):
    x = self.pool(x)
    x = self.conv(x)
    return x
  
class last_conv(nn.Module):
  def __init__(self, in_ch, out_ch):
    super(last_conv, self).__init__()
    self.conv1 = nn.Conv2d(in_ch, out_ch, 1)
    
  def forward(self, x):
    x = self.conv1(x)
    return x


class Unet(nn.Module):
  def __init__(self, channels, classes):
    super(Unet, self).__init__()
    self.inp = input_conv(channels, 64)
    self.down1 = down(64, 128)
    self.down2 = down(128, 256)
    self.down3 = down(256, 512)
    self.down4 = down(512, 1024)
    self.up1 = up(1024, 512)
    self.up2 = up(512, 256)
    self.up3 = up(256, 128)
    self.up4 = up(128, 64)
    self.out = last_conv(64, classes)
    
  def forward(self, x):
    x1 = self.inp(x)
    x2 = self.down1(x1)
    x3 = self.down2(x2)
    x4 = self.down3(x3)
    x5 = self.down4(x4)
    x = self.up1(x5, x4)
    x = self.up2(x, x3)
    x = self.up3(x, x2)
    x = self.up1(x, x1)
    x = self.out(x)
    return x


model = Unet(3, 1)
</code></pre>
<p><strong>This is the training loop</strong></p>
<pre><code class="lang-auto">for epoch in range(5):
    for i, data in enumerate(trainloader):
        inputs, labels = data
        inputs = Variable(inputs).cuda()
        labels = Variable(labels).cuda()
        
        # forward + backward + optimize
        
        # zeroes the gradient buffers of all parameters
        optimizer.zero_grad()
        #forward pass
        outputs = model_pytorch(inputs)
        # calculate the loss
        loss = loss_function(outputs, labels)
        # backpropagation
        loss.backward()
        # Does the update after calculating the gradients
        optimizer.step()
        
        if (i+1) % 5 == 0: # print every 100 mini-batches
            print('[%d, %5d] loss: %.4f' % (epoch, i+1, loss.data[0]))
</code></pre>

junyuseu: <p>It means your input should have 3 channels , but you give a 64 channels input. The input are organized in [N, C, W, H] format, your input, also data layer, should have 3 channels. You should check your code.</p>

yashkatariya: <p>My input does have 3 channels. The input to the Unet is 3, 1 which corresponds to 3 channels and 1 class.</p>

SimonW: <p>I believe in <a class="mention" href="/u/junyuseu">@junyuseu</a>’s answer input means input to conv layer, not the input to the actual net work. In fact, unless your in_ch always equals to out_ch, the double_conv module will definitely throw such error.</p>

junyuseu: <p><img src="https://discuss.pytorch.org/uploads/default/original/2X/4/41449d124ccfdc20f2b42a9ce1a656db8da0ee5b.png" alt="image" data-base62-sha1="9jo2rF7MnnWOrUSgMUId5FJsgHx" width="464" height="114"><br>
The error is here. In the double_conv class, the first conv layer’s input dim is in_ch(3), out dim is out_ch(64), the second conv layer the same, but this layer’s input dim is 64, not in_ch(3)</p>

yashkatariya: <p>Yup, made a silly mistake. Thanks for the help</p>

Silva_Rubanza: <p>i got the same kind of runtime error " Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 4, 224, 224] to have 3 channels, but got 4 channels instead "</p>
<p>Been trying to correct it for a while now but cant seem to see where the mistake is .<br>
This is my model arch.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/c/c1590fb0e64ef59920b583ebc73ae62a87d60f52.png" data-download-href="https://discuss.pytorch.org/uploads/default/c1590fb0e64ef59920b583ebc73ae62a87d60f52" title="27%20PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/c/c1590fb0e64ef59920b583ebc73ae62a87d60f52_2_690x194.png" alt="27%20PM" data-base62-sha1="rAqXOrPlXsyfKLRB72jS3Dspzou" width="690" height="194" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/c/c1590fb0e64ef59920b583ebc73ae62a87d60f52_2_690x194.png, https://discuss.pytorch.org/uploads/default/optimized/2X/c/c1590fb0e64ef59920b583ebc73ae62a87d60f52_2_1035x291.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/c/c1590fb0e64ef59920b583ebc73ae62a87d60f52_2_1380x388.png 2x" data-dominant-color="F2F2F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">27%20PM</span><span class="informations">2376×670 73.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
any help would be appreciated</p>

ptrblck: <p>Your input contains 4 channels, while the first conv layer expects an input with 3 channels.<br>
If you are dealing with RGB images, the 4th channel might be the alpha channel, which could just be removed.<br>
If you are using a custom <code>DataLoader</code>, you could probably just use:</p>
<pre><code class="lang-python">def __getitem__(self, index):
    img = Image.open(self.paths[index]).convert('RGB')
    ...
    # Alternatively remove the alpha channel from the tensor
    img = Image.open(self.paths[index])
    x = TF.to_tensor(img)
    x = x[:3]
    ...
</code></pre>

aksg87: <p>Thanks for your response! I had a different but related question.</p>
<p>If you are dealing with a 1-channel grayscale image but want to utilize a pretrained network (I am working off of this resnet18 based <a href="https://github.com/usuyama/pytorch-unet" rel="nofollow noopener">repo</a>…</p>
<p>Would you suggest duplicating the channel 3 times so it fits the model or is there a better approach?</p>

ptrblck: <p>I think this would be the easiest approach.<br>
Alternatively, you could also try to reduce the filter channels (mean, sum, ?) and see, if that might give a better performance (I haven’t compared these approaches yet).</p>

gss: <p>I am getting the same error. Any idea where that might be in my code. I don’t seem to figure out where the mistake is…</p>
<pre><code class="lang-auto">    class CNN(nn.Module):
        def __init__(self):
            super(CNN, self).__init__()

            self.conv1 = nn.Conv2d(4, 7, (3, 3))
            # self.convnorm1 = nn.BatchNorm2d(7)
            # self.pool1 = nn.MaxPool2d((2, 2))

            self.conv2 = nn.Conv2d(7, 14, (3, 3))
            self.convnorm2 = nn.BatchNorm2d(14)
            # self.pool2 = nn.MaxPool2d((2, 2))
            #
            self.conv3 = nn.Conv2d(14, 28, (3, 3))
            # self.convnorm3 = nn.BatchNorm2d(28)
            # self.pool3 = nn.MaxPool2d((2, 2))

            self.conv4 = nn.Conv2d(28, 56, (3, 3))
            self.convnorm4 = nn.BatchNorm2d(56)
            # self.pool4 = nn.MaxPool2d((2, 2))

            self.conv5= nn.Conv2d(56, 112, (3, 3))
            self.convnorm5 = nn.BatchNorm2d(112)
            # self.pool5 = nn.MaxPool2d((2, 2))

            self.conv6 = nn.Conv2d(112, 224, (2, 2))
            self.convnorm6 = nn.BatchNorm2d(224)
            # self.pool6 = nn.MaxPool2d((2, 2))
            #
            # self.conv7 = nn.Conv2d(224, 448, (2, 2))
            # self.convnorm7 = nn.BatchNorm2d(448)
            # self.pool7 = nn.MaxPool2d((2, 2))

            self.linear1 = nn.Linear(47096, 200)
            self.linear1_bn = nn.BatchNorm1d(200)
            self.drop = nn.Dropout(DROPOUT)
            self.linear2 = nn.Linear(200, 17)
            self.act = torch.relu

        def forward(self, x):
            # x = self.act(self.conv1(x))
            # x = self.pool1(self.convnorm1(self.act(self.conv1(x))))
            #x = self.pool1(self.act(self.conv1(x)))
            x = self.act(self.conv1(x))
            #x = self.pool2(self.convnorm2(self.act(self.conv2(x))))
            x = self.convnorm2(self.act(self.conv2(x)))
            x = self.act(self.conv3(x))
            # x = self.pool3(self.convnorm3(self.act(self.conv3(x))))
            # x = self.pool4(self.convnorm4(self.act(self.conv4(x))))
            x =self.convnorm4(self.act(self.conv4(x)))
            # x = self.pool5(self.convnorm5(self.act(self.conv5(x))))
            # x = self.pool6(self.convnorm6(self.act(self.conv6(x))))
            # x = self.pool7(self.convnorm7(self.act(self.conv7(x))))
            x = self.drop((self.linear1_bn(self.act(self.linear1(x.view(len(x), -1))))))
            return self.linear2(x)
</code></pre>

ptrblck: <p>What is your input shape?<br>
The code works fine using this snippet:</p>
<pre><code class="lang-python">model = CNN()
x = torch.randn(2, 4, 37, 37)
out = model(x)
</code></pre>

adarsh_pandey: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> sir please explain what is input shape …i forgot …and also  …i am having a similar error with my code …plz help me figure it out …<br>
here is the snippet::</p>
<h1>-<em>- coding: utf-8 -</em>-</h1>
<p>import cv2<br>
import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import os<br>
from PIL import Image<br>
import torch.optim as optim</p>
<p>from torch.autograd import Variable<br>
import numpy as np<br>
import json<br>
<span class="hashtag">#loading</span> the cascades</p>
<p>face_cascade =cv2.CascadeClassifier(“haarcascade_frontalface_default.xml”)<br>
eye_cascade =cv2.CascadeClassifier(“haarcascade_eye.xml”)<br>
<span class="hashtag">#definnig</span> a function that will do the detections<br>
def detect(gray,frame):<br>
faces=face_cascade.detectMultiScale(gray,1.3,5)<br>
for (x,y,w,h) in faces :<br>
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)<br>
roi_gray=gray[y:y+h,x:x+w]<br>
roi_color=frame[y:y+h,x:x+w]<br>
eyes=eye_cascade.detectMultiScale(roi_gray,1.1,3)<br>
for (ex,ey,ew,eh) in eyes :<br>
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)<br>
return frame<br>
<span class="hashtag">#applying</span> the detect function</p>
<p>class CNN(nn.Module):<br>
def <strong>init</strong>(self,num_inputs):<br>
super(CNN, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(num_inputs, 32, 3, stride=2, padding=1)<br>
self.conv2 = nn.Conv2d(32, 32, 3, stride=2, padding=1)<br>
self.conv3 = nn.Conv2d(32, 32, 3, stride=2, padding=1)<br>
self.conv4 = nn.Conv2d(32, 32, 3, stride=2, padding=1)<br>
self.fc1 = nn.Linear(in_features =32<em>3</em>3 , out_features = 40)<br>
self.fc2 = nn.Linear(in_features = 40, out_features = 2)<br>
def forward(self,x):<br>
x = F.relu(F.max_pool2d(self.conv1(x), 3, 2))<br>
x = F.relu(F.max_pool2d(self.conv2(x), 3, 2))<br>
x = F.relu(F.max_pool2d(self.conv3(x), 3, 2))<br>
x = x.view(x.size(0), -1)<br>
x = F.relu(self.fc1(x))<br>
x = self.fc2(x)<br>
return x<br>
<span class="hashtag">#train</span> your neural network<br>
cnn=CNN(3)<span class="hashtag">#specify</span> the number of input parameters</p>
<h1>Setting some hyperparameters</h1>
<p>batchSize = 4 # We set the size of the batch.<br>
imageSize = 64 # We set the size of the generated images (64x64).<br>
path=r"C:\Users\SHRIKANT\Music\Downloads\Simple_Linear_Regression\computer-vision\Module 3 - GANs"</p>
<h1>Creating the transformations</h1>
<p><span class="hashtag">#transform</span> = transforms.Compose([transforms.Scale(imageSize), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),]) # We create a list of transformations (scaling, tensor conversion, normalization) to apply to the input images.</p>
<p><span class="hashtag">#dataloader</span> = torch.utils.data.DataLoader(dataset, batch_size = batchSize, shuffle = True, num_workers = 2) # We use dataLoader to get the images of the training set batch by batch.<br>
os.chdir(path)<br>
loss = nn.MSELoss()</p>
<p>for epoch in range(25):<br>
<span class="hashtag">#train</span> to find adarsh pandey</p>
<pre><code>  for l in range(1,66):
      fak=Image.open(str(l)+".png").convert("RGB")
      img = Image.open(self.paths[index])
      x = TF.to_tensor(img)
      x = x[:3]
       #with open (str(l)+".png",'rb') as fak:  
      input=Variable(torch.from_numpy(np.array(fak,dtype='uint8'))).unsqueeze(0)
      #np.array( img, dtype='uint8' )
      output=cnn(input)
      optimizer = optim.Adam(cnn.parameters(), lr = 0.001)
      #what if optimizer.zero_grad()was here?? any effect .......plz check it 
      loss_error=loss(output,input)
      optimizer.zero_grad()
      
      loss_error.backward()
      optimizer.step()
  #training for identyfying of shubhi pandey
  
  
  #training ends here 
  probs=F.softmax(output)
  label=probs.multinomial()[0]
  print(label)
</code></pre>
<p>video_capture=cv2.VideoCapture(0)<br>
#f_name=100<br>
while True:</p>
<pre><code>_,frame=video_capture.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
canvas=detect(gray,frame)



#os.chdir(path)

#cv2.imwrite(str(f_name)+".png",frame)
cv2.imshow('Video',canvas)
#f_name=f_name+1
if cv2.waitKey(1) &amp; 0xFF ==ord('q'):
    break
</code></pre>
<p>video_capture.release()<br>
cv2.destroyAllWindows()</p>

ptrblck: <p>Input shape refers to the shape of your input tensor(s), which you are passing to your model.<br>
E.g. <code>nn.Conv2d</code> modules expect a 4-dimensional tensor with the shape <code>[batch_size, channels, height, width]</code>.</p>
<p>Could you remove unrelated code snippets (e.g. the opencv cascade etc.) and post a (minimal) code snippet to reproduce your error using random input data, please?</p>
<p>PS: You can add code snippets by wrapping them in three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

Aakash_Bhatia: <p>class LeNet(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(LeNet, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(1, 6, 5,bias=False)<br>
self.conv2 = nn.Conv2d(6, 16, 5)<br>
self.fc1 = nn.Linear(16 * 5 * 5, 120)<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc3 = nn.Linear(84, 10)</p>
<pre><code>def forward(self, x):
    out = self.conv1(x)
    out = F.relu(out)
    out = F.max_pool2d(out, 2)
    out = F.relu(self.conv2(out))
    out = F.max_pool2d(out, 2)
    out = out.view(out.size(0), -1)
    out = F.relu(self.fc1(out))
    out = F.relu(self.fc2(out))
    out = self.fc3(out)
    return out
</code></pre>
<p>model = LeNet().to(device=device)</p>
<p>if args.model:<br>
if os.path.isfile(args.model):<br>
print("=&gt; loading checkpoint ‘{}’".format(args.model))<br>
checkpoint = torch.load(args.model)<br>
args.start_epoch = checkpoint[‘epoch’]<br>
best_prec1 = checkpoint[‘best_prec1’]<br>
model.load_state_dict(checkpoint[‘state_dict’])<br>
print("=&gt; loaded checkpoint ‘{}’ (epoch {}) Prec1: {:f}".format(args.model, checkpoint[‘epoch’], best_prec1))<br>
else:<br>
print("=&gt; no checkpoint found at ‘{}’".format(args.resume))</p>
<p>print(‘Pre-processing Successful!’)</p>
<p>def test(model):<br>
kwargs = {‘num_workers’: 1, ‘pin_memory’: True} if args.cuda else {}<br>
test_loader = torch.utils.data.DataLoader(<br>
datasets.MNIST(’./data’, train=True, download=True,<br>
transform=transforms.Compose([<br>
transforms.Resize((32, 32)),<br>
transforms.ToTensor(),<br>
transforms.Normalize((0.1307,), (0.3081,))<br>
])),<br>
batch_size=args.test_batch_size, shuffle=True, **kwargs)</p>
<p>getting this error: RuntimeError: Given groups=1, weight of size 3 6 5 5, expected input[256, 3, 14, 14] to have 6 channels, but got 3 channels instead<br>
if i change bias = True then i am getting this error:<br>
RuntimeError: Given weight of size 3 1 5 5, expected bias to be 1-dimensional with 3 elements, but got bias of size [6] instead</p>

ptrblck: <p>Answered <a href="https://discuss.pytorch.org/t/runtimeerror-given-weight-of-size-3-1-5-5-expected-bias-to-be-1-dimensional-with-3-elements-but-got-bias-of-size-6-instead/65982/2">here</a>.</p>

Maria_Fernanda_De_La: <p>Hello, I have this dataset maker and I get a similar issue. I tried what you suggested but I get this error:</p>
<pre><code class="lang-auto"> return data[:3], target[:3], index
IndexError: invalid index to scalar variable.
</code></pre>
<p>Do you know why it is a scalar instead of a tensor?</p>
<pre><code class="lang-auto">class MyDataset(Dataset):
    def __init__(self,remove_list):
        self.cifar10 = datasets.CIFAR10(root='./data',

                                        download=False,
                                        train=True,
                                        transform=transform)
        self.data = self.cifar10.data
        # self.data = np.vstack(self.data).reshape(-1, 3, 32, 32)
        # self.data = self.data.transpose((0, 2, 3, 1))
        self.targets = self.cifar10.targets
        self.final_data, self.final_targets = self.__remove__(remove_list)
      
    def __getitem__(self, index):
        data, target = self.final_data[index], self.final_targets[index]
        
        return data[:3], target[:3], index

    def __len__(self):
        return len(self.final_data)

    def __remove__(self, remove_list):
        data = np.delete(self.data, remove_list, axis=0)        
        targets = np.delete(self.targets, remove_list, axis=0)
        
        return data, targets
</code></pre>

ptrblck: <p><code>target</code> could be a scalar, as you are calling <code>self.final_targets</code> with a single index.<br>
Also <code>data</code> should be a single sample and I’m not sure what the <code>[:3]</code> indexing is doing.</p>
<p>Could you explain your use case a bit and what you are trying to achieve?</p>

Anshuman_Senapati: <p>I’m getting a very similar error.</p>
<p>“Given groups=1, weight of size [10, 3, 7, 7], expected input[1, 64, 64, 3] to have 3 channels, but got 64 channels instead”</p>
<p>I understand the input should be [batch, channels, height, width] but my environment returns me arrays of size (64, 64, 3). How do I make it (3, 64, 64), input it into the nn.Conv2d and get around this error?</p>
<p>Please help. <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

ptrblck: <p>If you are loading the images with e.g. <code>OpenCV</code>, you would get an array in the shape <code>[H, W, C]</code> and would need to permute it to <code>[C, H, W]</code>.<br>
If you want to perform this permutation on the numpy array, you could use <code>np.transpose</code>, while <code>tensor.permute</code> should be used in PyTorch.</p>