相关文章推荐
想发财的茴香  ·  wpf checkbox ...·  1 年前    · 
幸福的草稿本  ·  SQLServer ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

I am trying to run following code but getting an error:

import torch.nn as nn
import torch.nn.functional as F
class EmbeddingNet(nn.Module):
    def __init__(self):
        super(EmbeddingNet, self).__init__()
        self.convnet = nn.Sequential(nn.Conv2d(1, 32, 5), nn.PReLU(),
                                     nn.MaxPool2d(2, stride=2),
                                     nn.Conv2d(32, 64, 5), nn.PReLU(),
                                     nn.MaxPool2d(2, stride=2))
        self.fc = nn.Sequential(nn.Linear(64 * 4 * 4, 256),
                                nn.PReLU(),
                                nn.Linear(256, 256),
                                nn.PReLU(),
                                nn.Linear(256, 2)
    def forward(self, x):
        output = self.convnet(x)
        output = output.view(output.size()[0], -1)
        output = self.fc(output)
        return output
    def get_embedding(self, x):
        return self.forward(x)
class EmbeddingNetL2(EmbeddingNet):
    def __init__(self):
        super(EmbeddingNetL2, self).__init__()
    def forward(self, x):
        output = super(EmbeddingNetL2, self).forward(x)
        output /= output.pow(2).sum(1, keepdim=True).sqrt()
        return output
    def get_embedding(self, x):
        return self.forward(x)'''enter code here
                Please don't significantly change the question after it has been answered. Since you already asked a new question, I have reverted your edit for this one.
– GoodDeeds
                May 2, 2021 at 23:49
                Okay. I am newbie to StackOverflow. Can you please answer the new error if possible. Why I am getting that error.
– Beta
                May 2, 2021 at 23:52

Error is very simple .Its saying instead of 1 channel you have given 3 channel images.

one change would be in this block

class EmbeddingNet(nn.Module):
  def __init__(self):
    super(EmbeddingNet, self).__init__()
    self.convnet = nn.Sequential(nn.Conv2d(3, 32, 5),  #instead of 1 i have made it 3
                                 nn.PReLU(),
                                 nn.MaxPool2d(2, stride=2),
                                 nn.Conv2d(32, 64, 5), nn.PReLU(),
                                 nn.MaxPool2d(2, stride=2))
    self.fc = nn.Sequential(nn.Linear(64 * 4 * 4, 256),
                            nn.PReLU(),
                            nn.Linear(256, 256),
                            nn.PReLU(),
                            nn.Linear(256, 2)

EDIT to next error:
change to this

self.fc = nn.Sequential(nn.Linear(64 * 61 * 61, 256), #here is the change
                    nn.PReLU(),
                    nn.Linear(256, 256),
                    nn.PReLU(),
                    nn.Linear(256, 2)
                Thanks. It solved the error, my kernel wasn't initialized so didn't pick the updated file.  But I got another error.  RuntimeError: mat1 dim 1 must match mat2 dim 0
– Beta
                May 2, 2021 at 20:58
                Can u paste the full error message @sharad and also input size of your image in dataloader.of possible post your full code
– Prajot Kuvalekar
                May 2, 2021 at 21:37
                Hello @Prajot I have added scereenshot above. I have added print statement and the output are as below: torch.Size([256, 3, 256, 256]) torch.Size([256, 64, 61, 61]) torch.Size([256, 238144])
– Beta
                May 2, 2021 at 21:45
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.