Freeze few layers after a certain epoch

I have a compute vision model with ResNet18 backbone. On top, there are two sequential branches for two different tasks. After a few epochs I see, one branch accuracy becomes saturated while the other one gradually increases. Now, I want to freeze the parameter update of the saturated branch to prevent overfitting. So, the condition would be either let’s say 95% accuracy of that branch or 30 epochs. How can I do that?

You could use the current accuracy as a condition and set the .requires_grad attributes of the parameters which should be frozen to False.