Spaces:
Running on Zero
Running on Zero
File size: 1,961 Bytes
fee9d25 | 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 | # Copyright (c) OpenMMLab. All rights reserved.
from typing import Optional, Union
from mmengine.hooks import Hook
from mmengine.model import is_model_wrapper
from mmengine.registry import MODELS
from torch import nn
from mmdet.registry import HOOKS
@HOOKS.register_module()
class DataPreprocessorSwitchHook(Hook):
"""Switch the data_preprocessor during training.
Args:
switch_epoch (int): switch pipeline at this epoch.
switch_data_preprocessor (list[dict]): the data_preprocessor to switch
to.
"""
def __init__(
self,
switch_epoch,
switch_data_preprocessor: Optional[Union[dict, nn.Module]] = None,
) -> None:
self.switch_epoch = switch_epoch
if switch_data_preprocessor is None:
switch_data_preprocessor = dict(type='BaseDataPreprocessor')
if isinstance(switch_data_preprocessor, nn.Module):
self.switch_data_preprocessor = switch_data_preprocessor
elif isinstance(switch_data_preprocessor, dict):
self.switch_data_preprocessor = MODELS.build(
switch_data_preprocessor)
else:
raise TypeError('switch_data_preprocessor should be a `dict` or '
'`nn.Module` instance, but got '
f'{type(switch_data_preprocessor)}')
self._has_switched = False
def before_train_epoch(self, runner) -> None:
epoch = runner.epoch
model = runner.model
# TODO: refactor after mmengine using model wrapper
if is_model_wrapper(model):
model = model.module
if epoch >= self.switch_epoch and not self._has_switched:
runner.logger.info(
f'Switch data_preprocessor after epoch: {self.switch_epoch}!')
model.data_preprocessor = self.switch_data_preprocessor.to(
model.data_preprocessor.device)
self._has_switched = True
|