@@ -62,14 +62,20 @@ def __init__(
6262
6363 def save_checkpoint (self , model , optimizer , args , epoch , model_ema = None , metric = None , use_amp = False ):
6464 assert epoch >= 0
65+ tmp_save_path = os .path .join (self .checkpoint_dir , 'tmp' + self .extension )
66+ last_save_path = os .path .join (self .checkpoint_dir , 'last' + self .extension )
67+ self ._save (tmp_save_path , model , optimizer , args , epoch , model_ema , metric , use_amp )
68+ if os .path .exists (last_save_path ):
69+ os .unlink (last_save_path ) # required for Windows support.
70+ os .rename (tmp_save_path , last_save_path )
6571 worst_file = self .checkpoint_files [- 1 ] if self .checkpoint_files else None
6672 if (len (self .checkpoint_files ) < self .max_history
6773 or metric is None or self .cmp (metric , worst_file [1 ])):
6874 if len (self .checkpoint_files ) >= self .max_history :
6975 self ._cleanup_checkpoints (1 )
7076 filename = '-' .join ([self .save_prefix , str (epoch )]) + self .extension
7177 save_path = os .path .join (self .checkpoint_dir , filename )
72- self . _save ( save_path , model , optimizer , args , epoch , model_ema , metric , use_amp )
78+ os . link ( last_save_path , save_path )
7379 self .checkpoint_files .append ((save_path , metric ))
7480 self .checkpoint_files = sorted (
7581 self .checkpoint_files , key = lambda x : x [1 ],
@@ -83,7 +89,10 @@ def save_checkpoint(self, model, optimizer, args, epoch, model_ema=None, metric=
8389 if metric is not None and (self .best_metric is None or self .cmp (metric , self .best_metric )):
8490 self .best_epoch = epoch
8591 self .best_metric = metric
86- shutil .copyfile (save_path , os .path .join (self .checkpoint_dir , 'model_best' + self .extension ))
92+ best_save_path = os .path .join (self .checkpoint_dir , 'model_best' + self .extension )
93+ if os .path .exists (best_save_path ):
94+ os .unlink (best_save_path )
95+ os .link (last_save_path , best_save_path )
8796
8897 return (None , None ) if self .best_metric is None else (self .best_metric , self .best_epoch )
8998
0 commit comments