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

Adds the option of saving the last model on checkpoint #1908

Merged
merged 5 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion pytorch_lightning/callbacks/model_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ModelCheckpoint(Callback):

monitor: quantity to monitor.
verbose: verbosity mode. Default: ``False``.
save_last: always saves the model at the end of the epoch. Default: ``False``.
save_top_k: if `save_top_k == k`,
awaelchli marked this conversation as resolved.
Show resolved Hide resolved
the best k models according to
the quantity monitored will be saved.
Expand Down Expand Up @@ -83,7 +84,7 @@ class ModelCheckpoint(Callback):
"""

def __init__(self, filepath: Optional[str] = None, monitor: str = 'val_loss', verbose: bool = False,
save_top_k: int = 1, save_weights_only: bool = False,
save_last: bool = False, save_top_k: int = 1, save_weights_only: bool = False,
mode: str = 'auto', period: int = 1, prefix: str = ''):
super().__init__()
if save_top_k > 0 and filepath is not None and os.path.isdir(filepath) and len(os.listdir(filepath)) > 0:
Expand All @@ -103,6 +104,7 @@ def __init__(self, filepath: Optional[str] = None, monitor: str = 'val_loss', ve
else:
self.dirpath, self.filename = os.path.split(filepath)
os.makedirs(self.dirpath, exist_ok=True)
self.save_last = save_last
self.save_top_k = save_top_k
self.save_weights_only = save_weights_only
self.period = period
Expand Down Expand Up @@ -217,6 +219,10 @@ def on_validation_end(self, trainer, pl_module):

self.epoch_last_check = epoch

if self.save_last:
filepath = os.path.join(self.dirpath, self.prefix + 'last.ckpt')
self._save_model(filepath)

filepath = self.format_checkpoint_name(epoch, metrics)
version_cnt = 0
while os.path.isfile(filepath):
Expand Down
19 changes: 11 additions & 8 deletions tests/trainer/test_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,21 @@ def test_dp_output_reduce():
assert reduced['b']['c'] == out['b']['c']


@pytest.mark.parametrize(["save_top_k", "file_prefix", "expected_files"], [
pytest.param(-1, '', {'epoch=4.ckpt', 'epoch=3.ckpt', 'epoch=2.ckpt', 'epoch=1.ckpt', 'epoch=0.ckpt'},
@pytest.mark.parametrize(["save_top_k", "save_last", "file_prefix", "expected_files"], [
pytest.param(-1, False, '', {'epoch=4.ckpt', 'epoch=3.ckpt', 'epoch=2.ckpt', 'epoch=1.ckpt', 'epoch=0.ckpt'},
id="CASE K=-1 (all)"),
pytest.param(1, 'test_prefix_', {'test_prefix_epoch=4.ckpt'},
pytest.param(1, False, 'test_prefix_', {'test_prefix_epoch=4.ckpt'},
id="CASE K=1 (2.5, epoch 4)"),
pytest.param(2, '', {'epoch=4.ckpt', 'epoch=2.ckpt'},
pytest.param(2, False, '', {'epoch=4.ckpt', 'epoch=2.ckpt'},
id="CASE K=2 (2.5 epoch 4, 2.8 epoch 2)"),
pytest.param(4, '', {'epoch=1.ckpt', 'epoch=4.ckpt', 'epoch=3.ckpt', 'epoch=2.ckpt'},
pytest.param(4, False, '', {'epoch=1.ckpt', 'epoch=4.ckpt', 'epoch=3.ckpt', 'epoch=2.ckpt'},
id="CASE K=4 (save all 4 base)"),
pytest.param(3, '', {'epoch=2.ckpt', 'epoch=3.ckpt', 'epoch=4.ckpt'},
pytest.param(3, False, '', {'epoch=2.ckpt', 'epoch=3.ckpt', 'epoch=4.ckpt'},
id="CASE K=3 (save the 2nd, 3rd, 4th model)"),
pytest.param(1, True, '', {'epoch=4.ckpt', 'last.ckpt'},
id="CASE K=3 (save the 2nd, 3rd, 4th model)"),
lgvaz marked this conversation as resolved.
Show resolved Hide resolved
])
def test_model_checkpoint_options(tmpdir, save_top_k, file_prefix, expected_files):
def test_model_checkpoint_options(tmpdir, save_top_k, save_last, file_prefix, expected_files):
"""Test ModelCheckpoint options."""

def mock_save_function(filepath, *args):
Expand All @@ -276,7 +278,8 @@ def mock_save_function(filepath, *args):
# simulated losses
losses = [10, 9, 2.8, 5, 2.5]

checkpoint_callback = ModelCheckpoint(tmpdir, save_top_k=save_top_k, prefix=file_prefix, verbose=1)
checkpoint_callback = ModelCheckpoint(tmpdir, save_top_k=save_top_k, save_last=save_last,
prefix=file_prefix, verbose=1)
checkpoint_callback.save_function = mock_save_function
awaelchli marked this conversation as resolved.
Show resolved Hide resolved
trainer = Trainer()

Expand Down