Building a 3D Unet

username: jueqi_wang
cooked: <p>I am building a 3D Unet and here is my model:</p>
<pre><code class="lang-auto">UNet(
  (encoder): Encoder(
    (encoding_blocks): ModuleList(
      (0): EncodingBlock(
        (conv1): ConvolutionalBlock(
          (conv_layer): Conv3d(1, 8, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
          (activation_layer): PReLU(num_parameters=1)
          (block): Sequential(
            (0): Conv3d(1, 8, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
            (1): PReLU(num_parameters=1)
          )
        )
        (conv2): ConvolutionalBlock(
          (conv_layer): Conv3d(8, 8, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
          (activation_layer): PReLU(num_parameters=1)
          (dropout_layer): Dropout3d(p=0.3, inplace=False)
          (block): Sequential(
            (0): Conv3d(8, 8, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
            (1): PReLU(num_parameters=1)
            (2): Dropout3d(p=0.3, inplace=False)
          )
        )
        (downsample): MaxPool3d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
      )
      (1): EncodingBlock(
        (conv1): ConvolutionalBlock(
          (conv_layer): Conv3d(8, 16, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
          (activation_layer): PReLU(num_parameters=1)
          (block): Sequential(
            (0): Conv3d(8, 16, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
            (1): PReLU(num_parameters=1)
          )
        )
        (conv2): ConvolutionalBlock(
          (conv_layer): Conv3d(16, 16, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
          (activation_layer): PReLU(num_parameters=1)
          (dropout_layer): Dropout3d(p=0.3, inplace=False)
          (block): Sequential(
            (0): Conv3d(16, 16, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
            (1): PReLU(num_parameters=1)
            (2): Dropout3d(p=0.3, inplace=False)
          )
        )
        (downsample): MaxPool3d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
      )
      (2): EncodingBlock(
        (conv1): ConvolutionalBlock(
          (conv_layer): Conv3d(16, 32, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
          (activation_layer): PReLU(num_parameters=1)
          (block): Sequential(
            (0): Conv3d(16, 32, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
            (1): PReLU(num_parameters=1)
          )
        )
        (conv2): ConvolutionalBlock(
          (conv_layer): Conv3d(32, 32, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
          (activation_layer): PReLU(num_parameters=1)
          (dropout_layer): Dropout3d(p=0.3, inplace=False)
          (block): Sequential(
            (0): Conv3d(32, 32, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
            (1): PReLU(num_parameters=1)
            (2): Dropout3d(p=0.3, inplace=False)
          )
        )
        (downsample): MaxPool3d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
      )
    )
  )
  (decoder): Decoder(
    (decoding_blocks): ModuleList(
      (0): DecodingBlock(
        (upsample): Upsample(scale_factor=2.0, mode=trilinear)
        (conv1): ConvolutionalBlock(
          (conv_layer): Conv3d(64, 32, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
          (activation_layer): PReLU(num_parameters=1)
          (dropout_layer): Dropout3d(p=0.3, inplace=False)
          (block): Sequential(
            (0): Conv3d(64, 32, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
            (1): PReLU(num_parameters=1)
            (2): Dropout3d(p=0.3, inplace=False)
          )
        )
      )
      (1): DecodingBlock(
        (upsample): Upsample(scale_factor=2.0, mode=trilinear)
        (conv1): ConvolutionalBlock(
          (conv_layer): Conv3d(32, 16, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
          (activation_layer): PReLU(num_parameters=1)
          (dropout_layer): Dropout3d(p=0.3, inplace=False)
          (block): Sequential(
            (0): Conv3d(32, 16, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
            (1): PReLU(num_parameters=1)
            (2): Dropout3d(p=0.3, inplace=False)
          )
        )
      )
      (2): DecodingBlock(
        (upsample): Upsample(scale_factor=2.0, mode=trilinear)
        (conv1): ConvolutionalBlock(
          (conv_layer): Conv3d(16, 8, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
          (activation_layer): PReLU(num_parameters=1)
          (dropout_layer): Dropout3d(p=0.3, inplace=False)
          (block): Sequential(
            (0): Conv3d(16, 8, kernel_size=(5, 5, 5), stride=(1, 1, 1), padding=(2, 2, 2))
            (1): PReLU(num_parameters=1)
            (2): Dropout3d(p=0.3, inplace=False)
          )
        )
      )
    )
  )
  (classifier): ConvolutionalBlock(
    (conv_layer): Conv3d(8, 2, kernel_size=(1, 1, 1), stride=(1, 1, 1))
    (dropout_layer): Dropout3d(p=0.3, inplace=False)
    (block): Sequential(
      (0): Conv3d(8, 2, kernel_size=(1, 1, 1), stride=(1, 1, 1))
      (1): Dropout3d(p=0.3, inplace=False)
    )
  )
)
</code></pre>
<p>However I got this error:</p>
<pre><code class="lang-auto">RuntimeError: Given groups=1, weight of size [16, 32, 5, 5, 5], expected input[1, 48, 120, 120, 120] to have 32 channels, but got 48 channels instead
</code></pre>
<p>Can someone tell me where I am wrong? Thanks a lot!</p>

username: ptrblck
cooked: <p>It seems <code>model.decoder[1].conv1.conv_layer</code> gets a wrong input tensor, which is expected to have 32 channels, but has 48.<br>
I would recommend to print the shapes of all tensors in your <code>forward</code> method, which would make debugging easier.<br>
I guess you might have concatenated two activations with 32 and 16 channels accidentally.</p>

username: jueqi_wang
cooked: <p>Yes, you are right. I have found it. Thanks!</p>