From 6ed7a82981c07cd940e29e65a2c4d9c826a97f88 Mon Sep 17 00:00:00 2001 From: tripleMu <865626@163.com> Date: Thu, 26 May 2022 18:39:35 +0800 Subject: [PATCH 01/14] Merge Master --- mmcv/fileio/file_client.py | 25 ++++++++++++------------- mmcv/runner/epoch_based_runner.py | 5 ++++- mmcv/runner/hooks/evaluation.py | 8 ++++---- mmcv/runner/iter_based_runner.py | 4 ++++ mmcv/version.py | 2 +- tests/test_fileclient.py | 3 ++- tests/test_parallel.py | 4 ++-- 7 files changed, 29 insertions(+), 22 deletions(-) diff --git a/mmcv/fileio/file_client.py b/mmcv/fileio/file_client.py index 49775db895..ee7c3164e2 100644 --- a/mmcv/fileio/file_client.py +++ b/mmcv/fileio/file_client.py @@ -791,17 +791,12 @@ class FileClient: 'petrel': PetrelBackend, 'http': HTTPBackend, } - # This collection is used to record the overridden backends, and when a - # backend appears in the collection, the singleton pattern is disabled for - # that backend, because if the singleton pattern is used, then the object - # returned will be the backend before overwriting - _overridden_backends: set = set() - _prefix_to_backends: dict = { + + _prefix_to_backends = { 's3': PetrelBackend, 'http': HTTPBackend, 'https': HTTPBackend, } - _overridden_prefixes: set = set() _instances: dict = {} @@ -825,10 +820,7 @@ def __new__(cls, backend=None, prefix=None, **kwargs): for key, value in kwargs.items(): arg_key += f':{key}:{value}' - # if a backend was overridden, it will create a new object - if (arg_key in cls._instances - and backend not in cls._overridden_backends - and prefix not in cls._overridden_prefixes): + if arg_key in cls._instances: _instance = cls._instances[arg_key] else: # create a new object and put it to _instance @@ -922,7 +914,9 @@ def _register_backend(cls, name, backend, force=False, prefixes=None): 'add "force=True" if you want to override it') if name in cls._backends and force: - cls._overridden_backends.add(name) + for arg_key, instance in list(cls._instances.items()): + if isinstance(instance.client, cls._backends[name]): + cls._instances.pop(arg_key) cls._backends[name] = backend if prefixes is not None: @@ -934,7 +928,12 @@ def _register_backend(cls, name, backend, force=False, prefixes=None): if prefix not in cls._prefix_to_backends: cls._prefix_to_backends[prefix] = backend elif (prefix in cls._prefix_to_backends) and force: - cls._overridden_prefixes.add(prefix) + overridden_backend = cls._prefix_to_backends[prefix] + if isinstance(overridden_backend, list): + overridden_backend = tuple(overridden_backend) + for arg_key, instance in list(cls._instances.items()): + if isinstance(instance.client, overridden_backend): + cls._instances.pop(arg_key) cls._prefix_to_backends[prefix] = backend else: raise KeyError( diff --git a/mmcv/runner/epoch_based_runner.py b/mmcv/runner/epoch_based_runner.py index 078e91df3f..7a1b10302a 100644 --- a/mmcv/runner/epoch_based_runner.py +++ b/mmcv/runner/epoch_based_runner.py @@ -45,10 +45,12 @@ def train(self, data_loader, **kwargs): self.call_hook('before_train_epoch') time.sleep(2) # Prevent possible deadlock during epoch transition for i, data_batch in enumerate(self.data_loader): + self.data_batch = data_batch self._inner_iter = i self.call_hook('before_train_iter') self.run_iter(data_batch, train_mode=True, **kwargs) self.call_hook('after_train_iter') + del self.data_batch self._iter += 1 self.call_hook('after_train_epoch') @@ -62,11 +64,12 @@ def val(self, data_loader, **kwargs): self.call_hook('before_val_epoch') time.sleep(2) # Prevent possible deadlock during epoch transition for i, data_batch in enumerate(self.data_loader): + self.data_batch = data_batch self._inner_iter = i self.call_hook('before_val_iter') self.run_iter(data_batch, train_mode=False) self.call_hook('after_val_iter') - + del self.data_batch self.call_hook('after_val_epoch') def run(self, data_loaders, workflow, max_epochs=None, **kwargs): diff --git a/mmcv/runner/hooks/evaluation.py b/mmcv/runner/hooks/evaluation.py index 987bacd4a4..c79628f981 100644 --- a/mmcv/runner/hooks/evaluation.py +++ b/mmcv/runner/hooks/evaluation.py @@ -160,10 +160,10 @@ def _init_rule(self, rule, key_indicator): specified as 'greater'. 2. Or if the key indicator is in ``self.less_keys``, the rule will be specified as 'less'. - 3. Or if the key indicator is equal to the substring in any one item - in ``self.greater_keys``, the rule will be specified as 'greater'. - 4. Or if the key indicator is equal to the substring in any one item - in ``self.less_keys``, the rule will be specified as 'less'. + 3. Or if any one item in ``self.greater_keys`` is a substring of + key_indicator , the rule will be specified as 'greater'. + 4. Or if any one item in ``self.less_keys`` is a substring of + key_indicator , the rule will be specified as 'less'. Args: rule (str | None): Comparison rule for best score. diff --git a/mmcv/runner/iter_based_runner.py b/mmcv/runner/iter_based_runner.py index 8ab5a041bf..2de7317b7a 100644 --- a/mmcv/runner/iter_based_runner.py +++ b/mmcv/runner/iter_based_runner.py @@ -57,6 +57,7 @@ def train(self, data_loader, **kwargs): self.data_loader = data_loader self._epoch = data_loader.epoch data_batch = next(data_loader) + self.data_batch = data_batch self.call_hook('before_train_iter') outputs = self.model.train_step(data_batch, self.optimizer, **kwargs) if not isinstance(outputs, dict): @@ -65,6 +66,7 @@ def train(self, data_loader, **kwargs): self.log_buffer.update(outputs['log_vars'], outputs['num_samples']) self.outputs = outputs self.call_hook('after_train_iter') + del self.data_batch self._inner_iter += 1 self._iter += 1 @@ -74,6 +76,7 @@ def val(self, data_loader, **kwargs): self.mode = 'val' self.data_loader = data_loader data_batch = next(data_loader) + self.data_batch = data_batch self.call_hook('before_val_iter') outputs = self.model.val_step(data_batch, **kwargs) if not isinstance(outputs, dict): @@ -82,6 +85,7 @@ def val(self, data_loader, **kwargs): self.log_buffer.update(outputs['log_vars'], outputs['num_samples']) self.outputs = outputs self.call_hook('after_val_iter') + del self.data_batch self._inner_iter += 1 def run(self, data_loaders, workflow, max_iters=None, **kwargs): diff --git a/mmcv/version.py b/mmcv/version.py index 7f87f54f6c..b137c0ab68 100644 --- a/mmcv/version.py +++ b/mmcv/version.py @@ -1,5 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. -__version__ = '1.5.1' +__version__ = '1.5.2' def parse_version_info(version_str: str, length: int = 4) -> tuple: diff --git a/tests/test_fileclient.py b/tests/test_fileclient.py index 92d370245d..292779f36a 100644 --- a/tests/test_fileclient.py +++ b/tests/test_fileclient.py @@ -655,7 +655,8 @@ def get_text(self, filepath): FileClient.register_backend('dummy_backend', DummyBackend2, force=True) client3 = FileClient(backend='dummy_backend') client4 = FileClient(backend='dummy_backend') - assert client3 is not client4 + assert client2 is not client3 + assert client3 is client4 def test_parse_uri_prefix(self): # input path is None diff --git a/tests/test_parallel.py b/tests/test_parallel.py index 9d1e823130..814aaeadfb 100644 --- a/tests/test_parallel.py +++ b/tests/test_parallel.py @@ -82,7 +82,7 @@ def forward(self, *args, **kwargs): 'mmpose module wrapper', parent=MODULE_WRAPPERS, scope='mmpose') @MMRAZOR_MODULE_WRAPPERS.register_module() - class ModuleWrapperInRazor(object): + class ModuleWrapperInRazor: def __init__(self, module): self.module = module @@ -91,7 +91,7 @@ def forward(self, *args, **kwargs): return self.module(*args, **kwargs) @MMPOSE_MODULE_WRAPPERS.register_module() - class ModuleWrapperInPose(object): + class ModuleWrapperInPose: def __init__(self, module): self.module = module From 035627fb86ba7b4f3ec2092ab2d3f49d69d15f5e Mon Sep 17 00:00:00 2001 From: tripleMu <865626@163.com> Date: Thu, 26 May 2022 18:40:51 +0800 Subject: [PATCH 02/14] Add typehint in mmcv/ops/* --- mmcv/ops/psa_mask.py | 13 +++++---- mmcv/ops/riroi_align_rotated.py | 33 ++++++++++++----------- mmcv/ops/roi_align.py | 39 ++++++++++++++------------- mmcv/ops/roi_align_rotated.py | 38 ++++++++++++++------------ mmcv/ops/roi_pool.py | 19 +++++++++---- mmcv/ops/roiaware_pool3d.py | 17 ++++++++---- mmcv/ops/roipoint_pool3d.py | 16 ++++++++--- mmcv/ops/rotated_feature_align.py | 18 ++++++++----- mmcv/ops/scatter_points.py | 19 +++++++++---- mmcv/ops/sparse_functional.py | 33 ++++++++++++++--------- mmcv/ops/sparse_modules.py | 14 +++++----- mmcv/ops/sparse_structure.py | 19 +++++++------ mmcv/ops/sync_bn.py | 32 +++++++++++++--------- mmcv/ops/three_interpolate.py | 4 +-- mmcv/ops/three_nn.py | 4 +-- mmcv/ops/upfirdn2d.py | 44 ++++++++++++++++++++----------- mmcv/ops/voxelize.py | 31 ++++++++++++---------- 17 files changed, 239 insertions(+), 154 deletions(-) diff --git a/mmcv/ops/psa_mask.py b/mmcv/ops/psa_mask.py index 0786e2d25f..fc351ccbfb 100644 --- a/mmcv/ops/psa_mask.py +++ b/mmcv/ops/psa_mask.py @@ -1,4 +1,7 @@ # Modified from https://github.com/hszhao/semseg/blob/master/lib/psa +from typing import Optional + +import torch from torch import nn from torch.autograd import Function from torch.nn.modules.utils import _pair @@ -12,7 +15,7 @@ class PSAMaskFunction(Function): @staticmethod - def symbolic(g, input, psa_type, mask_size): + def symbolic(g, input: torch.Tensor, psa_type: str, mask_size: int): return g.op( 'mmcv::MMCVPSAMask', input, @@ -20,7 +23,7 @@ def symbolic(g, input, psa_type, mask_size): mask_size_i=mask_size) @staticmethod - def forward(ctx, input, psa_type, mask_size): + def forward(ctx, input: torch.Tensor, psa_type: str, mask_size: int): ctx.psa_type = psa_type ctx.mask_size = _pair(mask_size) ctx.save_for_backward(input) @@ -45,7 +48,7 @@ def forward(ctx, input, psa_type, mask_size): return output @staticmethod - def backward(ctx, grad_output): + def backward(ctx, grad_output: torch.Tensor): input = ctx.saved_tensors[0] psa_type = ctx.psa_type h_mask, w_mask = ctx.mask_size @@ -71,7 +74,7 @@ def backward(ctx, grad_output): class PSAMask(nn.Module): - def __init__(self, psa_type, mask_size=None): + def __init__(self, psa_type: str, mask_size: Optional[tuple] = None): super().__init__() assert psa_type in ['collect', 'distribute'] if psa_type == 'collect': @@ -82,7 +85,7 @@ def __init__(self, psa_type, mask_size=None): self.mask_size = mask_size self.psa_type = psa_type - def forward(self, input): + def forward(self, input: torch.Tensor): return psa_mask(input, self.psa_type_enum, self.mask_size) def __repr__(self): diff --git a/mmcv/ops/riroi_align_rotated.py b/mmcv/ops/riroi_align_rotated.py index af1e6098e4..42e95efedc 100644 --- a/mmcv/ops/riroi_align_rotated.py +++ b/mmcv/ops/riroi_align_rotated.py @@ -1,4 +1,7 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Any, Union + +import torch import torch.nn as nn from torch.autograd import Function @@ -11,14 +14,14 @@ class RiRoIAlignRotatedFunction(Function): @staticmethod - def forward(ctx, - features, - rois, - out_size, - spatial_scale, - num_samples=0, - num_orientations=8, - clockwise=False): + def forward(ctx: Any, + features: torch.Tensor, + rois: torch.Tensor, + out_size: Union[int, tuple], + spatial_scale: int, + num_samples: int = 0, + num_orientations: int = 8, + clockwise: bool = False) -> torch.Tensor: if isinstance(out_size, int): out_h = out_size out_w = out_size @@ -54,7 +57,7 @@ def forward(ctx, return output @staticmethod - def backward(ctx, grad_output): + def backward(ctx: Any, grad_output: torch.Tensor): feature_size = ctx.feature_size spatial_scale = ctx.spatial_scale num_orientations = ctx.num_orientations @@ -111,11 +114,11 @@ class RiRoIAlignRotated(nn.Module): """ def __init__(self, - out_size, - spatial_scale, - num_samples=0, - num_orientations=8, - clockwise=False): + out_size: tuple, + spatial_scale: int, + num_samples: int = 0, + num_orientations: int = 8, + clockwise: bool = False): super().__init__() self.out_size = out_size @@ -124,7 +127,7 @@ def __init__(self, self.num_orientations = int(num_orientations) self.clockwise = clockwise - def forward(self, features, rois): + def forward(self, features: torch.Tensor, rois: torch.Tensor): return RiRoIAlignRotatedFunction.apply(features, rois, self.out_size, self.spatial_scale, self.num_samples, diff --git a/mmcv/ops/roi_align.py b/mmcv/ops/roi_align.py index 7e387f944b..aa24f9a571 100644 --- a/mmcv/ops/roi_align.py +++ b/mmcv/ops/roi_align.py @@ -1,4 +1,6 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Any + import torch import torch.nn as nn from torch.autograd import Function @@ -14,8 +16,9 @@ class RoIAlignFunction(Function): @staticmethod - def symbolic(g, input, rois, output_size, spatial_scale, sampling_ratio, - pool_mode, aligned): + def symbolic(g, input: torch.Tensor, rois: torch.Tensor, + output_size: tuple, spatial_scale: float, sampling_ratio: int, + pool_mode: str, aligned: bool): from ..onnx import is_custom_op_loaded has_custom_op = is_custom_op_loaded() if has_custom_op: @@ -62,14 +65,14 @@ def symbolic(g, input, rois, output_size, spatial_scale, sampling_ratio, mode_s=pool_mode) @staticmethod - def forward(ctx, - input, - rois, - output_size, - spatial_scale=1.0, - sampling_ratio=0, - pool_mode='avg', - aligned=True): + def forward(ctx: Any, + input: torch.Tensor, + rois: torch.Tensor, + output_size: int, + spatial_scale: float = 1.0, + sampling_ratio: int = 0, + pool_mode: str = 'avg', + aligned: bool = True) -> torch.Tensor: ctx.output_size = _pair(output_size) ctx.spatial_scale = spatial_scale ctx.sampling_ratio = sampling_ratio @@ -108,7 +111,7 @@ def forward(ctx, @staticmethod @once_differentiable - def backward(ctx, grad_output): + def backward(ctx: Any, grad_output: torch.Tensor) -> tuple: rois, argmax_y, argmax_x = ctx.saved_tensors grad_input = grad_output.new_zeros(ctx.input_shape) # complex head architecture may cause grad_output uncontiguous. @@ -175,12 +178,12 @@ class RoIAlign(nn.Module): }, cls_name='RoIAlign') def __init__(self, - output_size, - spatial_scale=1.0, - sampling_ratio=0, - pool_mode='avg', - aligned=True, - use_torchvision=False): + output_size: tuple, + spatial_scale: float = 1.0, + sampling_ratio: int = 0, + pool_mode: str = 'avg', + aligned: bool = True, + use_torchvision: bool = False): super().__init__() self.output_size = _pair(output_size) @@ -190,7 +193,7 @@ def __init__(self, self.aligned = aligned self.use_torchvision = use_torchvision - def forward(self, input, rois): + def forward(self, input: torch.Tensor, rois: torch.Tensor) -> torch.Tensor: """ Args: input: NCHW images diff --git a/mmcv/ops/roi_align_rotated.py b/mmcv/ops/roi_align_rotated.py index 8551b172b5..0efe10a7fe 100644 --- a/mmcv/ops/roi_align_rotated.py +++ b/mmcv/ops/roi_align_rotated.py @@ -1,4 +1,7 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Any, Union + +import torch import torch.nn as nn from torch.autograd import Function from torch.nn.modules.utils import _pair @@ -12,8 +15,9 @@ class RoIAlignRotatedFunction(Function): @staticmethod - def symbolic(g, input, rois, output_size, spatial_scale, sampling_ratio, - aligned, clockwise): + def symbolic(g, input: torch.Tensor, rois: torch.Tensor, + output_size: Union[int, tuple], spatial_scale: float, + sampling_ratio: int, aligned: bool, clockwise: bool): if isinstance(output_size, int): out_h = output_size out_w = output_size @@ -37,14 +41,14 @@ def symbolic(g, input, rois, output_size, spatial_scale, sampling_ratio, clockwise_i=clockwise) @staticmethod - def forward(ctx, - input, - rois, - output_size, - spatial_scale, - sampling_ratio=0, - aligned=True, - clockwise=False): + def forward(ctx: Any, + input: torch.Tensor, + rois: torch.Tensor, + output_size: Union[int, tuple], + spatial_scale: float, + sampling_ratio: int = 0, + aligned: bool = True, + clockwise: bool = False) -> torch.Tensor: ctx.output_size = _pair(output_size) ctx.spatial_scale = spatial_scale ctx.sampling_ratio = sampling_ratio @@ -71,7 +75,7 @@ def forward(ctx, return output @staticmethod - def backward(ctx, grad_output): + def backward(ctx: Any, grad_output: torch.Tensor): feature_size = ctx.feature_size rois = ctx.saved_tensors[0] assert feature_size is not None @@ -151,11 +155,11 @@ class RoIAlignRotated(nn.Module): }, cls_name='RoIAlignRotated') def __init__(self, - output_size, - spatial_scale, - sampling_ratio=0, - aligned=True, - clockwise=False): + output_size: Union[int, tuple], + spatial_scale: float, + sampling_ratio: int = 0, + aligned: bool = True, + clockwise: bool = False): super().__init__() self.output_size = _pair(output_size) @@ -164,7 +168,7 @@ def __init__(self, self.aligned = aligned self.clockwise = clockwise - def forward(self, input, rois): + def forward(self, input: torch.Tensor, rois: torch.Tensor) -> torch.Tensor: return RoIAlignRotatedFunction.apply(input, rois, self.output_size, self.spatial_scale, self.sampling_ratio, self.aligned, diff --git a/mmcv/ops/roi_pool.py b/mmcv/ops/roi_pool.py index f63faabb46..cb98c7971a 100644 --- a/mmcv/ops/roi_pool.py +++ b/mmcv/ops/roi_pool.py @@ -1,4 +1,6 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Any, Union + import torch import torch.nn as nn from torch.autograd import Function @@ -14,7 +16,8 @@ class RoIPoolFunction(Function): @staticmethod - def symbolic(g, input, rois, output_size, spatial_scale): + def symbolic(g, input: torch.Tensor, rois: torch.Tensor, + output_size: Union[int, tuple], spatial_scale: float): return g.op( 'MaxRoiPool', input, @@ -23,7 +26,11 @@ def symbolic(g, input, rois, output_size, spatial_scale): spatial_scale_f=spatial_scale) @staticmethod - def forward(ctx, input, rois, output_size, spatial_scale=1.0): + def forward(ctx: Any, + input: torch.Tensor, + rois: torch.Tensor, + output_size: Union[int, tuple], + spatial_scale: float = 1.0) -> torch.Tensor: ctx.output_size = _pair(output_size) ctx.spatial_scale = spatial_scale ctx.input_shape = input.size() @@ -49,7 +56,7 @@ def forward(ctx, input, rois, output_size, spatial_scale=1.0): @staticmethod @once_differentiable - def backward(ctx, grad_output): + def backward(ctx: Any, grad_output: torch.Tensor): rois, argmax = ctx.saved_tensors grad_input = grad_output.new_zeros(ctx.input_shape) @@ -70,13 +77,15 @@ def backward(ctx, grad_output): class RoIPool(nn.Module): - def __init__(self, output_size, spatial_scale=1.0): + def __init__(self, + output_size: Union[int, tuple], + spatial_scale: float = 1.0): super().__init__() self.output_size = _pair(output_size) self.spatial_scale = float(spatial_scale) - def forward(self, input, rois): + def forward(self, input: torch.Tensor, rois: torch.Tensor) -> torch.Tensor: return roi_pool(input, rois, self.output_size, self.spatial_scale) def __repr__(self): diff --git a/mmcv/ops/roiaware_pool3d.py b/mmcv/ops/roiaware_pool3d.py index 2096b436f0..40511bb925 100644 --- a/mmcv/ops/roiaware_pool3d.py +++ b/mmcv/ops/roiaware_pool3d.py @@ -1,4 +1,6 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Any, Union + import torch from torch import nn as nn from torch.autograd import Function @@ -25,7 +27,10 @@ class RoIAwarePool3d(nn.Module): Default: 'max'. """ - def __init__(self, out_size, max_pts_per_voxel=128, mode='max'): + def __init__(self, + out_size: Union[int, tuple], + max_pts_per_voxel: int = 128, + mode: str = 'max'): super().__init__() self.out_size = out_size @@ -34,7 +39,8 @@ def __init__(self, out_size, max_pts_per_voxel=128, mode='max'): pool_mapping = {'max': 0, 'avg': 1} self.mode = pool_mapping[mode] - def forward(self, rois, pts, pts_feature): + def forward(self, rois: torch.Tensor, pts: torch.Tensor, + pts_feature: torch.Tensor) -> torch.Tensor: """ Args: rois (torch.Tensor): [N, 7], in LiDAR coordinate, @@ -55,8 +61,9 @@ def forward(self, rois, pts, pts_feature): class RoIAwarePool3dFunction(Function): @staticmethod - def forward(ctx, rois, pts, pts_feature, out_size, max_pts_per_voxel, - mode): + def forward(ctx: Any, rois: torch.Tensor, pts: torch.Tensor, + pts_feature: torch.Tensor, out_size: Union[int, tuple], + max_pts_per_voxel: int, mode: int) -> torch.Tensor: """ Args: rois (torch.Tensor): [N, 7], in LiDAR coordinate, @@ -108,7 +115,7 @@ def forward(ctx, rois, pts, pts_feature, out_size, max_pts_per_voxel, return pooled_features @staticmethod - def backward(ctx, grad_out): + def backward(ctx: Any, grad_out: torch.Tensor): ret = ctx.roiaware_pool3d_for_backward pts_idx_of_voxels, argmax, mode, num_pts, num_channels = ret diff --git a/mmcv/ops/roipoint_pool3d.py b/mmcv/ops/roipoint_pool3d.py index 2a9315bd99..320720e9b2 100644 --- a/mmcv/ops/roipoint_pool3d.py +++ b/mmcv/ops/roipoint_pool3d.py @@ -1,3 +1,6 @@ +from typing import Any + +import torch from torch import nn as nn from torch.autograd import Function @@ -17,11 +20,12 @@ class RoIPointPool3d(nn.Module): Default: 512. """ - def __init__(self, num_sampled_points=512): + def __init__(self, num_sampled_points: int = 512): super().__init__() self.num_sampled_points = num_sampled_points - def forward(self, points, point_features, boxes3d): + def forward(self, points: torch.Tensor, point_features: torch.Tensor, + boxes3d: torch.Tensor) -> tuple: """ Args: points (torch.Tensor): Input points whose shape is (B, N, C). @@ -41,7 +45,11 @@ def forward(self, points, point_features, boxes3d): class RoIPointPool3dFunction(Function): @staticmethod - def forward(ctx, points, point_features, boxes3d, num_sampled_points=512): + def forward(ctx: Any, + points: torch.Tensor, + point_features: torch.Tensor, + boxes3d: torch.Tensor, + num_sampled_points: int = 512) -> tuple: """ Args: points (torch.Tensor): Input points whose shape is (B, N, C). @@ -73,5 +81,5 @@ def forward(ctx, points, point_features, boxes3d, num_sampled_points=512): return pooled_features, pooled_empty_flag @staticmethod - def backward(ctx, grad_out): + def backward(ctx: Any, grad_out: torch.Tensor) -> None: raise NotImplementedError diff --git a/mmcv/ops/rotated_feature_align.py b/mmcv/ops/rotated_feature_align.py index 95353b70e3..15792dd588 100644 --- a/mmcv/ops/rotated_feature_align.py +++ b/mmcv/ops/rotated_feature_align.py @@ -1,4 +1,6 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Any + import torch from torch.autograd import Function from torch.autograd.function import once_differentiable @@ -21,7 +23,8 @@ class RotatedFeatureAlignFunction(Function): """ @staticmethod - def symbolic(g, features, best_rbboxes, spatial_scale, points): + def symbolic(g, features: torch.Tensor, best_rbboxes: torch.Tensor, + spatial_scale: float, points: int): assert points in [1, 5] return g.op( 'mmcv::MMCVRotatedFeatureAlign', @@ -31,7 +34,8 @@ def symbolic(g, features, best_rbboxes, spatial_scale, points): points_i=points) @staticmethod - def forward(ctx, features, best_rbboxes, spatial_scale, points): + def forward(ctx: Any, features: torch.Tensor, best_rbboxes: torch.Tensor, + spatial_scale: float, points: int) -> torch.Tensor: """ Args: features (torch.Tensor): Input features with shape [N,C,H,W]. @@ -60,7 +64,7 @@ def forward(ctx, features, best_rbboxes, spatial_scale, points): @staticmethod @once_differentiable - def backward(ctx, grad_output): + def backward(ctx: Any, grad_output: torch.Tensor) -> tuple: """ Args: grad_output (torch.Tensor): The gradiant of output features @@ -84,9 +88,9 @@ def backward(ctx, grad_output): return grad_input, None, None, None -def rotated_feature_align(features, - best_rbboxes, - spatial_scale=1 / 8, - points=1): +def rotated_feature_align(features: torch.Tensor, + best_rbboxes: torch.Tensor, + spatial_scale: float = 1 / 8, + points: int = 1): return RotatedFeatureAlignFunction.apply(features, best_rbboxes, spatial_scale, points) diff --git a/mmcv/ops/scatter_points.py b/mmcv/ops/scatter_points.py index f8a4a5565a..f7c9b5ab41 100644 --- a/mmcv/ops/scatter_points.py +++ b/mmcv/ops/scatter_points.py @@ -1,4 +1,6 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Any, List, Optional + import torch import torch.nn.functional as F from torch import nn @@ -14,7 +16,10 @@ class _DynamicScatter(Function): @staticmethod - def forward(ctx, feats, coors, reduce_type='max'): + def forward(ctx: Any, + feats: torch.Tensor, + coors: torch.Tensor, + reduce_type: str = 'max') -> tuple: """convert kitti points(N, >=3) to voxels. Args: @@ -42,7 +47,9 @@ def forward(ctx, feats, coors, reduce_type='max'): return voxel_feats, voxel_coors @staticmethod - def backward(ctx, grad_voxel_feats, grad_voxel_coors=None): + def backward(ctx: Any, + grad_voxel_feats: torch.Tensor, + grad_voxel_coors: Optional[torch.Tensor] = None) -> tuple: (feats, voxel_feats, point2voxel_map, voxel_points_count) = ctx.saved_tensors grad_feats = torch.zeros_like(feats) @@ -73,14 +80,16 @@ class DynamicScatter(nn.Module): into voxel. """ - def __init__(self, voxel_size, point_cloud_range, average_points: bool): + def __init__(self, voxel_size: List, point_cloud_range: List, + average_points: bool): super().__init__() self.voxel_size = voxel_size self.point_cloud_range = point_cloud_range self.average_points = average_points - def forward_single(self, points, coors): + def forward_single(self, points: torch.Tensor, + coors: torch.Tensor) -> tuple: """Scatters points into voxels. Args: @@ -97,7 +106,7 @@ def forward_single(self, points, coors): reduce = 'mean' if self.average_points else 'max' return dynamic_scatter(points.contiguous(), coors.contiguous(), reduce) - def forward(self, points, coors): + def forward(self, points: torch.Tensor, coors: torch.Tensor) -> tuple: """Scatters points/features into voxels. Args: diff --git a/mmcv/ops/sparse_functional.py b/mmcv/ops/sparse_functional.py index aef4320d84..c0da17d4c2 100644 --- a/mmcv/ops/sparse_functional.py +++ b/mmcv/ops/sparse_functional.py @@ -11,7 +11,9 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from typing import Any +import torch from torch.autograd import Function from . import sparse_ops as ops @@ -25,8 +27,10 @@ class SparseConvFunction(Function): """ @staticmethod - def forward(ctx, features, filters, indice_pairs, indice_pair_num, - num_activate_out): + def forward(ctx: Any, features: torch.Tensor, + filters: torch.nn.parameter.Parameter, + indice_pairs: torch.Tensor, indice_pair_num: torch.Tensor, + num_activate_out: torch.Tensor) -> torch.Tensor: """ Args: features (torch.Tensor): Features that needs to convolute. @@ -44,7 +48,7 @@ def forward(ctx, features, filters, indice_pairs, indice_pair_num, indice_pair_num, num_activate_out, False) @staticmethod - def backward(ctx, grad_output): + def backward(ctx: Any, grad_output: torch.Tensor) -> tuple: indice_pairs, indice_pair_num, features, filters = ctx.saved_tensors input_bp, filters_bp = ops.indice_conv_backward( features, filters, grad_output, indice_pairs, indice_pair_num, @@ -56,8 +60,10 @@ def backward(ctx, grad_output): class SparseInverseConvFunction(Function): @staticmethod - def forward(ctx, features, filters, indice_pairs, indice_pair_num, - num_activate_out): + def forward(ctx: Any, features: torch.Tensor, + filters: torch.nn.parameter.Parameter, + indice_pairs: torch.Tensor, indice_pair_num: torch.Tensor, + num_activate_out: torch.Tensor) -> torch.Tensor: """ Args: features (torch.Tensor): Features that needs to convolute. @@ -75,7 +81,7 @@ def forward(ctx, features, filters, indice_pairs, indice_pair_num, indice_pair_num, num_activate_out, True, False) @staticmethod - def backward(ctx, grad_output): + def backward(ctx: Any, grad_output: torch.Tensor) -> tuple: indice_pairs, indice_pair_num, features, filters = ctx.saved_tensors input_bp, filters_bp = ops.indice_conv_backward( features, filters, grad_output, indice_pairs, indice_pair_num, @@ -87,8 +93,10 @@ def backward(ctx, grad_output): class SubMConvFunction(Function): @staticmethod - def forward(ctx, features, filters, indice_pairs, indice_pair_num, - num_activate_out): + def forward(ctx: Any, features: torch.Tensor, + filters: torch.nn.parameter.Parameter, + indice_pairs: torch.Tensor, indice_pair_num: torch.Tensor, + num_activate_out: torch.Tensor) -> torch.Tensor: """ Args: features (torch.Tensor): Features that needs to convolute. @@ -106,7 +114,7 @@ def forward(ctx, features, filters, indice_pairs, indice_pair_num, indice_pair_num, num_activate_out, False, True) @staticmethod - def backward(ctx, grad_output): + def backward(ctx: Any, grad_output: torch.Tensor) -> tuple: indice_pairs, indice_pair_num, features, filters = ctx.saved_tensors input_bp, filters_bp = ops.indice_conv_backward( features, filters, grad_output, indice_pairs, indice_pair_num, @@ -118,8 +126,9 @@ def backward(ctx, grad_output): class SparseMaxPoolFunction(Function): @staticmethod - def forward(ctx, features, indice_pairs, indice_pair_num, - num_activate_out): + def forward(ctx, features: torch.Tensor, indice_pairs: torch.Tensor, + indice_pair_num: torch.Tensor, + num_activate_out: torch.Tensor) -> torch.Tensor: """ Args: features (torch.Tensor): Features that needs to convolute. @@ -137,7 +146,7 @@ def forward(ctx, features, indice_pairs, indice_pair_num, return out @staticmethod - def backward(ctx, grad_output): + def backward(ctx: Any, grad_output: torch.Tensor) -> tuple: indice_pairs, indice_pair_num, features, out = ctx.saved_tensors input_bp = ops.indice_maxpool_backward(features, out, grad_output, indice_pairs, indice_pair_num) diff --git a/mmcv/ops/sparse_modules.py b/mmcv/ops/sparse_modules.py index a721b30e5a..4376c29e16 100644 --- a/mmcv/ops/sparse_modules.py +++ b/mmcv/ops/sparse_modules.py @@ -13,6 +13,7 @@ # limitations under the License. import sys from collections import OrderedDict +from typing import Any, List, Optional, Union import torch from torch import nn @@ -20,17 +21,18 @@ from .sparse_structure import SparseConvTensor -def is_spconv_module(module): +def is_spconv_module(module: tuple) -> bool: spconv_modules = (SparseModule, ) return isinstance(module, spconv_modules) -def is_sparse_conv(module): +def is_sparse_conv(module: Any) -> bool: from .sparse_conv import SparseConvolution return isinstance(module, SparseConvolution) -def _mean_update(vals, m_vals, t): +def _mean_update(vals: Union[int, List], m_vals: Union[int, List], + t: float) -> List: outputs = [] if not isinstance(vals, list): vals = [vals] @@ -101,7 +103,7 @@ def __init__(self, *args, **kwargs): self.add_module(name, module) self._sparity_dict = {} - def __getitem__(self, idx): + def __getitem__(self, idx: int): if not (-len(self) <= idx < len(self)): raise IndexError(f'index {idx} is out of range') if idx < 0: @@ -118,14 +120,14 @@ def __len__(self): def sparity_dict(self): return self._sparity_dict - def add(self, module, name=None): + def add(self, module: Any, name: Optional[str] = None): if name is None: name = str(len(self._modules)) if name in self._modules: raise KeyError('name exists') self.add_module(name, module) - def forward(self, input): + def forward(self, input: torch.Tensor) -> torch.Tensor: for k, module in self._modules.items(): if is_spconv_module(module): assert isinstance(input, SparseConvTensor) diff --git a/mmcv/ops/sparse_structure.py b/mmcv/ops/sparse_structure.py index 82d04167cc..e349c9acd4 100644 --- a/mmcv/ops/sparse_structure.py +++ b/mmcv/ops/sparse_structure.py @@ -1,8 +1,11 @@ +from typing import Dict, List, Optional, Tuple, Union + import numpy as np import torch -def scatter_nd(indices, updates, shape): +def scatter_nd(indices: torch.Tensor, updates: torch.Tensor, + shape: torch.Tensor) -> torch.Tensor: """pytorch edition of tensorflow scatter_nd. this function don't contain except handle code. so use this carefully when @@ -21,18 +24,18 @@ def scatter_nd(indices, updates, shape): class SparseConvTensor: def __init__(self, - features, - indices, - spatial_shape, - batch_size, - grid=None): + features: torch.Tensor, + indices: torch.Tensor, + spatial_shape: Union[List, Tuple], + batch_size: int, + grid: Optional[torch.Tensor] = None): self.features = features self.indices = indices if self.indices.dtype != torch.int32: self.indices.int() self.spatial_shape = spatial_shape self.batch_size = batch_size - self.indice_dict = {} + self.indice_dict: Dict = {} self.grid = grid @property @@ -46,7 +49,7 @@ def find_indice_pair(self, key): return self.indice_dict[key] return None - def dense(self, channels_first=True): + def dense(self, channels_first: bool = True) -> torch.Tensor: output_shape = [self.batch_size] + list( self.spatial_shape) + [self.features.shape[1]] res = scatter_nd(self.indices.long(), self.features, output_shape) diff --git a/mmcv/ops/sync_bn.py b/mmcv/ops/sync_bn.py index e24ecb1a0a..fc0a723ed6 100644 --- a/mmcv/ops/sync_bn.py +++ b/mmcv/ops/sync_bn.py @@ -1,4 +1,6 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Optional + import torch import torch.distributed as dist import torch.nn.functional as F @@ -19,8 +21,10 @@ class SyncBatchNormFunction(Function): @staticmethod - def symbolic(g, input, running_mean, running_var, weight, bias, momentum, - eps, group, group_size, stats_mode): + def symbolic(g, input: torch.Tensor, running_mean: torch.Tensor, + running_var: torch.Tensor, weight: torch.Tensor, + bias: torch.Tensor, momentum: float, eps: float, group: int, + group_size: int, stats_mode: str): return g.op( 'mmcv::MMCVSyncBatchNorm', input, @@ -35,8 +39,10 @@ def symbolic(g, input, running_mean, running_var, weight, bias, momentum, stats_mode=stats_mode) @staticmethod - def forward(self, input, running_mean, running_var, weight, bias, momentum, - eps, group, group_size, stats_mode): + def forward(self, input: torch.Tensor, running_mean: torch.Tensor, + running_var: torch.Tensor, weight: torch.Tensor, + bias: torch.Tensor, momentum: float, eps: float, group: int, + group_size: int, stats_mode: str): self.momentum = momentum self.eps = eps self.group = group @@ -126,7 +132,7 @@ def forward(self, input, running_mean, running_var, weight, bias, momentum, @staticmethod @once_differentiable - def backward(self, grad_output): + def backward(self, grad_output: torch.Tensor) -> tuple: norm, std, weight = self.saved_tensors grad_weight = torch.zeros_like(weight) grad_bias = torch.zeros_like(weight) @@ -191,13 +197,13 @@ class SyncBatchNorm(Module): """ def __init__(self, - num_features, - eps=1e-5, - momentum=0.1, - affine=True, - track_running_stats=True, - group=None, - stats_mode='default'): + num_features: int, + eps: float = 1e-5, + momentum: float = 0.1, + affine: bool = True, + track_running_stats: bool = True, + group: Optional[int] = None, + stats_mode: str = 'default'): super().__init__() self.num_features = num_features self.eps = eps @@ -239,7 +245,7 @@ def reset_parameters(self): self.weight.data.uniform_() # pytorch use ones_() self.bias.data.zero_() - def forward(self, input): + def forward(self, input: torch.Tensor) -> torch.Tensor: if input.dim() < 2: raise ValueError( f'expected at least 2D input, got {input.dim()}D input') diff --git a/mmcv/ops/three_interpolate.py b/mmcv/ops/three_interpolate.py index 7256e91f6f..12b2f7611e 100644 --- a/mmcv/ops/three_interpolate.py +++ b/mmcv/ops/three_interpolate.py @@ -1,4 +1,4 @@ -from typing import Tuple +from typing import Any, Tuple import torch from torch.autograd import Function @@ -17,7 +17,7 @@ class ThreeInterpolate(Function): """ @staticmethod - def forward(ctx, features: torch.Tensor, indices: torch.Tensor, + def forward(ctx: Any, features: torch.Tensor, indices: torch.Tensor, weight: torch.Tensor) -> torch.Tensor: """ Args: diff --git a/mmcv/ops/three_nn.py b/mmcv/ops/three_nn.py index 25d60bb26c..7893c8363b 100644 --- a/mmcv/ops/three_nn.py +++ b/mmcv/ops/three_nn.py @@ -1,4 +1,4 @@ -from typing import Tuple +from typing import Any, Tuple import torch from torch.autograd import Function @@ -16,7 +16,7 @@ class ThreeNN(Function): """ @staticmethod - def forward(ctx, target: torch.Tensor, + def forward(ctx: Any, target: torch.Tensor, source: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """ Args: diff --git a/mmcv/ops/upfirdn2d.py b/mmcv/ops/upfirdn2d.py index 255354dad6..91b4823cbe 100644 --- a/mmcv/ops/upfirdn2d.py +++ b/mmcv/ops/upfirdn2d.py @@ -95,6 +95,8 @@ # ======================================================================= +from typing import Any, List, Tuple, Union + import torch from torch.autograd import Function from torch.nn import functional as F @@ -108,8 +110,11 @@ class UpFirDn2dBackward(Function): @staticmethod - def forward(ctx, grad_output, kernel, grad_kernel, up, down, pad, g_pad, - in_size, out_size): + def forward(ctx: Any, grad_output: torch.Tensor, kernel: torch.Tensor, + grad_kernel: torch.Tensor, up: tuple, down: tuple, pad: tuple, + g_pad: tuple, in_size: Union[List, + Tuple], out_size: Union[List, + Tuple]): up_x, up_y = up down_x, down_y = down @@ -149,7 +154,7 @@ def forward(ctx, grad_output, kernel, grad_kernel, up, down, pad, g_pad, return grad_input @staticmethod - def backward(ctx, gradgrad_input): + def backward(ctx: Any, gradgrad_input: torch.Tensor) -> tuple: kernel, = ctx.saved_tensors gradgrad_input = gradgrad_input.reshape(-1, ctx.in_size[2], @@ -177,7 +182,8 @@ def backward(ctx, gradgrad_input): class UpFirDn2d(Function): @staticmethod - def forward(ctx, input, kernel, up, down, pad): + def forward(ctx: Any, input: torch.Tensor, kernel: torch.Tensor, up: tuple, + down: tuple, pad: tuple) -> torch.Tensor: up_x, up_y = up down_x, down_y = down pad_x0, pad_x1, pad_y0, pad_y1 = pad @@ -222,7 +228,7 @@ def forward(ctx, input, kernel, up, down, pad): return out @staticmethod - def backward(ctx, grad_output): + def backward(ctx: Any, grad_output: torch.Tensor) -> tuple: kernel, grad_kernel = ctx.saved_tensors grad_input = UpFirDn2dBackward.apply( @@ -240,7 +246,12 @@ def backward(ctx, grad_output): return grad_input, None, None, None, None -def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)): +def upfirdn2d( + input: torch.Tensor, + kernel: torch.Tensor, + up: Union[int, tuple] = 1, + down: Union[int, tuple] = 1, + pad: tuple = (0, 0)) -> torch.Tensor: # noqa E125 """UpFRIDn for 2d features. UpFIRDn is short for upsample, apply FIR filter and downsample. More @@ -264,14 +275,14 @@ def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)): """ if input.device.type == 'cpu': if len(pad) == 2: - pad = (pad[0], pad[1], pad[0], pad[1]) - - up = to_2tuple(up) - - down = to_2tuple(down) - - out = upfirdn2d_native(input, kernel, up[0], up[1], down[0], down[1], - pad[0], pad[1], pad[2], pad[3]) + pad = (pad[0], pad[1], pad[0], pad[1]) # type: ignore + + out = upfirdn2d_native(input, kernel, + to_2tuple(up)[0], + to_2tuple(up)[1], + to_2tuple(down)[0], + to_2tuple(down)[1], pad[0], pad[1], pad[2], + pad[3]) else: _up = to_2tuple(up) @@ -287,8 +298,9 @@ def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)): return out -def upfirdn2d_native(input, kernel, up_x, up_y, down_x, down_y, pad_x0, pad_x1, - pad_y0, pad_y1): +def upfirdn2d_native(input: torch.Tensor, kernel: torch.Tensor, up_x: int, + up_y: int, down_x: int, down_y: int, pad_x0: int, + pad_x1: int, pad_y0: int, pad_y1: int) -> torch.Tensor: _, channel, in_h, in_w = input.shape input = input.reshape(-1, in_h, in_w, 1) diff --git a/mmcv/ops/voxelize.py b/mmcv/ops/voxelize.py index ee4e0ae8f6..1fe2037065 100644 --- a/mmcv/ops/voxelize.py +++ b/mmcv/ops/voxelize.py @@ -1,4 +1,6 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Any, List, Tuple, Union + import torch from torch import nn from torch.autograd import Function @@ -13,13 +15,14 @@ class _Voxelization(Function): @staticmethod - def forward(ctx, - points, - voxel_size, - coors_range, - max_points=35, - max_voxels=20000, - deterministic=True): + def forward( + ctx: Any, + points: torch.Tensor, + voxel_size: Union[tuple, float], + coors_range: Union[tuple, float], + max_points: int = 35, + max_voxels: int = 20000, + deterministic: bool = True) -> Union[Tuple[torch.Tensor], Tuple]: """Convert kitti points(N, >=3) to voxels. Args: @@ -111,11 +114,11 @@ class Voxelization(nn.Module): """ def __init__(self, - voxel_size, - point_cloud_range, - max_num_points, - max_voxels=20000, - deterministic=True): + voxel_size: List, + point_cloud_range: List, + max_num_points: int, + max_voxels: Union[tuple, int] = 20000, + deterministic: bool = True): """ Args: voxel_size (list): list [x, y, z] size of three dimension @@ -150,7 +153,7 @@ def __init__(self, point_cloud_range, dtype=torch.float32) voxel_size = torch.tensor(voxel_size, dtype=torch.float32) grid_size = (point_cloud_range[3:] - - point_cloud_range[:3]) / voxel_size + point_cloud_range[:3]) / voxel_size # type: ignore grid_size = torch.round(grid_size).long() input_feat_shape = grid_size[:2] self.grid_size = grid_size @@ -158,7 +161,7 @@ def __init__(self, # [w, h, d] -> [d, h, w] self.pcd_shape = [*input_feat_shape, 1][::-1] - def forward(self, input): + def forward(self, input: torch.Tensor) -> torch.Tensor: if self.training: max_voxels = self.max_voxels[0] else: From ed59600657ec16aa94ba8ee24ee4ed119c618ac3 Mon Sep 17 00:00:00 2001 From: tripleMu <865626@163.com> Date: Sat, 28 May 2022 20:40:40 +0800 Subject: [PATCH 03/14] Fix --- mmcv/ops/voxelize.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mmcv/ops/voxelize.py b/mmcv/ops/voxelize.py index 1fe2037065..992ce68fd2 100644 --- a/mmcv/ops/voxelize.py +++ b/mmcv/ops/voxelize.py @@ -152,8 +152,9 @@ def __init__(self, point_cloud_range = torch.tensor( point_cloud_range, dtype=torch.float32) voxel_size = torch.tensor(voxel_size, dtype=torch.float32) - grid_size = (point_cloud_range[3:] - - point_cloud_range[:3]) / voxel_size # type: ignore + grid_size = ( + point_cloud_range[3:] - # type: ignore + point_cloud_range[:3]) / voxel_size # type: ignore grid_size = torch.round(grid_size).long() input_feat_shape = grid_size[:2] self.grid_size = grid_size From bbe9ebf6786ed0fd2bf5fa6211ae2e2fc08c2af3 Mon Sep 17 00:00:00 2001 From: tripleMu <92794867+triple-Mu@users.noreply.github.com> Date: Tue, 31 May 2022 09:58:22 +0800 Subject: [PATCH 04/14] Update mmcv/ops/roi_align.py Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com> --- mmcv/ops/roi_align.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmcv/ops/roi_align.py b/mmcv/ops/roi_align.py index aa24f9a571..cb5d486e77 100644 --- a/mmcv/ops/roi_align.py +++ b/mmcv/ops/roi_align.py @@ -16,7 +16,7 @@ class RoIAlignFunction(Function): @staticmethod - def symbolic(g, input: torch.Tensor, rois: torch.Tensor, + def symbolic(g: torch._C.Graph, input: torch.Tensor, rois: torch.Tensor, output_size: tuple, spatial_scale: float, sampling_ratio: int, pool_mode: str, aligned: bool): from ..onnx import is_custom_op_loaded From c52913d98b9da5416cae226cad3d0c7fabe99198 Mon Sep 17 00:00:00 2001 From: tripleMu <865626@163.com> Date: Tue, 31 May 2022 11:01:27 +0800 Subject: [PATCH 05/14] Fix --- mmcv/ops/roi_align_rotated.py | 9 ++++++--- mmcv/ops/roi_pool.py | 8 +++++--- mmcv/ops/roiaware_pool3d.py | 6 ++++-- mmcv/ops/roipoint_pool3d.py | 2 +- mmcv/ops/rotated_feature_align.py | 5 +++-- mmcv/ops/sparse_modules.py | 6 +++--- mmcv/ops/sync_bn.py | 8 ++++---- mmcv/ops/upfirdn2d.py | 5 ++--- 8 files changed, 28 insertions(+), 21 deletions(-) diff --git a/mmcv/ops/roi_align_rotated.py b/mmcv/ops/roi_align_rotated.py index 0efe10a7fe..7b005326e6 100644 --- a/mmcv/ops/roi_align_rotated.py +++ b/mmcv/ops/roi_align_rotated.py @@ -1,5 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. -from typing import Any, Union +from typing import Any, Optional, Tuple, Union import torch import torch.nn as nn @@ -15,7 +15,7 @@ class RoIAlignRotatedFunction(Function): @staticmethod - def symbolic(g, input: torch.Tensor, rois: torch.Tensor, + def symbolic(g: torch._C.Graph, input: torch.Tensor, rois: torch.Tensor, output_size: Union[int, tuple], spatial_scale: float, sampling_ratio: int, aligned: bool, clockwise: bool): if isinstance(output_size, int): @@ -75,7 +75,10 @@ def forward(ctx: Any, return output @staticmethod - def backward(ctx: Any, grad_output: torch.Tensor): + def backward( + ctx: Any, grad_output: torch.Tensor + ) -> Tuple[Optional[torch.Tensor], Optional[torch.Tensor], None, None, + None, None, None]: feature_size = ctx.feature_size rois = ctx.saved_tensors[0] assert feature_size is not None diff --git a/mmcv/ops/roi_pool.py b/mmcv/ops/roi_pool.py index cb98c7971a..0f71c886b1 100644 --- a/mmcv/ops/roi_pool.py +++ b/mmcv/ops/roi_pool.py @@ -1,5 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. -from typing import Any, Union +from typing import Any, Tuple, Union import torch import torch.nn as nn @@ -16,7 +16,7 @@ class RoIPoolFunction(Function): @staticmethod - def symbolic(g, input: torch.Tensor, rois: torch.Tensor, + def symbolic(g: torch._C.Graph, input: torch.Tensor, rois: torch.Tensor, output_size: Union[int, tuple], spatial_scale: float): return g.op( 'MaxRoiPool', @@ -56,7 +56,9 @@ def forward(ctx: Any, @staticmethod @once_differentiable - def backward(ctx: Any, grad_output: torch.Tensor): + def backward( + ctx: Any, grad_output: torch.Tensor + ) -> Tuple[torch.Tensor, None, None, None]: rois, argmax = ctx.saved_tensors grad_input = grad_output.new_zeros(ctx.input_shape) diff --git a/mmcv/ops/roiaware_pool3d.py b/mmcv/ops/roiaware_pool3d.py index 40511bb925..9a09049b55 100644 --- a/mmcv/ops/roiaware_pool3d.py +++ b/mmcv/ops/roiaware_pool3d.py @@ -1,5 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. -from typing import Any, Union +from typing import Any, Tuple, Union import torch from torch import nn as nn @@ -115,7 +115,9 @@ def forward(ctx: Any, rois: torch.Tensor, pts: torch.Tensor, return pooled_features @staticmethod - def backward(ctx: Any, grad_out: torch.Tensor): + def backward( + ctx: Any, grad_out: torch.Tensor + ) -> Tuple[None, None, torch.Tensor, None, None, None]: ret = ctx.roiaware_pool3d_for_backward pts_idx_of_voxels, argmax, mode, num_pts, num_channels = ret diff --git a/mmcv/ops/roipoint_pool3d.py b/mmcv/ops/roipoint_pool3d.py index 320720e9b2..9118312944 100644 --- a/mmcv/ops/roipoint_pool3d.py +++ b/mmcv/ops/roipoint_pool3d.py @@ -81,5 +81,5 @@ def forward(ctx: Any, return pooled_features, pooled_empty_flag @staticmethod - def backward(ctx: Any, grad_out: torch.Tensor) -> None: + def backward(ctx: Any, grad_out: torch.Tensor) -> torch.Tensor: raise NotImplementedError diff --git a/mmcv/ops/rotated_feature_align.py b/mmcv/ops/rotated_feature_align.py index 15792dd588..d2630edcd9 100644 --- a/mmcv/ops/rotated_feature_align.py +++ b/mmcv/ops/rotated_feature_align.py @@ -23,8 +23,9 @@ class RotatedFeatureAlignFunction(Function): """ @staticmethod - def symbolic(g, features: torch.Tensor, best_rbboxes: torch.Tensor, - spatial_scale: float, points: int): + def symbolic(g: torch._C.Graph, features: torch.Tensor, + best_rbboxes: torch.Tensor, spatial_scale: float, + points: int): assert points in [1, 5] return g.op( 'mmcv::MMCVRotatedFeatureAlign', diff --git a/mmcv/ops/sparse_modules.py b/mmcv/ops/sparse_modules.py index 4376c29e16..12d4e084e9 100644 --- a/mmcv/ops/sparse_modules.py +++ b/mmcv/ops/sparse_modules.py @@ -21,12 +21,12 @@ from .sparse_structure import SparseConvTensor -def is_spconv_module(module: tuple) -> bool: +def is_spconv_module(module: nn.Module) -> bool: spconv_modules = (SparseModule, ) return isinstance(module, spconv_modules) -def is_sparse_conv(module: Any) -> bool: +def is_sparse_conv(module: nn.Module) -> bool: from .sparse_conv import SparseConvolution return isinstance(module, SparseConvolution) @@ -103,7 +103,7 @@ def __init__(self, *args, **kwargs): self.add_module(name, module) self._sparity_dict = {} - def __getitem__(self, idx: int): + def __getitem__(self, idx: int) -> torch.Tensor: if not (-len(self) <= idx < len(self)): raise IndexError(f'index {idx} is out of range') if idx < 0: diff --git a/mmcv/ops/sync_bn.py b/mmcv/ops/sync_bn.py index fc0a723ed6..78ff6632e8 100644 --- a/mmcv/ops/sync_bn.py +++ b/mmcv/ops/sync_bn.py @@ -21,10 +21,10 @@ class SyncBatchNormFunction(Function): @staticmethod - def symbolic(g, input: torch.Tensor, running_mean: torch.Tensor, - running_var: torch.Tensor, weight: torch.Tensor, - bias: torch.Tensor, momentum: float, eps: float, group: int, - group_size: int, stats_mode: str): + def symbolic(g: torch._C.Graph, input: torch.Tensor, + running_mean: torch.Tensor, running_var: torch.Tensor, + weight: torch.Tensor, bias: torch.Tensor, momentum: float, + eps: float, group: int, group_size: int, stats_mode: str): return g.op( 'mmcv::MMCVSyncBatchNorm', input, diff --git a/mmcv/ops/upfirdn2d.py b/mmcv/ops/upfirdn2d.py index 91b4823cbe..1619c429d0 100644 --- a/mmcv/ops/upfirdn2d.py +++ b/mmcv/ops/upfirdn2d.py @@ -112,9 +112,8 @@ class UpFirDn2dBackward(Function): @staticmethod def forward(ctx: Any, grad_output: torch.Tensor, kernel: torch.Tensor, grad_kernel: torch.Tensor, up: tuple, down: tuple, pad: tuple, - g_pad: tuple, in_size: Union[List, - Tuple], out_size: Union[List, - Tuple]): + g_pad: tuple, in_size: Union[List, Tuple], + out_size: Union[List, Tuple]) -> torch.Tensor: up_x, up_y = up down_x, down_y = down From db32d55b80c66bb9f2d741d92e4699d7002a16bb Mon Sep 17 00:00:00 2001 From: tripleMu <865626@163.com> Date: Wed, 1 Jun 2022 16:11:13 +0800 Subject: [PATCH 06/14] Fix --- mmcv/ops/psa_mask.py | 7 ++++--- mmcv/ops/riroi_align_rotated.py | 3 ++- mmcv/ops/sync_bn.py | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/mmcv/ops/psa_mask.py b/mmcv/ops/psa_mask.py index fc351ccbfb..71a7a0c7e1 100644 --- a/mmcv/ops/psa_mask.py +++ b/mmcv/ops/psa_mask.py @@ -23,7 +23,8 @@ def symbolic(g, input: torch.Tensor, psa_type: str, mask_size: int): mask_size_i=mask_size) @staticmethod - def forward(ctx, input: torch.Tensor, psa_type: str, mask_size: int): + def forward(ctx, input: torch.Tensor, psa_type: str, + mask_size: int) -> torch.Tensor: ctx.psa_type = psa_type ctx.mask_size = _pair(mask_size) ctx.save_for_backward(input) @@ -48,7 +49,7 @@ def forward(ctx, input: torch.Tensor, psa_type: str, mask_size: int): return output @staticmethod - def backward(ctx, grad_output: torch.Tensor): + def backward(ctx, grad_output: torch.Tensor) -> torch.Tensor: input = ctx.saved_tensors[0] psa_type = ctx.psa_type h_mask, w_mask = ctx.mask_size @@ -85,7 +86,7 @@ def __init__(self, psa_type: str, mask_size: Optional[tuple] = None): self.mask_size = mask_size self.psa_type = psa_type - def forward(self, input: torch.Tensor): + def forward(self, input: torch.Tensor) -> torch.Tensor: return psa_mask(input, self.psa_type_enum, self.mask_size) def __repr__(self): diff --git a/mmcv/ops/riroi_align_rotated.py b/mmcv/ops/riroi_align_rotated.py index 42e95efedc..de309e87c2 100644 --- a/mmcv/ops/riroi_align_rotated.py +++ b/mmcv/ops/riroi_align_rotated.py @@ -127,7 +127,8 @@ def __init__(self, self.num_orientations = int(num_orientations) self.clockwise = clockwise - def forward(self, features: torch.Tensor, rois: torch.Tensor): + def forward(self, features: torch.Tensor, + rois: torch.Tensor) -> torch.Tensor: return RiRoIAlignRotatedFunction.apply(features, rois, self.out_size, self.spatial_scale, self.num_samples, diff --git a/mmcv/ops/sync_bn.py b/mmcv/ops/sync_bn.py index 78ff6632e8..e119670901 100644 --- a/mmcv/ops/sync_bn.py +++ b/mmcv/ops/sync_bn.py @@ -42,7 +42,7 @@ def symbolic(g: torch._C.Graph, input: torch.Tensor, def forward(self, input: torch.Tensor, running_mean: torch.Tensor, running_var: torch.Tensor, weight: torch.Tensor, bias: torch.Tensor, momentum: float, eps: float, group: int, - group_size: int, stats_mode: str): + group_size: int, stats_mode: str) -> torch.Tensor: self.momentum = momentum self.eps = eps self.group = group From be5dc960c1cbd5e58964cb1214612205d6245802 Mon Sep 17 00:00:00 2001 From: tripleMu <865626@163.com> Date: Wed, 1 Jun 2022 16:50:32 +0800 Subject: [PATCH 07/14] Fix --- mmcv/ops/psa_mask.py | 6 ++++-- mmcv/ops/riroi_align_rotated.py | 11 +++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/mmcv/ops/psa_mask.py b/mmcv/ops/psa_mask.py index 71a7a0c7e1..8e4c964d6c 100644 --- a/mmcv/ops/psa_mask.py +++ b/mmcv/ops/psa_mask.py @@ -1,5 +1,5 @@ # Modified from https://github.com/hszhao/semseg/blob/master/lib/psa -from typing import Optional +from typing import Optional, Tuple import torch from torch import nn @@ -49,7 +49,9 @@ def forward(ctx, input: torch.Tensor, psa_type: str, return output @staticmethod - def backward(ctx, grad_output: torch.Tensor) -> torch.Tensor: + def backward( + ctx, grad_output: torch.Tensor + ) -> Tuple[torch.Tensor, None, None, None]: input = ctx.saved_tensors[0] psa_type = ctx.psa_type h_mask, w_mask = ctx.mask_size diff --git a/mmcv/ops/riroi_align_rotated.py b/mmcv/ops/riroi_align_rotated.py index de309e87c2..744187fb5f 100644 --- a/mmcv/ops/riroi_align_rotated.py +++ b/mmcv/ops/riroi_align_rotated.py @@ -1,5 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. -from typing import Any, Union +from typing import Any, Optional, Tuple, Union import torch import torch.nn as nn @@ -57,7 +57,9 @@ def forward(ctx: Any, return output @staticmethod - def backward(ctx: Any, grad_output: torch.Tensor): + def backward( + ctx: Any, grad_output: torch.Tensor + ) -> Optional[Tuple[torch.Tensor, None, None, None, None, None, None]]: feature_size = ctx.feature_size spatial_scale = ctx.spatial_scale num_orientations = ctx.num_orientations @@ -70,7 +72,7 @@ def backward(ctx: Any, grad_output: torch.Tensor): out_w = grad_output.size(3) out_h = grad_output.size(2) - grad_input = grad_rois = None + grad_input = None if ctx.needs_input_grad[0]: grad_input = rois.new_zeros(batch_size, num_channels, feature_h, @@ -86,7 +88,8 @@ def backward(ctx: Any, grad_output: torch.Tensor): num_orientations=num_orientations, clockwise=clockwise) - return grad_input, grad_rois, None, None, None, None, None + return grad_input, None, None, None, None, None, None + return None riroi_align_rotated = RiRoIAlignRotatedFunction.apply From 2dc676abbe5c1f1a289ea9719233a293aad761e1 Mon Sep 17 00:00:00 2001 From: tripleMu <92794867+triple-Mu@users.noreply.github.com> Date: Fri, 17 Jun 2022 20:49:04 +0800 Subject: [PATCH 08/14] Update mmcv/ops/riroi_align_rotated.py Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> --- mmcv/ops/riroi_align_rotated.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmcv/ops/riroi_align_rotated.py b/mmcv/ops/riroi_align_rotated.py index 744187fb5f..ad70c7ac8c 100644 --- a/mmcv/ops/riroi_align_rotated.py +++ b/mmcv/ops/riroi_align_rotated.py @@ -118,7 +118,7 @@ class RiRoIAlignRotated(nn.Module): def __init__(self, out_size: tuple, - spatial_scale: int, + spatial_scale: float, num_samples: int = 0, num_orientations: int = 8, clockwise: bool = False): From 9845d2e8171d379678adf381ba8f24c4050f60ed Mon Sep 17 00:00:00 2001 From: tripleMu <92794867+triple-Mu@users.noreply.github.com> Date: Fri, 17 Jun 2022 20:49:10 +0800 Subject: [PATCH 09/14] Update mmcv/ops/riroi_align_rotated.py Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> --- mmcv/ops/riroi_align_rotated.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmcv/ops/riroi_align_rotated.py b/mmcv/ops/riroi_align_rotated.py index ad70c7ac8c..1de810cc5f 100644 --- a/mmcv/ops/riroi_align_rotated.py +++ b/mmcv/ops/riroi_align_rotated.py @@ -18,7 +18,7 @@ def forward(ctx: Any, features: torch.Tensor, rois: torch.Tensor, out_size: Union[int, tuple], - spatial_scale: int, + spatial_scale: float, num_samples: int = 0, num_orientations: int = 8, clockwise: bool = False) -> torch.Tensor: From ad1df0ecf99631c4358b01984072ed2d709413b7 Mon Sep 17 00:00:00 2001 From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> Date: Sat, 18 Jun 2022 17:52:39 +0800 Subject: [PATCH 10/14] remove type hints of all symbolic methods --- mmcv/ops/roi_align.py | 5 ++--- mmcv/ops/roi_align_rotated.py | 5 ++--- mmcv/ops/roi_pool.py | 3 +-- mmcv/ops/rotated_feature_align.py | 6 ++---- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/mmcv/ops/roi_align.py b/mmcv/ops/roi_align.py index cb5d486e77..ca802f60cd 100644 --- a/mmcv/ops/roi_align.py +++ b/mmcv/ops/roi_align.py @@ -16,9 +16,8 @@ class RoIAlignFunction(Function): @staticmethod - def symbolic(g: torch._C.Graph, input: torch.Tensor, rois: torch.Tensor, - output_size: tuple, spatial_scale: float, sampling_ratio: int, - pool_mode: str, aligned: bool): + def symbolic(g, input, rois, output_size, spatial_scale, sampling_ratio, + pool_mode, aligned): from ..onnx import is_custom_op_loaded has_custom_op = is_custom_op_loaded() if has_custom_op: diff --git a/mmcv/ops/roi_align_rotated.py b/mmcv/ops/roi_align_rotated.py index 7b005326e6..f970ef4d8a 100644 --- a/mmcv/ops/roi_align_rotated.py +++ b/mmcv/ops/roi_align_rotated.py @@ -15,9 +15,8 @@ class RoIAlignRotatedFunction(Function): @staticmethod - def symbolic(g: torch._C.Graph, input: torch.Tensor, rois: torch.Tensor, - output_size: Union[int, tuple], spatial_scale: float, - sampling_ratio: int, aligned: bool, clockwise: bool): + def symbolic(g, input, rois, output_size, spatial_scale, sampling_ratio, + aligned, clockwise): if isinstance(output_size, int): out_h = output_size out_w = output_size diff --git a/mmcv/ops/roi_pool.py b/mmcv/ops/roi_pool.py index 0f71c886b1..e295b6a0c1 100644 --- a/mmcv/ops/roi_pool.py +++ b/mmcv/ops/roi_pool.py @@ -16,8 +16,7 @@ class RoIPoolFunction(Function): @staticmethod - def symbolic(g: torch._C.Graph, input: torch.Tensor, rois: torch.Tensor, - output_size: Union[int, tuple], spatial_scale: float): + def symbolic(g, input, rois, output_size, spatial_scale): return g.op( 'MaxRoiPool', input, diff --git a/mmcv/ops/rotated_feature_align.py b/mmcv/ops/rotated_feature_align.py index d2630edcd9..7d5954ddf2 100644 --- a/mmcv/ops/rotated_feature_align.py +++ b/mmcv/ops/rotated_feature_align.py @@ -23,9 +23,7 @@ class RotatedFeatureAlignFunction(Function): """ @staticmethod - def symbolic(g: torch._C.Graph, features: torch.Tensor, - best_rbboxes: torch.Tensor, spatial_scale: float, - points: int): + def symbolic(g, features, best_rbboxes, spatial_scale, points): assert points in [1, 5] return g.op( 'mmcv::MMCVRotatedFeatureAlign', @@ -92,6 +90,6 @@ def backward(ctx: Any, grad_output: torch.Tensor) -> tuple: def rotated_feature_align(features: torch.Tensor, best_rbboxes: torch.Tensor, spatial_scale: float = 1 / 8, - points: int = 1): + points: int = 1) -> torch.Tensor: return RotatedFeatureAlignFunction.apply(features, best_rbboxes, spatial_scale, points) From df0aa4c437b04c953d5420270e233dea7b74d9ac Mon Sep 17 00:00:00 2001 From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> Date: Sat, 18 Jun 2022 17:53:17 +0800 Subject: [PATCH 11/14] remove type hints of all symbolic methods --- mmcv/ops/sync_bn.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mmcv/ops/sync_bn.py b/mmcv/ops/sync_bn.py index e119670901..ce8727cb37 100644 --- a/mmcv/ops/sync_bn.py +++ b/mmcv/ops/sync_bn.py @@ -21,10 +21,8 @@ class SyncBatchNormFunction(Function): @staticmethod - def symbolic(g: torch._C.Graph, input: torch.Tensor, - running_mean: torch.Tensor, running_var: torch.Tensor, - weight: torch.Tensor, bias: torch.Tensor, momentum: float, - eps: float, group: int, group_size: int, stats_mode: str): + def symbolic(g, input, running_mean, running_var, weight, bias, momentum, + eps, group, group_size, stats_mode): return g.op( 'mmcv::MMCVSyncBatchNorm', input, From 88ce86f3d5a159914c56cfa2e5d5291cd9689ff3 Mon Sep 17 00:00:00 2001 From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> Date: Sat, 18 Jun 2022 20:02:13 +0800 Subject: [PATCH 12/14] minor refinement --- mmcv/ops/psa_mask.py | 2 +- mmcv/ops/roipoint_pool3d.py | 6 +++--- mmcv/ops/scatter_points.py | 6 +++--- mmcv/ops/sparse_functional.py | 6 +++--- mmcv/ops/sparse_modules.py | 2 +- mmcv/ops/sparse_structure.py | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/mmcv/ops/psa_mask.py b/mmcv/ops/psa_mask.py index 8e4c964d6c..45f4946662 100644 --- a/mmcv/ops/psa_mask.py +++ b/mmcv/ops/psa_mask.py @@ -15,7 +15,7 @@ class PSAMaskFunction(Function): @staticmethod - def symbolic(g, input: torch.Tensor, psa_type: str, mask_size: int): + def symbolic(g, input, psa_type, mask_size): return g.op( 'mmcv::MMCVPSAMask', input, diff --git a/mmcv/ops/roipoint_pool3d.py b/mmcv/ops/roipoint_pool3d.py index 9118312944..0167deef6a 100644 --- a/mmcv/ops/roipoint_pool3d.py +++ b/mmcv/ops/roipoint_pool3d.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, Tuple import torch from torch import nn as nn @@ -25,7 +25,7 @@ def __init__(self, num_sampled_points: int = 512): self.num_sampled_points = num_sampled_points def forward(self, points: torch.Tensor, point_features: torch.Tensor, - boxes3d: torch.Tensor) -> tuple: + boxes3d: torch.Tensor) -> Tuple[torch.Tensor]: """ Args: points (torch.Tensor): Input points whose shape is (B, N, C). @@ -49,7 +49,7 @@ def forward(ctx: Any, points: torch.Tensor, point_features: torch.Tensor, boxes3d: torch.Tensor, - num_sampled_points: int = 512) -> tuple: + num_sampled_points: int = 512) -> Tuple[torch.Tensor]: """ Args: points (torch.Tensor): Input points whose shape is (B, N, C). diff --git a/mmcv/ops/scatter_points.py b/mmcv/ops/scatter_points.py index f7c9b5ab41..de3d0f36ca 100644 --- a/mmcv/ops/scatter_points.py +++ b/mmcv/ops/scatter_points.py @@ -1,5 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. -from typing import Any, List, Optional +from typing import Any, List, Optional, Tuple import torch import torch.nn.functional as F @@ -19,7 +19,7 @@ class _DynamicScatter(Function): def forward(ctx: Any, feats: torch.Tensor, coors: torch.Tensor, - reduce_type: str = 'max') -> tuple: + reduce_type: str = 'max') -> Tuple[torch.Tensor]: """convert kitti points(N, >=3) to voxels. Args: @@ -89,7 +89,7 @@ def __init__(self, voxel_size: List, point_cloud_range: List, self.average_points = average_points def forward_single(self, points: torch.Tensor, - coors: torch.Tensor) -> tuple: + coors: torch.Tensor) -> Tuple[torch.Tensor]: """Scatters points into voxels. Args: diff --git a/mmcv/ops/sparse_functional.py b/mmcv/ops/sparse_functional.py index c0da17d4c2..7bc74e7d86 100644 --- a/mmcv/ops/sparse_functional.py +++ b/mmcv/ops/sparse_functional.py @@ -28,7 +28,7 @@ class SparseConvFunction(Function): @staticmethod def forward(ctx: Any, features: torch.Tensor, - filters: torch.nn.parameter.Parameter, + filters: torch.nn.Parameter, indice_pairs: torch.Tensor, indice_pair_num: torch.Tensor, num_activate_out: torch.Tensor) -> torch.Tensor: """ @@ -61,7 +61,7 @@ class SparseInverseConvFunction(Function): @staticmethod def forward(ctx: Any, features: torch.Tensor, - filters: torch.nn.parameter.Parameter, + filters: torch.nn.Parameter, indice_pairs: torch.Tensor, indice_pair_num: torch.Tensor, num_activate_out: torch.Tensor) -> torch.Tensor: """ @@ -94,7 +94,7 @@ class SubMConvFunction(Function): @staticmethod def forward(ctx: Any, features: torch.Tensor, - filters: torch.nn.parameter.Parameter, + filters: torch.nn.Parameter, indice_pairs: torch.Tensor, indice_pair_num: torch.Tensor, num_activate_out: torch.Tensor) -> torch.Tensor: """ diff --git a/mmcv/ops/sparse_modules.py b/mmcv/ops/sparse_modules.py index 12d4e084e9..20a92aa279 100644 --- a/mmcv/ops/sparse_modules.py +++ b/mmcv/ops/sparse_modules.py @@ -120,7 +120,7 @@ def __len__(self): def sparity_dict(self): return self._sparity_dict - def add(self, module: Any, name: Optional[str] = None): + def add(self, module: Any, name: Optional[str] = None) -> None: if name is None: name = str(len(self._modules)) if name in self._modules: diff --git a/mmcv/ops/sparse_structure.py b/mmcv/ops/sparse_structure.py index e349c9acd4..54229887c6 100644 --- a/mmcv/ops/sparse_structure.py +++ b/mmcv/ops/sparse_structure.py @@ -35,7 +35,7 @@ def __init__(self, self.indices.int() self.spatial_shape = spatial_shape self.batch_size = batch_size - self.indice_dict: Dict = {} + self.indice_dict: dict = {} self.grid = grid @property From c038621b6f485715bb9af96a7f7b66cfd878a021 Mon Sep 17 00:00:00 2001 From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> Date: Sat, 18 Jun 2022 20:08:40 +0800 Subject: [PATCH 13/14] minor refinement --- mmcv/ops/upfirdn2d.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mmcv/ops/upfirdn2d.py b/mmcv/ops/upfirdn2d.py index 1619c429d0..434238359a 100644 --- a/mmcv/ops/upfirdn2d.py +++ b/mmcv/ops/upfirdn2d.py @@ -276,12 +276,12 @@ def upfirdn2d( if len(pad) == 2: pad = (pad[0], pad[1], pad[0], pad[1]) # type: ignore - out = upfirdn2d_native(input, kernel, - to_2tuple(up)[0], - to_2tuple(up)[1], - to_2tuple(down)[0], - to_2tuple(down)[1], pad[0], pad[1], pad[2], - pad[3]) + _up = to_2tuple(up) + + _down = to_2tuple(down) + + out = upfirdn2d_native(input, kernel, _up[0], _up[1], _down[0], + _down[1], pad[0], pad[1], pad[2], pad[3]) else: _up = to_2tuple(up) From 6161a7ff46cc1e2bbe56c34927883ef7e5f82239 Mon Sep 17 00:00:00 2001 From: zhouzaida Date: Sat, 18 Jun 2022 20:50:34 +0800 Subject: [PATCH 14/14] minor fix --- mmcv/ops/roipoint_pool3d.py | 12 +++++++----- mmcv/ops/scatter_points.py | 10 ++++++---- mmcv/ops/sparse_functional.py | 9 +++------ mmcv/ops/sparse_structure.py | 2 +- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/mmcv/ops/roipoint_pool3d.py b/mmcv/ops/roipoint_pool3d.py index 0167deef6a..3c16f5fa67 100644 --- a/mmcv/ops/roipoint_pool3d.py +++ b/mmcv/ops/roipoint_pool3d.py @@ -45,11 +45,13 @@ def forward(self, points: torch.Tensor, point_features: torch.Tensor, class RoIPointPool3dFunction(Function): @staticmethod - def forward(ctx: Any, - points: torch.Tensor, - point_features: torch.Tensor, - boxes3d: torch.Tensor, - num_sampled_points: int = 512) -> Tuple[torch.Tensor]: + def forward( + ctx: Any, + points: torch.Tensor, + point_features: torch.Tensor, + boxes3d: torch.Tensor, + num_sampled_points: int = 512 + ) -> Tuple[torch.Tensor, torch.Tensor]: """ Args: points (torch.Tensor): Input points whose shape is (B, N, C). diff --git a/mmcv/ops/scatter_points.py b/mmcv/ops/scatter_points.py index de3d0f36ca..5d881bfe63 100644 --- a/mmcv/ops/scatter_points.py +++ b/mmcv/ops/scatter_points.py @@ -19,7 +19,7 @@ class _DynamicScatter(Function): def forward(ctx: Any, feats: torch.Tensor, coors: torch.Tensor, - reduce_type: str = 'max') -> Tuple[torch.Tensor]: + reduce_type: str = 'max') -> Tuple[torch.Tensor, torch.Tensor]: """convert kitti points(N, >=3) to voxels. Args: @@ -88,8 +88,9 @@ def __init__(self, voxel_size: List, point_cloud_range: List, self.point_cloud_range = point_cloud_range self.average_points = average_points - def forward_single(self, points: torch.Tensor, - coors: torch.Tensor) -> Tuple[torch.Tensor]: + def forward_single( + self, points: torch.Tensor, + coors: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """Scatters points into voxels. Args: @@ -106,7 +107,8 @@ def forward_single(self, points: torch.Tensor, reduce = 'mean' if self.average_points else 'max' return dynamic_scatter(points.contiguous(), coors.contiguous(), reduce) - def forward(self, points: torch.Tensor, coors: torch.Tensor) -> tuple: + def forward(self, points: torch.Tensor, + coors: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """Scatters points/features into voxels. Args: diff --git a/mmcv/ops/sparse_functional.py b/mmcv/ops/sparse_functional.py index 7bc74e7d86..5a80a545aa 100644 --- a/mmcv/ops/sparse_functional.py +++ b/mmcv/ops/sparse_functional.py @@ -27,8 +27,7 @@ class SparseConvFunction(Function): """ @staticmethod - def forward(ctx: Any, features: torch.Tensor, - filters: torch.nn.Parameter, + def forward(ctx: Any, features: torch.Tensor, filters: torch.nn.Parameter, indice_pairs: torch.Tensor, indice_pair_num: torch.Tensor, num_activate_out: torch.Tensor) -> torch.Tensor: """ @@ -60,8 +59,7 @@ def backward(ctx: Any, grad_output: torch.Tensor) -> tuple: class SparseInverseConvFunction(Function): @staticmethod - def forward(ctx: Any, features: torch.Tensor, - filters: torch.nn.Parameter, + def forward(ctx: Any, features: torch.Tensor, filters: torch.nn.Parameter, indice_pairs: torch.Tensor, indice_pair_num: torch.Tensor, num_activate_out: torch.Tensor) -> torch.Tensor: """ @@ -93,8 +91,7 @@ def backward(ctx: Any, grad_output: torch.Tensor) -> tuple: class SubMConvFunction(Function): @staticmethod - def forward(ctx: Any, features: torch.Tensor, - filters: torch.nn.Parameter, + def forward(ctx: Any, features: torch.Tensor, filters: torch.nn.Parameter, indice_pairs: torch.Tensor, indice_pair_num: torch.Tensor, num_activate_out: torch.Tensor) -> torch.Tensor: """ diff --git a/mmcv/ops/sparse_structure.py b/mmcv/ops/sparse_structure.py index 54229887c6..83907ab556 100644 --- a/mmcv/ops/sparse_structure.py +++ b/mmcv/ops/sparse_structure.py @@ -1,4 +1,4 @@ -from typing import Dict, List, Optional, Tuple, Union +from typing import List, Optional, Tuple, Union import numpy as np import torch