I found this to be a better method to do the same. Since self.num_classes is used only in the end. We can do something like this:
# change the last conv2d layer
net.classifier._modules["1"] = nn.Conv2d(512, num_of_output_classes, kernel_size=(1, 1))
# change the internal num_classes variable rather than redefining the forward pass
net.num_classes = num_of_output_classes