Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom hook by config file #970

Merged
merged 4 commits into from
May 13, 2021
Merged

Conversation

mzr1996
Copy link
Member

@mzr1996 mzr1996 commented Apr 20, 2021

Refer to #967
I noticed that mmcv hooks already have priority. Therefore, I assign different priorities to default hooks, so that, we can use priority value to indicate when a hook should run.
For example, I add a custom hook as:

@HOOKS.register_module()
class CustomHook(Hook):
    def __init__(self, p) -> None:
        super().__init__()
        self.p = p

    def __repr__(self) -> str:
        return f'<CustomHook> with priority {self.p}'

And edit the base_runner.call_hook function to show the effect.

    def call_hook(self, fn_name):
        """Call all hooks.

        Args:
            fn_name (str): The function name in each hook to be called, such as
                "before_train_epoch".
        """
        self.logger.info('-------------------- start call hook -------------------- ')
        for hook in self._hooks:
            self.logger.info(f"{hook} call {fn_name}")
            getattr(hook, fn_name)(self)
        self.logger.info('-------------------- end call hook -------------------- ')

Then, we can add custom hooks by config file with:

custom_train_hooks = [dict(type='CustomHook', priority=1, p=1),
                      dict(type='CustomHook', priority=89, p=89)]

And get the result (I use mmcls to show the effect):

2021-04-20 10:24:08,234 - mmcls - INFO - -------------------- start call hook -------------------- 
2021-04-20 10:24:08,234 - mmcls - INFO - <CustomHook> with priority 1 call before_run
2021-04-20 10:24:08,234 - mmcls - INFO - <mmcv.runner.hooks.lr_updater.StepLrUpdaterHook object at 0x7f80bcee1550> call before_run
2021-04-20 10:24:08,234 - mmcls - INFO - <mmcv.runner.hooks.optimizer.OptimizerHook object at 0x7f80bce80ee0> call before_run
2021-04-20 10:24:08,234 - mmcls - INFO - <mmcls.core.evaluation.eval_hooks.EvalHook object at 0x7f80bceaa8e0> call before_run
2021-04-20 10:24:08,234 - mmcls - INFO - <mmcv.runner.hooks.checkpoint.CheckpointHook object at 0x7f80bce99400> call before_run
2021-04-20 10:24:08,234 - mmcls - INFO - <mmcv.runner.hooks.iter_timer.IterTimerHook object at 0x7f80bce89ca0> call before_run
2021-04-20 10:24:08,235 - mmcls - INFO - <CustomHook> with priority 89 call before_run
2021-04-20 10:24:08,235 - mmcls - INFO - <mmcv.runner.hooks.logger.text.TextLoggerHook object at 0x7f80bce89bb0> call before_run
2021-04-20 10:24:08,235 - mmcls - INFO - -------------------- end call hook -------------------- 
2021-04-20 10:24:08,235 - mmcls - INFO - -------------------- start call hook -------------------- 
2021-04-20 10:24:08,235 - mmcls - INFO - <CustomHook> with priority 1 call before_train_epoch
2021-04-20 10:24:08,235 - mmcls - INFO - <mmcv.runner.hooks.lr_updater.StepLrUpdaterHook object at 0x7f80bcee1550> call before_train_epoch
2021-04-20 10:24:08,235 - mmcls - INFO - <mmcv.runner.hooks.optimizer.OptimizerHook object at 0x7f80bce80ee0> call before_train_epoch
2021-04-20 10:24:08,235 - mmcls - INFO - <mmcls.core.evaluation.eval_hooks.EvalHook object at 0x7f80bceaa8e0> call before_train_epoch
2021-04-20 10:24:08,235 - mmcls - INFO - <mmcv.runner.hooks.checkpoint.CheckpointHook object at 0x7f80bce99400> call before_train_epoch
2021-04-20 10:24:08,235 - mmcls - INFO - <mmcv.runner.hooks.iter_timer.IterTimerHook object at 0x7f80bce89ca0> call before_train_epoch
2021-04-20 10:24:08,235 - mmcls - INFO - <CustomHook> with priority 89 call before_train_epoch
2021-04-20 10:24:08,235 - mmcls - INFO - <mmcv.runner.hooks.logger.text.TextLoggerHook object at 0x7f80bce89bb0> call before_train_epoch
2021-04-20 10:24:08,236 - mmcls - INFO - -------------------- end call hook -------------------- 

@CLAassistant
Copy link

CLAassistant commented Apr 20, 2021

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Apr 20, 2021

Codecov Report

Merging #970 (858ab1f) into master (f61295d) will increase coverage by 0.23%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #970      +/-   ##
==========================================
+ Coverage   64.57%   64.81%   +0.23%     
==========================================
  Files         152      152              
  Lines        9810     9827      +17     
  Branches     1781     1788       +7     
==========================================
+ Hits         6335     6369      +34     
+ Misses       3145     3129      -16     
+ Partials      330      329       -1     
Flag Coverage Δ
unittests 64.81% <100.00%> (+0.23%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
mmcv/runner/base_runner.py 77.41% <100.00%> (+6.83%) ⬆️
mmcv/runner/hooks/lr_updater.py 69.42% <0.00%> (+0.27%) ⬆️
mmcv/ops/deform_conv.py 61.76% <0.00%> (+0.28%) ⬆️
mmcv/runner/iter_based_runner.py 56.83% <0.00%> (+0.71%) ⬆️
mmcv/runner/hooks/logger/pavi.py 70.58% <0.00%> (+1.47%) ⬆️
mmcv/runner/priority.py 68.42% <0.00%> (+10.52%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f61295d...858ab1f. Read the comment docs.

@ZwwWayne
Copy link
Collaborator

Overall the modification LGTM. Could you add a unit test to test those priorities of hooks with custom hooks, e.g., using the example you show in the PR message?

@mzr1996
Copy link
Member Author

mzr1996 commented May 11, 2021

Overall the modification LGTM. Could you add a unit test to test those priorities of hooks with custom hooks, e.g., using the example you show in the PR message?

Ok, I have added the unittest.

@ZwwWayne ZwwWayne merged commit 15bcaa9 into open-mmlab:master May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants