RuntimeError: Given transposed=1, weight of size [64, 1, 4], expected input[2, 128, 74] to have 64 channels, but got 128 channels instead -> unet error

heyme: <p>Hi! I’m trying to implement very very simple UNET from this code.</p>
<pre><code class="lang-python">class unet(nn.Module):
    def __init__(self, ngf=64, norm_layer=nn.BatchNorm1d):
        super(unet, self).__init__()

        # construct unet structure
        unet_block = skipconnection_block(ngf*2, ngf, submodule=None, norm_layer=norm_layer, inner=True)
        unet_block = skipconnection_block(ngf, 1, submodule=unet_block, norm_layer=norm_layer, outer=True)
        self.model = unet_block

    def forward(self, x):
        self.unet = nn.Sequential(self.model)
        x = self.unet(x)
        return x


class skipconnection_block(nn.Module):
    def __init__(self, inner_nc, outer_nc, submodule=None, outer=False, inner=False, norm_layer=nn.BatchNorm1d):
        super(skipconnection_block, self).__init__()
        self.outer = outer

        downrelu = nn.LeakyReLU(0.2, True)
        uprelu = nn.ReLU(True)

        if inner:
            downconv_0 = nn.Conv1d(in_channels=outer_nc, out_channels=inner_nc, kernel_size=4, stride=2, padding=0)
            upconv_0 = nn.ConvTranspose1d(in_channels=inner_nc, out_channels=outer_nc, kernel_size=4, stride=2, padding=0)
            down = [downrelu, downconv_0]
            up = [uprelu, upconv_0, norm_layer(outer_nc)]
            model = down + up

        elif outer:
            downconv_1 = nn.Conv1d(in_channels=outer_nc, out_channels=inner_nc, kernel_size=4, stride=2, padding=0)
            upconv_1 = nn.ConvTranspose1d(in_channels=inner_nc, out_channels=outer_nc, kernel_size=4, stride=2, padding=0)
            down = [downrelu, downconv_1, norm_layer(inner_nc)]
            up = [uprelu, upconv_1, norm_layer(outer_nc)]
            model = down + [submodule] + up

        self.model = nn.Sequential(*model)

    def forward(self, x):
        if self.outer:
            return self.model(x)
        else:
            return torch.cat([x, self.model(x)], 1)
</code></pre>
<p>and when i try like this,</p>
<pre><code class="lang-python">def load_skip_model():
    unet_ = unet()
    return unet_

if __name__=='__main__':
    unet = load_skip_model()

    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    unet.to(device)
    print(torchsummary.summary(unet, (1, 150)))
</code></pre>
<p>i got this result below.</p>
<pre><code class="lang-python">Traceback (most recent call last):
  File "model.py", line 47, in &lt;module&gt;
    print(torchsummary.summary(unet, (1, 150)))
  File "/usr/local/lib/python3.8/site-packages/torchsummary/torchsummary.py", line 72, in summary
    model(*x)
  File "/usr/local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/AutoEncoder_conv1d/networks_test.py", line 41, in forward
    x = self.unet(x)
  File "/usr/local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/torch/nn/modules/container.py", line 117, in forward
    input = module(input)
  File "/usr/local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/AutoEncoder_conv1d/networks_test.py", line 90, in forward
    return self.model(x)
  File "/usr/local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/torch/nn/modules/container.py", line 117, in forward
    input = module(input)
  File "/usr/local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 761, in forward
    return F.conv_transpose1d(
RuntimeError: Given transposed=1, weight of size [64, 1, 4], expected input[2, 128, 74] to have 64 channels, but got 128 channels instead
</code></pre>
<p>I dont understand why i got this. can anyone please give some help…?? thank uu</p>

ptrblck: <p>The error is raised in a transposed conv layer as the number of expected input channels doesn’t match the number of channels in the input activation.</p>
<p>Your current model architecture looks a bit “interleaved”:</p>
<pre><code class="lang-python">unet(
  (model): skipconnection_block(
    (model): Sequential(
      (0): LeakyReLU(negative_slope=0.2, inplace=True)
      (1): Conv1d(1, 64, kernel_size=(4,), stride=(2,))
      (2): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (3): skipconnection_block(
        (model): Sequential(
          (0): LeakyReLU(negative_slope=0.2, inplace=True)
          (1): Conv1d(64, 128, kernel_size=(4,), stride=(2,))
          (2): ReLU(inplace=True)
          (3): ConvTranspose1d(128, 64, kernel_size=(4,), stride=(2,))
          (4): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (4): ReLU(inplace=True)
      (5): ConvTranspose1d(64, 1, kernel_size=(4,), stride=(2,))
      (6): BatchNorm1d(1, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
  )
  (unet): Sequential(
    (0): skipconnection_block(
      (model): Sequential(
        (0): LeakyReLU(negative_slope=0.2, inplace=True)
        (1): Conv1d(1, 64, kernel_size=(4,), stride=(2,))
        (2): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (3): skipconnection_block(
          (model): Sequential(
            (0): LeakyReLU(negative_slope=0.2, inplace=True)
            (1): Conv1d(64, 128, kernel_size=(4,), stride=(2,))
            (2): ReLU(inplace=True)
            (3): ConvTranspose1d(128, 64, kernel_size=(4,), stride=(2,))
            (4): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          )
        )
        (4): ReLU(inplace=True)
        (5): ConvTranspose1d(64, 1, kernel_size=(4,), stride=(2,))
        (6): BatchNorm1d(1, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
  )
)
</code></pre>
<p>As you are creating the <code>self.model</code> module in the <code>forward</code> and are also reusing the previously defined <code>self.model = unet_block</code>.<br>
Add print statements your class and check the shapes of the output activations as well as the general execution logic.</p>