In general, we have to calculate the dimension of previous layer to declare a fully connected layer(flattened layer) after performing convolutions and pooling operations.
Some forums also displayed the use of functions for the purpose like
def count_input_neuron(model, image_dim):
return model(torch.rand(1, *(image_dim))).data.view(1, -1).size(1)
which can be seen here on Pytorch forum and Stack overflow
whereas some used flatten function of the torch