# 迁移 MMCV 参数调度器到 MMEngine MMCV 1.x 版本使用 [LrUpdaterHook](https://mmcv.readthedocs.io/zh_CN/v1.6.0/api.html#mmcv.runner.LrUpdaterHook) 和 [MomentumUpdaterHook](https://mmcv.readthedocs.io/zh_CN/v1.6.0/api.html#mmcv.runner.MomentumUpdaterHook) 来调整学习率和动量。 但随着深度学习算法训练方式的不断发展,使用 Hook 修改学习率已经难以满足更加丰富的自定义需求,因此 MMEngine 提供了参数调度器(ParamScheduler)。 一方面,参数调度器的接口与 PyTroch 的学习率调度器(LRScheduler)对齐,另一方面,参数调度器提供了更丰富的功能,详细请参考[参数调度器使用指南](../tutorials/param_scheduler.md)。 ## 学习率调度器(LrUpdater)迁移 MMEngine 中使用 LRScheduler 替代 LrUpdaterHook,配置文件中的字段从原本的 `lr_config` 修改为 `param_scheduler`。 MMCV 中的学习率配置与 MMEngine 中的参数调度器配置对应关系如下: ### 学习率预热(Warmup)迁移 由于 MMEngine 中的学习率调度器在实现时增加了 begin 和 end 参数,指定了调度器的生效区间,所以可以通过调度器组合的方式实现学习率预热。MMCV 中有 3 种学习率预热方式,分别是 `'constant'`, `'linear'`, `'exp'`,在 MMEngine 中对应的配置应修改为: #### 常数预热(constant)
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict( warmup='constant', warmup_ratio=0.1, warmup_iters=500, warmup_by_epoch=False ) ``` | ```python param_scheduler = [ dict(type='ConstantLR', factor=0.1, begin=0, end=500, by_epoch=False), dict(...) # 主学习率调度器配置 ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict( warmup='linear', warmup_ratio=0.1, warmup_iters=500, warmup_by_epoch=False ) ``` | ```python param_scheduler = [ dict(type='LinearLR', start_factor=0.1, begin=0, end=500, by_epoch=False), dict(...) # 主学习率调度器配置 ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict( warmup='exp', warmup_ratio=0.1, warmup_iters=500, warmup_by_epoch=False ) ``` | ```python param_scheduler = [ dict(type='ExponentialLR', gamma=0.1, begin=0, end=500, by_epoch=False), dict(...) # 主学习率调度器配置 ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict(policy='fixed') ``` | ```python param_scheduler = [ dict(type='ConstantLR', factor=1) ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict( policy='step', step=[8, 11], gamma=0.1, by_epoch=True ) ``` | ```python param_scheduler = [ dict(type='MultiStepLR', milestone=[8, 11], gamma=0.1, by_epoch=True) ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict( policy='poly', power=0.7, min_lr=0.001, by_epoch=True ) ``` | ```python param_scheduler = [ dict(type='PolyLR', power=0.7, eta_min=0.001, begin=0, end=num_epochs, by_epoch=True) ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict( policy='exp', power=0.5, by_epoch=True ) ``` | ```python param_scheduler = [ dict(type='ExponentialLR', gamma=0.5, begin=0, end=num_epochs, by_epoch=True) ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict( policy='CosineAnnealing', min_lr=0.5, by_epoch=True ) ``` | ```python param_scheduler = [ dict(type='CosineAnnealingLR', eta_min=0.5, T_max=num_epochs, begin=0, end=num_epochs, by_epoch=True) ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict( policy='FlatCosineAnnealing', start_percent=0.5, min_lr=0.005, by_epoch=True ) ``` | ```python param_scheduler = [ dict(type='ConstantLR', factor=1, begin=0, end=num_epochs * 0.75) dict(type='CosineAnnealingLR', eta_min=0.005, begin=num_epochs * 0.75, end=num_epochs, T_max=num_epochs * 0.25, by_epoch=True) ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict(policy='CosineRestart', periods=[5, 10, 15], restart_weights=[1, 0.7, 0.3], min_lr=0.001, by_epoch=True) ``` | ```python param_scheduler = [ dict(type='CosineRestartLR', periods=[5, 10, 15], restart_weights=[1, 0.7, 0.3], eta_min=0.001, by_epoch=True) ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict(policy='OneCycle', max_lr=0.02, total_steps=90000, pct_start=0.3, anneal_strategy='cos', div_factor=25, final_div_factor=1e4, three_phase=True, by_epoch=False) ``` | ```python param_scheduler = [ dict(type='OneCycleLR', eta_max=0.02, total_steps=90000, pct_start=0.3, anneal_strategy='cos', div_factor=25, final_div_factor=1e4, three_phase=True, by_epoch=False) ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict( policy='LinearAnnealing', min_lr_ratio=0.01, by_epoch=True ) ``` | ```python param_scheduler = [ dict(type='LinearLR', start_factor=1, end_factor=0.01, begin=0, end=num_epochs, by_epoch=True) ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict(...) momentum_config = dict( policy='CosineAnnealing', min_momentum=0.1, by_epoch=True ) ``` | ```python param_scheduler = [ # 学习率调度器配置 dict(...), # 动量调度器配置 dict(type='CosineAnnealingMomentum', eta_min=0.1, T_max=num_epochs, begin=0, end=num_epochs, by_epoch=True) ] ``` |
MMCV-1.x | MMEngine |
---|---|
```python lr_config = dict( policy='CosineAnnealing', min_lr=0.5, by_epoch=False ) ``` | ```python param_scheduler = [ dict( type='CosineAnnealingLR', eta_min=0.5, T_max=num_epochs, by_epoch=True, # 注意,by_epoch 需要设置为 True convert_to_iter_based=True # 转换为按 iter 更新参数 ) ] ``` |