def test_model_checkpoint_options(tmp_path): """Test ModelCheckpoint options.""" def mock_save_function(filepath): open(filepath, 'a').close() hparams = tutils.get_hparams() _ = LightningTestModel(hparams) # simulated losses save_dir = tmp_path / "1" save_dir.mkdir() losses = [10, 9, 2.8, 5, 2.5] # ----------------- # CASE K=-1 (all) w = ModelCheckpoint(save_dir, save_top_k=-1, verbose=1) w.save_function = mock_save_function for i, loss in enumerate(losses): w.on_epoch_end(i, logs={'val_loss': loss}) file_lists = set(os.listdir(save_dir)) assert len(file_lists) == len( losses), "Should save all models when save_top_k=-1" # verify correct naming for i in range(0, len(losses)): assert f'_ckpt_epoch_{i}.ckpt' in file_lists save_dir = tmp_path / "2" save_dir.mkdir() # ----------------- # CASE K=0 (none) w = ModelCheckpoint(save_dir, save_top_k=0, verbose=1) w.save_function = mock_save_function for i, loss in enumerate(losses): w.on_epoch_end(i, logs={'val_loss': loss}) file_lists = os.listdir(save_dir) assert len(file_lists) == 0, "Should save 0 models when save_top_k=0" save_dir = tmp_path / "3" save_dir.mkdir() # ----------------- # CASE K=1 (2.5, epoch 4) w = ModelCheckpoint(save_dir, save_top_k=1, verbose=1, prefix='test_prefix') w.save_function = mock_save_function for i, loss in enumerate(losses): w.on_epoch_end(i, logs={'val_loss': loss}) file_lists = set(os.listdir(save_dir)) assert len(file_lists) == 1, "Should save 1 model when save_top_k=1" assert 'test_prefix_ckpt_epoch_4.ckpt' in file_lists save_dir = tmp_path / "4" save_dir.mkdir() # ----------------- # CASE K=2 (2.5 epoch 4, 2.8 epoch 2) # make sure other files don't get deleted w = ModelCheckpoint(save_dir, save_top_k=2, verbose=1) open(f'{save_dir}/other_file.ckpt', 'a').close() w.save_function = mock_save_function for i, loss in enumerate(losses): w.on_epoch_end(i, logs={'val_loss': loss}) file_lists = set(os.listdir(save_dir)) assert len(file_lists) == 3, 'Should save 2 model when save_top_k=2' assert '_ckpt_epoch_4.ckpt' in file_lists assert '_ckpt_epoch_2.ckpt' in file_lists assert 'other_file.ckpt' in file_lists save_dir = tmp_path / "5" save_dir.mkdir() # ----------------- # CASE K=4 (save all 4 models) # multiple checkpoints within same epoch w = ModelCheckpoint(save_dir, save_top_k=4, verbose=1) w.save_function = mock_save_function for loss in losses: w.on_epoch_end(0, logs={'val_loss': loss}) file_lists = set(os.listdir(save_dir)) assert len( file_lists ) == 4, 'Should save all 4 models when save_top_k=4 within same epoch' save_dir = tmp_path / "6" save_dir.mkdir() # ----------------- # CASE K=3 (save the 2nd, 3rd, 4th model) # multiple checkpoints within same epoch w = ModelCheckpoint(save_dir, save_top_k=3, verbose=1) w.save_function = mock_save_function for loss in losses: w.on_epoch_end(0, logs={'val_loss': loss}) file_lists = set(os.listdir(save_dir)) assert len(file_lists) == 3, 'Should save 3 models when save_top_k=3' assert '_ckpt_epoch_0_v2.ckpt' in file_lists assert '_ckpt_epoch_0_v1.ckpt' in file_lists assert '_ckpt_epoch_0.ckpt' in file_lists