RuntimeError: size mismatch, m1: [4 x 512], m2: [1024 x 512]

a better way to write this since it flatten the output of conv layer is

shape = torch.prod(torch.tensor(x.shape[1:])).item()
x =  x.view(-1, shape)

Also

should be

self.fc1 = nn.Linear(Number of Flattened Features from last layer , 1024,...)

Further, read this post for better understanding.