Convolution and pooling layers need a method to calculate output size

Is there a simple way to calculate the output size of multiple convolution and pooling layers? I mean the automatical way. Suppose I have 10 such layers, then when I decide to change the first layer, I would need to recalculate the output size of input images fed through the whole 10 layers which is too troublesome.
I suggest the convolution and pooling modules should have a method like: conv.output_size(input_size) to make things easier.

You can implement such a method in your model class using random input. It should be something like this:

def calculate_size(self, input_size):
    x = torch.randn(input_size).unsqueeze(0)
    output = self.layers(x)
    return output.size()[1:]

This will use one forward pass to calculate the output shape. You should of course use the layers you need the output shape for.

Thanks, that’s a clever trick.