if you want to save some additional file in checkpoints where PyTorch lightning save latest or best model in certain folder automatically, add this function in
.
#training class using pl
class my_trainer(pl.LightningModule):
def __init__(self, cfg):
super().__init__()
..
add this model to save additional file
.
def on_save_checkpoint(self, checkpoint):
# Call the parent method first (optional)
super().on_save_checkpoint(checkpoint)
# Your custom code to save additional files
dirpath = None
for callback in self.trainer.callbacks:
if isinstance(callback, ModelCheckpoint):
dirpath = callback.dirpath
break
if dirpath is not None:
additional_filepath = os.path.join(dirpath, "my_additional_file.txt")
with open(additional_filepath, "w") as f:
f.write("Some additional data")
print(f"Saved additional file to {additional_filepath}")
else:
print("Could not find ModelCheckpoint dirpath to save additional file.")
..
ok, now try it!
Good luck!
www.marearts.com
No comments:
Post a Comment