def test_callbacks_restore_backbone(tmp_path):
    """Test callbacks restore is called after optimizers have been re-created but before optimizer states reload."""

    ckpt = ModelCheckpoint(dirpath=tmp_path, save_last=True)
    trainer = Trainer(
        default_root_dir=tmp_path,
        limit_train_batches=1,
        limit_val_batches=1,
        max_epochs=2,
        enable_progress_bar=False,
        callbacks=[ckpt, BackboneFinetuning(unfreeze_backbone_at_epoch=1)],
    )
    trainer.fit(BackboneBoringModel())

    # initialize a trainer that continues the previous training
    trainer = Trainer(
        default_root_dir=tmp_path,
        limit_train_batches=1,
        limit_val_batches=1,
        max_epochs=3,
        enable_progress_bar=False,
        callbacks=BackboneFinetuning(unfreeze_backbone_at_epoch=1),
    )
    trainer.fit(BackboneBoringModel(), ckpt_path=ckpt.last_model_path)
Test callbacks restore is called after optimizers have been re-created but before optimizer states reload.
tests/tests_pytorch/callbacks/test_finetuning_callback.py