How can I resize Olivetti Dataset images 64x64 to 32x32 ?? I am getting error
batch_boyut = 2

train_loader = torch.utils.data.DataLoader(
    X_egitim, batch_size=batch_boyut)
val_loader = torch.utils.data.DataLoader(
    X_val, batch_size=batch_boyut)

class CNNModule(nn.Module):
    def __init__(self):
        super(CNNModule, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        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, 40)

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

model = CNNModule()
print(model)
model.forward(X_egitim)


I am getting this error.How can I solve? How can resize olivetti dataset images from 64x64 to 32x32 ?
I think I should change x.view but I don't know how I can

x = x.view(-1, 16 * 5 * 5)

RuntimeError: shape '[-1, 400]' is invalid for input of size 865280

I assume your input shape is 320 x 1 x 64 x 64.

I think you need to understand what is the output shape of convolution and max-pool operations. In your model, you have 2 CNN layers, followed by the max-pooling layer.

First CNN and max-pool layer:

x = self.pool(f.relu(self.conv1(x)))


Since conv1 has a 2d kernel of size 5 x 5, the output shape of conv1 will be (6 x 60 x 60). After passing through the max-pooling layer with kernel size 2 x 2, you will get 6 x 30 x 30 shaped tensor. You can easily compute the output shape of tensor as follows.

64 x 64 -&gt; 60 x 60 -&gt; 30 x 30


Where, 60 = (64 - cnn_kernel_size + 1) and, 30 = 60 // max_pool_kernel_size.

Similarly, after the second CNN and max-pool layer:

x = self.pool(f.relu(self.conv2(x)))


The shape of x will be: 320 x 16 x 13 x 13. Here, 13 = 26 // 2 and 26 = 30 - 5 + 1. Now, when you are trying to reshape x as x.view(-1, 16 * 5 * 5), it is throwing the error.

So, either you should revise the following layers which would take input of shape -1, 16 * 13 * 13, or you revise the CNN layers to finally get output tensor of shape -1, 16 * 5 * 5.



Note, if you have an input tensor of shape 32 x 32, you won't face the problem. Why?

Because: 32 -&gt; (32 - 5 + 1) // 2 -&gt; 14 -&gt; (14 - 5 + 1) // 2 -&gt; 5

So, if you want to convert size 64 x 64 to 32 x 32, you may consider performing downscaling using some operations, e.g., max-pooling 2d (with kernel_size 2 x 2), a linear transformation, etc.
