How to apply early stopping in training code?

I am training deeplab but facing overfitting issue. To overcome this issue, i wanna add early stopping (i.e., patience) in my training block to avoid overfitting. However, I don’t know how and where to insert early stopping code in training code block. I have attached my training execution code below.

I shall be grateful, if anybody help me out.