Skip to content

Commit

Permalink
filter_empty_instances with box threshold
Browse files Browse the repository at this point in the history
Summary:
Minor change to enable passing a threshold through filter_empty_instances.
Due to floating point issues I had boxes with width 1e-14 that passed the 0 threshold but blew up later.
Also fixed the type hinting to float. These are floats.
Pull Request resolved: facebookresearch#1198

Reviewed By: rbgirshick

Differential Revision: D21030901

Pulled By: ppwwyyxx

fbshipit-source-id: 5eedb9c4b801bcb85e5b040cd915628f0ec279e6
  • Loading branch information
Aleksey Nozdryn-Plotnicki authored and facebook-github-bot committed Apr 15, 2020
1 parent 5f7bbed commit 2c346e0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions detectron2/data/detection_utils.py
Expand Up @@ -325,22 +325,23 @@ def annotations_to_instances_rotated(annos, image_size):
return target


def filter_empty_instances(instances, by_box=True, by_mask=True):
def filter_empty_instances(instances, by_box=True, by_mask=True, box_threshold=1e-5):
"""
Filter out empty instances in an `Instances` object.
Args:
instances (Instances):
by_box (bool): whether to filter out instances with empty boxes
by_mask (bool): whether to filter out instances with empty masks
box_threshold (float): minimum width and height to be considered non-empty
Returns:
Instances: the filtered instances.
"""
assert by_box or by_mask
r = []
if by_box:
r.append(instances.gt_boxes.nonempty())
r.append(instances.gt_boxes.nonempty(threshold=box_threshold))
if instances.has("gt_masks") and by_mask:
r.append(instances.gt_masks.nonempty())

Expand Down
2 changes: 1 addition & 1 deletion detectron2/structures/boxes.py
Expand Up @@ -187,7 +187,7 @@ def clip(self, box_size: BoxSizeType) -> None:
self.tensor[:, 2].clamp_(min=0, max=w)
self.tensor[:, 3].clamp_(min=0, max=h)

def nonempty(self, threshold: int = 0) -> torch.Tensor:
def nonempty(self, threshold: float = 0.0) -> torch.Tensor:
"""
Find boxes that are non-empty.
A box is considered empty, if either of its side is no larger than threshold.
Expand Down
2 changes: 1 addition & 1 deletion detectron2/structures/rotated_boxes.py
Expand Up @@ -298,7 +298,7 @@ def clip(self, box_size: Boxes.BoxSizeType, clip_angle_threshold: float = 1.0) -
self.tensor[idx, 2] = torch.min(self.tensor[idx, 2], x2 - x1)
self.tensor[idx, 3] = torch.min(self.tensor[idx, 3], y2 - y1)

def nonempty(self, threshold: int = 0) -> torch.Tensor:
def nonempty(self, threshold: float = 0.0) -> torch.Tensor:
"""
Find boxes that are non-empty.
A box is considered empty, if either of its side is no larger than threshold.
Expand Down

0 comments on commit 2c346e0

Please sign in to comment.