Size input image in CNN

Julien_Guegan: <p>Hello,</p>
<p>I have a trained CNN for segmentation with a certain input image size and now I want to use it and predict some output for test image. As you might know for segmentation, it is not required to use the same image size and so I wanted to do some experiment and try different size (I have some other problematic to justify this). But the thing is when doing</p>
<blockquote>
<p>proba  = model.forward(new_image)</p>
</blockquote>
<p>I get the following error :</p>
<p><strong>RuntimeError: Sizes of tensors must match except in dimension 1. Got 64 and 63 in dimension 2 (The offending index is 1)</strong></p>
<p>And it kind of appear that my input size can’t be other that <strong>a multiple of 32</strong> … I wanted to know if someone has some info about this topic, is it normal ? Does it depend from my network architecture (from what operation) ? Or does it depend from my hardware architecture (I am on CPU now) ?</p>
<p>Thank you</p>

Henry_Chibueze: <p>U can see this as matrix multiplication.<br>
Assuming u have a matrix called input that is being multiplied by a matrix called weight to give another called output such that<br>
input x weight = output.</p>
<p>If input shape = (1, 5)<br>
and output shape = (1, 10)<br>
It means that the shape of ur weights = (5, 10)</p>
<p>Now that the network has been trained on such inputs and outputs, the weights will have a fixed shape. This means that any input that is not of shape (1, 5) will be incompatible with the weight and will hence throw an error.</p>
<p>So yes it is normal to get these kinda errors if u try to run inference on a differently sized image with a network that was trained on a specific image size.</p>
<p>So what I suggest is to create and algorithms the scales down the input the the required size.</p>
<p>I really Hope this was helpful.<img src="https://discuss.pytorch.org/images/emoji/apple/kissing.png?v=9" title=":kissing:" class="emoji" alt=":kissing:"></p>

Julien_Guegan: <p>No, for segmentation task it works for different size because you are doing only convolution operation</p>

Henry_Chibueze: <p>What’s the shape of ur testing input ?<br>
Like: (N, C, H, W)</p>
<p>Have u checked if maybe u transposed the input matrix wrongly too?</p>
<p><img src="https://discuss.pytorch.org/images/emoji/apple/thinking.png?v=9" title=":thinking:" class="emoji only-emoji" alt=":thinking:"></p>

Julien_Guegan: <p>As I said, I can confirm that I don’t have any error if the input image size is a multiple of 32 … The error appears only for specific input image size … So it can’t be a problem of transposed matrix</p>

Henry_Chibueze: <p>Ok fair point<br>
Can I see the code of ur network conv layers</p>

Julien_Guegan: <p>OK …  Here it is :</p>
<pre><code class="lang-auto">
&gt; Unet(
&gt;   (encoder): MobileNetV2Encoder(
&gt;     (features): Sequential(
&gt;       (0): ConvBNReLU(
&gt;         (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
&gt;         (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         (2): ReLU6(inplace=True)
&gt;       )
&gt;       (1): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=32, bias=False)
&gt;             (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): Conv2d(32, 16, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (2): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (2): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(16, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(96, 96, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=96, bias=False)
&gt;             (1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(96, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (3): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(24, 144, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(144, 144, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=144, bias=False)
&gt;             (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(144, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (4): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(24, 144, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(144, 144, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=144, bias=False)
&gt;             (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(144, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (5): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(32, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=192, bias=False)
&gt;             (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(192, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (6): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(32, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=192, bias=False)
&gt;             (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(192, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (7): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(32, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(192, 192, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=192, bias=False)
&gt;             (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(192, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (8): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(64, 384, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(384, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=384, bias=False)
&gt;             (1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(384, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (9): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(64, 384, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(384, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=384, bias=False)
&gt;             (1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(384, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (10): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(64, 384, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(384, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=384, bias=False)
&gt;             (1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(384, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (11): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(64, 384, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(384, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=384, bias=False)
&gt;             (1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(384, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (12): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(96, 576, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(576, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(576, 576, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=576, bias=False)
&gt;             (1): BatchNorm2d(576, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(576, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (13): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(96, 576, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(576, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(576, 576, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=576, bias=False)
&gt;             (1): BatchNorm2d(576, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(576, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (14): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(96, 576, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(576, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(576, 576, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=576, bias=False)
&gt;             (1): BatchNorm2d(576, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(576, 160, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (15): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(160, 960, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(960, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(960, 960, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=960, bias=False)
&gt;             (1): BatchNorm2d(960, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(960, 160, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (16): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(160, 960, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(960, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(960, 960, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=960, bias=False)
&gt;             (1): BatchNorm2d(960, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(960, 160, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (17): InvertedResidual(
&gt;         (conv): Sequential(
&gt;           (0): ConvBNReLU(
&gt;             (0): Conv2d(160, 960, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;             (1): BatchNorm2d(960, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (1): ConvBNReLU(
&gt;             (0): Conv2d(960, 960, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=960, bias=False)
&gt;             (1): BatchNorm2d(960, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;             (2): ReLU6(inplace=True)
&gt;           )
&gt;           (2): Conv2d(960, 320, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;           (3): BatchNorm2d(320, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         )
&gt;       )
&gt;       (18): ConvBNReLU(
&gt;         (0): Conv2d(320, 1280, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;         (1): BatchNorm2d(1280, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;         (2): ReLU6(inplace=True)
&gt;       )
&gt;     )
&gt;   )
&gt;   (decoder): UnetDecoder(
&gt;     (center): Identity()
&gt;     (blocks): ModuleList(
&gt;       (0): DecoderBlock(
&gt;         (conv1): Conv2dReLU(
&gt;           (0): Conv2d(1376, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
&gt;           (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;           (2): ReLU(inplace=True)
&gt;         )
&gt;         (attention1): Attention(
&gt;           (attention): Identity()
&gt;         )
&gt;         (conv2): Conv2dReLU(
&gt;           (0): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
&gt;           (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;           (2): ReLU(inplace=True)
&gt;         )
&gt;         (attention2): Attention(
&gt;           (attention): Identity()
&gt;         )
&gt;       )
&gt;       (1): DecoderBlock(
&gt;         (conv1): Conv2dReLU(
&gt;           (0): Conv2d(288, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
&gt;           (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;           (2): ReLU(inplace=True)
&gt;         )
&gt;         (attention1): Attention(
&gt;           (attention): Identity()
&gt;         )
&gt;         (conv2): Conv2dReLU(
&gt;           (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
&gt;           (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;           (2): ReLU(inplace=True)
&gt;         )
&gt;         (attention2): Attention(
&gt;           (attention): Identity()
&gt;         )
&gt;       )
&gt;       (2): DecoderBlock(
&gt;         (conv1): Conv2dReLU(
&gt;           (0): Conv2d(152, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
&gt;           (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;           (2): ReLU(inplace=True)
&gt;         )
&gt;         (attention1): Attention(
&gt;           (attention): Identity()
&gt;         )
&gt;         (conv2): Conv2dReLU(
&gt;           (0): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
&gt;           (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;           (2): ReLU(inplace=True)
&gt;         )
&gt;         (attention2): Attention(
&gt;           (attention): Identity()
&gt;         )
&gt;       )
&gt;       (3): DecoderBlock(
&gt;         (conv1): Conv2dReLU(
&gt;           (0): Conv2d(80, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
&gt;           (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;           (2): ReLU(inplace=True)
&gt;         )
&gt;         (attention1): Attention(
&gt;           (attention): Identity()
&gt;         )
&gt;         (conv2): Conv2dReLU(
&gt;           (0): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
&gt;           (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;           (2): ReLU(inplace=True)
&gt;         )
&gt;         (attention2): Attention(
&gt;           (attention): Identity()
&gt;         )
&gt;       )
&gt;       (4): DecoderBlock(
&gt;         (conv1): Conv2dReLU(
&gt;           (0): Conv2d(32, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
&gt;           (1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;           (2): ReLU(inplace=True)
&gt;         )
&gt;         (attention1): Attention(
&gt;           (attention): Identity()
&gt;         )
&gt;         (conv2): Conv2dReLU(
&gt;           (0): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
&gt;           (1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
&gt;           (2): ReLU(inplace=True)
&gt;         )
&gt;         (attention2): Attention(
&gt;           (attention): Identity()
&gt;         )
&gt;       )
&gt;     )
&gt;   )
&gt;   (segmentation_head): SegmentationHead(
&gt;     (0): Conv2d(16, 1, kernel_size=(1, 1), stride=(1, 1), bias=False)
&gt;     (1): Identity()
&gt;     (2): Activation(
&gt;       (activation): Sigmoid()
&gt;     )
&gt;   )
&gt; )
</code></pre>

Henry_Chibueze: <p>So I’ve seen the structure of ur network.<br>
U said ur network only accept inputs with size of multiple of 32 so I was just thinking what r the exact multiples of 32 u tested it with (eg: 64, 128, etc)<br>
Have u tried and image of size exactly equal to 32?</p>
<p>Also I’ve not completely studied ur architecture but from what I’m seeing, ur network seems to be downsampling the input with it’s kernals and strides without upsampling back and from what I know about segmentation, the output is usually the same size as it’s image but with annotated pixels <img src="https://discuss.pytorch.org/images/emoji/apple/man_shrugging.png?v=9" title=":man_shrugging:" class="emoji" alt=":man_shrugging:"><br>
So what if u feed in an image of 1 x 3 x 64 x 64 what do u get as it’s output.</p>

Julien_Guegan: <blockquote>
<p>U said ur network only accept inputs with size of multiple of 32 so I was just thinking what r the exact multiples of 32 u tested it with (eg: 64, 128, etc)<br>
Have u tried and image of size exactly equal to 32?</p>
</blockquote>
<p><strong>Yes I have tried 32,64,224,448 (and some others…)</strong></p>
<blockquote>
<p>ur network seems to be downsampling the input with it’s kernals and strides without upsampling back and from what I know about segmentation, the output is usually the same size as it’s image but with annotated pixels <img src="https://discuss.pytorch.org/images/emoji/apple/man_shrugging.png?v=12" title=":man_shrugging:" class="emoji" alt=":man_shrugging:" loading="lazy" width="20" height="20"><br>
So what if u feed in an image of 1 x 3 x 64 x 64 what do u get as it’s output.</p>
</blockquote>
<p><strong>It might not appear in the lines I have copy/paste above but I can confirm that I have an encoder/decoder architecture (with upsampling in the decoder part). I have actually used a Unet model with the package <a href="https://github.com/qubvel/segmentation_models.pytorch" rel="noopener nofollow ugc">smp</a>. And indeed my output is a mask with exactly the same size as the input (for height and width) …</strong></p>

Henry_Chibueze: <p>Well the only way to actually help u now is to reimplement ur network architecture<br>
And see things for myself coz currently I’m not able to spot any thing that looks wrong here.<br>
I’ll probably get back to u tomorrow.</p>

Julien_Guegan: <p>I suggest you to install the package <strong>segmentation-models-pytorch</strong> then to create the same model as me with :</p>
<pre><code class="lang-auto">model = smp.Unet(encoder_name="mobilenet_v2",  encoder_weights='imagenet', activation=activation)
model.segmentation_head[0] = nn.Conv2d(16, num_classes, kernel_size=(1, 1), stride=(1, 1), bias=False) # to have one class in output
</code></pre>

Julien_Guegan: <p>The solution of the question is about the architecture. For segmentation task, I was using an encoder/decoder architecture (Unet). And indeed the input size had to be a multiple of 32 due to multiple <strong>undersampling</strong>, <strong>oversampling</strong> and <strong>copy feature</strong>. It might be possible to solve this problem by adapting or changing the architecture</p>