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

Refactor one-stage get_bboxes logic #5317

Merged
merged 73 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
77da249
revert batch to single
hhaAndroid Jun 7, 2021
2ad29eb
update anchor_head
hhaAndroid Jun 8, 2021
2db854a
replace preds with bboxes
hhaAndroid Jun 8, 2021
060b1c7
add point_bbox_coder
hhaAndroid Jun 8, 2021
172d37a
FCOS add get_selected_priori
hhaAndroid Jun 8, 2021
dd9e514
unified anchor-free and anchor-based get_bbox_single
hhaAndroid Jun 8, 2021
64912e2
update code
hhaAndroid Jun 11, 2021
91c8d46
update reppoints and sabl
hhaAndroid Jun 11, 2021
a135f18
add sparse priors
jshilong Jun 14, 2021
63364cd
add mlvlpointsgenerator
jshilong Jun 14, 2021
e084958
revert __init__ of core
jshilong Jun 14, 2021
706379c
refactor reppoints
jshilong Jun 14, 2021
566b973
delete label channal
jshilong Jun 15, 2021
6f9d973
add docstr
jshilong Jun 15, 2021
f3aafe0
fix typo
jshilong Jun 15, 2021
18a0fdc
fix args
jshilong Jun 15, 2021
f29b5a9
fix typo
jshilong Jun 15, 2021
d8b121f
fix doc
jshilong Jun 16, 2021
07e182e
fix stride_h
jshilong Jun 16, 2021
6c8f5f5
add offset
jshilong Jun 16, 2021
8197ce3
Unified bbox coder
hhaAndroid Jun 16, 2021
af493a2
add offset
jshilong Jun 17, 2021
6040da8
remove point_bbox_coder.py
hhaAndroid Jun 17, 2021
42746c2
fix docstr
jshilong Jun 17, 2021
48dce47
merge PR5347
hhaAndroid Jun 18, 2021
17a6cca
merge PR5349
hhaAndroid Jun 18, 2021
211299a
new interface of single_proir
jshilong Jun 18, 2021
d0386ea
Merge commit 'refs/pull/5349/head' of https://github.com/open-mmlab/m…
hhaAndroid Jun 18, 2021
0927d88
fix device
jshilong Jun 18, 2021
5d2f464
Merge commit 'refs/pull/5349/head' of https://github.com/open-mmlab/m…
hhaAndroid Jun 18, 2021
d75e072
add unitest
jshilong Jun 18, 2021
946c23d
add cuda unitest
jshilong Jun 18, 2021
2ad1337
add more cuda unintest
jshilong Jun 18, 2021
5da6a00
Merge commit 'refs/pull/5349/head' of https://github.com/open-mmlab/m…
hhaAndroid Jun 18, 2021
6a5eee8
fix reppoints
jshilong Jun 18, 2021
05f18ae
Merge commit 'refs/pull/5349/head' of https://github.com/open-mmlab/m…
hhaAndroid Jun 18, 2021
19a727b
fix device
jshilong Jun 18, 2021
ed2477e
Merge commit 'refs/pull/5349/head' of https://github.com/open-mmlab/m…
hhaAndroid Jun 18, 2021
f432b9c
update all prior
hhaAndroid Jun 18, 2021
40d214c
Merge branch 'master' of https://github.com/open-mmlab/mmdetection in…
hhaAndroid Jun 18, 2021
73de85f
update vfnet
hhaAndroid Jun 18, 2021
00cf8de
add unintest for ssd and yolo and rename prior_idxs
jshilong Jun 20, 2021
e4d1c3e
add docstr for MlvlPointGenerator
jshilong Jun 21, 2021
5322cdc
Merge commit 'refs/pull/5349/head' of https://github.com/open-mmlab/m…
hhaAndroid Jun 21, 2021
6a3bf1f
update reppoints and rpnhead
hhaAndroid Jun 21, 2021
3717743
add space
jshilong Jun 22, 2021
ea79bf3
add num_base_priors
jshilong Jun 22, 2021
d02dc7f
update some model
hhaAndroid Jun 23, 2021
9299b52
update docstr
hhaAndroid Jun 23, 2021
b9fd000
fixAugFPN test and lint.
hhaAndroid Jun 23, 2021
dad4896
Merge commit 'refs/pull/5349/head' of https://github.com/open-mmlab/m…
hhaAndroid Jun 24, 2021
ce8bf29
merge master
hhaAndroid Jun 24, 2021
264dae3
Fix autoassign
hhaAndroid Jun 24, 2021
98cba8c
add docs
hhaAndroid Jun 25, 2021
a08c732
merge master
hhaAndroid Jun 25, 2021
5f55ad6
Merge branch 'master' of https://github.com/open-mmlab/mmdetection in…
hhaAndroid Jul 8, 2021
da0d9d5
Unified fcos decoding
hhaAndroid Jul 8, 2021
36b43d4
Merge branch 'master' of https://github.com/open-mmlab/mmdetection in…
hhaAndroid Jul 8, 2021
cfb6500
merge 5374
hhaAndroid Jul 8, 2021
226f833
update docstr
hhaAndroid Jul 8, 2021
f40d903
fix train error
hhaAndroid Jul 8, 2021
b04730d
Fix Vfnet
hhaAndroid Jul 12, 2021
556d5a3
Fix conflicts
hhaAndroid Jul 12, 2021
9aed4b9
Fix some
hhaAndroid Jul 12, 2021
59807b6
update centernet
hhaAndroid Jul 13, 2021
15390e2
revert
hhaAndroid Jul 13, 2021
73bb7c5
add warnings
hhaAndroid Jul 13, 2021
b9546b2
fix unittest error
hhaAndroid Jul 13, 2021
8dd5274
delete duplicated
hhaAndroid Jul 14, 2021
e09f4a8
fix comment
hhaAndroid Jul 15, 2021
f5f9486
fix docs
hhaAndroid Jul 22, 2021
f05d7df
fix type
hhaAndroid Aug 10, 2021
7fae84d
merge refactor_dense
hhaAndroid Aug 11, 2021
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
Prev Previous commit
Next Next commit
new interface of single_proir
  • Loading branch information
