title: RuntimeError: Given groups=1, weight of size [64, 3, 3, 3], expected input[64, 1, 32, 32] to have 3 channels, but got 1 channels instead
body: <p>I am working on SVHN dataset, and I got this error, only during training phase. During the instantiation of the model, it works.</p>
  <pre><code>RuntimeError: Given groups=1, weight of size [64, 3, 3, 3], expected input[64, 1, 32, 32] to have 3 channels, but got 1 channels instead
  </code></pre>
  <p>To be sure of having 3 channels, I wrote that Grayscale(3) transformation.</p>
  <pre><code># Compose transformations
  data_transform = transforms.Compose([
    transforms.Resize((32,32)),
    transforms.Grayscale(num_output_channels=3),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
  ])
  
  # Compose transformations
  test_transform = transforms.Compose([
    transforms.Resize((32,32)),
    transforms.Grayscale(num_output_channels=3),
    transforms.ToTensor(),
  ])
  
  # Load MNIST dataset with transforms
  train_set_svhn = torchvision.datasets.SVHN(root=base_dir, split='train', download=True, transform=data_transform, target_transform=None)
  test_set_svhn = torchvision.datasets.SVHN(root=base_dir, split='test', download=True, transform=test_transform)
  </code></pre>
  <pre><code>class VGG16(nn.Module):
  
      def __init__(self, num_classes):
          super(VGG16, self).__init__()
  
          # calculate same padding:
          # (w - k + 2*p)/s + 1 = o
          # =&gt; p = (s(o-1) - w + k)/2
  
          self.block_1 = nn.Sequential(
              nn.Conv2d(in_channels=3,
                        out_channels=64,
                        kernel_size=(3, 3),
                        stride=(1, 1),
                        # (1(32-1)- 32 + 3)/2 = 1
                        padding=1),
              nn.BatchNorm2d(64),
              nn.ReLU(),
              nn.Conv2d(in_channels=64,
                        out_channels=64,
                        kernel_size=(3, 3),
                        stride=(1, 1),
                        padding=1),
              nn.BatchNorm2d(64),
              nn.ReLU(),
              nn.MaxPool2d(kernel_size=(2, 2),
                           stride=(2, 2))
          )
  
          self.block_2 = nn.Sequential(
              nn.Conv2d(in_channels=64,
                        out_channels=128,
                        kernel_size=(3, 3),
                        stride=(1, 1),
                        padding=1),
              nn.BatchNorm2d(128),
              nn.ReLU(),
              nn.Conv2d(in_channels=128,
                        out_channels=128,
                        kernel_size=(3, 3),
                        stride=(1, 1),
                        padding=1),
              nn.BatchNorm2d(128),
              nn.ReLU(),
              nn.MaxPool2d(kernel_size=(2, 2),
                           stride=(2, 2))
          )
          
          self.block_3 = nn.Sequential(
              nn.Conv2d(in_channels=128,
                        out_channels=256,
                        kernel_size=(3, 3),
                        stride=(1, 1),
                        padding=1),
              nn.BatchNorm2d(256),
              nn.ReLU(),
              nn.Conv2d(in_channels=256,
                        out_channels=256,
                        kernel_size=(3, 3),
                        stride=(1, 1),
                        padding=1),
              nn.BatchNorm2d(256),
              nn.ReLU(),
              nn.Conv2d(in_channels=256,
                        out_channels=256,
                        kernel_size=(3, 3),
                        stride=(1, 1),
                        padding=1),
              nn.BatchNorm2d(256),
              nn.ReLU(),
              nn.MaxPool2d(kernel_size=(2, 2),
                           stride=(2, 2))
          )
             
  
          self.classifier = nn.Sequential(
              nn.Linear(4096, 4096),
              nn.ReLU(True),
              nn.Dropout(p=0.25),
              nn.Linear(4096, 4096),
              nn.ReLU(True),
              nn.Dropout(p=0.25),
              nn.Linear(4096, num_classes),
          )
  
          for m in self.modules():
              if isinstance(m, torch.nn.Conv2d) or isinstance(m, torch.nn.Linear):
                  nn.init.kaiming_uniform_(m.weight, mode='fan_in', nonlinearity='leaky_relu')
  #                 nn.init.xavier_normal_(m.weight)
                  if m.bias is not None:
                      m.bias.detach().zero_()
  
          # self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
  
      def forward(self, x):
  
          x = self.block_1(x)
          x = self.block_2(x)
          x = self.block_3(x)
          # x = self.avgpool(x)
          x = x.view(x.size(0), -1)
          logits = self.classifier(x)
          probas = F.softmax(logits, dim=1)
          # probas = nn.Softmax(logits)
          return probas
          # return logits
  </code></pre>
  <p>I have no idea where that 1 comes from</p>
  <p>Moreover, this is the shape of the output of the model without classifier (fully connected layers):</p>
  <pre><code>output = model1(test_x)
  output.shape
  </code></pre>
  <pre><code>torch.Size([1, 256, 4, 4])
  </code></pre>
  <p>And indeed I will pass 256x4x4 as input to the first FC.</p>
  <p>Edit:
  <a href="https://i.stack.imgur.com/QKNbX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QKNbX.png" alt="enter image description here" /></a></p>