Step 2: Check fc1 input sizes
class CNN(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 10, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(10, 20, 5)
self.fc1 = nn.Linear(20 * 13 * 13, 50)
self.fc2 = nn.Linear(50, 5)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 20 * 13 * 13)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x: 20*13*13 correct. class CNN(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 10, 3)
self.pool = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(10 * 32 * 32, 5)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = x.view(-1, 10 * 32 * 32)
x = self.fc1(x)
return x: single conv kernel=3 gives ~10*31*31 but uses 10*32*32 wrong. class CNN(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 10, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(10, 20, 5)
self.fc1 = nn.Linear(20 * 12 * 12, 50)
self.fc2 = nn.Linear(50, 5)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 20 * 12 * 12)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x: 20*12*12 too small. class CNN(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 10, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(10, 20, 5)
self.fc1 = nn.Linear(20 * 14 * 14, 50)
self.fc2 = nn.Linear(50, 5)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 20 * 14 * 14)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x: 20*14*14 too big.