title: How to calculate kernel dimensions from original image dimensions?
body: <p><a href="https://github.com/kuangliu/pytorch-cifar/blob/master/models/resnet.py" rel="nofollow noreferrer">https://github.com/kuangliu/pytorch-cifar/blob/master/models/resnet.py</a></p>
  
  <p>From reading <a href="https://www.cs.toronto.edu/~kriz/cifar.html" rel="nofollow noreferrer">https://www.cs.toronto.edu/~kriz/cifar.html</a> the cifar dataset consists of images each with 32x32 dimension.</p>
  
  <p>My understanding of code : </p>
  
  <pre><code>self.conv1 = nn.Conv2d(3, 6, 5)
  self.conv2 = nn.Conv2d(6, 16, 5)
  self.fc1   = nn.Linear(16*5*5, 120)
  </code></pre>
  
  <p>Is : </p>
  
  <pre><code>self.conv1 = nn.Conv2d(3, 6, 5) # 3 channels in, 6 channels out ,  kernel size of 5
  self.conv2 = nn.Conv2d(6, 16, 5) # 6 channels in, 16 channels out ,  kernel size of 5
  self.fc1   = nn.Linear(16*5*5, 120) # 16*5*5 in features , 120 ouot feature
  </code></pre>
  
  <p>From resnet.py the following :</p>
  
  <pre><code>self.fc1   = nn.Linear(16*5*5, 120)
  </code></pre>
  
  <p>From <a href="http://cs231n.github.io/convolutional-networks/" rel="nofollow noreferrer">http://cs231n.github.io/convolutional-networks/</a> the following is stated : </p>
  
  <blockquote>
    <p>Summary. To summarize, the Conv Layer:</p>
    
    <p>Accepts a volume of size W1×H1×D1 Requires four hyperparameters:
    Number of filters K, their spatial extent F, the stride S, the amount
    of zero padding P. Produces a volume of size W2×H2×D2 where:
    W2=(W1−F+2P)/S+1 H2=(H1−F+2P)/S+1 (i.e. width and height are computed
    equally by symmetry) D2=K With parameter sharing, it introduces F⋅F⋅D1
    weights per filter, for a total of (F⋅F⋅D1)⋅K weights and K biases. In
    the output volume, the d-th depth slice (of size W2×H2) is the result
    of performing a valid convolution of the d-th filter over the input
    volume with a stride of S, and then offset by d-th bias.</p>
  </blockquote>
  
  <p>From this I'm attempting to understand how the training image dimension 32x32 (1024 pixels) is transformed to feature map (16*5*5 -> 400) as aprt of <code>nn.Linear(16*5*5, 120)</code> </p>
  
  <p>From <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Conv2d" rel="nofollow noreferrer">https://pytorch.org/docs/stable/nn.html#torch.nn.Conv2d</a> can see default stride is 1 and padding is 0. </p>
  
  <p>What are steps to arrive at 16*5*5 from image dimension of 32*32 and can 16*5*5 be derived from above steps ?  </p>
  
  <p>From above steps how to calculate <code>spatial extent</code> ?</p>
  
  <p>Update :</p>
  
  <p>Source code :</p>
  
  <pre><code>'''LeNet in PyTorch.'''
  import torch.nn as nn
  import torch.nn.functional as F
  
  class LeNet(nn.Module):
      def __init__(self):
          super(LeNet, self).__init__()
          self.conv1 = nn.Conv2d(3, 6, 5)
          self.conv2 = nn.Conv2d(6, 16, 5)
          self.fc1   = nn.Linear(16*5*5, 120)
          self.fc2   = nn.Linear(120, 84)
          self.fc3   = nn.Linear(84, 10)
  
      def forward(self, x):
          out = F.relu(self.conv1(x))
          out = F.max_pool2d(out, 2)
          out = F.relu(self.conv2(out))
          out = F.max_pool2d(out, 2)
          out = out.view(out.size(0), -1)
          out = F.relu(self.fc1(out))
          out = F.relu(self.fc2(out))
          out = self.fc3(out)
          return out
  </code></pre>
  
  <p>Taken from <a href="https://github.com/kuangliu/pytorch-cifar/blob/master/models/lenet.py" rel="nofollow noreferrer">https://github.com/kuangliu/pytorch-cifar/blob/master/models/lenet.py</a></p>
  
  <p>My understanding is that the convolution operation is applied to the image data per kernel. So if 5 kernels are set then 5 convolutions are applied to the data which generates a 5 dimensional image representation.</p>