Hi everyone,
I know this topic has been posted several times - but I still cannot fix it and I just don’t find the mistake in my implementation…
Image Size: 224x224 RGB
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
# input img: (32, 3, 224, 224) -> batch, color, height, width
self.conv1 = nn.Conv2d(3, 6, 5) #input, output, kernel_size
self.conv2 = nn.Conv2d(6, 12, 5)
self.fc1 = nn.Linear(12*53*53, 120) # creates a matrix with x by 120
self.fc2 = nn.Linear(120, 60)
self.out = nn.Linear(60, 10)
def forward(self, t):
#convlayer
t = self.conv1(t)
print('conv1', t.shape)
t = F.relu(t)
t = F.max_pool2d(t, kernel_size = 2, stride = 2)
print('conv1pool', t.shape)
t = self.conv2(t)
print('conv2', t.shape)
t = F.relu(t)
t = F.max_pool2d(t, kernel_size = 2, stride = 2)
print('conv2pool', t.shape)
#fc
t = torch.flatten(t, start_dim=1)
#t = t.reshape(-1, 12*53*53) #flatten tensor
print('flattened t', t.shape)
t = self.fc1(t)
t = F.relu(t)
t = self.fc1(t)
t = F.relu(t)
t = self.out(t)
#softmax missing for class probabilities
return t
I then create a network instance:
cnn = CNN()
Load a batch of data and give them to the cnn:
images, labels = next(iter(trainloader))
images.shape
[222]: torch.Size([32, 3, 224, 224])
cnn(images)
Tensor shapes as printed:
conv1 torch.Size([32, 6, 220, 220])
conv1pool torch.Size([32, 6, 110, 110])
conv2 torch.Size([32, 12, 106, 106])
conv2pool torch.Size([32, 12, 53, 53])
flattened t torch.Size([32, 33708])
Happy to hear your thoughts.