File size: 2,314 Bytes
cc0dd3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright (c) OpenMMLab. All rights reserved.
import operator as op
from typing import Any, Optional, Union

from mmengine.hooks import Hook

from mmpretrain.registry import HOOKS
from mmpretrain.utils import get_ori_model


@HOOKS.register_module()
class WarmupParamHook(Hook):
    """This is a hook used for changing the parameters other than optimizations
    that need to warmup inside the module.

    This hook can extend with more detailed warmup rule if necessary.

    Args:
        param_name (str): The parameter name that needs to be altered.
        module_name (str): Module name that belongs to the model. Such as
            `head`, `head.loss`, etc.
        warmup_epochs (int): The warmup epochs for this parameter.
    """

    def __init__(
        self,
        param_name: str,
        module_name: str,
        warmup_epochs: int,
    ) -> None:
        self.param_name = param_name
        self.warmup_epochs = warmup_epochs
        # getter for module which saves the changed parameter
        self.module_getter = op.attrgetter(module_name)

    def get_param(self, runner) -> Any:
        """Get the parameter."""
        try:
            module = self.module_getter(get_ori_model(runner.model))
            return getattr(module, self.param_name)
        except AttributeError as e:
            raise AttributeError(f'{e}. Please check hook settings.')

    def set_param(self, runner, value) -> None:
        """Set the parameter."""
        try:
            module = self.module_getter(get_ori_model(runner.model))
            setattr(module, self.param_name, value)
        except AttributeError as e:
            raise AttributeError(f'{e}. Please check hook settings.')

    def before_train(self, runner) -> None:
        """Get the original value before train."""
        self.ori_val = self.get_param(runner)

    def before_train_iter(
            self,
            runner,
            batch_idx: int,
            data_batch: Optional[Union[dict, tuple, list]] = None) -> None:
        """Set the warmup value before each train iter."""
        cur_iter = runner.iter
        iters_per_epoch = runner.max_iters / runner.max_epochs
        new_val = self.ori_val * min(
            1, cur_iter / (self.warmup_epochs * iters_per_epoch))
        self.set_param(runner, new_val)