Ejemplo n.º 1
0
 def resize_and_crop_boxes(self):
     """Resize boxes and crop it to the self._output dimension."""
     boxlist = preprocessor.box_list.BoxList(self._boxes)
     boxes = preprocessor.box_list_scale(boxlist, self._scaled_height,
                                         self._scaled_width).get()
     # Clip the boxes.
     boxes = self.clip_boxes(boxes)
     # Filter out ground truth boxes that are all zeros and corresponding classes
     # and masks.
     indices = tf.where(tf.not_equal(tf.reduce_sum(boxes, axis=1), 0))
     boxes = tf.gather_nd(boxes, indices)
     classes = tf.gather_nd(self._classes, indices)
     self._masks = tf.gather_nd(self._masks, indices)
     return boxes, classes
Ejemplo n.º 2
0
def _box_loss(box_outputs, box_targets, num_positives, delta=0.1):
  """Computes box regression loss."""
  # delta is typically around the mean value of regression target.
  # for instances, the regression targets of 512x512 input with 6 anchors on
  # P3-P7 pyramid is about [0.1, 0.1, 0.2, 0.2].
  normalizer = num_positives * 4.0
  mask = tf.not_equal(box_targets, 0.0)
  box_loss = tf.losses.huber_loss(
      box_targets,
      box_outputs,
      weights=mask,
      delta=delta,
      reduction=tf.losses.Reduction.SUM)
  box_loss /= normalizer
  return box_loss
Ejemplo n.º 3
0
 def resize_and_crop_boxes(self):
   """Resize boxes and crop it to the self._output dimension."""
   boxlist = preprocessor.box_list.BoxList(self._boxes)
   boxes = preprocessor.box_list_scale(
       boxlist, self._scaled_height, self._scaled_width).get()
   # Adjust box coordinates based on the offset.
   box_offset = tf.stack([self._crop_offset_y, self._crop_offset_x,
                          self._crop_offset_y, self._crop_offset_x,])
   boxes -= tf.to_float(tf.reshape(box_offset, [1, 4]))
   # Clip the boxes.
   boxes = self.clip_boxes(boxes)
   # Filter out ground truth boxes that are all zeros.
   indices = tf.where(tf.not_equal(tf.reduce_sum(boxes, axis=1), 0))
   boxes = tf.gather_nd(boxes, indices)
   classes = tf.gather_nd(self._classes, indices)
   return boxes, classes
Ejemplo n.º 4
0
    def label_anchors(self, gt_boxes, gt_labels):
        """Labels anchors with ground truth inputs.

    Args:
      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.
      gt_labels: A integer tensor with shape [N, 1] representing groundtruth
        classes.
    Returns:
      cls_targets_dict: ordered dictionary with keys
        [min_level, min_level+1, ..., max_level]. The values are tensor with
        shape [height_l, width_l, num_anchors * num_classes]. The
        height_l and width_l represent the dimension of class logits at l-th
        level.
      box_targets_dict: ordered dictionary with keys
        [min_level, min_level+1, ..., max_level]. The values are tensor with
        shape [height_l, width_l, num_anchors * 4]. The height_l and
        width_l represent the dimension of bounding box regression output at
        l-th level.
      num_positives: scalar tensor storing number of positives in an image.
    """
        gt_box_list = box_list.BoxList(gt_boxes)
        anchor_box_list = box_list.BoxList(self._anchors.boxes)

        # cls_weights, box_weights are not used
        cls_targets, _, box_targets, _, matches = self._target_assigner.assign(
            anchor_box_list, gt_box_list, gt_labels)

        # class labels start from 1 and the background class = -1
        cls_targets -= 1

        # create one-hot labels
        cls_targets_one_hot = tf.one_hot(tf.cast(cls_targets, dtype=tf.int32),
                                         self._num_classes)
        cls_targets_one_hot = tf.reshape(cls_targets_one_hot,
                                         [-1, self._num_classes])

        cls_targets_dict = self._unpack_labels(cls_targets_one_hot)
        box_targets_dict = self._unpack_labels(box_targets)
        num_positives = tf.reduce_sum(
            tf.cast(tf.not_equal(matches.match_results, -1), tf.float32))

        return cls_targets_dict, box_targets_dict, num_positives