How to store best models checkpoints, not only newest 5, in Tensorflow Object Detection API?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When training object detection models, saving only the latest checkpoints is often not enough. The most recent checkpoint might not be the best one, especially if validation metrics fluctuate late in training. A better strategy is to keep both recent recovery checkpoints and top performing checkpoints ranked by evaluation metrics.
Why Latest Checkpoints and Best Checkpoints Solve Different Problems
Latest checkpoints help you resume interrupted training quickly. Best checkpoints help you deploy a model that actually performs well on validation data. These are separate goals and should be managed separately.
In TensorFlow workflows, teams usually need both:
- rolling checkpoints for fault tolerance
- metric ranked checkpoints for model selection
If you rely on only one policy, you usually lose either recoverability or quality.
Baseline Checkpoint Management in TensorFlow
tf.train.CheckpointManager provides rolling retention with max_to_keep. This is useful for saving the newest checkpoints during training.
After seven saves, only the newest five remain in ckpts/latest.
Keep Top K Checkpoints by Validation Metric
To preserve best models, add a second retention flow keyed by validation score. The simple pattern below copies checkpoint files to a dedicated best folder and tracks ranking in JSON.
Use this keeper right after each evaluation pass.
Integrating with TensorFlow Object Detection API
Object Detection API training often uses separate training and evaluation binaries. A practical setup is:
- training job writes rolling checkpoints in one directory
- evaluation job computes
mAPfor each checkpoint - evaluation callback copies top checkpoints to best directory
If you use a custom training script, you can run evaluation every N steps and call consider directly. If you use pipeline based scripts, parse evaluator output and trigger retention logic as a post evaluation step.
Keep the metric name stable. If you switch between [email protected] and [email protected]:0.95 during one run, ranking becomes inconsistent.
Deployment Workflow with Best Checkpoints
At deployment time, load best checkpoint metadata and choose top ranked candidate. Optionally keep top two for A B comparisons.
Then restore with your checkpoint object using the selected prefix.
Common Pitfalls
A common mistake is overwriting the same best checkpoint name repeatedly. Use unique checkpoint prefixes or include global step in file names so ranking logic can distinguish candidates.
Another issue is comparing metrics from different validation datasets. If dataset or preprocessing changes, old and new scores are not comparable, so top K selection becomes misleading.
A third issue is keeping only best checkpoints and deleting all recent checkpoints. If training crashes, you may lose resume capability. Keep a rolling latest directory and a separate best directory.
Teams also forget to store metadata about metric type and evaluation config. Without that context, future readers cannot trust score comparisons.
Summary
- Keep latest and best checkpoints separately because they serve different goals
- Use
CheckpointManagerfor rolling retention and custom ranking for best models - Rank checkpoints with one stable validation metric
- Preserve metadata so checkpoint scores are interpretable later
- Maintain both recoverability and model quality in the same training pipeline
Related reading
- How to suppress all autograph warnings from Tensorflow?
- How to suppress specific warning in Tensorflow Python
- How to Suppress Tensorflow warning displayed in result
- How to suppress verbose Tensorflow logging?
- How to test one single image in pytorch
- How to train an artificial neural network to play Diablo 2 using visual input?
- How to store neural network knowledge data?
- how to store numpy arrays as tfrecord?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.