Why am I getting calculated padding input size per channel smaller than kernel size?
I have the following model but its returning an error. Not sure why. I have tried googling but not found anything so far. My input is an numpy array of 6 by 6.
class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 16, kernel_size=(3,3), stride=1, padding=0)
        self.conv2 = nn.Conv2d(16, 32, kernel_size=(3,3), stride=1, padding=0)
        self.conv3 = nn.Conv2d(32, 64, kernel_size=(3,3), stride=1, padding=0)

        self.fc1 = nn.Linear(64*4*4, 320)
        self.fc2 = nn.Linear(320, 160)
        self.out = nn.Linear(160, 2)


    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = F.max_pool2d(x, kernel_size=2, stride=2)

        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, kernel_size=2, stride=2)

        x = self.conv3(x)
        x = F.relu(x)
        x = F.max_pool2d(x, kernel_size=2, stride=2)

        x = x.reshape(-1, 64*4*4)
        #x = torch.flatten(x)

        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.out(x)
        return F.softmax(x, dim=1)

My input is a 6x6 numpy array and I get the following error, any idea why?
RuntimeError: Calculated padded input size per channel: (2 x 2). Kernel size: (3 x 3). Kernel size can't be greater than actual input size


Here is what you may do and I used the padding=1 as proposed by Szymon Maszke. This padding is added to the convolution and to maxpooling.
import numpy
import torch
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 16, kernel_size=(3,3), stride=1, padding=1)
        self.conv2 = nn.Conv2d(16, 32, kernel_size=(3,3), stride=1, padding=1)
        self.conv3 = nn.Conv2d(32, 64, kernel_size=(3,3), stride=1, padding=1)

        self.fc1 = nn.Linear(64*4*4, 320)
        self.fc2 = nn.Linear(320, 160)
        self.out = nn.Linear(160, 2)


    def forward(self, x): 
        x = self.conv1(x) 
        x = F.relu(x)
        x = F.max_pool2d(x, kernel_size=2, stride=2)
        x = self.conv2(x)        
        x = F.relu(x)
        x = F.max_pool2d(x, kernel_size=3, stride=2, padding=1) 
        x = self.conv3(x)
        x = F.relu(x)
        x = F.max_pool2d(x, kernel_size=3, stride=2, padding=1)
        x = x.reshape(-1, 64*4*4)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.out(x)
        return F.softmax(x, dim=1)

a = numpy.random.rand(6,6)
print(a)
data = torch.tensor(a).float()
print(data.shape)
# data.unsqueeze_(0).unsqueeze_(0)
data= data.expand(16, 1 ,-1,-1)
print(data.shape)

n=Net()
print(&quot;Start&quot;)
o = n(data)
print(o)

Out:
[[0.89695967 0.09447725 0.0905144  0.52694105 0.66000333 0.10537102]
 [0.32854697 0.86046884 0.29804184 0.62988374 0.5965067  0.54139821]
 [0.41561266 0.95484358 0.82919364 0.75556819 0.77373267 0.52209278]
 [0.46406436 0.6553954  0.60010151 0.86314529 0.70020608 0.16471554]
 [0.72863547 0.83846636 0.95122373 0.84322402 0.32264676 0.1233866 ]
 [0.75767067 0.56546123 0.7765021  0.35303595 0.3254407  0.84033049]]
torch.Size([6, 6])
torch.Size([16, 1, 6, 6])
Start
tensor([[0.5134, 0.4866]], grad_fn=&lt;SoftmaxBackward&gt;)

By default in PyTorch padding=0, so you need to explicitly set padding=1 when needed.
