Skip to content

Commit a7a58c4

Browse files
committed
update ckpt_manager.py
1 parent e2b17bd commit a7a58c4

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

ckpt_manager.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CheckpointManager:
3434
def __init__(self):
3535
self.model_name = None
3636
self.checkpoint_path = None
37+
self.best_metric = None
3738

3839
def set_model_name(self, model_name):
3940
self.model_name = model_name
@@ -69,16 +70,33 @@ def init_checkpoint_dir(self):
6970
print(f'checkpoint path : {self.checkpoint_path}')
7071

7172
def remove_last_model(self):
72-
for last_model_path in glob(f'{self.checkpoint_path}/model_*_iter.h5'):
73+
for last_model_path in glob(f'{self.checkpoint_path}/last_*.h5'):
7374
os.remove(last_model_path)
7475

75-
def save_last_model(self, model, iteration_count):
76+
def save_last_model(self, model, iteration_count, content=''):
7677
self.make_checkpoint_dir()
77-
save_path = f'{self.checkpoint_path}/model_{iteration_count}_iter.h5'
78+
save_path = f'{self.checkpoint_path}/last_{iteration_count}_iter{content}.h5'
7879
model.save(save_path, include_optimizer=False)
7980
backup_path = f'{save_path}.bak'
8081
sh.move(save_path, backup_path)
8182
self.remove_last_model()
8283
sh.move(backup_path, save_path)
8384
return save_path
8485

86+
def remove_best_model(self):
87+
for best_model_path in glob(f'{self.checkpoint_path}/best_*.h5'):
88+
os.remove(best_model_path)
89+
90+
def save_best_model(self, model, iteration_count, metric, content=''):
91+
save_path = None
92+
if self.best_metric is None or metric > self.best_metric:
93+
self.best_metric = metric
94+
self.make_checkpoint_dir()
95+
save_path = f'{self.checkpoint_path}/best_{iteration_count}_iter{content}.h5'
96+
model.save(save_path, include_optimizer=False)
97+
backup_path = f'{save_path}.bak'
98+
sh.move(save_path, backup_path)
99+
self.remove_best_model()
100+
sh.move(backup_path, save_path)
101+
return save_path
102+

0 commit comments

Comments
 (0)