Where Does Detectron2 Save Trained Models?
When training a model with Detectron2, the model weights and configuration are typically saved to a specified output directory. This directory is set through the configuration object (cfg) before training begins. Here are the key steps to specify and locate the trained model weights:
1. Set the Output Directory
In Detectron2, you can specify the output directory in the configurasion object cfg, typically during the configuration part of your training script. For example:
cfg.OUTPUT_DIR = "./output"
This line saves all output (including trained model weights, log files, and possibly evaluation results) in the output folder under the current working directory.
2. Model Weight Files
After training completes, Detectron2 usually saves the following files to the directory specified by OUTPUT_DIR:
- Model Weights (
.pthfile): These are PyTorch model weight files generated during training. They are saved at each checkpoint and at the end of training. The filename often includes the training iteration number, e.g.,model_final.pthfor the final trained model. - Configuration File: Sometimes the current training configuration is saved as a file (typically
.yamlformat) for reproducibility or further analysis.
3. Locate the Trained Model
If you have configured the output directory as described, after training you can find the model weight file (such as model_final.pth) directly in that directory. For example:
project_directory/
├── output/
│ ├── model_final.pth
│ ├── last_checkpoint
│ └── ...
4. Use the Trained Model
After training, you can use these saved weight files for model evaluation, continued trainign, or actual prediction tasks. Load the model weights typically by:
cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
Ensure that cfg.MODEL.WEIGHTS correctly points to the saved weight file.
Summary
Managing trained models in Detectron2 mainly ivnolves properly configuring the output directory and understanding how model weights and other related files are saved. By setting cfg.OUTPUT_DIR, you control where all output files are stored, ensuring the training outputs are organized and easily accessible. This facilitates model management, deployment, evaluation, and further optimization.