Skip to content

Commit

Permalink
Fix bc break of fp16 (#3822)
Browse files Browse the repository at this point in the history
* fix bc break of mmdet.core.fp16, refactor import of wrap_fp16_model

* changed warning method

* added docstring for Depr_Fp16OptimizerHook

* docformatter

* fix docstring

* changed names from depr to deprecated
  • Loading branch information
RyanXLi authored Sep 24, 2020
1 parent 9c95543 commit bf01bdd
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .dev_scripts/batch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import torch
from mmcv import Config, get_logger
from mmcv.parallel import MMDataParallel, MMDistributedDataParallel
from mmcv.runner import get_dist_info, init_dist, load_checkpoint
from mmcv.runner import (get_dist_info, init_dist, load_checkpoint,
wrap_fp16_model)

from mmdet.apis import multi_gpu_test, single_gpu_test
from mmdet.core import wrap_fp16_model
from mmdet.datasets import (build_dataloader, build_dataset,
replace_ImageToTensor)
from mmdet.models import build_detector
Expand Down
1 change: 1 addition & 0 deletions mmdet/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .anchor import * # noqa: F401, F403
from .bbox import * # noqa: F401, F403
from .evaluation import * # noqa: F401, F403
from .fp16 import * # noqa: F401, F403
from .mask import * # noqa: F401, F403
from .post_processing import * # noqa: F401, F403
from .utils import * # noqa: F401, F403
8 changes: 8 additions & 0 deletions mmdet/core/fp16/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .deprecated_fp16_utils import \
DeprecatedFp16OptimizerHook as Fp16OptimizerHook
from .deprecated_fp16_utils import deprecated_auto_fp16 as auto_fp16
from .deprecated_fp16_utils import deprecated_force_fp32 as force_fp32
from .deprecated_fp16_utils import \
deprecated_wrap_fp16_model as wrap_fp16_model

__all__ = ['auto_fp16', 'force_fp32', 'Fp16OptimizerHook', 'wrap_fp16_model']
47 changes: 47 additions & 0 deletions mmdet/core/fp16/deprecated_fp16_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import warnings

from mmcv.runner import (Fp16OptimizerHook, auto_fp16, force_fp32,
wrap_fp16_model)


class DeprecatedFp16OptimizerHook(Fp16OptimizerHook):
"""A wrapper class for the FP16 optimizer hook. This class wraps
:class:`Fp16OptimizerHook` in `mmcv.runner` and shows a warning that the
:class:`Fp16OptimizerHook` from `mmdet.core` will be deprecated.
Refer to :class:`Fp16OptimizerHook` in `mmcv.runner` for more details.
Args:
loss_scale (float): Scale factor multiplied with loss.
"""

def __init__(*args, **kwargs):
super().__init__(*args, **kwargs)
warnings.warn(
'Importing Fp16OptimizerHook from "mmdet.core" will be '
'deprecated in the future. Please import them from "mmcv.runner" '
'instead')


def deprecated_auto_fp16(*args, **kwargs):
warnings.warn(
'Importing auto_fp16 from "mmdet.core" will be '
'deprecated in the future. Please import them from "mmcv.runner" '
'instead')
return auto_fp16(*args, **kwargs)


def deprecated_force_fp32(*args, **kwargs):
warnings.warn(
'Importing force_fp32 from "mmdet.core" will be '
'deprecated in the future. Please import them from "mmcv.runner" '
'instead')
return force_fp32(*args, **kwargs)


def deprecated_wrap_fp16_model(*args, **kwargs):
warnings.warn(
'Importing wrap_fp16_model from "mmdet.core" will be '
'deprecated in the future. Please import them from "mmcv.runner" '
'instead')
wrap_fp16_model(*args, **kwargs)
3 changes: 1 addition & 2 deletions tools/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from mmcv import Config
from mmcv.cnn import fuse_conv_bn
from mmcv.parallel import MMDataParallel
from mmcv.runner import load_checkpoint
from mmcv.runner.fp16_utils import wrap_fp16_model
from mmcv.runner import load_checkpoint, wrap_fp16_model

from mmdet.datasets import (build_dataloader, build_dataset,
replace_ImageToTensor)
Expand Down
8 changes: 2 additions & 6 deletions tools/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
from mmcv import Config, DictAction
from mmcv.cnn import fuse_conv_bn
from mmcv.parallel import MMDataParallel, MMDistributedDataParallel
from mmcv.runner import get_dist_info, init_dist, load_checkpoint
from mmcv.runner import (get_dist_info, init_dist, load_checkpoint,
wrap_fp16_model)

from mmdet.apis import multi_gpu_test, single_gpu_test
from mmdet.datasets import (build_dataloader, build_dataset,
replace_ImageToTensor)
from mmdet.models import build_detector

try:
from mmcv.runner import wrap_fp16_model
except ImportError:
from mmcv.runner.fp16_utils import wrap_fp16_model


def parse_args():
parser = argparse.ArgumentParser(
Expand Down
8 changes: 2 additions & 6 deletions tools/test_robustness.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import torch
import torch.distributed as dist
from mmcv.parallel import MMDataParallel, MMDistributedDataParallel
from mmcv.runner import get_dist_info, init_dist, load_checkpoint
from mmcv.runner import (get_dist_info, init_dist, load_checkpoint,
wrap_fp16_model)
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from robustness_eval import get_results
Expand All @@ -20,11 +21,6 @@
from mmdet.datasets import build_dataloader, build_dataset
from mmdet.models import build_detector

try:
from mmcv.runner import wrap_fp16_model
except ImportError:
from mmcv.runner.fp16_utils import wrap_fp16_model


def coco_eval_with_return(result_files,
result_types,
Expand Down

0 comments on commit bf01bdd

Please sign in to comment.