jshilong committed Jun 18, 2021
commit 211299a85cc91d290b9b927eec0c445e1f5b8ab8
73 changes: 47 additions & 26 deletions mmdet/core/anchor/anchor_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,45 @@ def grid_priors(self, featmap_sizes, device='cuda'):
multi_level_anchors = []
for i in range(self.num_levels):
anchors = self.single_level_grid_priors(
self.base_anchors[i].to(device),
featmap_sizes[i],
self.strides[i],
device=device)
featmap_sizes[i], level_idx=i, device=device)
multi_level_anchors.append(anchors)
return multi_level_anchors

def single_level_grid_priors(self, featmap_size, level_idx, device='cuda'):
"""Generate grid anchors of a single level.

Note:
This function is usually called by method ``self.grid_priors``.

Args:
featmap_size (tuple[int]): Size of the feature maps.
level_idx (int): The index of corresponding feature map level.
device (str, optional): The device the tensor will be put on.
Defaults to 'cuda'.

Returns:
torch.Tensor: Anchors in the overall feature maps.
"""

base_anchors = self.base_anchors[level_idx]
feat_h, feat_w = featmap_size
stride_w, stride_h = self.strides[level_idx]
shift_x = torch.arange(0, feat_w, device=device) * stride_w
shift_y = torch.arange(0, feat_h, device=device) * stride_h

shift_xx, shift_yy = self._meshgrid(shift_x, shift_y)
shifts = torch.stack([shift_xx, shift_yy, shift_xx, shift_yy], dim=-1)
shifts = shifts.type_as(base_anchors)
# first feat_w elements correspond to the first row of shifts
# add A anchors (1, A, 4) to K shifts (K, 1, 4) to get
# shifted anchors (K, A, 4), reshape to (K*A, 4)

all_anchors = base_anchors[None, :, :] + shifts[:, None, :]
all_anchors = all_anchors.view(-1, 4)
# first A rows correspond to A anchors of (0, 0) in feature map,
# then (0, 1), (0, 2), ...
return all_anchors

def grid_anchors(self, featmap_sizes, device='cuda'):

