RuntimeError: Expected 4-dimensional input for 4-dimensional weight [16, 1, 9, 9], but got 5-dimensional input of size [1, 50, 1, 256, 256] instead

username: Sam3
cooked: <p>Hi ,<br>
i have this error in my fusion cnn . two input images should be fused in cnn, images size is (256*256).<br>
If someone have an idee , please help.</p>
<p>Thanks.</p>
<p>My cnn and my training code:</p>
<p><span class="hashtag">#define</span> the network<br>
class FunFuseAn(nn.Module):<br>
def  <strong>init</strong>(self):<br>
super(FunFuseAn, self).<strong>init</strong>()<br>
####<span class="hashtag">#mri</span> lf layer 1#####<br>
self.mri_lf = nn.Sequential( <span class="hashtag">#input</span> shape (,1,256,256)<br>
nn.Conv2d(in_channels=1, out_channels=16, kernel_size=9, stride=1, padding=4),<br>
nn.BatchNorm2d(16),<br>
nn.LeakyReLU(0.2,inplace=True)) <span class="hashtag">#output</span> shape (,16,256,256)<br>
####<span class="hashtag">#mri</span> hf layers#####<br>
self.mri_hf = nn.Sequential(  <span class="hashtag">#input</span> shape (,1,256,256)<br>
nn.Conv2d(in_channels = 1, out_channels = 16, kernel_size  = 3, stride= 1, padding = 1),<br>
nn.BatchNorm2d(16),<br>
nn.LeakyReLU(0.2,inplace=True),<br>
nn.Conv2d(in_channels  = 16, out_channels = 32, kernel_size = 3, stride = 1, padding = 1),<br>
nn.BatchNorm2d(32),<br>
nn.LeakyReLU(0.2,inplace=True),<br>
nn.Conv2d(in_channels  = 32, out_channels = 64, kernel_size  = 3, stride = 1, padding = 1),<br>
nn.BatchNorm2d(64),<br>
nn.LeakyReLU(0.2,inplace=True)) <span class="hashtag">#output</span> shape (,64,256,256)<br>
####<span class="hashtag">#pet</span> lf layer 1#####<br>
self.pet_lf = nn.Sequential( <span class="hashtag">#input</span> shape (,1,256,256)<br>
nn.Conv2d(in_channels=1, out_channels=16, kernel_size=7, stride=1, padding=3),<br>
nn.BatchNorm2d(16),<br>
nn.LeakyReLU(0.2,inplace=True)) <span class="hashtag">#output</span> shape (,16,256,256)<br>
####<span class="hashtag">#pet</span> hf layers#####<br>
self.pet_hf = nn.Sequential(  <span class="hashtag">#input</span> shape (,1,256,256)<br>
nn.Conv2d(in_channels = 1, out_channels = 16, kernel_size  = 5, stride= 1, padding = 2),<br>
nn.BatchNorm2d(16),<br>
nn.LeakyReLU(0.2,inplace=True),<br>
nn.Conv2d(in_channels  = 16, out_channels = 32, kernel_size = 5, stride = 1, padding = 2),<br>
nn.BatchNorm2d(32),<br>
nn.LeakyReLU(0.2,inplace=True),<br>
nn.Conv2d(in_channels  = 32, out_channels = 64, kernel_size  = 3, stride = 1, padding = 1),<br>
nn.BatchNorm2d(64),<br>
nn.LeakyReLU(0.2,inplace=True)) <span class="hashtag">#output</span> shape (,64,256,256)<br>
####<span class="hashtag">#reconstruction</span> layer 1#####<br>
self.recon1 = nn.Sequential(  <span class="hashtag">#input</span> shape (, 64, 256, 256)<br>
nn.Conv2d(in_channels  = 64,  out_channels = 32, kernel_size  = 5, stride = 1, padding = 2),<br>
nn.BatchNorm2d(32),<br>
nn.LeakyReLU(0.2,inplace=True),<br>
nn.Conv2d(in_channels  = 32, out_channels = 16, kernel_size  = 5, stride = 1, padding = 2),<br>
nn.BatchNorm2d(16),<br>
nn.LeakyReLU(0.2,inplace=True)) <span class="hashtag">#output</span> shape (,16, 256, 256)<br>
####<span class="hashtag">#reconstruction</span> layer 2#####<br>
self.recon2 = nn.Sequential(      <span class="hashtag">#input</span> shape (,16, 256, 256)<br>
nn.Conv2d(in_channels  = 16, out_channels = 1, kernel_size  = 5, stride = 1, padding = 2))   <span class="hashtag">#output</span> shape (,1,256,256)<br>
def forward(self, x, y):<br>
<span class="hashtag">#mri</span> lf layer 1<br>
<span class="hashtag">#x1</span> = x.view(x.size(0), -1)</p>
<pre><code>    x1 = self.mri_lf(x)
    #mri hf layers
   # x2 = x.view(x.size(0), -1) 
    #x2 = x.reshape(-1,1,256,256)
    x2 = self.mri_hf(x)
    #pet lf layer 1
    #y1 = x.view(x.size(0), -1) 
  
    y1 = self.pet_lf(y)
    #pet hf layers
    #y2 = x.view(x.size(0), -1) 
    #y2 = x.reshape(-1,1,256,256)
    y2 = self.pet_hf(y)
    #high frequency fusion layer
    fuse_hf = x2 + y2
    #reconstruction layer1
    recon_hf = self.recon1(fuse_hf)
    #low frequency fusion layer
    fuse_lf = (x1 + y1 + recon_hf)/3
    #reconstruction layer2
    recon3 = self.recon2(fuse_lf)
    #tanh layer
    fused = torch.tanh(recon3)  
    fused = fused.reshape(-1)
    return fused
    #execute the network
</code></pre>
<p>x = torch.randn(14, 1, 256, 256)<br>
y = torch.randn(14, 1,256,256)</p>
<p>cnn = FunFuseAn()<br>
cnn = cnn.float()<br>
output = cnn(x, y)<br>
print(cnn)</p>
<p><span class="hashtag">#mage_length</span>, image_width, gray_channels, batch_size, epoch, lr, images_pet, images_mri = init_param()</p>
<p><span class="hashtag">#import</span> train dataset</p>
<p><span class="hashtag">#define</span> the network</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):</p>
<pre><code>    #data.sequeeze(dim=1)
for step, (b_x,b_y ) in enumerate(train_data_loader):
   
    b_x = torch.randn(14, 1,256 , 256)
    b_y = torch.randn( 14,1,256 , 256)
    b_x = b_x.unsqueeze(0)
    b_y = b_y.unsqueeze(0)
   
    output = cnn(b_x,b_y)          
    loss = loss_func(output.squeeze(),b_y)+loss_func(output.squeeze(), b_x)
       
    # 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()                # apply gradients</code></pre>

username: achyut-srivastava
cooked: <p>I would suggest to print the size of your input tensor to your model to check and then reshape it if needed.</p>