Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filename parameter to ModelCheckpoint #2584

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pytorch_lightning/callbacks/model_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ModelCheckpoint(Callback):

def __init__(self, filepath: Optional[str] = None, monitor: str = 'val_loss', verbose: bool = False,
save_last: bool = False, save_top_k: int = 1, save_weights_only: bool = False,
mode: str = 'auto', period: int = 1, prefix: str = ''):
mode: str = 'auto', period: int = 1, prefix: str = '', filename: Optional[str] = None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing docs for the new parameter

super().__init__()
if save_top_k > 0 and filepath is not None and os.path.isdir(filepath) and len(os.listdir(filepath)) > 0:
rank_zero_warn(
Expand All @@ -110,9 +110,11 @@ def __init__(self, filepath: Optional[str] = None, monitor: str = 'val_loss', ve
self.monitor = monitor
self.verbose = verbose
if filepath is None: # will be determined by trainer at runtime
self.dirpath, self.filename = None, None
self.dirpath, self.filename = None, filename
else:
if os.path.isdir(filepath):
if filename:
self.dirpath, self.filename = filepath, filename
elif os.path.isdir(filepath):
self.dirpath, self.filename = filepath, '{epoch}'
else:
filepath = os.path.realpath(filepath)
Expand Down Expand Up @@ -251,7 +253,8 @@ def on_train_start(self, trainer, pl_module):
if self.dirpath is not None:
return # short circuit

self.filename = '{epoch}'
if self.filename is None:
self.filename = '{epoch}'

if trainer.logger is not None:
if trainer.weights_save_path != trainer.default_root_dir:
Expand Down
37 changes: 37 additions & 0 deletions tests/callbacks/test_model_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,43 @@ def test_model_checkpoint_path(tmpdir, logger_version, expected):
assert ckpt_version == expected


@pytest.mark.parametrize(
'filepath,filename,tgt_dir,tgt_filename',
[
(None, None, 'lightning_logs/version_0/checkpoints', 'epoch=4.ckpt'),
(None, 'test_{epoch}', 'lightning_logs/version_0/checkpoints', 'test_epoch=4.ckpt'),
('checkpoints', None, 'checkpoints', 'epoch=4.ckpt'),
('checkpoints', '{v_num}', 'checkpoints', 'v_num=0_v0.ckpt'),
('checkpoints/{v_num}', None, 'checkpoints', 'v_num=0.ckpt'),
('checkpoints/{v_num}', None, 'checkpoints', 'v_num=0_v0.ckpt')
],
)
def test_model_checkpoint_filename(tmpdir, filepath, filename, tgt_dir, tgt_filename):
"""Test that the checkpoint path is built from filepath and filename"""
tutils.reset_seed()
model = EvalModelTemplate()

if filepath is not None:
filepath = tmpdir / filepath
os.makedirs(tmpdir / tgt_dir)

checkpoint = ModelCheckpoint(filepath=filepath, filename=filename, save_top_k=-1)

trainer = Trainer(
default_root_dir=tmpdir,
max_epochs=5,
checkpoint_callback=checkpoint
)
trainer.fit(model)

if filename is None:
filename = '{epoch}'

assert os.path.relpath(trainer.ckpt_path, tmpdir) == tgt_dir
assert os.path.relpath(checkpoint.dirpath, tmpdir) == tgt_dir
assert os.path.exists(os.path.join(trainer.ckpt_path, tgt_filename))


def test_pickling(tmpdir):
ckpt = ModelCheckpoint(tmpdir)

Expand Down