@@ -35,6 +35,26 @@ class KerasTrainerOptions:
3535 enable_check_numerics : bool = False
3636
3737
38+ @dataclasses .dataclass
39+ class KerasTrainerCheckpointOptions :
40+ """Provides Keras Checkpointing related configuration options.
41+
42+ Attributes:
43+ checkpoint_dir: Directory path to save checkpoint files.
44+ best_checkpoint: Filename for the best checkpoint.
45+ latest_checkpoint: Filename for the latest checkpoint.
46+ """
47+ checkpoint_dir : Optional [str ] = None
48+ best_checkpoint : str = "best"
49+ latest_checkpoint : str = "latest"
50+
51+ def best_checkpoint_filepath (self ) -> str :
52+ return os .path .join (self .checkpoint_dir , self .best_checkpoint )
53+
54+ def latest_checkpoint_filepath (self ) -> str :
55+ return os .path .join (self .checkpoint_dir , self .latest_checkpoint )
56+
57+
3858class KerasTrainer :
3959 """Trains using the `tf.keras.Model.fit` training loop."""
4060
@@ -43,7 +63,7 @@ def __init__(
4363 strategy : tf .distribute .Strategy ,
4464 * ,
4565 model_dir : str ,
46- ckpts_dir : Optional [str ] = None ,
66+ checkpoint_options : Optional [KerasTrainerCheckpointOptions ] = None ,
4767 backup_dir : Optional [str ] = None ,
4868 steps_per_epoch : Optional [int ] = None ,
4969 validation_steps : Optional [int ] = None ,
@@ -58,8 +78,9 @@ def __init__(
5878 Args:
5979 strategy: A `tf.distribute.Strategy.`
6080 model_dir: A model directory for summaries.
61- ckpts_dir: An optional directory for checkpoints, if unset;
62- `os.path.join(model_dir, "ckpts")` is used.
81+ checkpoint_options: An optional configuration for checkpointing related
82+ configs. If checkpoint_options.checkpoint_dir is unset;
83+ `os.path.join(model_dir, "ckpnt")` is used.
6384 backup_dir: An optional directory for backup, if unset;
6485 `(os.path.join(model_dir, "backup"),)` is used.
6586 steps_per_epoch: An optional steps per epoch, if unspecified: epochs are
@@ -87,15 +108,16 @@ def __init__(
87108 raise ValueError ("`restore_best_weights` requires a "
88109 "`checkpoint_every_n_steps` other than \" never\" " )
89110
90- if ckpts_dir is None :
91- ckpts_dir = os .path .join (model_dir , "ckpts" )
111+ if checkpoint_options is None :
112+ checkpoint_options = KerasTrainerCheckpointOptions ()
113+ checkpoint_options .checkpoint_dir = os .path .join (model_dir , "ckpnt" )
92114
93115 if backup_dir is None :
94116 backup_dir = os .path .join (model_dir , "backup" )
95117
96118 self ._strategy = strategy
97119 self ._model_dir = model_dir
98- self ._ckpts_dir = ckpts_dir
120+ self ._checkpoint_options = checkpoint_options
99121 self ._backup_dir = backup_dir
100122 self ._steps_per_epoch = steps_per_epoch
101123 self ._validation_steps = validation_steps
@@ -215,12 +237,12 @@ def per_replica_ds_fn(input_context, *, delegate, repeat):
215237 if checkpoint_every_n_steps != "never" :
216238 callbacks += [
217239 tf .keras .callbacks .ModelCheckpoint (
218- filepath = os . path . join ( self . _ckpts_dir , "latest" ),
240+ filepath = self . _checkpoint_options . latest_checkpoint_filepath ( ),
219241 save_best_only = False ,
220242 save_weights_only = True ,
221243 save_freq = checkpoint_every_n_steps ),
222244 tf .keras .callbacks .ModelCheckpoint (
223- filepath = os . path . join ( self . _ckpts_dir , "best" ),
245+ filepath = self . _checkpoint_options . best_checkpoint_filepath ( ),
224246 save_best_only = True ,
225247 save_weights_only = True ,
226248 save_freq = "epoch" )
@@ -256,7 +278,6 @@ def per_replica_ds_fn(input_context, *, delegate, repeat):
256278 callbacks = callbacks )
257279
258280 if self ._restore_best_weights :
259- model .load_weights (os . path . join ( self . _ckpts_dir , "best" ))
281+ model .load_weights (self . _checkpoint_options . best_checkpoint_filepath ( ))
260282
261283 return model
262-
0 commit comments