Classification trainer: restore optimizer/scheduler state on resume - #63
Open
mohsayed27 wants to merge 1 commit into
Open
Classification trainer: restore optimizer/scheduler state on resume#63mohsayed27 wants to merge 1 commit into
mohsayed27 wants to merge 1 commit into
Conversation
Trainer_Classification.train() always called build_optimizer()/build_scheduler() fresh, so Adam's momentum and the scheduler's LR-schedule state silently reset on every resume even though _save_checkpoint() had been writing them into every checkpoint all along -- only the epoch counter was ever restored. Port localization_trainer.py's build_trainer(..., resume_from=...) pattern: Trainer_Classification.load() gains resume_training=True, which builds a fresh optimizer/scheduler from config and restores their state via the existing load_checkpoint() plumbing, stashing the result in self._resume_state. Trainer_Classification.train() gains a resume_from parameter that reuses that state instead of rebuilding it, seeds best-metric tracking, continues from the saved epoch, and guards against resuming a checkpoint that already reached TRAIN.epochs. apis/classification.py wires weights= on ClassificationModel.train() to request resume_training the same way LocalizationModel.train() already does for weights= on its own train(). No behavior change for fresh training (resume_from=None keeps the original build_optimizer()/build_scheduler() path byte-for-byte) or for load_weights() /infer() (resume_training defaults to False there too). Adds tests/test_classification_trainer_resume.py, which trains a real epoch, saves, and asserts the resumed Adam/StepLR state_dict matches the checkpoint rather than a freshly-initialized optimizer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Trainer_Classification.train()always calledbuild_optimizer()/build_scheduler()fresh, so Adam's momentum and the scheduler's LR-schedule state silently reset on every resume — even though_save_checkpoint()had been writing both into every checkpoint all along. Only the epoch counter was ever actually restored.localization_trainer.py'sbuild_trainer(..., resume_from=...)already solves this correctly for the localization task. This PR ports that pattern to the classification trainer:Trainer_Classification.load()gainsresume_training=False; whenTrue, it rebuilds the optimizer/scheduler from config and restores their state via the existingload_checkpoint()plumbing (which already supported this — it just had no caller wiring it up for classification), stashing the result inself._resume_state.Trainer_Classification.train()gainsresume_from=None; when given, it reuses that optimizer/scheduler instead of rebuilding them, continues from the saved epoch, seeds best-metric tracking, and raises a clear error if the checkpoint already reachedTRAIN.epochs(mirroring localization's guard).apis/classification.pywiresweights=onClassificationModel.train()to requestresume_training, the same wayLocalizationModel.train()already treatsweights=as a resume trigger — no new public API surface.Fresh training (
resume_from=None) is unchanged — verified byte-for-byte identical code path — and so isload_weights()/inference (resume_trainingdefaults toFalsethere too).Test plan
tests/test_classification_trainer_resume.py: trains one real epoch with a real Adam optimizer, saves via_save_checkpoint, resumes, and asserts the resumed optimizer'sstate_dict()["state"]matches the checkpoint's saved momentum buffers field-by-field (while a freshly-built optimizer over the same model has empty state) — proves actual restoration, not just "doesn't crash."TypeErroron the new keyword arguments, confirming they're genuinely exercising the new code.maintip after rebasing.🤖 Generated with Claude Code