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

fix AT_CHECK #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 23 additions & 15 deletions mmdet/core/bbox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
from .assigners import AssignResult, BaseAssigner, MaxIoUAssigner
from .bbox_target import bbox_target
from .geometry import bbox_overlaps
from .assigners import (AssignResult, BaseAssigner, CenterRegionAssigner,
MaxIoUAssigner)
from .builder import build_assigner, build_bbox_coder, build_sampler
from .coder import (BaseBBoxCoder, DeltaXYWHBBoxCoder, PseudoBBoxCoder,
TBLRBBoxCoder)
from .iou_calculators import BboxOverlaps2D, bbox_overlaps
from .samplers import (BaseSampler, CombinedSampler,
InstanceBalancedPosSampler, IoUBalancedNegSampler,
PseudoSampler, RandomSampler, SamplingResult)
from .transforms import (bbox2delta, bbox2result, bbox2roi, bbox_flip,
bbox_mapping, bbox_mapping_back, delta2bbox,
distance2bbox, roi2bbox)
OHEMSampler, PseudoSampler, RandomSampler,
SamplingResult, ScoreHLRSampler)
from .transforms import (bbox2distance, bbox2result, bbox2roi, bbox_flip,
bbox_mapping, bbox_mapping_back, distance2bbox,
roi2bbox, bbox2delta, delta2bbox)


from .bbox_target import bbox_target
from .assign_sampling import assign_and_sample

from .assign_sampling import ( # isort:skip, avoid recursive imports
assign_and_sample, build_assigner, build_sampler)

__all__ = [
'bbox_overlaps', 'BaseAssigner', 'MaxIoUAssigner', 'AssignResult',
'BaseSampler', 'PseudoSampler', 'RandomSampler',
'bbox_overlaps', 'BboxOverlaps2D', 'BaseAssigner', 'MaxIoUAssigner',
'AssignResult', 'BaseSampler', 'PseudoSampler', 'RandomSampler',
'InstanceBalancedPosSampler', 'IoUBalancedNegSampler', 'CombinedSampler',
'SamplingResult', 'build_assigner', 'build_sampler', 'assign_and_sample',
'bbox2delta', 'delta2bbox', 'bbox_flip', 'bbox_mapping',
'bbox_mapping_back', 'bbox2roi', 'roi2bbox', 'bbox2result',
'distance2bbox', 'bbox_target'
'OHEMSampler', 'SamplingResult', 'ScoreHLRSampler', 'build_assigner',
'build_sampler', 'bbox_flip', 'bbox_mapping', 'bbox_mapping_back',
'bbox2roi', 'roi2bbox', 'bbox2result', 'distance2bbox', 'bbox2distance',
'build_bbox_coder', 'BaseBBoxCoder', 'PseudoBBoxCoder',
'DeltaXYWHBBoxCoder', 'TBLRBBoxCoder', 'CenterRegionAssigner',
'assign_and_sample', 'bbox2delta', 'delta2bbox', 'bbox_target'
]
3 changes: 2 additions & 1 deletion mmdet/core/bbox/assigners/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from .assign_result import AssignResult
from .atss_assigner import ATSSAssigner
from .base_assigner import BaseAssigner
from .center_region_assigner import CenterRegionAssigner
from .max_iou_assigner import MaxIoUAssigner
from .point_assigner import PointAssigner

__all__ = [
'BaseAssigner', 'MaxIoUAssigner', 'ApproxMaxIoUAssigner', 'AssignResult',
'PointAssigner', 'ATSSAssigner'
'PointAssigner', 'ATSSAssigner', 'CenterRegionAssigner'
]
46 changes: 26 additions & 20 deletions mmdet/core/bbox/assigners/approx_max_iou_assigner.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import torch

from ..geometry import bbox_overlaps
from ..builder import BBOX_ASSIGNERS
from ..iou_calculators import build_iou_calculator
from .max_iou_assigner import MaxIoUAssigner


