One simple method would be to add a print statement with the shape information after layer2 and define the number of input features based on this.
Otherwise, you could also calculate the shape manually using the convolution / pooling arguments, but the first approach might be faster and simpler.