Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update type hints on part 10. #10040

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions mmdet/models/task_modules/coders/bucketing_bbox_coder.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Copyright (c) OpenMMLab. All rights reserved.
from typing import Optional, Sequence, 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


Expand Down Expand Up @@ -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: bool = True,
**kwargs) -> None:
super().__init__(**kwargs)
self.num_buckets = num_buckets
self.scale_factor = scale_factor
Expand All @@ -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.

Expand All @@ -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
) -> Tuple[Union[Tensor, BaseBoxes], Tensor]:
"""Apply transformation `pred_bboxes` to `boxes`.
Args:
boxes (torch.Tensor or :obj:`BaseBoxes`): Basic boxes.
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -268,13 +280,14 @@ 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: 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.

Expand Down
77 changes: 45 additions & 32 deletions mmdet/models/task_modules/coders/delta_xywh_bbox_coder.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Copyright (c) OpenMMLab. All rights reserved.
import warnings
from typing import Optional, Sequence, 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


Expand All @@ -32,20 +34,21 @@ 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: bool = True,
add_ctr_clamp: bool = False,
ctr_clamp: int = 32,
**kwargs) -> None:
super().__init__(**kwargs)
self.means = target_means
self.stds = target_stds
self.clip_border = clip_border
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``.

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -168,15 +179,16 @@ 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: Optional[Union[Sequence[int], Tensor,
Sequence[Sequence[int]]]] = None,
wh_ratio_clip: float = 16 / 1000,
clip_border: 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
Expand Down Expand Up @@ -267,15 +279,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,
hhaAndroid marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
22 changes: 18 additions & 4 deletions mmdet/models/task_modules/coders/distance_point_bbox_coder.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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: Optional[float] = None,
eps: float = 0.1) -> Tensor:
"""Encode bounding box to distances.

Args:
Expand All @@ -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:
Expand Down
51 changes: 31 additions & 20 deletions mmdet/models/task_modules/coders/legacy_delta_xywh_bbox_coder.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Copyright (c) OpenMMLab. All rights reserved.
from typing import Optional, Sequence, 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


Expand Down Expand Up @@ -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``.

Expand All @@ -60,11 +64,14 @@ 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[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:
Expand All @@ -91,10 +98,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
Expand Down Expand Up @@ -139,12 +148,14 @@ 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: 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.

Typically the rois are anchor or proposed bounding boxes and the deltas are
Expand Down
Loading