Pytorch code with 5 convo layers ! Please help me to understand

roberto_bruzzese: <h1>
<a name="five-convo-layer-relu-activation-function-1" class="anchor" href="#five-convo-layer-relu-activation-function-1"></a>five convo layer , relu activation function</h1>
<p>class ConvNet(nn.Module):<br>
def <strong>init</strong>(self, num_classes=110):<br>
super(ConvNet, self).<strong>init</strong>()</p>
<pre><code>    self.features = torch.nn.Sequential(
        
        nn.Conv2d(3, 224, kernel_size=11, stride=2, padding=2),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=3, stride=2),

        nn.Conv2d(224, 192, kernel_size=5, padding=1),
        nn.ReLU(),
        nn.MaxPool2d(kernel_size=3, stride=2),

        nn.Conv2d(192, 384, kernel_size=3, padding=1),
        nn.ReLU(),

        nn.Conv2d(384, 256, kernel_size=3, padding=1),
        nn.ReLU(),

        nn.Conv2d(256, 256, kernel_size=3, padding=1),
        nn.ReLU(),

        nn.MaxPool2d(kernel_size=3, stride=2),
    )
    self.avgpool = nn.AdaptiveAvgPool2d((6, 6))

    self.classifier = torch.nn.Sequential(
        
        nn.Dropout(),
        nn.Linear(256 * 6 * 6, 4096), # output of last conv layer*target output h*w
        nn.ReLU(),
        
        nn.Dropout(),
        nn.Linear(4096, 4096),
        nn.ReLU(),
        nn.Linear(4096, num_classes)
    )

def forward(self, x):
    x = self.features(x)
    x = self.avgpool(x)
    x = torch.flatten(x, 1)
    out = self.classifier(x)
    return out
</code></pre>
<p>If I understand in the first convo layer the input channels is 3 , and the output produced by the convolution is 224. But what is the input image ?</p>

ptrblck: <p>Could you explain your questions a bit more and in particular which part of the code is unclear, please?</p>

roberto_bruzzese: <aside class="quote no-group" data-username="roberto_bruzzese" data-post="1" data-topic="176345">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/roberto_bruzzese/48/47219_2.png" class="avatar"> roberto_bruzzese:</div>
<blockquote>
<p>If I understand in the first convo layer the input channels is 3 , and the output produced by the convolution is 224. But what is the input image ?</p>
</blockquote>
</aside>
<p>If I understand in the first convo layer the input channels is 3 , and the output produced by the convolution is 224. But what is the input image ?</p>

ptrblck: <p>Yes, the input tensor is expected to have 3 channels and the output activation will have 224 channels.<br>
The actual input is undefined, since it will be passed to the model’s <code>forward</code> method after initializing the model. <a href="https://pytorch.org/tutorials/beginner/nn_tutorial.html">This tutorial</a> might be a good starter.</p>

roberto_bruzzese: <p>My question is how do I check if all the convo layers dimensionality are correct? For example the second convo layer is nn.Conv2d(224, 192, kernel_size=5, padding=1),. That means that the MaxPool2d(kernel_size=3, stride=2), of the first layer does not change the 224 dimension ?</p>

ptrblck: <p>Yes, pooling layers do not change the channel dimension of input activations, but their spatial size.<br>
Conv layers apply their filters on the activation via a convolution or cross-correlation and are thus also flexible regarding the spatial size of the input as long as their kernels are not larger than the input.<br>
Also, 224 is often the spatial size of the input activation while you are using it as the number of output channels, which would work, but don’t confuse these dimensions.<br>
The inputs are defined as <code>[batch_size, channels, height, width]</code>.</p>

roberto_bruzzese: <p>In order to make a more detailed calculation, if the input image into the first convo layer is an image of 224x224 pixels which will be the output dimension after the   first convo layer nn.Conv2d(3, 224, kernel_size=11, stride=2, padding=2) ?</p>

ptrblck: <p>You could use the formula defined in the <a href="https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d">docs</a> to calculate the spatial output size. <a class="mention" href="/u/j_johnson">@J_Johnson</a> provided a convenient util. method <a href="https://discuss.pytorch.org/t/size-mismatch-error-in-cnn-fc-layer/174553/3">here</a> to also calcualte it:</p>
<pre><code class="lang-python">def calc_conv_size(length, kernel_size, stride=1, padding=0, dilation=1):
    return math.floor((length + 2*padding - dilation*(kernel_size-1)-1)/stride+1)

