CNN With 3 Conv Layer

Hi
I want to make a CNN with 3 convolution layers and 3 pooling layers, the net is like this :
convolution-pooling-convolution-pooling-convolution-pooling-fc1-fc2
the pooling is 2x2 and its stride is 1 and the convolutions are 5x5
the output sizes are respectively : 32,64,128,512,64
my network code is this :

class Net(nn.Module):
def init(self):
super(Net, self).init()
self.conv1 = nn.Conv2d(3, 32, 5)
self.pool = nn.AvgPool2d(2, 2, 1)
self.conv2 = nn.Conv2d(32, 64, 5)
self.conv3 = nn.Conv2d(64, 128, 5)
self.fc1 = nn.Linear(128 * 5 * 5, 512)
self.fc2 = nn.Linear(512, 64)

def forward(self, x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = self.pool(F.relu(self.conv3(x)))
    x = x.view(-1, 128 * 5 * 5)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    return x

I got this error : x = x.view(-1, 128 * 5 * 5)
RuntimeError: invalid argument 2: size ‘[-1 x 3200]’ is invalid for input with 512 elements at /opt/conda/conda-bld/pytorch_1524577177097/work/aten/src/TH/THStorage.c:37

What is you input size? Note that you’ll lose 2 pixels at each conv layer, as your kernel_size=5 and you don’t use any padding.
Also the arguments for nn.AvgPool2d are kernel_size, stride, padding. So currently you use stride=2 and add padding=1.

To easily debug your shape, just add print(x.shape) before the .view op to see the current output shape.

1 Like

My input size is 32x32
What should be the numbers in x.view?

Just as @ptrblck pointed out, print(x.shape) before .view() to check the numbers.

1 Like

Thanks , it got solved

How did you resolve it?