diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 56e66cc155..e581ce437a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,12 +4,8 @@ repos: rev: 3.8.3 hooks: - id: flake8 - - repo: https://github.com/asottile/seed-isort-config - rev: v2.2.0 - hooks: - - id: seed-isort-config - - repo: https://github.com/timothycrosley/isort - rev: 4.3.21 + - repo: https://github.com/PyCQA/isort + rev: 5.10.1 hooks: - id: isort - repo: https://github.com/pre-commit/mirrors-yapf diff --git a/mmcv/cnn/bricks/transformer.py b/mmcv/cnn/bricks/transformer.py index 4b30f1c2c2..70c6623c74 100644 --- a/mmcv/cnn/bricks/transformer.py +++ b/mmcv/cnn/bricks/transformer.py @@ -19,7 +19,8 @@ # Avoid BC-breaking of importing MultiScaleDeformableAttention from this file try: - from mmcv.ops.multi_scale_deform_attn import MultiScaleDeformableAttention # noqa F401 + from mmcv.ops.multi_scale_deform_attn import \ + MultiScaleDeformableAttention # noqa F401 warnings.warn( ImportWarning( '``MultiScaleDeformableAttention`` has been moved to ' diff --git a/mmcv/fileio/handlers/yaml_handler.py b/mmcv/fileio/handlers/yaml_handler.py index c5aa2eea1e..60911e7e60 100644 --- a/mmcv/fileio/handlers/yaml_handler.py +++ b/mmcv/fileio/handlers/yaml_handler.py @@ -2,7 +2,8 @@ import yaml try: - from yaml import CLoader as Loader, CDumper as Dumper + from yaml import CDumper as Dumper + from yaml import CLoader as Loader except ImportError: from yaml import Loader, Dumper diff --git a/mmcv/onnx/symbolic.py b/mmcv/onnx/symbolic.py index 94cc1c620d..efb947d9de 100644 --- a/mmcv/onnx/symbolic.py +++ b/mmcv/onnx/symbolic.py @@ -409,8 +409,8 @@ def cummin(g, input, dim): @parse_args('v', 'v', 'is') def roll(g, input, shifts, dims): - from torch.onnx.symbolic_opset9 import squeeze from packaging import version + from torch.onnx.symbolic_opset9 import squeeze input_shape = g.op('Shape', input) need_flatten = len(dims) == 0 diff --git a/mmcv/ops/nms.py b/mmcv/ops/nms.py index 6ea21bc198..13dca55c68 100644 --- a/mmcv/ops/nms.py +++ b/mmcv/ops/nms.py @@ -48,6 +48,7 @@ def symbolic(g, bboxes, scores, iou_threshold, offset, score_threshold, offset_i=int(offset)) else: from torch.onnx.symbolic_opset9 import select, squeeze, unsqueeze + from ..onnx.onnx_utils.symbolic_helper import _size_helper boxes = unsqueeze(g, bboxes, 0) diff --git a/mmcv/ops/roi_align.py b/mmcv/ops/roi_align.py index 0755aefc66..12beb8e52e 100644 --- a/mmcv/ops/roi_align.py +++ b/mmcv/ops/roi_align.py @@ -30,9 +30,10 @@ def symbolic(g, input, rois, output_size, spatial_scale, sampling_ratio, mode_s=pool_mode, aligned_i=aligned) else: - from torch.onnx.symbolic_opset9 import sub, squeeze - from torch.onnx.symbolic_helper import _slice_helper from torch.onnx import TensorProtoDataType + from torch.onnx.symbolic_helper import _slice_helper + from torch.onnx.symbolic_opset9 import squeeze, sub + # batch_indices = rois[:, 0].long() batch_indices = _slice_helper( g, rois, axes=[1], starts=[0], ends=[1]) diff --git a/mmcv/utils/__init__.py b/mmcv/utils/__init__.py index 478f015111..019e77f568 100644 --- a/mmcv/utils/__init__.py +++ b/mmcv/utils/__init__.py @@ -37,16 +37,20 @@ ] else: from .env import collect_env + from .hub import load_url from .logging import get_logger, print_log from .parrots_jit import jit, skip_no_elena - from .parrots_wrapper import ( - TORCH_VERSION, BuildExtension, CppExtension, CUDAExtension, DataLoader, - PoolDataLoader, SyncBatchNorm, _AdaptiveAvgPoolNd, _AdaptiveMaxPoolNd, - _AvgPoolNd, _BatchNorm, _ConvNd, _ConvTransposeMixin, _InstanceNorm, - _MaxPoolNd, get_build_config, is_rocm_pytorch, _get_cuda_home) + # yapf: disable + from .parrots_wrapper import (TORCH_VERSION, BuildExtension, CppExtension, + CUDAExtension, DataLoader, PoolDataLoader, + SyncBatchNorm, _AdaptiveAvgPoolNd, + _AdaptiveMaxPoolNd, _AvgPoolNd, _BatchNorm, + _ConvNd, _ConvTransposeMixin, _get_cuda_home, + _InstanceNorm, _MaxPoolNd, get_build_config, + is_rocm_pytorch) + # yapf: enable from .registry import Registry, build_from_cfg from .trace import is_jit_tracing - from .hub import load_url __all__ = [ 'Config', 'ConfigDict', 'DictAction', 'collect_env', 'get_logger', 'print_log', 'is_str', 'iter_cast', 'list_cast', 'tuple_cast', diff --git a/mmcv/utils/hub.py b/mmcv/utils/hub.py index b5a91b5175..f8f1505c31 100644 --- a/mmcv/utils/hub.py +++ b/mmcv/utils/hub.py @@ -10,12 +10,13 @@ '1.7.0'): # Modified from https://github.com/pytorch/pytorch/blob/master/torch/hub.py import os - import torch - import warnings - from urllib.parse import urlparse import sys + import warnings import zipfile - from torch.hub import download_url_to_file, _get_torch_home, HASH_REGEX + from urllib.parse import urlparse + + import torch + from torch.hub import HASH_REGEX, _get_torch_home, download_url_to_file # Hub used to support automatically extracts from zipfile manually # compressed by users. The legacy zip format expects only one file from diff --git a/mmcv/utils/parrots_wrapper.py b/mmcv/utils/parrots_wrapper.py index 93c97640d4..cf6bf9ef86 100644 --- a/mmcv/utils/parrots_wrapper.py +++ b/mmcv/utils/parrots_wrapper.py @@ -83,8 +83,8 @@ def _get_norm(): from parrots.nn.modules.batchnorm import _BatchNorm, _InstanceNorm SyncBatchNorm_ = torch.nn.SyncBatchNorm2d else: - from torch.nn.modules.instancenorm import _InstanceNorm from torch.nn.modules.batchnorm import _BatchNorm + from torch.nn.modules.instancenorm import _InstanceNorm SyncBatchNorm_ = torch.nn.SyncBatchNorm return _BatchNorm, _InstanceNorm, SyncBatchNorm_ diff --git a/mmcv/utils/testing.py b/mmcv/utils/testing.py index a27f936da8..7b64e8fae3 100644 --- a/mmcv/utils/testing.py +++ b/mmcv/utils/testing.py @@ -111,8 +111,9 @@ def assert_is_norm_layer(module) -> bool: Returns: bool: Whether the module is a norm layer. """ - from .parrots_wrapper import _BatchNorm, _InstanceNorm from torch.nn import GroupNorm, LayerNorm + + from .parrots_wrapper import _BatchNorm, _InstanceNorm norm_layer_candidates = (_BatchNorm, _InstanceNorm, GroupNorm, LayerNorm) return isinstance(module, norm_layer_candidates) diff --git a/setup.cfg b/setup.cfg index 74f5a88506..bfce55261d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,7 +12,7 @@ split_before_expression_after_opening_paren = true [isort] line_length = 79 multi_line_output = 0 -known_standard_library = pkg_resources,setuptools,logging,os,warnings,abc +extra_standard_library = pkg_resources,setuptools,logging,os,warnings,abc known_first_party = mmcv known_third_party = addict,cv2,numpy,onnx,onnxruntime,packaging,pytest,pytorch_sphinx_theme,scipy,sphinx,tensorrt,torch,torchvision,yaml,yapf no_lines_before = STDLIB,LOCALFOLDER diff --git a/setup.py b/setup.py index f4691fb5ff..22437a896f 100644 --- a/setup.py +++ b/setup.py @@ -184,6 +184,7 @@ def get_extensions(): if EXT_TYPE == 'parrots': ext_name = 'mmcv._ext' from parrots.utils.build_extension import Extension + # new parrots op impl do not use MMCV_USE_PARROTS # define_macros = [('MMCV_USE_PARROTS', None)] define_macros = [] @@ -314,8 +315,8 @@ def get_extensions(): if EXT_TYPE == 'pytorch' and os.getenv('MMCV_WITH_ORT', '0') != '0': ext_name = 'mmcv._ext_ort' - from torch.utils.cpp_extension import library_paths, include_paths import onnxruntime + from torch.utils.cpp_extension import include_paths, library_paths library_dirs = [] libraries = [] include_dirs = [] diff --git a/tests/test_image/test_photometric.py b/tests/test_image/test_photometric.py index 71c0752de7..ee15d495ff 100644 --- a/tests/test_image/test_photometric.py +++ b/tests/test_image/test_photometric.py @@ -112,7 +112,7 @@ def test_imequalize(self, nb_rand_test=100): def _imequalize(img): # equalize the image using PIL.ImageOps.equalize - from PIL import ImageOps, Image + from PIL import Image, ImageOps img = Image.fromarray(img) equalized_img = np.asarray(ImageOps.equalize(img)) return equalized_img @@ -141,8 +141,8 @@ def test_adjust_brightness(self, nb_rand_test=100): def _adjust_brightness(img, factor): # adjust the brightness of image using # PIL.ImageEnhance.Brightness - from PIL.ImageEnhance import Brightness from PIL import Image + from PIL.ImageEnhance import Brightness img = Image.fromarray(img) brightened_img = Brightness(img).enhance(factor) return np.asarray(brightened_img) @@ -169,8 +169,9 @@ def _adjust_brightness(img, factor): def test_adjust_contrast(self, nb_rand_test=100): def _adjust_contrast(img, factor): - from PIL.ImageEnhance import Contrast from PIL import Image + from PIL.ImageEnhance import Contrast + # Image.fromarray defaultly supports RGB, not BGR. # convert from BGR to RGB img = Image.fromarray(img[..., ::-1], mode='RGB') @@ -204,8 +205,9 @@ def _adjust_contrast(img, factor): def test_auto_contrast(self, nb_rand_test=100): def _auto_contrast(img, cutoff=0): - from PIL.ImageOps import autocontrast from PIL import Image + from PIL.ImageOps import autocontrast + # Image.fromarray defaultly supports RGB, not BGR. # convert from BGR to RGB img = Image.fromarray(img[..., ::-1], mode='RGB') @@ -250,8 +252,8 @@ def test_adjust_sharpness(self, nb_rand_test=100): def _adjust_sharpness(img, factor): # adjust the sharpness of image using # PIL.ImageEnhance.Sharpness - from PIL.ImageEnhance import Sharpness from PIL import Image + from PIL.ImageEnhance import Sharpness img = Image.fromarray(img) sharpened_img = Sharpness(img).enhance(factor) return np.asarray(sharpened_img) diff --git a/tests/test_ops/test_border_align.py b/tests/test_ops/test_border_align.py index d8c2c0b247..410f0285f1 100644 --- a/tests/test_ops/test_border_align.py +++ b/tests/test_ops/test_border_align.py @@ -51,7 +51,7 @@ def _test_border_align_allclose(device, dtype, pool_size): if not torch.cuda.is_available() and device == 'cuda': pytest.skip('test requires GPU') try: - from mmcv.ops import border_align, BorderAlign + from mmcv.ops import BorderAlign, border_align except ModuleNotFoundError: pytest.skip('BorderAlign op is not successfully compiled') diff --git a/tests/test_runner/test_basemodule.py b/tests/test_runner/test_basemodule.py index 427cefb6c2..845086fe3a 100644 --- a/tests/test_runner/test_basemodule.py +++ b/tests/test_runner/test_basemodule.py @@ -88,9 +88,11 @@ def __init__(self, def test_initilization_info_logger(): # 'override' has higher priority + import os + import torch.nn as nn + from mmcv.utils.logging import get_logger - import os class OverloadInitConv(nn.Conv2d, BaseModule): diff --git a/tests/test_runner/test_checkpoint.py b/tests/test_runner/test_checkpoint.py index 0d9d58dcc0..99408d3cc2 100644 --- a/tests/test_runner/test_checkpoint.py +++ b/tests/test_runner/test_checkpoint.py @@ -197,7 +197,6 @@ def __init__(self): def test_load_checkpoint(): import os - import re import tempfile @@ -230,7 +229,6 @@ def __init__(self): def test_load_checkpoint_metadata(): import os - import tempfile from mmcv.runner import load_checkpoint, save_checkpoint @@ -301,7 +299,6 @@ def _load_from_state_dict(self, state_dict, prefix, local_metadata, def test_load_classes_name(): import os - import tempfile from mmcv.runner import load_checkpoint, save_checkpoint @@ -335,7 +332,6 @@ def test_load_classes_name(): def test_checkpoint_loader(): import os - import tempfile from mmcv.runner import CheckpointLoader, _load_checkpoint, save_checkpoint diff --git a/tests/test_utils/test_misc.py b/tests/test_utils/test_misc.py index 6070624c3e..2b14c00778 100644 --- a/tests/test_utils/test_misc.py +++ b/tests/test_utils/test_misc.py @@ -132,7 +132,6 @@ def func_c(): def test_import_modules_from_strings(): # multiple imports import os.path as osp_ - import sys as sys_ osp, sys = mmcv.import_modules_from_strings(['os.path', 'sys']) assert osp == osp_