mmengine/tests/test_hooks/test_param_scheduler_hook.py

187 lines
6.4 KiB
Python
Raw Normal View History

# Copyright (c) OpenMMLab. All rights reserved.
import copy
from unittest.mock import Mock
from mmengine.hooks import ParamSchedulerHook
[Feature] Support ReduceOnPlateauParamScheduler(#819) * [Feature] Add ReduceOnPlateauParamScheduler and change ParamSchedulerHook * [Feature] add ReduceOnPlateauLR and ReduceOnPlateauMomentum * pre-commit check * add a little docs * change position * fix the conflict between isort and yapf * fix ParamSchedulerHook after_val_epoch execute without train_loop and param_schedulers built * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * update ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ParamSchedulerHook * fix get need_step_args attribute error in ParamSchedulerHook * fix load_state_dict error for rule in ReduceOnPlateauParamScheduler * add docs for ParamSchedulerHook and fix a few codes * [Docs] add ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ReduceOnPlateauLR docs * [Refactor] adjust the order of import * [Fix] add init check for threshold in ReduceOnPlateauParamScheduler * [Test] add test for ReduceOnPlateauParamScheduler, ReduceOnPlateauLR and ReduceOnPlateauMomentum * [Fix] fix no attribute self.min_value * [Fix] fix numerical problem in tests * [Fix] fix error in tests * [Fix] fix ignore first param in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] increase coverage * [Fix] fix count self._global_step bug and docs * [Fix] fix tests * [Fix] modified ParamSchedulerHook test * Update mmengine/optim/scheduler/param_scheduler.py Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * [Fix] modified something according to commented * [Docs] add api for en and zh_cn * [Fix] fix bug in test_param_scheduler_hook.py * [Test] support more complicated test modes(less, greater, rel, abs) for ReduceOnPlateauParamScheduler * [Docs] add docs for rule * [Fix] fix pop from empty list bug in test * [Fix] fix check param_schedulers is not built bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix scheduler type bug * [Test] rename step_args to step_kwargs * [Fix] remove redundancy check * [Test] remove redundancy check * Apply suggestions from code review Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> * [Test] fix some defects Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
2023-01-16 11:39:03 +08:00
from mmengine.optim import _ParamScheduler
from mmengine.testing import RunnerTestCase
class TestParamSchedulerHook(RunnerTestCase):
error_msg = ('runner.param_schedulers should be list of ParamScheduler or '
'a dict containing list of ParamScheduler')
[Feature] Support ReduceOnPlateauParamScheduler(#819) * [Feature] Add ReduceOnPlateauParamScheduler and change ParamSchedulerHook * [Feature] add ReduceOnPlateauLR and ReduceOnPlateauMomentum * pre-commit check * add a little docs * change position * fix the conflict between isort and yapf * fix ParamSchedulerHook after_val_epoch execute without train_loop and param_schedulers built * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * update ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ParamSchedulerHook * fix get need_step_args attribute error in ParamSchedulerHook * fix load_state_dict error for rule in ReduceOnPlateauParamScheduler * add docs for ParamSchedulerHook and fix a few codes * [Docs] add ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ReduceOnPlateauLR docs * [Refactor] adjust the order of import * [Fix] add init check for threshold in ReduceOnPlateauParamScheduler * [Test] add test for ReduceOnPlateauParamScheduler, ReduceOnPlateauLR and ReduceOnPlateauMomentum * [Fix] fix no attribute self.min_value * [Fix] fix numerical problem in tests * [Fix] fix error in tests * [Fix] fix ignore first param in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] increase coverage * [Fix] fix count self._global_step bug and docs * [Fix] fix tests * [Fix] modified ParamSchedulerHook test * Update mmengine/optim/scheduler/param_scheduler.py Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * [Fix] modified something according to commented * [Docs] add api for en and zh_cn * [Fix] fix bug in test_param_scheduler_hook.py * [Test] support more complicated test modes(less, greater, rel, abs) for ReduceOnPlateauParamScheduler * [Docs] add docs for rule * [Fix] fix pop from empty list bug in test * [Fix] fix check param_schedulers is not built bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix scheduler type bug * [Test] rename step_args to step_kwargs * [Fix] remove redundancy check * [Test] remove redundancy check * Apply suggestions from code review Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> * [Test] fix some defects Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
2023-01-16 11:39:03 +08:00
def test_after_train_iter(self):
# runner.param_schedulers should be a list or dict
with self.assertRaisesRegex(TypeError, self.error_msg):
hook = ParamSchedulerHook()
runner = Mock()
scheduler = Mock()
scheduler.step = Mock()
scheduler.by_epoch = False
runner.param_schedulers = scheduler
hook.after_train_iter(runner, 0)
scheduler.step.assert_called()
# runner.param_schedulers is a list of schedulers
hook = ParamSchedulerHook()
runner = Mock()
scheduler = Mock()
scheduler.step = Mock()
scheduler.by_epoch = False
runner.param_schedulers = [scheduler]
hook.after_train_iter(runner, 0)
scheduler.step.assert_called()
# runner.param_schedulers is a dict containing list of schedulers
scheduler1 = Mock()
scheduler1.step = Mock()
scheduler1.by_epoch = False
scheduler2 = Mock()
scheduler2.step = Mock()
scheduler2.by_epoch = False
runner.param_schedulers = dict(key1=[scheduler1], key2=[scheduler2])
hook.after_train_epoch(runner)
hook.after_train_iter(runner, 0)
[Feature] Support ReduceOnPlateauParamScheduler(#819) * [Feature] Add ReduceOnPlateauParamScheduler and change ParamSchedulerHook * [Feature] add ReduceOnPlateauLR and ReduceOnPlateauMomentum * pre-commit check * add a little docs * change position * fix the conflict between isort and yapf * fix ParamSchedulerHook after_val_epoch execute without train_loop and param_schedulers built * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * update ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ParamSchedulerHook * fix get need_step_args attribute error in ParamSchedulerHook * fix load_state_dict error for rule in ReduceOnPlateauParamScheduler * add docs for ParamSchedulerHook and fix a few codes * [Docs] add ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ReduceOnPlateauLR docs * [Refactor] adjust the order of import * [Fix] add init check for threshold in ReduceOnPlateauParamScheduler * [Test] add test for ReduceOnPlateauParamScheduler, ReduceOnPlateauLR and ReduceOnPlateauMomentum * [Fix] fix no attribute self.min_value * [Fix] fix numerical problem in tests * [Fix] fix error in tests * [Fix] fix ignore first param in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] increase coverage * [Fix] fix count self._global_step bug and docs * [Fix] fix tests * [Fix] modified ParamSchedulerHook test * Update mmengine/optim/scheduler/param_scheduler.py Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * [Fix] modified something according to commented * [Docs] add api for en and zh_cn * [Fix] fix bug in test_param_scheduler_hook.py * [Test] support more complicated test modes(less, greater, rel, abs) for ReduceOnPlateauParamScheduler * [Docs] add docs for rule * [Fix] fix pop from empty list bug in test * [Fix] fix check param_schedulers is not built bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix scheduler type bug * [Test] rename step_args to step_kwargs * [Fix] remove redundancy check * [Test] remove redundancy check * Apply suggestions from code review Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> * [Test] fix some defects Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
2023-01-16 11:39:03 +08:00
scheduler1.step.assert_called()
scheduler2.step.assert_called()
[Feature] Support ReduceOnPlateauParamScheduler(#819) * [Feature] Add ReduceOnPlateauParamScheduler and change ParamSchedulerHook * [Feature] add ReduceOnPlateauLR and ReduceOnPlateauMomentum * pre-commit check * add a little docs * change position * fix the conflict between isort and yapf * fix ParamSchedulerHook after_val_epoch execute without train_loop and param_schedulers built * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * update ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ParamSchedulerHook * fix get need_step_args attribute error in ParamSchedulerHook * fix load_state_dict error for rule in ReduceOnPlateauParamScheduler * add docs for ParamSchedulerHook and fix a few codes * [Docs] add ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ReduceOnPlateauLR docs * [Refactor] adjust the order of import * [Fix] add init check for threshold in ReduceOnPlateauParamScheduler * [Test] add test for ReduceOnPlateauParamScheduler, ReduceOnPlateauLR and ReduceOnPlateauMomentum * [Fix] fix no attribute self.min_value * [Fix] fix numerical problem in tests * [Fix] fix error in tests * [Fix] fix ignore first param in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] increase coverage * [Fix] fix count self._global_step bug and docs * [Fix] fix tests * [Fix] modified ParamSchedulerHook test * Update mmengine/optim/scheduler/param_scheduler.py Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * [Fix] modified something according to commented * [Docs] add api for en and zh_cn * [Fix] fix bug in test_param_scheduler_hook.py * [Test] support more complicated test modes(less, greater, rel, abs) for ReduceOnPlateauParamScheduler * [Docs] add docs for rule * [Fix] fix pop from empty list bug in test * [Fix] fix check param_schedulers is not built bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix scheduler type bug * [Test] rename step_args to step_kwargs * [Fix] remove redundancy check * [Test] remove redundancy check * Apply suggestions from code review Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> * [Test] fix some defects Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
2023-01-16 11:39:03 +08:00
def test_after_train_epoch(self):
# runner.param_schedulers should be a list or dict
with self.assertRaisesRegex(TypeError, self.error_msg):
hook = ParamSchedulerHook()
runner = Mock()
scheduler = Mock()
scheduler.step = Mock()
scheduler.by_epoch = True
runner.param_schedulers = scheduler
[Feature] Support ReduceOnPlateauParamScheduler(#819) * [Feature] Add ReduceOnPlateauParamScheduler and change ParamSchedulerHook * [Feature] add ReduceOnPlateauLR and ReduceOnPlateauMomentum * pre-commit check * add a little docs * change position * fix the conflict between isort and yapf * fix ParamSchedulerHook after_val_epoch execute without train_loop and param_schedulers built * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * update ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ParamSchedulerHook * fix get need_step_args attribute error in ParamSchedulerHook * fix load_state_dict error for rule in ReduceOnPlateauParamScheduler * add docs for ParamSchedulerHook and fix a few codes * [Docs] add ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ReduceOnPlateauLR docs * [Refactor] adjust the order of import * [Fix] add init check for threshold in ReduceOnPlateauParamScheduler * [Test] add test for ReduceOnPlateauParamScheduler, ReduceOnPlateauLR and ReduceOnPlateauMomentum * [Fix] fix no attribute self.min_value * [Fix] fix numerical problem in tests * [Fix] fix error in tests * [Fix] fix ignore first param in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] increase coverage * [Fix] fix count self._global_step bug and docs * [Fix] fix tests * [Fix] modified ParamSchedulerHook test * Update mmengine/optim/scheduler/param_scheduler.py Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * [Fix] modified something according to commented * [Docs] add api for en and zh_cn * [Fix] fix bug in test_param_scheduler_hook.py * [Test] support more complicated test modes(less, greater, rel, abs) for ReduceOnPlateauParamScheduler * [Docs] add docs for rule * [Fix] fix pop from empty list bug in test * [Fix] fix check param_schedulers is not built bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix scheduler type bug * [Test] rename step_args to step_kwargs * [Fix] remove redundancy check * [Test] remove redundancy check * Apply suggestions from code review Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> * [Test] fix some defects Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
2023-01-16 11:39:03 +08:00
hook.after_train_epoch(runner)
scheduler.step.assert_called()
# runner.param_schedulers is a list of schedulers
hook = ParamSchedulerHook()
runner = Mock()
scheduler = Mock()
scheduler.step = Mock()
scheduler.by_epoch = True
runner.param_schedulers = [scheduler]
hook.after_train_epoch(runner)
scheduler.step.assert_called()
# runner.param_schedulers is a dict containing list of schedulers
scheduler1 = Mock()
scheduler1.step = Mock()
scheduler1.by_epoch = True
scheduler2 = Mock()
scheduler2.step = Mock()
scheduler2.by_epoch = True
runner.param_schedulers = dict(key1=[scheduler1], key2=[scheduler2])
hook.after_train_epoch(runner)
scheduler1.step.assert_called()
scheduler2.step.assert_called()
[Feature] Support ReduceOnPlateauParamScheduler(#819) * [Feature] Add ReduceOnPlateauParamScheduler and change ParamSchedulerHook * [Feature] add ReduceOnPlateauLR and ReduceOnPlateauMomentum * pre-commit check * add a little docs * change position * fix the conflict between isort and yapf * fix ParamSchedulerHook after_val_epoch execute without train_loop and param_schedulers built * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * update ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ParamSchedulerHook * fix get need_step_args attribute error in ParamSchedulerHook * fix load_state_dict error for rule in ReduceOnPlateauParamScheduler * add docs for ParamSchedulerHook and fix a few codes * [Docs] add ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ReduceOnPlateauLR docs * [Refactor] adjust the order of import * [Fix] add init check for threshold in ReduceOnPlateauParamScheduler * [Test] add test for ReduceOnPlateauParamScheduler, ReduceOnPlateauLR and ReduceOnPlateauMomentum * [Fix] fix no attribute self.min_value * [Fix] fix numerical problem in tests * [Fix] fix error in tests * [Fix] fix ignore first param in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] increase coverage * [Fix] fix count self._global_step bug and docs * [Fix] fix tests * [Fix] modified ParamSchedulerHook test * Update mmengine/optim/scheduler/param_scheduler.py Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * [Fix] modified something according to commented * [Docs] add api for en and zh_cn * [Fix] fix bug in test_param_scheduler_hook.py * [Test] support more complicated test modes(less, greater, rel, abs) for ReduceOnPlateauParamScheduler * [Docs] add docs for rule * [Fix] fix pop from empty list bug in test * [Fix] fix check param_schedulers is not built bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix scheduler type bug * [Test] rename step_args to step_kwargs * [Fix] remove redundancy check * [Test] remove redundancy check * Apply suggestions from code review Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> * [Test] fix some defects Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
2023-01-16 11:39:03 +08:00
def test_after_val_epoch(self):
metrics = dict(loss=1.0)
# mock super _ParamScheduler class
class MockParamScheduler(_ParamScheduler):
def __init__(self):
pass
def _get_value(self):
pass
# runner.param_schedulers should be a list or dict
with self.assertRaisesRegex(TypeError, self.error_msg):
[Feature] Support ReduceOnPlateauParamScheduler(#819) * [Feature] Add ReduceOnPlateauParamScheduler and change ParamSchedulerHook * [Feature] add ReduceOnPlateauLR and ReduceOnPlateauMomentum * pre-commit check * add a little docs * change position * fix the conflict between isort and yapf * fix ParamSchedulerHook after_val_epoch execute without train_loop and param_schedulers built * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * update ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ParamSchedulerHook * fix get need_step_args attribute error in ParamSchedulerHook * fix load_state_dict error for rule in ReduceOnPlateauParamScheduler * add docs for ParamSchedulerHook and fix a few codes * [Docs] add ReduceOnPlateauParamScheduler, ReduceOnPlateauMomentum and ReduceOnPlateauLR docs * [Refactor] adjust the order of import * [Fix] add init check for threshold in ReduceOnPlateauParamScheduler * [Test] add test for ReduceOnPlateauParamScheduler, ReduceOnPlateauLR and ReduceOnPlateauMomentum * [Fix] fix no attribute self.min_value * [Fix] fix numerical problem in tests * [Fix] fix error in tests * [Fix] fix ignore first param in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] fix bug in tests * [Fix] increase coverage * [Fix] fix count self._global_step bug and docs * [Fix] fix tests * [Fix] modified ParamSchedulerHook test * Update mmengine/optim/scheduler/param_scheduler.py Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> * [Fix] modified something according to commented * [Docs] add api for en and zh_cn * [Fix] fix bug in test_param_scheduler_hook.py * [Test] support more complicated test modes(less, greater, rel, abs) for ReduceOnPlateauParamScheduler * [Docs] add docs for rule * [Fix] fix pop from empty list bug in test * [Fix] fix check param_schedulers is not built bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix step_args bug and without runner._train_loop bug * [Fix] fix scheduler type bug * [Test] rename step_args to step_kwargs * [Fix] remove redundancy check * [Test] remove redundancy check * Apply suggestions from code review Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> * [Test] fix some defects Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
2023-01-16 11:39:03 +08:00
hook = ParamSchedulerHook()
runner = Mock()
scheduler = Mock()
scheduler.step = Mock()
scheduler.by_epoch = True
scheduler.need_val_args = True
runner.param_schedulers = scheduler
hook.after_val_epoch(runner, metrics)
# runner.param_schedulers is a list of schedulers
hook = ParamSchedulerHook()
runner = Mock()
scheduler = MockParamScheduler()
scheduler.step = Mock()
scheduler.by_epoch = True
scheduler.need_val_args = True
runner.param_schedulers = [scheduler]
hook.after_val_epoch(runner, metrics)
scheduler.step.assert_called_with(metrics)
# runner.param_schedulers is a dict containing list of schedulers
scheduler1 = MockParamScheduler()
scheduler1.step = Mock()
scheduler1.by_epoch = True
scheduler1.need_val_args = True
scheduler2 = MockParamScheduler()
scheduler2.step = Mock()
scheduler2.by_epoch = True
scheduler2.need_val_args = True
runner.param_schedulers = dict(key1=[scheduler1], key2=[scheduler2])
hook.after_val_epoch(runner, metrics)
scheduler1.step.assert_called_with(metrics)
scheduler2.step.assert_called_with(metrics)
def test_with_runner(self):
cfg = copy.deepcopy(self.epoch_based_cfg)
cfg.train_cfg.max_epochs = 3
cfg.param_scheduler = [
dict(
type='ConstantLR',
factor=0.5,
begin=0,
),
dict(
type='ConstantLR',
factor=0.5,
begin=1,
)
]
init_lr = cfg.optim_wrapper.optimizer.lr
runner = self.build_runner(cfg)
runner.train()
# Length of train log is 4
# Learning rate of the first epoch is init_lr*0.5
# Learning rate of the second epoch is init_lr*0.5*0.5
# Learning rate of the last epoch will be reset to 0.1
train_lr = list(runner.message_hub.get_scalar('train/lr')._log_history)
target_lr = [init_lr * 0.5] * 4 + \
[init_lr * 0.5 * 0.5] * 4 + \
[init_lr] * 4
self.assertListEqual(train_lr, target_lr)
cfg = copy.deepcopy(self.iter_based_cfg)
cfg.param_scheduler = [
dict(
type='ConstantLR',
factor=0.5,
begin=0,
by_epoch=False,
),
dict(
type='ConstantLR',
factor=0.5,
begin=4,
by_epoch=False,
)
]
init_lr = cfg.optim_wrapper.optimizer.lr
runner = self.build_runner(cfg)
runner.train()
# Learning rate of 1-4 iteration is init_lr*0.5
# Learning rate of 5-11 iteration is init_lr*0.5*0.5
train_lr = list(runner.message_hub.get_scalar('train/lr')._log_history)
target_lr = [init_lr * 0.5] * 4 + \
[init_lr * 0.5 * 0.5] * 7 + \
[init_lr]
self.assertListEqual(train_lr, target_lr)