warnings.warn('``grid_anchors`` would be deprecated soon. Please use '
Expand All @@ -243,21 +275,6 @@ def single_level_grid_anchors(self,
featmap_size,
stride=(16, 16),
device='cuda'):
warnings.warn(
'``single_level_grid_anchors`` would be deprecated soon. '
'Please use ``single_level_grid_priors`` ')

return self.single_level_grid_priors(
base_anchors=base_anchors,
featmap_size=featmap_size,
stride=stride,
device=device)

def single_level_grid_priors(self,
base_anchors,
featmap_size,
stride=(16, 16),
device='cuda'):
"""Generate grid anchors of a single level.

Note:
Expand All @@ -275,6 +292,10 @@ def single_level_grid_priors(self,
torch.Tensor: Anchors in the overall feature maps.
"""
# keep as Tensor, so that we can covert to ONNX correctly
warnings.warn(
'``single_level_grid_anchors`` would be deprecated soon. '
'Please use ``single_level_grid_priors`` ')

feat_h, feat_w = featmap_size
stride_w, stride_h = stride
shift_x = torch.arange(0, feat_w, device=device) * stride_w
Expand Down Expand Up @@ -353,15 +374,15 @@ def single_level_valid_flags(self,
return valid

def sparse_priors(self,
prior_indexs,
prior_idx,
featmap_size,
level_idx,
dtype,
device='cuda'):
"""Generate sparse anchors according to the ``prior_indexs``.
"""Generate sparse anchors according to the ``prior_idx``.

Args:
prior_indexs (Tensor): The index of corresponding anchors
prior_idx (Tensor): The index of corresponding anchors
in the feature map.
featmap_size (tuple[int]): feature map size arrange as (h, w).
level_idx (int): The level index of corresponding feature
Expand All @@ -371,15 +392,15 @@ def sparse_priors(self,
located.
Returns:
Tensor: Anchor with shape (N, 4), N should be equal to
the length of ``prior_indexs``.
the length of ``prior_idx``.
"""

height, width = featmap_size
num_base_anchors = self.num_base_anchors[level_idx]
base_anchor_id = prior_indexs % num_base_anchors
x = (prior_indexs //
base_anchor_id = prior_idx % num_base_anchors
x = (prior_idx //
num_base_anchors) % width * self.strides[level_idx][0]
y = (prior_indexs // width //
y = (prior_idx // width //
num_base_anchors) % height * self.strides[level_idx][1]
priors = torch.stack([x, y, x, y], 1).to(
dtype) + self.base_anchors[level_idx][base_anchor_id, :].to(device)
Expand Down
21 changes: 10 additions & 11 deletions mmdet/core/anchor/point_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ def grid_priors(self, featmap_sizes, device='cuda', with_stride=False):
for i in range(self.num_levels):
priors = self.single_level_grid_priors(
featmap_sizes[i],
stride=self.strides[i],
level_idx=i,
device=device,
with_stride=with_stride)
multi_level_priors.append(priors)
return multi_level_priors

def single_level_grid_priors(self,
featmap_size,
stride=None,
level_idx,
device='cuda',
with_stride=False):
"""Generate grid Points of a single level.
Expand All @@ -112,8 +112,7 @@ def single_level_grid_priors(self,
Args:
featmap_size (tuple[int]): Size of the feature maps, arrange as
(h, w).
stride (int, tuple[int], optional): Stride of the feature map
in order (w, h). Defaults to None.
level_idx (int): The index of corresponding feature map level.
device (str, optional): The device the tensor will be put on.
Defaults to 'cuda'.
with_stride (bool): Concatenate the stride to the last dimension
Expand All @@ -130,7 +129,7 @@ def single_level_grid_priors(self,
(coord_x, coord_y, stride_w, stride_h).
"""
feat_h, feat_w = featmap_size
stride_w, stride_h = stride
stride_w, stride_h = self.strides[level_idx]
shift_x = (torch.arange(0., feat_w, device=device) +
self.offset) * stride_w
shift_y = (torch.arange(0., feat_h, device=device) +
Expand Down Expand Up @@ -204,15 +203,15 @@ def single_level_valid_flags(self,
return valid

def sparse_priors(self,
prior_indexs,
prior_idx,
featmap_size,
level_idx,
dtype,
device='cuda'):
"""Generate sparse points according to the ``prior_indexs``.
"""Generate sparse points according to the ``prior_idx``.

Args:
prior_indexs (Tensor): The index of corresponding anchors
prior_idx (Tensor): The index of corresponding anchors
in the feature map.
featmap_size (tuple[int]): feature map size arrange as (w, h).
level_idx (int): The level index of corresponding feature
Expand All @@ -222,12 +221,12 @@ def sparse_priors(self,
located.
Returns:
Tensor: Anchor with shape (N, 2), N should be equal to
the length of ``prior_indexs``. And last dimension
the length of ``prior_idx``. And last dimension
2 represent (coord_x, coord_y).
"""
height, width = featmap_size
x = (prior_indexs % width + self.offset) * self.strides[level_idx][0]
y = ((prior_indexs // width) % height +
x = (prior_idx % width + self.offset) * self.strides[level_idx][0]
y = ((prior_idx // width) % height +
self.offset) * self.strides[level_idx][1]
prioris = torch.stack([x, y], 1).to(dtype)
prioris = prioris.to(device)
Expand Down
17 changes: 13 additions & 4 deletions tests/test_utils/test_anchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ def test_standard_points_generator():
assert (priors_half_offset[1][0] - priors[1][0]).sum() == 10 * 0.5 * 2


def test_sparse_prior():
from mmdet.core.anchor import MlvlPointGenerator
mlvl_points = MlvlPointGenerator(strides=[4, 10], offset=0)
prior_indexs = torch.Tensor([[0, 2, 4, 5, 6, 9]], dtype=torch.Long)

featmap_sizes = [(3, 5), (6, 4)]
grid_anchors = mlvl_points.grid_priors(
featmap_sizes=featmap_sizes, with_stride=False)
sparse_prior = mlvl_points.sparse_priors(
prior_indexs=prior_indexs, featmap_size=featmap_sizes[0], level_idx=0)
assert (sparse_prior == grid_anchors[0][prior_indexs]).all()


def test_standard_anchor_generator():
from mmdet.core.anchor import build_anchor_generator
anchor_generator_cfg = dict(
Expand All @@ -71,10 +84,6 @@ def test_standard_anchor_generator():
assert anchor_generator is not None


def test_sparse_prior():
pass


def test_strides():
from mmdet.core import AnchorGenerator
# Square strides
Expand Down