@BBOX_ASSIGNERS.register_module()
class ApproxMaxIoUAssigner(MaxIoUAssigner):
"""Assign a corresponding gt bbox or background to each bbox.

Each proposals will be assigned with `-1`, `0`, or a positive integer
indicating the ground truth index.
Each proposals will be assigned with an integer indicating the ground truth
index. (semi-positive index: gt label (0-based), -1: background)

- -1: don't care
- 0: negative sample, no assigned gt
- positive integer: positive sample, index (1-based) of assigned gt
- -1: negative sample, no assigned gt
- semi-positive integer: positive sample, index (0-based) of assigned gt

Args:
pos_iou_thr (float): IoU threshold for positive bboxes.
Expand All @@ -27,6 +28,9 @@ class ApproxMaxIoUAssigner(MaxIoUAssigner):
ignoring any bboxes.
ignore_wrt_candidates (bool): Whether to compute the iof between
`bboxes` and `gt_bboxes_ignore`, or the contrary.
match_low_quality (bool): Whether to allow quality matches. This is
usually allowed for RPN and single stage detectors, but not allowed
in the second stage.
gpu_assign_thr (int): The upper bound of the number of GT for GPU
assign. When the number of gt is above this threshold, will assign
on CPU device. Negative values mean not assign on CPU.
Expand All @@ -39,14 +43,18 @@ def __init__(self,
gt_max_assign_all=True,
ignore_iof_thr=-1,
ignore_wrt_candidates=True,
gpu_assign_thr=-1):
match_low_quality=True,
gpu_assign_thr=-1,
iou_calculator=dict(type='BboxOverlaps2D')):
self.pos_iou_thr = pos_iou_thr
self.neg_iou_thr = neg_iou_thr
self.min_pos_iou = min_pos_iou
self.gt_max_assign_all = gt_max_assign_all
self.ignore_iof_thr = ignore_iof_thr
self.ignore_wrt_candidates = ignore_wrt_candidates
self.gpu_assign_thr = gpu_assign_thr
self.match_low_quality = match_low_quality
self.iou_calculator = build_iou_calculator(iou_calculator)

def assign(self,
approxs,
Expand All @@ -59,14 +67,14 @@ def assign(self,

This method assign a gt bbox to each group of approxs (bboxes),
each group of approxs is represent by a base approx (bbox) and
will be assigned with -1, 0, or a positive number.
-1 means don't care, 0 means negative sample,
positive number is the index (1-based) of assigned gt.
will be assigned with -1, or a semi-positive number.
background_label (-1) means negative sample,
semi-positive number is the index (0-based) of assigned gt.
The assignment is done in following steps, the order matters.

1. assign every bbox to -1
1. assign every bbox to background_label (-1)
2. use the max IoU of each group of approxs to assign
2. assign proposals whose iou with all gts < neg_iou_thr to 0
2. assign proposals whose iou with all gts < neg_iou_thr to background
3. for each bbox, if the iou with its nearest gt >= pos_iou_thr,
assign it to that bbox
4. for each gt bbox, assign its nearest proposals (may be more than
Expand Down Expand Up @@ -110,23 +118,21 @@ def assign(self,
gt_bboxes_ignore = gt_bboxes_ignore.cpu()
if gt_labels is not None:
gt_labels = gt_labels.cpu()
all_overlaps = bbox_overlaps(approxs, gt_bboxes)
all_overlaps = self.iou_calculator(approxs, gt_bboxes)

overlaps, _ = all_overlaps.view(approxs_per_octave, num_squares,
num_gts).max(dim=0)
overlaps = torch.transpose(overlaps, 0, 1)

bboxes = squares[:, :4]

if (self.ignore_iof_thr > 0 and gt_bboxes_ignore is not None
and gt_bboxes_ignore.numel() > 0 and bboxes.numel() > 0):
and gt_bboxes_ignore.numel() > 0 and squares.numel() > 0):
if self.ignore_wrt_candidates:
ignore_overlaps = bbox_overlaps(
bboxes, gt_bboxes_ignore, mode='iof')
ignore_overlaps = self.iou_calculator(
squares, gt_bboxes_ignore, mode='iof')
ignore_max_overlaps, _ = ignore_overlaps.max(dim=1)
else:
ignore_overlaps = bbox_overlaps(
gt_bboxes_ignore, bboxes, mode='iof')
ignore_overlaps = self.iou_calculator(
gt_bboxes_ignore, squares, mode='iof')
ignore_max_overlaps, _ = ignore_overlaps.max(dim=0)
overlaps[:, ignore_max_overlaps > self.ignore_iof_thr] = -1

Expand Down
64 changes: 38 additions & 26 deletions mmdet/core/bbox/assigners/assign_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@


class AssignResult(util_mixins.NiceRepr):
"""
Stores assignments between predicted and truth boxes.
"""Stores assignments between predicted and truth boxes.

Attributes:
num_gts (int): the number of truth boxes considered when computing this
Expand Down Expand Up @@ -45,55 +44,60 @@ def __init__(self, num_gts, gt_inds, max_overlaps, labels=None):
self.gt_inds = gt_inds
self.max_overlaps = max_overlaps
self.labels = labels
# Interface for possible user-defined properties
self._extra_properties = {}

@property
def num_preds(self):
"""
Return the number of predictions in this assignment
"""
"""int: the number of predictions in this assignment"""
return len(self.gt_inds)

def set_extra_property(self, key, value):
"""Set user-defined new property."""
assert key not in self.info
self._extra_properties[key] = value

def get_extra_property(self, key):
"""Get user-defined property."""
return self._extra_properties.get(key, None)

@property
def info(self):
"""
Returns a dictionary of info about the object
"""
return {
"""dict: a dictionary of info about the object"""
basic_info = {
'num_gts': self.num_gts,
'num_preds': self.num_preds,
'gt_inds': self.gt_inds,
'max_overlaps': self.max_overlaps,
'labels': self.labels,
}
basic_info.update(self._extra_properties)
return basic_info

def __nice__(self):
"""
Create a "nice" summary string describing this assign result
"""
"""str: a "nice" summary string describing this assign result"""
parts = []
parts.append('num_gts={!r}'.format(self.num_gts))
parts.append(f'num_gts={self.num_gts!r}')
if self.gt_inds is None:
parts.append('gt_inds={!r}'.format(self.gt_inds))
parts.append(f'gt_inds={self.gt_inds!r}')
else:
parts.append('gt_inds.shape={!r}'.format(
tuple(self.gt_inds.shape)))
parts.append(f'gt_inds.shape={tuple(self.gt_inds.shape)!r}')
if self.max_overlaps is None:
parts.append('max_overlaps={!r}'.format(self.max_overlaps))
parts.append(f'max_overlaps={self.max_overlaps!r}')
else:
parts.append('max_overlaps.shape={!r}'.format(
tuple(self.max_overlaps.shape)))
parts.append('max_overlaps.shape='
f'{tuple(self.max_overlaps.shape)!r}')
if self.labels is None:
parts.append('labels={!r}'.format(self.labels))
parts.append(f'labels={self.labels!r}')
else:
parts.append('labels.shape={!r}'.format(tuple(self.labels.shape)))
parts.append(f'labels.shape={tuple(self.labels.shape)!r}')
return ', '.join(parts)

@classmethod
def random(cls, **kwargs):
"""
Create random AssignResult for tests or debugging.
"""Create random AssignResult for tests or debugging.

Kwargs:
Args:
num_preds: number of predicted boxes
num_gts: number of true boxes
p_ignore (float): probability of a predicted box assinged to an
Expand All @@ -104,7 +108,7 @@ def random(cls, **kwargs):
rng (None | int | numpy.random.RandomState): seed or state

Returns:
AssignResult :
:obj:`AssignResult`: Randomly generated assign results.

Example:
>>> from mmdet.core.bbox.assigners.assign_result import * # NOQA
Expand Down Expand Up @@ -172,7 +176,10 @@ def random(cls, **kwargs):
labels = torch.zeros(num_preds, dtype=torch.int64)
else:
labels = torch.from_numpy(
rng.randint(1, num_classes + 1, size=num_preds))
# remind that we set FG labels to [0, num_class-1]
# since mmdet v2.0
# BG cat_id: num_class
rng.randint(0, num_classes, size=num_preds))
labels[~is_assigned] = 0
else:
labels = None
Expand All @@ -181,6 +188,11 @@ def random(cls, **kwargs):
return self

def add_gt_(self, gt_labels):
"""Add ground truth as assigned results.

