From e0cdf2419434c3e1a5882fd22d940c0d2bc22b76 Mon Sep 17 00:00:00 2001 From: Asklv Date: Tue, 28 Mar 2023 17:58:49 +0800 Subject: [PATCH 1/4] Update type hints on part 10. --- .../coders/bucketing_bbox_coder.py | 62 +++++++++------ .../coders/delta_xywh_bbox_coder.py | 76 +++++++++++-------- .../coders/distance_point_bbox_coder.py | 22 +++++- .../coders/legacy_delta_xywh_bbox_coder.py | 48 +++++++----- .../task_modules/coders/pseudo_bbox_coder.py | 12 ++- .../task_modules/coders/tblr_bbox_coder.py | 39 +++++++--- .../task_modules/coders/yolo_bbox_coder.py | 14 +++- 7 files changed, 174 insertions(+), 99 deletions(-) diff --git a/mmdet/models/task_modules/coders/bucketing_bbox_coder.py b/mmdet/models/task_modules/coders/bucketing_bbox_coder.py index 56abc372bdb..4fb314eca49 100644 --- a/mmdet/models/task_modules/coders/bucketing_bbox_coder.py +++ b/mmdet/models/task_modules/coders/bucketing_bbox_coder.py @@ -1,10 +1,14 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Optional, Tuple, Union + import numpy as np import torch import torch.nn.functional as F +from torch import Tensor from mmdet.registry import TASK_UTILS -from mmdet.structures.bbox import HorizontalBoxes, bbox_rescale, get_box_tensor +from mmdet.structures.bbox import (BaseBoxes, HorizontalBoxes, bbox_rescale, + get_box_tensor) from .base_bbox_coder import BaseBBoxCoder @@ -32,13 +36,13 @@ class BucketingBBoxCoder(BaseBBoxCoder): """ def __init__(self, - num_buckets, - scale_factor, - offset_topk=2, - offset_upperbound=1.0, - cls_ignore_neighbor=True, - clip_border=True, - **kwargs): + num_buckets: int, + scale_factor: int, + offset_topk: int = 2, + offset_upperbound: float = 1.0, + cls_ignore_neighbor: bool = True, + clip_border: Optional[bool] = True, + **kwargs) -> None: super().__init__(**kwargs) self.num_buckets = num_buckets self.scale_factor = scale_factor @@ -47,7 +51,8 @@ def __init__(self, self.cls_ignore_neighbor = cls_ignore_neighbor self.clip_border = clip_border - def encode(self, bboxes, gt_bboxes): + def encode(self, bboxes: Union[Tensor, BaseBoxes], + gt_bboxes: Union[Tensor, BaseBoxes]) -> Tuple[Tensor]: """Get bucketing estimation and fine regression targets during training. @@ -71,7 +76,12 @@ def encode(self, bboxes, gt_bboxes): self.cls_ignore_neighbor) return encoded_bboxes - def decode(self, bboxes, pred_bboxes, max_shape=None): + def decode( + self, + bboxes: Union[Tensor, BaseBoxes], + pred_bboxes: Tensor, + max_shape: Optional[Tuple[int]] = None + ) -> Union[Tensor, BaseBoxes]: """Apply transformation `pred_bboxes` to `boxes`. Args: boxes (torch.Tensor or :obj:`BaseBoxes`): Basic boxes. @@ -97,7 +107,9 @@ def decode(self, bboxes, pred_bboxes, max_shape=None): return bboxes, loc_confidence -def generat_buckets(proposals, num_buckets, scale_factor=1.0): +def generat_buckets(proposals: Tensor, + num_buckets: int, + scale_factor: float = 1.0) -> Tuple[Tensor]: """Generate buckets w.r.t bucket number and scale factor of proposals. Args: @@ -145,13 +157,13 @@ def generat_buckets(proposals, num_buckets, scale_factor=1.0): return bucket_w, bucket_h, l_buckets, r_buckets, t_buckets, d_buckets -def bbox2bucket(proposals, - gt, - num_buckets, - scale_factor, - offset_topk=2, - offset_upperbound=1.0, - cls_ignore_neighbor=True): +def bbox2bucket(proposals: Tensor, + gt: Tensor, + num_buckets: int, + scale_factor: float, + offset_topk: int = 2, + offset_upperbound: float = 1.0, + cls_ignore_neighbor: bool = True) -> Tuple[Tensor]: """Generate buckets estimation and fine regression targets. Args: @@ -268,13 +280,13 @@ def bbox2bucket(proposals, return offsets, offsets_weights, bucket_labels, bucket_cls_weights -def bucket2bbox(proposals, - cls_preds, - offset_preds, - num_buckets, - scale_factor=1.0, - max_shape=None, - clip_border=True): +def bucket2bbox(proposals: Tensor, + cls_preds: Tensor, + offset_preds: Tensor, + num_buckets: int, + scale_factor: float = 1.0, + max_shape: Tuple[int, int] = None, + clip_border: Optional[bool] = True) -> Tuple[Tensor]: """Apply bucketing estimation (cls preds) and fine regression (offset preds) to generate det bboxes. diff --git a/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py b/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py index 6bc9a9bdfb8..96e99201c37 100644 --- a/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py +++ b/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py @@ -1,11 +1,13 @@ # Copyright (c) OpenMMLab. All rights reserved. import warnings +from typing import Optional, Sequence, Tuple, Union import numpy as np import torch +from torch import Tensor from mmdet.registry import TASK_UTILS -from mmdet.structures.bbox import HorizontalBoxes, get_box_tensor +from mmdet.structures.bbox import BaseBoxes, HorizontalBoxes, get_box_tensor from .base_bbox_coder import BaseBBoxCoder @@ -32,12 +34,12 @@ class DeltaXYWHBBoxCoder(BaseBBoxCoder): """ def __init__(self, - target_means=(0., 0., 0., 0.), - target_stds=(1., 1., 1., 1.), - clip_border=True, - add_ctr_clamp=False, - ctr_clamp=32, - **kwargs): + target_means: Sequence[float] = (0., 0., 0., 0.), + target_stds: Sequence[float] = (1., 1., 1., 1.), + clip_border: Optional[bool] = True, + add_ctr_clamp: bool = False, + ctr_clamp: int = 32, + **kwargs) -> None: super().__init__(**kwargs) self.means = target_means self.stds = target_stds @@ -45,7 +47,8 @@ def __init__(self, self.add_ctr_clamp = add_ctr_clamp self.ctr_clamp = ctr_clamp - def encode(self, bboxes, gt_bboxes): + def encode(self, bboxes: Union[Tensor, BaseBoxes], + gt_bboxes: Union[Tensor, BaseBoxes]) -> Tensor: """Get box regression transformation deltas that can be used to transform the ``bboxes`` into the ``gt_bboxes``. @@ -65,11 +68,14 @@ def encode(self, bboxes, gt_bboxes): encoded_bboxes = bbox2delta(bboxes, gt_bboxes, self.means, self.stds) return encoded_bboxes - def decode(self, - bboxes, - pred_bboxes, - max_shape=None, - wh_ratio_clip=16 / 1000): + def decode( + self, + bboxes: Union[Tensor, BaseBoxes], + pred_bboxes: Tensor, + max_shape: Optional[Union[Sequence[int], Tensor, + Sequence[Sequence[int]]]] = None, + wh_ratio_clip: Optional[float] = 16 / 1000 + ) -> Union[Tensor, BaseBoxes]: """Apply transformation `pred_bboxes` to `boxes`. Args: @@ -123,7 +129,12 @@ def decode(self, return decoded_bboxes -def bbox2delta(proposals, gt, means=(0., 0., 0., 0.), stds=(1., 1., 1., 1.)): +def bbox2delta( + proposals: Tensor, + gt: Tensor, + means: Sequence[float] = (0., 0., 0., 0.), + stds: Sequence[float] = (1., 1., 1., 1.) +) -> Tensor: """Compute deltas of proposals w.r.t. gt. We usually compute the deltas of x, y, w, h of proposals w.r.t ground @@ -168,15 +179,15 @@ def bbox2delta(proposals, gt, means=(0., 0., 0., 0.), stds=(1., 1., 1., 1.)): return deltas -def delta2bbox(rois, - deltas, - means=(0., 0., 0., 0.), - stds=(1., 1., 1., 1.), - max_shape=None, - wh_ratio_clip=16 / 1000, - clip_border=True, - add_ctr_clamp=False, - ctr_clamp=32): +def delta2bbox(rois: Tensor, + deltas: Tensor, + means: Sequence[float] = (0., 0., 0., 0.), + stds: Sequence[float] = (1., 1., 1., 1.), + max_shape: Tuple[int, int] = None, + wh_ratio_clip: float = 16 / 1000, + clip_border: Optional[bool] = True, + add_ctr_clamp: bool = False, + ctr_clamp: int = 32) -> Tensor: """Apply deltas to shift/scale base boxes. Typically the rois are anchor or proposed bounding boxes and the deltas are @@ -267,15 +278,16 @@ def delta2bbox(rois, return bboxes -def onnx_delta2bbox(rois, - deltas, - means=(0., 0., 0., 0.), - stds=(1., 1., 1., 1.), - max_shape=None, - wh_ratio_clip=16 / 1000, - clip_border=True, - add_ctr_clamp=False, - ctr_clamp=32): +def onnx_delta2bbox(rois: Tensor, + deltas: Tensor, + means: Sequence[float] = (0., 0., 0., 0.), + stds: Sequence[float] = (1., 1., 1., 1.), + max_shape: Optional[Union[Sequence[int], Tensor, + Sequence[Sequence[int]]]] = None, + wh_ratio_clip: float = 16 / 1000, + clip_border: Optional[bool] = True, + add_ctr_clamp: bool = False, + ctr_clamp: int = 32) -> Tensor: """Apply deltas to shift/scale base boxes. Typically the rois are anchor or proposed bounding boxes and the deltas are diff --git a/mmdet/models/task_modules/coders/distance_point_bbox_coder.py b/mmdet/models/task_modules/coders/distance_point_bbox_coder.py index ff2bb54660c..ef74ca6abb1 100644 --- a/mmdet/models/task_modules/coders/distance_point_bbox_coder.py +++ b/mmdet/models/task_modules/coders/distance_point_bbox_coder.py @@ -1,6 +1,10 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Optional, Sequence, Union + +from torch import Tensor + from mmdet.registry import TASK_UTILS -from mmdet.structures.bbox import (HorizontalBoxes, bbox2distance, +from mmdet.structures.bbox import (BaseBoxes, HorizontalBoxes, bbox2distance, distance2bbox, get_box_tensor) from .base_bbox_coder import BaseBBoxCoder @@ -17,11 +21,15 @@ class DistancePointBBoxCoder(BaseBBoxCoder): border of the image. Defaults to True. """ - def __init__(self, clip_border=True, **kwargs): + def __init__(self, clip_border: Optional[bool] = True, **kwargs) -> None: super().__init__(**kwargs) self.clip_border = clip_border - def encode(self, points, gt_bboxes, max_dis=None, eps=0.1): + def encode(self, + points: Tensor, + gt_bboxes: Union[Tensor, BaseBoxes], + max_dis: float = None, + eps: float = 0.1) -> Tensor: """Encode bounding box to distances. Args: @@ -41,7 +49,13 @@ def encode(self, points, gt_bboxes, max_dis=None, eps=0.1): assert gt_bboxes.size(-1) == 4 return bbox2distance(points, gt_bboxes, max_dis, eps) - def decode(self, points, pred_bboxes, max_shape=None): + def decode( + self, + points: Tensor, + pred_bboxes: Tensor, + max_shape: Optional[Union[Sequence[int], Tensor, + Sequence[Sequence[int]]]] = None + ) -> Union[Tensor, BaseBoxes]: """Decode distance prediction to bounding box. Args: diff --git a/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py b/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py index 154016dd6fd..19928925b1b 100644 --- a/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py +++ b/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py @@ -1,9 +1,12 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Optional, Sequence, Tuple, Union + import numpy as np import torch +from torch import Tensor from mmdet.registry import TASK_UTILS -from mmdet.structures.bbox import HorizontalBoxes, get_box_tensor +from mmdet.structures.bbox import BaseBoxes, HorizontalBoxes, get_box_tensor from .base_bbox_coder import BaseBBoxCoder @@ -32,14 +35,15 @@ class LegacyDeltaXYWHBBoxCoder(BaseBBoxCoder): """ def __init__(self, - target_means=(0., 0., 0., 0.), - target_stds=(1., 1., 1., 1.), - **kwargs): + target_means: Sequence[float] = (0., 0., 0., 0.), + target_stds: Sequence[float] = (1., 1., 1., 1.), + **kwargs) -> None: super().__init__(**kwargs) self.means = target_means self.stds = target_stds - def encode(self, bboxes, gt_bboxes): + def encode(self, bboxes: Union[Tensor, BaseBoxes], + gt_bboxes: Union[Tensor, BaseBoxes]) -> Tensor: """Get box regression transformation deltas that can be used to transform the ``bboxes`` into the ``gt_bboxes``. @@ -60,11 +64,13 @@ def encode(self, bboxes, gt_bboxes): self.stds) return encoded_bboxes - def decode(self, - bboxes, - pred_bboxes, - max_shape=None, - wh_ratio_clip=16 / 1000): + def decode( + self, + bboxes: Union[Tensor, BaseBoxes], + pred_bboxes: Tensor, + max_shape: Optional[Tuple[int]] = None, + wh_ratio_clip: Optional[float] = 16 / 1000 + ) -> Union[Tensor, BaseBoxes]: """Apply transformation `pred_bboxes` to `boxes`. Args: @@ -91,10 +97,12 @@ def decode(self, return decoded_bboxes -def legacy_bbox2delta(proposals, - gt, - means=(0., 0., 0., 0.), - stds=(1., 1., 1., 1.)): +def legacy_bbox2delta( + proposals: Tensor, + gt: Tensor, + means: Sequence[float] = (0., 0., 0., 0.), + stds: Sequence[float] = (1., 1., 1., 1.) +) -> Tensor: """Compute deltas of proposals w.r.t. gt in the MMDet V1.x manner. We usually compute the deltas of x, y, w, h of proposals w.r.t ground @@ -139,12 +147,12 @@ def legacy_bbox2delta(proposals, return deltas -def legacy_delta2bbox(rois, - deltas, - means=(0., 0., 0., 0.), - stds=(1., 1., 1., 1.), - max_shape=None, - wh_ratio_clip=16 / 1000): +def legacy_delta2bbox(rois: Tensor, + deltas: Tensor, + means: Sequence[float] = (0., 0., 0., 0.), + stds: Sequence[float] = (1., 1., 1., 1.), + max_shape: Tuple[int, int] = None, + wh_ratio_clip: float = 16 / 1000): """Apply deltas to shift/scale base boxes in the MMDet V1.x manner. Typically the rois are anchor or proposed bounding boxes and the deltas are diff --git a/mmdet/models/task_modules/coders/pseudo_bbox_coder.py b/mmdet/models/task_modules/coders/pseudo_bbox_coder.py index 0eeeee484dd..9ee74311f6d 100644 --- a/mmdet/models/task_modules/coders/pseudo_bbox_coder.py +++ b/mmdet/models/task_modules/coders/pseudo_bbox_coder.py @@ -1,6 +1,10 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Union + +from torch import Tensor + from mmdet.registry import TASK_UTILS -from mmdet.structures.bbox import HorizontalBoxes, get_box_tensor +from mmdet.structures.bbox import BaseBoxes, HorizontalBoxes, get_box_tensor from .base_bbox_coder import BaseBBoxCoder @@ -11,12 +15,14 @@ class PseudoBBoxCoder(BaseBBoxCoder): def __init__(self, **kwargs): super().__init__(**kwargs) - def encode(self, bboxes, gt_bboxes): + def encode(self, bboxes: Tensor, gt_bboxes: Union[Tensor, + BaseBoxes]) -> Tensor: """torch.Tensor: return the given ``bboxes``""" gt_bboxes = get_box_tensor(gt_bboxes) return gt_bboxes - def decode(self, bboxes, pred_bboxes): + def decode(self, bboxes: Tensor, pred_bboxes: Union[Tensor, + BaseBoxes]) -> Tensor: """torch.Tensor: return the given ``pred_bboxes``""" if self.use_box_type: pred_bboxes = HorizontalBoxes(pred_bboxes) diff --git a/mmdet/models/task_modules/coders/tblr_bbox_coder.py b/mmdet/models/task_modules/coders/tblr_bbox_coder.py index f4a92ff14e3..74b388f7bad 100644 --- a/mmdet/models/task_modules/coders/tblr_bbox_coder.py +++ b/mmdet/models/task_modules/coders/tblr_bbox_coder.py @@ -1,8 +1,11 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Optional, Sequence, Union + import torch +from torch import Tensor from mmdet.registry import TASK_UTILS -from mmdet.structures.bbox import HorizontalBoxes, get_box_tensor +from mmdet.structures.bbox import BaseBoxes, HorizontalBoxes, get_box_tensor from .base_bbox_coder import BaseBBoxCoder @@ -23,12 +26,16 @@ class TBLRBBoxCoder(BaseBBoxCoder): border of the image. Defaults to True. """ - def __init__(self, normalizer=4.0, clip_border=True, **kwargs): + def __init__(self, + normalizer: Union[Sequence[float], float] = 4.0, + clip_border: bool = True, + **kwargs) -> None: super().__init__(**kwargs) self.normalizer = normalizer self.clip_border = clip_border - def encode(self, bboxes, gt_bboxes): + def encode(self, bboxes: Union[Tensor, BaseBoxes], + gt_bboxes: Union[Tensor, BaseBoxes]) -> Tensor: """Get box regression transformation deltas that can be used to transform the ``bboxes`` into the ``gt_bboxes`` in the (top, left, bottom, right) order. @@ -50,7 +57,13 @@ def encode(self, bboxes, gt_bboxes): bboxes, gt_bboxes, normalizer=self.normalizer) return encoded_bboxes - def decode(self, bboxes, pred_bboxes, max_shape=None): + def decode( + self, + bboxes: Union[Tensor, BaseBoxes], + pred_bboxes: Tensor, + max_shape: Optional[Union[Sequence[int], Tensor, + Sequence[Sequence[int]]]] = None + ) -> Union[Tensor, BaseBoxes]: """Apply transformation `pred_bboxes` to `boxes`. Args: @@ -80,7 +93,10 @@ def decode(self, bboxes, pred_bboxes, max_shape=None): return decoded_bboxes -def bboxes2tblr(priors, gts, normalizer=4.0, normalize_by_wh=True): +def bboxes2tblr(priors: Tensor, + gts: Tensor, + normalizer: Union[Sequence[float], float] = 4.0, + normalize_by_wh: bool = True) -> Tensor: """Encode ground truth boxes to tblr coordinate. It first convert the gt coordinate to tblr format, @@ -126,12 +142,13 @@ def bboxes2tblr(priors, gts, normalizer=4.0, normalize_by_wh=True): return loc / normalizer -def tblr2bboxes(priors, - tblr, - normalizer=4.0, - normalize_by_wh=True, - max_shape=None, - clip_border=True): +def tblr2bboxes(priors: Tensor, + tblr: Tensor, + normalizer: Union[Sequence[float], float] = 4.0, + normalize_by_wh: bool = True, + max_shape: Optional[Union[Sequence[int], Tensor, + Sequence[Sequence[int]]]] = None, + clip_border: bool = True) -> Tensor: """Decode tblr outputs to prediction boxes. The process includes 3 steps: 1) De-normalize tblr coordinates by diff --git a/mmdet/models/task_modules/coders/yolo_bbox_coder.py b/mmdet/models/task_modules/coders/yolo_bbox_coder.py index b903c16dbf2..2e1c766789b 100644 --- a/mmdet/models/task_modules/coders/yolo_bbox_coder.py +++ b/mmdet/models/task_modules/coders/yolo_bbox_coder.py @@ -1,8 +1,11 @@ # Copyright (c) OpenMMLab. All rights reserved. +from typing import Union + import torch +from torch import Tensor from mmdet.registry import TASK_UTILS -from mmdet.structures.bbox import HorizontalBoxes, get_box_tensor +from mmdet.structures.bbox import BaseBoxes, HorizontalBoxes, get_box_tensor from .base_bbox_coder import BaseBBoxCoder @@ -19,11 +22,13 @@ class YOLOBBoxCoder(BaseBBoxCoder): eps (float): Min value of cx, cy when encoding. """ - def __init__(self, eps=1e-6, **kwargs): + def __init__(self, eps: float = 1e-6, **kwargs): super().__init__(**kwargs) self.eps = eps - def encode(self, bboxes, gt_bboxes, stride): + def encode(self, bboxes: Union[Tensor, BaseBoxes], + gt_bboxes: Union[Tensor, BaseBoxes], + stride: Union[Tensor, int]) -> Tensor: """Get box regression transformation deltas that can be used to transform the ``bboxes`` into the ``gt_bboxes``. @@ -59,7 +64,8 @@ def encode(self, bboxes, gt_bboxes, stride): [x_center_target, y_center_target, w_target, h_target], dim=-1) return encoded_bboxes - def decode(self, bboxes, pred_bboxes, stride): + def decode(self, bboxes: Union[Tensor, BaseBoxes], pred_bboxes: Tensor, + stride: Union[Tensor, int]) -> Union[Tensor, BaseBoxes]: """Apply transformation `pred_bboxes` to `boxes`. Args: From 2ab601b436936d585334efaa83a4145672936b80 Mon Sep 17 00:00:00 2001 From: Asklv Date: Tue, 28 Mar 2023 22:40:09 +0800 Subject: [PATCH 2/4] Fix type hints. --- .../models/task_modules/coders/legacy_delta_xywh_bbox_coder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py b/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py index 19928925b1b..1ae83b932db 100644 --- a/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py +++ b/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py @@ -152,7 +152,7 @@ def legacy_delta2bbox(rois: Tensor, means: Sequence[float] = (0., 0., 0., 0.), stds: Sequence[float] = (1., 1., 1., 1.), max_shape: Tuple[int, int] = None, - wh_ratio_clip: float = 16 / 1000): + wh_ratio_clip: float = 16 / 1000) -> Tensor: """Apply deltas to shift/scale base boxes in the MMDet V1.x manner. Typically the rois are anchor or proposed bounding boxes and the deltas are From a8376eb0548e96a885103fbf93a290ed2effaaa8 Mon Sep 17 00:00:00 2001 From: Asklv Date: Thu, 30 Mar 2023 12:06:27 +0800 Subject: [PATCH 3/4] Fix type hints --- .../models/task_modules/coders/bucketing_bbox_coder.py | 10 +++++----- .../task_modules/coders/delta_xywh_bbox_coder.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mmdet/models/task_modules/coders/bucketing_bbox_coder.py b/mmdet/models/task_modules/coders/bucketing_bbox_coder.py index 4fb314eca49..e7d958d66b9 100644 --- a/mmdet/models/task_modules/coders/bucketing_bbox_coder.py +++ b/mmdet/models/task_modules/coders/bucketing_bbox_coder.py @@ -77,11 +77,11 @@ def encode(self, bboxes: Union[Tensor, BaseBoxes], return encoded_bboxes def decode( - self, - bboxes: Union[Tensor, BaseBoxes], - pred_bboxes: Tensor, - max_shape: Optional[Tuple[int]] = None - ) -> Union[Tensor, BaseBoxes]: + self, + bboxes: Union[Tensor, BaseBoxes], + pred_bboxes: Tensor, + max_shape: Optional[Tuple[int]] = None + ) -> Tuple[Union[Tensor, BaseBoxes], Tensor]: """Apply transformation `pred_bboxes` to `boxes`. Args: boxes (torch.Tensor or :obj:`BaseBoxes`): Basic boxes. diff --git a/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py b/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py index 96e99201c37..6fd66684e49 100644 --- a/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py +++ b/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py @@ -36,7 +36,7 @@ class DeltaXYWHBBoxCoder(BaseBBoxCoder): def __init__(self, target_means: Sequence[float] = (0., 0., 0., 0.), target_stds: Sequence[float] = (1., 1., 1., 1.), - clip_border: Optional[bool] = True, + clip_border: bool = True, add_ctr_clamp: bool = False, ctr_clamp: int = 32, **kwargs) -> None: From e3ca3ce2611cecc9f7fcafb42fd66b73a6b875ad Mon Sep 17 00:00:00 2001 From: Asklv Date: Mon, 3 Apr 2023 17:33:42 +0800 Subject: [PATCH 4/4] Fix max_shape type hints. --- mmdet/models/task_modules/coders/bucketing_bbox_coder.py | 9 +++++---- .../models/task_modules/coders/delta_xywh_bbox_coder.py | 7 ++++--- .../task_modules/coders/distance_point_bbox_coder.py | 2 +- .../task_modules/coders/legacy_delta_xywh_bbox_coder.py | 9 ++++++--- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/mmdet/models/task_modules/coders/bucketing_bbox_coder.py b/mmdet/models/task_modules/coders/bucketing_bbox_coder.py index e7d958d66b9..4044e1cd91d 100644 --- a/mmdet/models/task_modules/coders/bucketing_bbox_coder.py +++ b/mmdet/models/task_modules/coders/bucketing_bbox_coder.py @@ -1,5 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. -from typing import Optional, Tuple, Union +from typing import Optional, Sequence, Tuple, Union import numpy as np import torch @@ -41,7 +41,7 @@ def __init__(self, offset_topk: int = 2, offset_upperbound: float = 1.0, cls_ignore_neighbor: bool = True, - clip_border: Optional[bool] = True, + clip_border: bool = True, **kwargs) -> None: super().__init__(**kwargs) self.num_buckets = num_buckets @@ -285,8 +285,9 @@ def bucket2bbox(proposals: Tensor, offset_preds: Tensor, num_buckets: int, scale_factor: float = 1.0, - max_shape: Tuple[int, int] = None, - clip_border: Optional[bool] = True) -> Tuple[Tensor]: + max_shape: Optional[Union[Sequence[int], Tensor, + Sequence[Sequence[int]]]] = None, + clip_border: bool = True) -> Tuple[Tensor]: """Apply bucketing estimation (cls preds) and fine regression (offset preds) to generate det bboxes. diff --git a/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py b/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py index 6fd66684e49..f65748ac347 100644 --- a/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py +++ b/mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py @@ -1,6 +1,6 @@ # Copyright (c) OpenMMLab. All rights reserved. import warnings -from typing import Optional, Sequence, Tuple, Union +from typing import Optional, Sequence, Union import numpy as np import torch @@ -183,9 +183,10 @@ def delta2bbox(rois: Tensor, deltas: Tensor, means: Sequence[float] = (0., 0., 0., 0.), stds: Sequence[float] = (1., 1., 1., 1.), - max_shape: Tuple[int, int] = None, + max_shape: Optional[Union[Sequence[int], Tensor, + Sequence[Sequence[int]]]] = None, wh_ratio_clip: float = 16 / 1000, - clip_border: Optional[bool] = True, + clip_border: bool = True, add_ctr_clamp: bool = False, ctr_clamp: int = 32) -> Tensor: """Apply deltas to shift/scale base boxes. diff --git a/mmdet/models/task_modules/coders/distance_point_bbox_coder.py b/mmdet/models/task_modules/coders/distance_point_bbox_coder.py index ef74ca6abb1..ab26bf4b96c 100644 --- a/mmdet/models/task_modules/coders/distance_point_bbox_coder.py +++ b/mmdet/models/task_modules/coders/distance_point_bbox_coder.py @@ -28,7 +28,7 @@ def __init__(self, clip_border: Optional[bool] = True, **kwargs) -> None: def encode(self, points: Tensor, gt_bboxes: Union[Tensor, BaseBoxes], - max_dis: float = None, + max_dis: Optional[float] = None, eps: float = 0.1) -> Tensor: """Encode bounding box to distances. diff --git a/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py b/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py index 1ae83b932db..9eb1bedb3fb 100644 --- a/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py +++ b/mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py @@ -1,5 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. -from typing import Optional, Sequence, Tuple, Union +from typing import Optional, Sequence, Union import numpy as np import torch @@ -68,7 +68,8 @@ def decode( self, bboxes: Union[Tensor, BaseBoxes], pred_bboxes: Tensor, - max_shape: Optional[Tuple[int]] = None, + max_shape: Optional[Union[Sequence[int], Tensor, + Sequence[Sequence[int]]]] = None, wh_ratio_clip: Optional[float] = 16 / 1000 ) -> Union[Tensor, BaseBoxes]: """Apply transformation `pred_bboxes` to `boxes`. @@ -151,7 +152,9 @@ def legacy_delta2bbox(rois: Tensor, deltas: Tensor, means: Sequence[float] = (0., 0., 0., 0.), stds: Sequence[float] = (1., 1., 1., 1.), - max_shape: Tuple[int, int] = None, + max_shape: Optional[ + Union[Sequence[int], Tensor, + Sequence[Sequence[int]]]] = None, wh_ratio_clip: float = 16 / 1000) -> Tensor: """Apply deltas to shift/scale base boxes in the MMDet V1.x manner.