Based on the shape information in the error message, the error seems to be raised in self.linear, which expects the input activation to have 512 features.
However, out seems to have 25088 features and you could verify it by printing the shape after the flattening of the tensor in:
        ...
        out = F.avg_pool2d(out, 4)
        out = out.view(out.size(0), -1)
        out = F.dropout(out, p=0.5, training=self.training)
        print(out.shape)
        out = self.linear(out)

        return out

To fix this you would have to change the in_features in self.linear to 25088.