Args:
gt_labels (torch.Tensor): Labels of gt boxes
"""
self_inds = torch.arange(
1, len(gt_labels) + 1, dtype=torch.long, device=gt_labels.device)
self.gt_inds = torch.cat([self_inds, self.gt_inds])
Expand Down
35 changes: 27 additions & 8 deletions mmdet/core/bbox/assigners/atss_assigner.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import torch

from ..geometry import bbox_overlaps
from ..builder import BBOX_ASSIGNERS
from ..iou_calculators import build_iou_calculator
from .assign_result import AssignResult
from .base_assigner import BaseAssigner


@BBOX_ASSIGNERS.register_module()
class ATSSAssigner(BaseAssigner):
"""Assign a corresponding gt bbox or background to each bbox.

Expand All @@ -18,8 +20,13 @@ class ATSSAssigner(BaseAssigner):
topk (float): number of bbox selected in each level
"""

def __init__(self, topk):
def __init__(self,
topk,
iou_calculator=dict(type='BboxOverlaps2D'),
ignore_iof_thr=-1):
self.topk = topk
self.iou_calculator = build_iou_calculator(iou_calculator)
self.ignore_iof_thr = ignore_iof_thr

# https://github.com/sfzhang15/ATSS/blob/master/atss_core/modeling/rpn/atss/loss.py

Expand Down Expand Up @@ -61,7 +68,7 @@ def assign(self,
num_gt, num_bboxes = gt_bboxes.size(0), bboxes.size(0)

# compute iou between all bbox and gt
overlaps = bbox_overlaps(bboxes, gt_bboxes)
overlaps = self.iou_calculator(bboxes, gt_bboxes)

# assign 0 by default
assigned_gt_inds = overlaps.new_full((num_bboxes, ),
Expand All @@ -77,8 +84,9 @@ def assign(self,
if gt_labels is None:
assigned_labels = None
else:
assigned_labels = overlaps.new_zeros((num_bboxes, ),
dtype=torch.long)
assigned_labels = overlaps.new_full((num_bboxes, ),
-1,
dtype=torch.long)
return AssignResult(
num_gt, assigned_gt_inds, max_overlaps, labels=assigned_labels)

Expand All @@ -94,6 +102,15 @@ def assign(self,
distances = (bboxes_points[:, None, :] -
gt_points[None, :, :]).pow(2).sum(-1).sqrt()

if (self.ignore_iof_thr > 0 and gt_bboxes_ignore is not None
and gt_bboxes_ignore.numel() > 0 and bboxes.numel() > 0):
ignore_overlaps = self.iou_calculator(
bboxes, gt_bboxes_ignore, mode='iof')
ignore_max_overlaps, _ = ignore_overlaps.max(dim=1)
ignore_idxs = ignore_max_overlaps > self.ignore_iof_thr
distances[ignore_idxs, :] = INF
assigned_gt_inds[ignore_idxs] = -1

# Selecting candidates based on the center distance
candidate_idxs = []
start_idx = 0
Expand All @@ -102,8 +119,9 @@ def assign(self,
# select k bbox whose center are closest to the gt center
end_idx = start_idx + bboxes_per_level
distances_per_level = distances[start_idx:end_idx, :]
selectable_k = min(self.topk, bboxes_per_level)
_, topk_idxs_per_level = distances_per_level.topk(
self.topk, dim=0, largest=False)
selectable_k, dim=0, largest=False)
candidate_idxs.append(topk_idxs_per_level + start_idx)
start_idx = end_idx
candidate_idxs = torch.cat(candidate_idxs, dim=0)
Expand Down Expand Up @@ -148,8 +166,9 @@ def assign(self,
max_overlaps != -INF] = argmax_overlaps[max_overlaps != -INF] + 1

if gt_labels is not None:
assigned_labels = assigned_gt_inds.new_zeros((num_bboxes, ))
pos_inds = torch.nonzero(assigned_gt_inds > 0).squeeze()
assigned_labels = assigned_gt_inds.new_full((num_bboxes, ), -1)
pos_inds = torch.nonzero(
assigned_gt_inds > 0, as_tuple=False).squeeze()
if pos_inds.numel() > 0:
assigned_labels[pos_inds] = gt_labels[
assigned_gt_inds[pos_inds] - 1]
Expand Down
2 changes: 2 additions & 0 deletions mmdet/core/bbox/assigners/base_assigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


class BaseAssigner(metaclass=ABCMeta):
"""Base assigner that assigns boxes to ground truth boxes."""

@abstractmethod
def assign(self, bboxes, gt_bboxes, gt_bboxes_ignore=None, gt_labels=None):
"""Assign boxes to either a ground truth boxe or a negative boxes."""
pass
Loading