forked from vitoralbiero/img2pose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathearly_stop.py
29 lines (26 loc) · 1.03 KB
/
early_stop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class EarlyStop:
def __init__(self, patience=5, mode="max", threshold=0):
self.patience = patience
self.counter = 0
self.best_score = None
self.stop = False
self.mode = mode
self.threshold = threshold
self.val_score = float("Inf")
if mode == "max":
self.val_score *= -1
def __call__(self, val_score):
if self.best_score is None:
self.best_score = val_score
# if val score did not improve, add to early stop counter
elif (val_score < self.best_score + self.threshold and self.mode == "max") or (
val_score > self.best_score + self.threshold and self.mode == "min"
):
self.counter += 1
print(f"Early stop counter: {self.counter} out of {self.patience}")
# if not improve for patience times, stop training earlier
if self.counter >= self.patience:
self.stop = True
else:
self.best_score = val_score
self.counter = 0