conv = nn.Conv2d(3, 224, kernel_size=11, stride=2, padding=2) 
x = torch.randn(1, 3, 224, 224)
out = conv(x)
print(out.shape)
# torch.Size([1, 224, 109, 109])

calc_conv_size(length=224, kernel_size=11, stride=2, padding=2)
# 109
</code></pre>
<p>As you can see, you can also just run a forward pass and <code>print</code> the output shape.</p>

roberto_bruzzese: <aside class="quote no-group" data-username="ptrblck" data-post="8" data-topic="176345">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<pre><code class="lang-auto">def calc_conv_size(length, kernel_size, stride=1, padding=0, dilation=1):
    return math.floor((length + 2*padding - dilation*(kernel_size-1)-1)/stride+1)

conv = nn.Conv2d(3, 224, kernel_size=11, stride=2, padding=2) 
x = torch.randn(1, 3, 224, 224)
out = conv(x)
print(out.shape)
# torch.Size([1, 224, 109, 109])

calc_conv_size(length=224, kernel_size=11, stride=2, padding=2)
# 109
</code></pre>
</blockquote>
</aside>
<p>Thank you. By doing this calculation, at the end of the fifth convo layer it results to me the following dimension :<br>
torch.Size([1, 256, 127, 127])<br>
Do you think it is right?</p>
<p>I attach the following :<br>
<span class="hashtag-raw">#first</span> convo layer<br>
conv = nn.Conv2d(3, 224, kernel_size=11, stride=2, padding=2)<br>
x = torch.randn(1, 3, 224, 224)<br>
out = conv(x)<br>
print(out.shape)</p>
<h1><a name="torchsize1-224-109-109-1" class="anchor" href="#torchsize1-224-109-109-1"></a>torch.Size([1, 224, 109, 109])</h1>
<p>relu =  nn.ReLU()<br>
x = torch.randn(1, 224, 109, 109)<br>
out = relu(x)<br>
print(out.shape)<br>
maxpool2d = nn.MaxPool2d(kernel_size=3, stride=2)<br>
x =  torch.randn(1, 224, 109, 109)<br>
out = maxpool2d(x)<br>
print(out.shape)</p>
<p><span class="hashtag-raw">#second</span> convo layer<br>
conv = nn.Conv2d(224, 192, kernel_size=5,  padding=1)<br>
x = torch.randn(1, 224, 192, 192)<br>
out = conv(x)<br>
print(out.shape)</p>
<p>relu =  nn.ReLU()<br>
x = torch.randn(1, 192, 190, 190)<br>
out = relu(x)<br>
print(out.shape)<br>
maxpool2d = nn.MaxPool2d(kernel_size=3, stride=2)<br>
x =  torch.randn(1, 192, 190, 190)<br>
out = maxpool2d(x)<br>
print(out.shape)</p>
<p><span class="hashtag-raw">#third</span> convo layer<br>
conv = nn.Conv2d(192, 384, kernel_size=3,  padding=1)<br>
x = torch.randn(1, 192, 384, 384)<br>
out = conv(x)<br>
print(out.shape)</p>
<h1><a name="torchsize1-384-384-384-2" class="anchor" href="#torchsize1-384-384-384-2"></a>torch.Size([1, 384, 384, 384])</h1>
<p>relu =  nn.ReLU()<br>
x = torch.randn(1, 384, 384, 384)<br>
out = relu(x)<br>
print(out.shape)</p>
<p><span class="hashtag-raw">#fourth</span> convo layer<br>
conv = nn.Conv2d(384, 256, kernel_size=3,  padding=1)<br>
x = torch.randn(1, 384, 256, 256)<br>
out = conv(x)<br>
print(out.shape)</p>
<h1><a name="torchsize1-256-256-256-3" class="anchor" href="#torchsize1-256-256-256-3"></a>torch.Size([1, 256, 256, 256])</h1>
<p>relu =  nn.ReLU()<br>
x = torch.randn(1, 256, 256, 256)<br>
out = relu(x)<br>
print(out.shape)</p>
<p><span class="hashtag-raw">#fifth</span> convo layer<br>
conv = nn.Conv2d(256, 256, kernel_size=3,  padding=1)<br>
x = torch.randn(1, 256, 256, 256)<br>
out = conv(x)<br>
print(out.shape)</p>
<h1><a name="torchsize1-256-256-256-4" class="anchor" href="#torchsize1-256-256-256-4"></a>torch.Size([1, 256, 256, 256])</h1>
<p>relu =  nn.ReLU()<br>
x = torch.randn(1, 256, 256, 256)<br>
out = relu(x)<br>
print(out.shape)<br>
maxpool2d = nn.MaxPool2d(kernel_size=3, stride=2)<br>
x =  torch.randn(1, 256, 256, 256)<br>
out = maxpool2d(x)<br>
print(out.shape)</p>
<p>torch.Size([1, 224, 109, 109])<br>
torch.Size([1, 224, 109, 109])<br>
torch.Size([1, 224, 54, 54])<br>
torch.Size([1, 192, 190, 190])<br>
torch.Size([1, 192, 190, 190])<br>
torch.Size([1, 192, 94, 94])<br>
torch.Size([1, 384, 384, 384])<br>
torch.Size([1, 384, 384, 384])<br>
torch.Size([1, 256, 256, 256])<br>
torch.Size([1, 256, 256, 256])<br>
torch.Size([1, 256, 256, 256])<br>
torch.Size([1, 256, 256, 256])<br>
torch.Size([1, 256, 127, 127])</p>

