自动保存Bestmodel

1、修改训练方式为epoch

在mmseg的工程使用中,一般情况默认训练的次数是按照inter去计算的,比如swin中160000个inter,每4000次inter进行一次模型验证,并保存一次模型,这样的计算方式有时不能直接满足按epoch计算的训练方式。本用epoch来验证和保存模型,那么只需要修改config中的一处既可以。接下来就代码展示:

inter训练方式

runner = dict(type='IterBasedRunner', max_iters=160000)
#共有160000次inter
checkpoint_config = dict(by_epoch=False, interval=4000)
#每4000个inter保存一次模型
evaluation = dict(interval=4000, metric='mIoU', pre_eval=True)
#每4000个inter计算一次验证
  • epoch的训练方式
    runner = dict(type='EpochBasedRunner', max_epochs=100)
    #共100个epoch
    checkpoint_config = dict(by_epoch=True, interval=1)
    #每一次epoch都保存一次模型
    evaluation = dict(interval=1, metric='mIoU', pre_eval=True)
    #每一次epoch都计算一次验证

2、保存最好的模型

无论是每一次epoch都保存模型还是按照inter,每4000次inter都保存一次模型的方式,要想找到最优模型,还得自己去对着log文件找,太麻烦了。这个问题也可以简单解决,找到mmseg/apis/train.py,找到runner.register_hook这里,修改原有的钩子定义即可。
 

runner.register_hook(eval_hook(val_dataloader, **eval_cfg), priority='LOW')
#原来的,没有保存bestmodel
runner.register_hook(eval_hook(val_dataloader,save_best='mIoU', **eval_cfg), priority='LOW')
#修改后,自动保存bestmodel

 

 3、

猜你喜欢

转载自blog.csdn.net/weixin_45912366/article/details/129586674