open-mmlab / mmsegmentation

OpenMMLab Semantic Segmentation Toolbox and Benchmark.
https://mmsegmentation.readthedocs.io/en/main/
Apache License 2.0
8.04k stars 2.58k forks source link

i want to save best model with 'optimizer', 'param_schedulers' #3742

Open Md-Sayeed-Khan opened 2 months ago

0xD4rky commented 1 month ago

saving the best model along with the optimizer state and parameter schedulers typically involves configuring the training pipeline and checkpointing mechanisms.

suppose this is your sample training script which saves the best optimizer state and scheduler.

train_segmentor(config_file, 
                 checkpoint_config=dict(
                     interval=1,
                     out_dir='./checkpoints',
                     save_optimizer=True,
                     save_scheduler=True,
                     max_keep_ckpts=3
                 ),
                 evaluation=dict(
                     interval=1,
                     metric='mIoU',
                     save_best='mIoU'
                 ))

this is how you will save your checkpoint using different utilities of any openmmlab's toolkit.

from mmcv.runner import save_checkpoint
from mmseg.models import build_segmentor
from mmseg.datasets import build_dataset
from mmseg.apis import init_segmentor

checkpoint_path = 'path/to/your/manual_checkpoint.pth'
save_checkpoint(
    model,
    checkpoint_path,
    optimizer=model.optimizer,
    meta={'epoch': 10, 'iter': 1000}  # Example meta information
)