ptrblck: <p>Sorry, but I don’t understand your code and what you are trying to achieve with it.<br>
E.g. here:</p>
<pre><code class="lang-python">maxpool2d = nn.MaxPool2d(kernel_size=3, stride=2)
x = torch.randn(1, 224, 109, 109)
out = maxpool2d(x)
print(out.shape)

#second convo layer
conv = nn.Conv2d(224, 192, kernel_size=5, padding=1)
x = torch.randn(1, 224, 192, 192)
out = conv(x)
print(out.shape)

relu = nn.ReLU()
x = torch.randn(1, 192, 190, 190)
out = relu(x)
print(out.shape)
</code></pre>
<p>you are recreating the <code>x</code> tensor with a larger spatial shape than what the output of the pooling layer could be.<br>
What is this code supposed to do and why do you recreate tensors in each layer instead of reusing the output from the previous one?</p>

roberto_bruzzese: <aside class="quote no-group" data-username="roberto_bruzzese" data-post="7" data-topic="176345" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/roberto_bruzzese/48/47219_2.png" class="avatar"> roberto_bruzzese:</div>
<blockquote>
<p>In order to make a more detailed calculation, if the input image into the first convo layer is an image of 224x224 pixels which will be the output dimension after the first convo layer nn.Conv2d(3, 224, kernel_size=11, stride=2, padding=2) ?</p>
</blockquote>
</aside>
<p>when i asked the question … In order to make a more detailed calculation, if the input image into the first convo layer is an image of 224x224 pixels which will be the output dimension after the first convo layer nn.Conv2d(3, 224, kernel_size=11, stride=2, padding=2) ?<br>
you answered that the output dimension after the first convo layer was 109x109. Right ?<br>
And you added this code to show it :</p>
<p>conv = nn.Conv2d(3, 224, kernel_size=11, stride=2, padding=2)<br>
x = torch.randn(1, 3, 224, 224)<br>
out = conv(x)<br>
print(out.shape)</p>
<h1><a name="torchsize1-224-109-109-1" class="anchor" href="#torchsize1-224-109-109-1"></a>torch.Size([1, 224, 109, 109])</h1>
<p>In fact the output dimension after the print(out.shape) was 109.<br>
Therefore the next Relu was applied to an input image of 109x109 and this was the reason I wrote the code :<br>
relu = nn.ReLU()<br>
x = torch.randn(1, 224, 109, 109)<br>
out = relu(x)<br>
print(out.shape)</p>
<p>And printed the shape (that was not affected by the Relu, obviously). Then I proceeded to the next maxpool2d , by printing the out.shape after applying the maxpool2d to the input of the previous layer result.<br>
That’s the way I proceeded till the fifth layer to the final output dimension . Is it  clear now ?</p>

ptrblck: <p>Yes, but you could use the outputs directly instead since your re-creation of the intermediate tensors is prone to errors. Instead just add the <code>print</code> statements between each layer and keep the original <code>forward</code> method equal, i.e. just reuse the output activations from the previous layer for the next one.</p>

roberto_bruzzese: <p>Sorry but i do not understand what do you mean by re-creation. Can you make an example of how to do it ? In your answer of April 8 you suggested the sequence of code:</p>
<p>conv = nn.Conv2d(3, 224, kernel_size=11, stride=2, padding=2)<br>
x = torch.randn(1, 3, 224, 224)<br>
out = conv(x)<br>
print(out.shape)</p>
<h1>
<a name="torchsize1-224-109-109-1" class="anchor" href="#torchsize1-224-109-109-1"></a>torch.Size([1, 224, 109, 109])</h1>