Commit 376902ba authored by A. Unique TensorFlower's avatar A. Unique TensorFlower
Browse files

Internal change

PiperOrigin-RevId: 443422807
parent 9eed9883
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
"""Anchor box and labeler definition.""" """Anchor box and labeler definition."""
import collections import collections
from typing import Dict, Optional, Tuple
# Import libraries # Import libraries
...@@ -65,7 +66,7 @@ class Anchor(object): ...@@ -65,7 +66,7 @@ class Anchor(object):
self.image_size = image_size self.image_size = image_size
self.boxes = self._generate_boxes() self.boxes = self._generate_boxes()
def _generate_boxes(self): def _generate_boxes(self) -> tf.Tensor:
"""Generates multiscale anchor boxes. """Generates multiscale anchor boxes.
Returns: Returns:
...@@ -100,7 +101,7 @@ class Anchor(object): ...@@ -100,7 +101,7 @@ class Anchor(object):
boxes_all.append(boxes_l) boxes_all.append(boxes_l)
return tf.concat(boxes_all, axis=0) return tf.concat(boxes_all, axis=0)
def unpack_labels(self, labels): def unpack_labels(self, labels: tf.Tensor) -> Dict[str, tf.Tensor]:
"""Unpacks an array of labels into multiscales labels.""" """Unpacks an array of labels into multiscales labels."""
unpacked_labels = collections.OrderedDict() unpacked_labels = collections.OrderedDict()
count = 0 count = 0
...@@ -146,17 +147,24 @@ class AnchorLabeler(object): ...@@ -146,17 +147,24 @@ class AnchorLabeler(object):
force_match_for_each_col=True) force_match_for_each_col=True)
self.box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder() self.box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder()
def label_anchors(self, def label_anchors(
anchor_boxes, self,
gt_boxes, anchor_boxes: Dict[str, tf.Tensor],
gt_labels, gt_boxes: tf.Tensor,
gt_attributes=None, gt_labels: tf.Tensor,
gt_weights=None): gt_attributes: Optional[Dict[str, tf.Tensor]] = None,
gt_weights: Optional[tf.Tensor] = None
) -> Tuple[Dict[str, tf.Tensor], Dict[str, tf.Tensor], Dict[str, Dict[
str, tf.Tensor]], tf.Tensor, tf.Tensor]:
"""Labels anchors with ground truth inputs. """Labels anchors with ground truth inputs.
Args: Args:
anchor_boxes: A float tensor with shape [N, 4] representing anchor boxes. anchor_boxes: An ordered dictionary with keys
For each row, it stores [y0, x0, y1, x1] for four corners of a box. [min_level, min_level+1, ..., max_level]. The values are tensor with
shape [height_l, width_l, num_anchors_per_location * 4]. The height_l
and width_l represent the dimension of the feature pyramid at l-th
level. For each anchor box, the tensor stores [y0, x0, y1, x1] for the
four corners.
gt_boxes: A float tensor with shape [N, 4] representing groundtruth boxes. gt_boxes: A float tensor with shape [N, 4] representing groundtruth boxes.
For each row, it stores [y0, x0, y1, x1] for four corners of a box. For each row, it stores [y0, x0, y1, x1] for four corners of a box.
gt_labels: A integer tensor with shape [N, 1] representing groundtruth gt_labels: A integer tensor with shape [N, 1] representing groundtruth
...@@ -166,30 +174,29 @@ class AnchorLabeler(object): ...@@ -166,30 +174,29 @@ class AnchorLabeler(object):
representing groundtruth attributes. representing groundtruth attributes.
gt_weights: If not None, a float tensor with shape [N] representing gt_weights: If not None, a float tensor with shape [N] representing
groundtruth weights. groundtruth weights.
Returns: Returns:
cls_targets_dict: ordered dictionary with keys cls_targets_dict: An ordered dictionary with keys
[min_level, min_level+1, ..., max_level]. The values are tensor with [min_level, min_level+1, ..., max_level]. The values are tensor with
shape [height_l, width_l, num_anchors_per_location]. The height_l and shape [height_l, width_l, num_anchors_per_location]. The height_l and
width_l represent the dimension of class logits at l-th level. width_l represent the dimension of class logits at l-th level.
box_targets_dict: ordered dictionary with keys box_targets_dict: An ordered dictionary with keys
[min_level, min_level+1, ..., max_level]. The values are tensor with [min_level, min_level+1, ..., max_level]. The values are tensor with
shape [height_l, width_l, num_anchors_per_location * 4]. The height_l shape [height_l, width_l, num_anchors_per_location * 4]. The height_l
and width_l represent the dimension of bounding box regression output at and width_l represent the dimension of bounding box regression output at
l-th level. l-th level.
attribute_targets_dict: a dict with (name, attribute_targets) pairs. Each attribute_targets_dict: A dict with (name, attribute_targets) pairs. Each
`attribute_targets` represents an ordered dictionary with keys `attribute_targets` represents an ordered dictionary with keys
[min_level, min_level+1, ..., max_level]. The values are tensor with [min_level, min_level+1, ..., max_level]. The values are tensor with
shape [height_l, width_l, num_anchors_per_location * attribute_size]. shape [height_l, width_l, num_anchors_per_location * attribute_size].
The height_l and width_l represent the dimension of attribute prediction The height_l and width_l represent the dimension of attribute prediction
output at l-th level. output at l-th level.
cls_weights: A flattened Tensor with shape [batch_size, num_anchors], that cls_weights: A flattened Tensor with shape [num_anchors], that serves as
serves as masking / sample weight for classification loss. Its value masking / sample weight for classification loss. Its value is 1.0 for
is 1.0 for positive and negative matched anchors, and 0.0 for ignored positive and negative matched anchors, and 0.0 for ignored anchors.
anchors. box_weights: A flattened Tensor with shape [num_anchors], that serves as
box_weights: A flattened Tensor with shape [batch_size, num_anchors], that masking / sample weight for regression loss. Its value is 1.0 for
serves as masking / sample weight for regression loss. Its value is positive matched anchors, and 0.0 for negative and ignored anchors.
1.0 for positive matched anchors, and 0.0 for negative and ignored
anchors.
""" """
flattened_anchor_boxes = [] flattened_anchor_boxes = []
for anchors in anchor_boxes.values(): for anchors in anchor_boxes.values():
...@@ -286,25 +293,33 @@ class RpnAnchorLabeler(AnchorLabeler): ...@@ -286,25 +293,33 @@ class RpnAnchorLabeler(AnchorLabeler):
return (ignore_labels + positive_labels + negative_labels, return (ignore_labels + positive_labels + negative_labels,
positive_labels, negative_labels) positive_labels, negative_labels)
def label_anchors(self, anchor_boxes, gt_boxes, gt_labels): def label_anchors(
self, anchor_boxes: Dict[str, tf.Tensor], gt_boxes: tf.Tensor,
gt_labels: tf.Tensor
) -> Tuple[Dict[str, tf.Tensor], Dict[str, tf.Tensor]]:
"""Labels anchors with ground truth inputs. """Labels anchors with ground truth inputs.
Args: Args:
anchor_boxes: A float tensor with shape [N, 4] representing anchor boxes. anchor_boxes: An ordered dictionary with keys
For each row, it stores [y0, x0, y1, x1] for four corners of a box. [min_level, min_level+1, ..., max_level]. The values are tensor with
shape [height_l, width_l, num_anchors_per_location * 4]. The height_l
and width_l represent the dimension of the feature pyramid at l-th
level. For each anchor box, the tensor stores [y0, x0, y1, x1] for the
four corners.
gt_boxes: A float tensor with shape [N, 4] representing groundtruth boxes. gt_boxes: A float tensor with shape [N, 4] representing groundtruth boxes.
For each row, it stores [y0, x0, y1, x1] for four corners of a box. For each row, it stores [y0, x0, y1, x1] for four corners of a box.
gt_labels: A integer tensor with shape [N, 1] representing groundtruth gt_labels: A integer tensor with shape [N, 1] representing groundtruth
classes. classes.
Returns: Returns:
score_targets_dict: ordered dictionary with keys score_targets_dict: An ordered dictionary with keys
[min_level, min_level+1, ..., max_level]. The values are tensor with [min_level, min_level+1, ..., max_level]. The values are tensor with
shape [height_l, width_l, num_anchors]. The height_l and width_l shape [height_l, width_l, num_anchors_per_location]. The height_l and
represent the dimension of class logits at l-th level. width_l represent the dimension of class logits at l-th level.
box_targets_dict: ordered dictionary with keys box_targets_dict: An ordered dictionary with keys
[min_level, min_level+1, ..., max_level]. The values are tensor with [min_level, min_level+1, ..., max_level]. The values are tensor with
shape [height_l, width_l, num_anchors * 4]. The height_l and shape [height_l, width_l, num_anchors_per_location * 4]. The height_l
width_l represent the dimension of bounding box regression output at and width_l represent the dimension of bounding box regression output at
l-th level. l-th level.
""" """
flattened_anchor_boxes = [] flattened_anchor_boxes = []
...@@ -362,8 +377,27 @@ def build_anchor_generator(min_level, max_level, num_scales, aspect_ratios, ...@@ -362,8 +377,27 @@ def build_anchor_generator(min_level, max_level, num_scales, aspect_ratios,
return anchor_gen return anchor_gen
def unpack_targets(targets, anchor_boxes_dict): def unpack_targets(
"""Unpacks an array of labels into multiscales labels.""" targets: tf.Tensor,
anchor_boxes_dict: Dict[str, tf.Tensor]) -> Dict[str, tf.Tensor]:
"""Unpacks an array of labels into multiscales labels.
Args:
targets: A tensor with shape [num_anchors, M] representing the packed
targets with M values stored for each anchor.
anchor_boxes_dict: An ordered dictionary with keys
[min_level, min_level+1, ..., max_level]. The values are tensor with shape
[height_l, width_l, num_anchors_per_location * 4]. The height_l and
width_l represent the dimension of the feature pyramid at l-th level. For
each anchor box, the tensor stores [y0, x0, y1, x1] for the four corners.
Returns:
unpacked_targets: An ordered dictionary with keys
[min_level, min_level+1, ..., max_level]. The values are tensor with shape
[height_l, width_l, num_anchors_per_location * M]. The height_l and
width_l represent the dimension of the feature pyramid at l-th level. M is
the number of values stored for each anchor.
"""
unpacked_targets = collections.OrderedDict() unpacked_targets = collections.OrderedDict()
count = 0 count = 0
for level, anchor_boxes in anchor_boxes_dict.items(): for level, anchor_boxes in anchor_boxes_dict.items():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment