Can we pass images for which height!=width through our CNN for training in pytorch?

spaul13: <p>can we pass images for which height!=width through our CNN in Pytorch?</p>
<p>In CNN, I have convolution, batch-norm, max-pool, relu, and fully connected layers.</p>
<p>My network</p>
<p><em>self.conv_seqn = nn.Sequential(</em><br>
<em>nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=1),</em><br>
<em>nn.BatchNorm2d(32),</em><br>
<em>nn.ReLU(inplace=True),</em><br>
<em>nn.MaxPool2d(kernel_size=2, stride=2),</em><br>
<em>nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),</em><br>
<em>nn.ReLU(inplace=True),</em><br>
<em>nn.MaxPool2d(kernel_size=4, stride=4),</em><br>
<em>nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),</em><br>
<em>nn.ReLU(inplace=True),</em><br>
<em>nn.MaxPool2d(kernel_size=4, stride=4),</em><br>
<em>)</em><br>
<em>self.fc_seqn = nn.Sequential(</em><br>
<em>nn.Linear(1843200, 256),</em><br>
<em>nn.ReLU(inplace=True),</em><br>
<em>nn.Linear(256, total_configs)</em><br>
<em>)</em></p>
<p><em>forward()</em><br>
<em>{</em><br>
<em>x = self.conv_seqn(x)</em><br>
<em>x = x.view(x.size(0), -1)</em><br>
<em>x = self.fc_seqn(x)</em><br>
<em>return x</em><br>
<em>}</em></p>
<p>***If input image of size 3840X1920X3 after applying conv_seqn() it should be of size [1, 128, 120, 60] but I getting the size of [1,128,120,120] (batch size =1 here)</p>

ptrblck: <p>Yes, you can pass input tensors with a rectangular spatial size to the model.<br>
Could you post the input shape again by wrapping it into three backticks ``` please?<br>
I’m currently unsure, how <code>384021603</code> should be interpreted.</p>

spaul13: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> thanks for the comment. I have made the changes  to the post. The original size of the image is 3840X1920X3.</p>
<p>Can u plz tell me why for me I can’t pass a tensor of rectangular shape through convolution batchnorm maxpool layers?</p>

ptrblck: <p>Your used layers expect an input in the shape <code>[batch_size, channel, height, width]</code>, so you would have to add a batch dimension at dim0 and permute the input such that the channel dimension is in dim1.</p>
<p>Once you have the right shape, you could directly pass the input to the model.</p>

spaul13: <p>Thanks a lot for the reply. The resolution of the original input image tensor is  [1,3,3840,1920] but still after conv_seqn I am getting [1,128,120,120] instead of [1, 128, 120, 60]. Is there any other reasons for not getting the rectangular tensor properly through the convolution block?</p>

ptrblck: <p>Your <code>conv_seqn</code> outputs the desired shape for me:</p>
<pre><code class="lang-python">conv_seqn = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=4, stride=4),
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=4, stride=4),
)

x = torch.randn(1, 3, 3840, 1920)
out = conv_seqn(x)
print(out.shape)
</code></pre>