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

username: Sam3
cooked: <p>Hi,</p>
<p>i have this error in my code, how can i solve this error.<br>
my code:</p>
<p>torch.manual_seed(1)</p>
<h1>Hyper Parameters</h1>
<p>EPOCH = 1<br>
BATCH_SIZE = 500<br>
LR = 0.001</p>
<p>path=’/Users/lionardo/Desktop/data/train’<br>
<span class="hashtag">#traindir</span> = os.path.join(path, ‘train’)</p>
<p>train_dataset =  torchvision.datasets.ImageFolder(root=path,transform=torchvision.transforms.ToTensor())</p>
<p><span class="hashtag">#Load</span> our dataset<br>
<span class="hashtag">#define</span> testdata<br>
n = len(train_dataset)# total number of examples</p>
<p>train_data_set = transforms.ToPILImage(train_dataset)<br>
#train_dataset=torchvision.transforms.functional.to_tensor(train_dataset)</p>
<p>n_test = int(0.1 * n)  # take ~10% for test<br>
test_x = torch.utils.data.Subset(train_dataset, range(n_test))  # take first 10%<br>
<span class="hashtag">#torch</span>.unsqueeze(test_x, dim=1).type(torch.FloatTensor)[:200]/255.</p>
<p><span class="hashtag">#Make</span> the dataset iterable</p>
<p>train_loader = Data.DataLoader(dataset=train_dataset, batch_size=BATCH_SIZE, shuffle=True)</p>
<p><span class="hashtag">#define</span> the network<br>
class CNN(nn.Module):<br>
def  <strong>init</strong>(self):<br>
super(CNN, self).<strong>init</strong>()<br>
<span class="hashtag">#mri</span> lf layer 1<br>
self.conv1 = nn.Sequential(  # input shape (1, 28, 28)<br>
nn.Conv2d(<br>
in_channels=1,      # input height<br>
out_channels=16,    # n_filters<br>
kernel_size=5, # filter size<br>
stride=1,           # filter movement/step<br>
padding=2           #<br>
))</p>
<pre><code>    self.batch1 = nn.BatchNorm2d(16),
           #output shape (16, 28, 28)
    self.relu1 =  nn.LeakyReLU(0.2, True)    #activation
    
    #mri hf layer 1 
    self.conv2 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 16,
            out_channels = 32, 
            kernel_size  = 5,
            stride       = 1,
            padding      = 2
         ))
    
    self.batch2 = nn.BatchNorm2d(32),      #output shape (1,28,28)
    self.relu2 = nn.LeakyReLU(0.2,True)
    
    self.conv3 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 32,
            out_channels = 64, 
            kernel_size  = 5,
            stride       = 1,
            padding      = 2
             ))
    self.batch3 = nn.BatchNorm2d(64),      #output shape (1,28,28)
    self.relu3 = nn.LeakyReLU(0.2,True)
    
    self.conv4 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 64,
            out_channels = 32, 
            kernel_size  = 5,
            stride       = 1,
            padding      = 2
             ))
    
    self.batch4 = nn.BatchNorm2d(32),      #output shape (1,28,28)
    self.relu4 = nn.LeakyReLU(0.2,True)
    
    self.conv5 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 32,
            out_channels = 16, 
            kernel_size  = 5,
            stride       = 1,
            padding      = 2
        ))
    
    self.batch5 = nn.BatchNorm2d(16),      #output shape (1,28,28)
    self.relu5 = nn.LeakyReLU(0.2,True)
    
    self.conv6 = nn.Sequential(      # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 16,
            out_channels = 1, 
            kernel_size  = 5,
            stride       = 1,
            padding      = 2
             ))
    
    self.batch6 = nn.BatchNorm2d(1),      #output shape (1,28,28)
    self.relu6 = nn.LeakyReLU(0.2,True)
    

def forward(self, x,y):
   
    
    #x1 = x.reshape(-1,1,28,28) #careful: need to change by yourself
    x1= self.conv1(x)
    x1 = self.batch1(x)
    x1 = slef.relu1(x)
     #x = x.reshape(-1,1,28,28) #careful: need to change by yourself
    x1 = self.conv2(x)
    x1 = self.batch2(x)
    x1 = slef.relu2(x)
    x1 = self.conv3(x)
    x1 = self.batch3(x)
    x1 = slef.relu3(x)
    x1 = self.conv4(x)
    x1 = self.batch4(x)
    x1 = slef.relu4(x)
    x1 = self.conv5(x)
    x1 = self.batch5(x)
    x1 = slef.relu5(x)
    x1 = self.conv6(x)
    x1 = self.batch6(x)
    x1 = slef.relu6(x)
    return x1
    #x2 = y.reshape(-1,1,28,28) #careful: need to change by yourself
    x2= self.conv1(y)
    x2 = self.batch1(y)
    x2 = slef.relu1(y)
    # x = x.reshape(-1,1,28,28) #careful: need to change by yourself
    x2 = self.conv2(y)
    x2 = self.batch2(y)
    x2 = slef.relu2(y)
    x2 = self.conv3(y)
    x2 = self.batch3(y)
    x2 = slef.relu3(y)
    x2 = self.conv4(y)
    x2 = self.batch4(y)
    x2 = slef.relu4(y)
    x2 = self.conv5(y)
    x2 = self.batch5(y)
    x2 = slef.relu5(y)
    x2 = self.conv6(y)
    x2 = self.batch6(y)
    x2 = slef.relu6(y)
    
    return x2
    x = torch.cat((x1, x2), 1) # fusion x1 , x2
    x = torch.tanh(x)
    x = x.reshape(-1)
   # x = x.reshape(-1,1,28,28)
                               
    return x
    print(x.size())
    #execute the network
</code></pre>
<p>cnn = CNN()<br>
print(cnn)</p>
<p><span class="hashtag">#define</span> the optimizers and loss functions<br>
optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)   # optimize all cnn parameters<br>
loss_func = SSIM()  # the target label is not one-hotted</p>
<h1>training</h1>
<p>for epoch in range(EPOCH):<br>
for step, (b_x , b_y) in enumerate(train_loader):</p>
<pre><code>    #b_x = b_x.reshape(-1)
   # b_y= b_y.reshape(-1)
    output = cnn(b_x,b_y)               # cnn output
    loss = loss_func(output, b_x)+ loss_func(output,b_y)#+ cross entropy loss
    optimizer.zero_grad()           # clear gradients for this training step
    loss.backward()                 # backpropagation, compute gradients
    optimizer.step()</code></pre>

username: ptrblck
cooked: <p>Your first conv layer is expecting an input with a single channel, while you are passing an input with 3 channels.<br>
You could change the <code>in_channels</code> of this conv layer to 3 or pass a grayscale (single channel) image to the model.</p>