예제 #1
0
    def _assign_targets(self,
                        groundtruth_boxes_list,
                        groundtruth_classes_list,
                        groundtruth_keypoints_list=None,
                        groundtruth_weights_list=None):
        """Assign groundtruth targets.

    Adds a background class to each one-hot encoding of groundtruth classes
    and uses target assigner to obtain regression and classification targets.

    Args:
      groundtruth_boxes_list: a list of 2-D tensors of shape [num_boxes, 4]
        containing coordinates of the groundtruth boxes.
          Groundtruth boxes are provided in [y_min, x_min, y_max, x_max]
          format and assumed to be normalized and clipped
          relative to the image window with y_min <= y_max and x_min <= x_max.
      groundtruth_classes_list: a list of 2-D one-hot (or k-hot) tensors of
        shape [num_boxes, num_classes] containing the class targets with the 0th
        index assumed to map to the first non-background class.
      groundtruth_keypoints_list: (optional) a list of 3-D tensors of shape
        [num_boxes, num_keypoints, 2]
      groundtruth_weights_list: A list of 1-D tf.float32 tensors of shape
        [num_boxes] containing weights for groundtruth boxes.

    Returns:
      batch_cls_targets: a tensor with shape [batch_size, num_anchors,
        num_classes],
      batch_cls_weights: a tensor with shape [batch_size, num_anchors],
      batch_reg_targets: a tensor with shape [batch_size, num_anchors,
        box_code_dimension]
      batch_reg_weights: a tensor with shape [batch_size, num_anchors],
      match_list: a list of matcher.Match objects encoding the match between
        anchors and groundtruth boxes for each image of the batch,
        with rows of the Match objects corresponding to groundtruth boxes
        and columns corresponding to anchors.
    """
        groundtruth_boxlists = [
            box_list.BoxList(boxes) for boxes in groundtruth_boxes_list
        ]
        if self._add_background_class:
            groundtruth_classes_with_background_list = [
                tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT')
                for one_hot_encoding in groundtruth_classes_list
            ]
        else:
            groundtruth_classes_with_background_list = groundtruth_classes_list

        if groundtruth_keypoints_list is not None:
            for boxlist, keypoints in zip(groundtruth_boxlists,
                                          groundtruth_keypoints_list):
                boxlist.add_field(fields.BoxListFields.keypoints, keypoints)
        return target_assigner.batch_assign_targets(
            self._target_assigner, self.anchors, groundtruth_boxlists,
            groundtruth_classes_with_background_list, groundtruth_weights_list)
  def test_batch_assign_targets(self):
    box_list1 = box_list.BoxList(tf.constant([[0., 0., 0.2, 0.2]]))
    box_list2 = box_list.BoxList(tf.constant(
        [[0, 0.25123152, 1, 1],
         [0.015789, 0.0985, 0.55789, 0.3842]]
    ))

    gt_box_batch = [box_list1, box_list2]
    gt_class_targets = [None, None]

    prior_means = tf.constant([[0, 0, .25, .25],
                               [0, .25, 1, 1],
                               [0, .1, .5, .5],
                               [.75, .75, 1, 1]])
    prior_stddevs = tf.constant([[.1, .1, .1, .1],
                                 [.1, .1, .1, .1],
                                 [.1, .1, .1, .1],
                                 [.1, .1, .1, .1]])
    priors = box_list.BoxList(prior_means)
    priors.add_field('stddev', prior_stddevs)

    exp_reg_targets = [[[0, 0, -0.5, -0.5],
                        [0, 0, 0, 0],
                        [0, 0, 0, 0,],
                        [0, 0, 0, 0,],],
                       [[0, 0, 0, 0,],
                        [0, 0.01231521, 0, 0],
                        [0.15789001, -0.01500003, 0.57889998, -1.15799987],
                        [0, 0, 0, 0]]]
    exp_cls_weights = [[1, 1, 1, 1],
                       [1, 1, 1, 1]]
    exp_cls_targets = [[[1], [0], [0], [0]],
                       [[0], [1], [1], [0]]]
    exp_reg_weights = [[1, 0, 0, 0],
                       [0, 1, 1, 0]]
    exp_match_0 = [0]
    exp_match_1 = [1, 2]

    agnostic_target_assigner = self._get_agnostic_target_assigner()
    (cls_targets, cls_weights, reg_targets, reg_weights,
     match_list) = targetassigner.batch_assign_targets(
         agnostic_target_assigner, priors, gt_box_batch, gt_class_targets)
    self.assertTrue(isinstance(match_list, list) and len(match_list) == 2)
    with self.test_session() as sess:
      (cls_targets_out, cls_weights_out, reg_targets_out, reg_weights_out,
       match_out_0, match_out_1) = sess.run([
           cls_targets, cls_weights, reg_targets, reg_weights] + [
               match.matched_column_indices() for match in match_list])
      self.assertAllClose(cls_targets_out, exp_cls_targets)
      self.assertAllClose(cls_weights_out, exp_cls_weights)
      self.assertAllClose(reg_targets_out, exp_reg_targets)
      self.assertAllClose(reg_weights_out, exp_reg_weights)
      self.assertAllClose(match_out_0, exp_match_0)
      self.assertAllClose(match_out_1, exp_match_1)
예제 #3
0
    def test_batch_assign_multiclass_targets(self):
        box_list1 = box_list.BoxList(tf.constant([[0., 0., 0.2, 0.2]]))

        box_list2 = box_list.BoxList(
            tf.constant([[0, 0.25123152, 1, 1],
                         [0.015789, 0.0985, 0.55789, 0.3842]]))

        gt_box_batch = [box_list1, box_list2]

        class_targets1 = tf.constant([[0, 1, 0, 0]], tf.float32)
        class_targets2 = tf.constant([[0, 0, 0, 1], [0, 0, 1, 0]], tf.float32)

        gt_class_targets = [class_targets1, class_targets2]

        prior_means = tf.constant([[0, 0, .25, .25], [0, .25, 1, 1],
                                   [0, .1, .5, .5], [.75, .75, 1, 1]])
        prior_stddevs = tf.constant([[.1, .1, .1, .1], [.1, .1, .1, .1],
                                     [.1, .1, .1, .1], [.1, .1, .1, .1]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        exp_reg_targets = [[[0, 0, -0.5, -0.5], [0, 0, 0, 0], [0, 0, 0, 0],
                            [0, 0, 0, 0]],
                           [[0, 0, 0, 0], [0, 0.01231521, 0, 0],
                            [0.15789001, -0.01500003, 0.57889998, -1.15799987],
                            [0, 0, 0, 0]]]
        exp_cls_weights = [[1, 1, 1, 1], [1, 1, 1, 1]]
        exp_cls_targets = [[[0, 1, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0],
                            [1, 0, 0, 0]],
                           [[1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0],
                            [1, 0, 0, 0]]]
        exp_reg_weights = [[1, 0, 0, 0], [0, 1, 1, 0]]
        exp_match_0 = [0]
        exp_match_1 = [1, 2]

        multiclass_target_assigner = self._get_multi_class_target_assigner(
            num_classes=3)

        (cls_targets, cls_weights, reg_targets, reg_weights,
         match_list) = targetassigner.batch_assign_targets(
             multiclass_target_assigner, priors, gt_box_batch,
             gt_class_targets)
        self.assertTrue(isinstance(match_list, list) and len(match_list) == 2)
        with self.test_session() as sess:
            (cls_targets_out, cls_weights_out, reg_targets_out,
             reg_weights_out, match_out_0, match_out_1) = sess.run(
                 [cls_targets, cls_weights, reg_targets, reg_weights] +
                 [match.matched_column_indices() for match in match_list])
            self.assertAllClose(cls_targets_out, exp_cls_targets)
            self.assertAllClose(cls_weights_out, exp_cls_weights)
            self.assertAllClose(reg_targets_out, exp_reg_targets)
            self.assertAllClose(reg_weights_out, exp_reg_weights)
            self.assertAllClose(match_out_0, exp_match_0)
            self.assertAllClose(match_out_1, exp_match_1)
예제 #4
0
 def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2):
   box_list1 = box_list.BoxList(groundtruth_boxlist1)
   box_list2 = box_list.BoxList(groundtruth_boxlist2)
   gt_box_batch = [box_list1, box_list2]
   gt_class_targets = [None, None]
   anchors_boxlist = box_list.BoxList(anchor_means)
   agnostic_target_assigner = self._get_agnostic_target_assigner()
   (cls_targets, cls_weights, reg_targets, reg_weights,
    _) = targetassigner.batch_assign_targets(
        agnostic_target_assigner, anchors_boxlist, gt_box_batch,
        gt_class_targets)
   return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #5
0
  def _assign_targets(self, groundtruth_boxes_list, groundtruth_classes_list,
                      groundtruth_keypoints_list=None,
                      groundtruth_weights_list=None):
    """Assign groundtruth targets.

    Adds a background class to each one-hot encoding of groundtruth classes
    and uses target assigner to obtain regression and classification targets.

    Args:
      groundtruth_boxes_list: a list of 2-D tensors of shape [num_boxes, 4]
        containing coordinates of the groundtruth boxes.
          Groundtruth boxes are provided in [y_min, x_min, y_max, x_max]
          format and assumed to be normalized and clipped
          relative to the image window with y_min <= y_max and x_min <= x_max.
      groundtruth_classes_list: a list of 2-D one-hot (or k-hot) tensors of
        shape [num_boxes, num_classes] containing the class targets with the 0th
        index assumed to map to the first non-background class.
      groundtruth_keypoints_list: (optional) a list of 3-D tensors of shape
        [num_boxes, num_keypoints, 2]
      groundtruth_weights_list: A list of 1-D tf.float32 tensors of shape
        [num_boxes] containing weights for groundtruth boxes.

    Returns:
      batch_cls_targets: a tensor with shape [batch_size, num_anchors,
        num_classes],
      batch_cls_weights: a tensor with shape [batch_size, num_anchors],
      batch_reg_targets: a tensor with shape [batch_size, num_anchors,
        box_code_dimension]
      batch_reg_weights: a tensor with shape [batch_size, num_anchors],
      match_list: a list of matcher.Match objects encoding the match between
        anchors and groundtruth boxes for each image of the batch,
        with rows of the Match objects corresponding to groundtruth boxes
        and columns corresponding to anchors.
    """
    groundtruth_boxlists = [
        box_list.BoxList(boxes) for boxes in groundtruth_boxes_list
    ]
    if self._add_background_class:
      groundtruth_classes_with_background_list = [
          tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT')
          for one_hot_encoding in groundtruth_classes_list
      ]
    else:
      groundtruth_classes_with_background_list = groundtruth_classes_list

    if groundtruth_keypoints_list is not None:
      for boxlist, keypoints in zip(
          groundtruth_boxlists, groundtruth_keypoints_list):
        boxlist.add_field(fields.BoxListFields.keypoints, keypoints)
    return target_assigner.batch_assign_targets(
        self._target_assigner, self.anchors, groundtruth_boxlists,
        groundtruth_classes_with_background_list, self._unmatched_class_label,
        groundtruth_weights_list)
예제 #6
0
 def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2):
   box_list1 = box_list.BoxList(groundtruth_boxlist1)
   box_list2 = box_list.BoxList(groundtruth_boxlist2)
   gt_box_batch = [box_list1, box_list2]
   gt_class_targets = [None, None]
   anchors_boxlist = box_list.BoxList(anchor_means)
   agnostic_target_assigner = self._get_agnostic_target_assigner()
   (cls_targets, cls_weights, reg_targets, reg_weights,
    _) = targetassigner.batch_assign_targets(
        agnostic_target_assigner, anchors_boxlist, gt_box_batch,
        gt_class_targets)
   return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #7
0
    def _assign_targets(self, groundtruth_boxes_list,
                        groundtruth_classes_list):
        """Assign groundtruth targets.
  
        Adds a background class to each one-hot encoding of groundtruth classes
        and uses target assigner to obtain regression and classification targets.
  
        Args:
          groundtruth_boxes_list: a list of 2-D tensors of shape [num_boxes, 4]
            containing coordinates of the groundtruth boxes.
              Groundtruth boxes are provided in [y_min, x_min, y_max, x_max]
              format and assumed to be normalized and clipped
              relative to the image window with y_min <= y_max and x_min <= x_max.
          groundtruth_classes_list: a list of 2-D one-hot (or k-hot) tensors of
            shape [num_boxes, num_classes] containing the class targets with the 0th
            index assumed to map to the first non-background class.
  
        Returns:
          batch_cls_targets: a tensor with shape [batch_size, num_anchors,
            num_classes],
          batch_cls_weights: a tensor with shape [batch_size, num_anchors],
          batch_reg_targets: a tensor with shape [batch_size, num_anchors,
            box_code_dimension]
          batch_reg_weights: a tensor with shape [batch_size, num_anchors],
          match_list: a list of matcher.Match objects encoding the match between
            anchors and groundtruth boxes for each image of the batch,
            with rows of the Match objects corresponding to groundtruth boxes
            and columns corresponding to anchors.
        """

        groundtruth_boxes_list = tf.reshape(
            groundtruth_boxes_list,
            [self._batch_size * (self._seq_length), -1])
        groundtruth_boxes_list = tf.unstack(groundtruth_boxes_list, axis=0)
        groundtruth_boxlists = [
            box_list.BoxList(tf.expand_dims(boxes, axis=0))
            for boxes in groundtruth_boxes_list
        ]

        groundtruth_classes_list = tf.reshape(
            groundtruth_classes_list,
            [self._batch_size * (self._seq_length), -1])
        groundtruth_classes_list = tf.unstack(groundtruth_classes_list, axis=0)
        groundtruth_classes_with_background_list = [
            tf.reshape(tf.one_hot(one_hot_encoding, self.num_classes + 1),
                       [1, self.num_classes + 1])
            for one_hot_encoding in groundtruth_classes_list
        ]

        return target_assigner.batch_assign_targets(
            self._target_assigner, self.anchors, groundtruth_boxlists,
            groundtruth_classes_with_background_list)
예제 #8
0
        def graph_fn(anchor_means, groundtruth_box_corners, gt_class_targets):
            groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
            gt_box_batch = [groundtruth_boxlist]
            gt_class_targets_batch = [gt_class_targets]
            anchors_boxlist = box_list.BoxList(anchor_means)

            multiclass_target_assigner = self._get_multi_class_target_assigner(
                num_classes=3)

            (cls_targets, cls_weights, reg_targets, reg_weights,
             _) = targetassigner.batch_assign_targets(
                 multiclass_target_assigner, anchors_boxlist, gt_box_batch,
                 gt_class_targets_batch)
            return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #9
0
 def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2,
              class_targets1, class_targets2):
     box_list1 = box_list.BoxList(groundtruth_boxlist1)
     box_list2 = box_list.BoxList(groundtruth_boxlist2)
     gt_box_batch = [box_list1, box_list2]
     gt_class_targets = [class_targets1, class_targets2]
     anchors_boxlist = box_list.BoxList(anchor_means)
     multiclass_target_assigner = self._get_multi_dimensional_target_assigner(
         target_dimensions=(2, 3))
     (cls_targets, cls_weights, reg_targets, reg_weights,
      _) = targetassigner.batch_assign_targets(
          multiclass_target_assigner, anchors_boxlist, gt_box_batch,
          gt_class_targets)
     return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #10
0
    def graph_fn(anchor_means, groundtruth_box_corners, gt_class_targets):
      groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
      gt_box_batch = [groundtruth_boxlist]
      gt_class_targets_batch = [gt_class_targets]
      anchors_boxlist = box_list.BoxList(anchor_means)

      multiclass_target_assigner = self._get_target_assigner()
      num_classes = 3
      unmatched_class_label = tf.constant([1] + num_classes * [0], tf.float32)
      (cls_targets, cls_weights, reg_targets, reg_weights,
       _) = targetassigner.batch_assign_targets(
           multiclass_target_assigner, anchors_boxlist,
           gt_box_batch, gt_class_targets_batch, unmatched_class_label)
      return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #11
0
 def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2,
              class_targets1, class_targets2):
   box_list1 = box_list.BoxList(groundtruth_boxlist1)
   box_list2 = box_list.BoxList(groundtruth_boxlist2)
   gt_box_batch = [box_list1, box_list2]
   gt_class_targets = [class_targets1, class_targets2]
   anchors_boxlist = box_list.BoxList(anchor_means)
   multiclass_target_assigner = self._get_multi_dimensional_target_assigner(
       target_dimensions=(2, 3))
   (cls_targets, cls_weights, reg_targets, reg_weights,
    _) = targetassigner.batch_assign_targets(
        multiclass_target_assigner, anchors_boxlist, gt_box_batch,
        gt_class_targets)
   return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #12
0
    def graph_fn(anchor_means, groundtruth_box_corners, gt_class_targets):
      groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
      gt_box_batch = [groundtruth_boxlist]
      gt_class_targets_batch = [gt_class_targets]
      anchors_boxlist = box_list.BoxList(anchor_means)

      multiclass_target_assigner = self._get_multi_class_target_assigner(
          num_classes=3)

      (cls_targets, cls_weights, reg_targets, reg_weights,
       _) = targetassigner.batch_assign_targets(
           multiclass_target_assigner, anchors_boxlist,
           gt_box_batch, gt_class_targets_batch)
      return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #13
0
 def graph_fn(anchor_means, anchor_stddevs, groundtruth_boxlist1,
              groundtruth_boxlist2, class_targets1, class_targets2):
   box_list1 = box_list.BoxList(groundtruth_boxlist1)
   box_list2 = box_list.BoxList(groundtruth_boxlist2)
   gt_box_batch = [box_list1, box_list2]
   gt_class_targets = [class_targets1, class_targets2]
   anchors_boxlist = box_list.BoxList(anchor_means)
   anchors_boxlist.add_field('stddev', anchor_stddevs)
   multiclass_target_assigner = self._get_multi_class_target_assigner(
       num_classes=3)
   (cls_targets, cls_weights, reg_targets, reg_weights,
    _) = targetassigner.batch_assign_targets(
        multiclass_target_assigner, anchors_boxlist, gt_box_batch,
        gt_class_targets)
   return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #14
0
 def graph_fn(anchor_means, anchor_stddevs, groundtruth_boxlist1,
              groundtruth_boxlist2, class_targets1, class_targets2):
     box_list1 = box_list.BoxList(groundtruth_boxlist1)
     box_list2 = box_list.BoxList(groundtruth_boxlist2)
     gt_box_batch = [box_list1, box_list2]
     gt_class_targets = [class_targets1, class_targets2]
     anchors_boxlist = box_list.BoxList(anchor_means)
     anchors_boxlist.add_field('stddev', anchor_stddevs)
     multiclass_target_assigner = self._get_multi_class_target_assigner(
         num_classes=3)
     (cls_targets, cls_weights, reg_targets, reg_weights,
      _) = targetassigner.batch_assign_targets(
          multiclass_target_assigner, anchors_boxlist, gt_box_batch,
          gt_class_targets)
     return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #15
0
        def graph_fn(anchor_means, groundtruth_box_corners, gt_class_targets):
            groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners)
            gt_box_batch = [groundtruth_boxlist]
            gt_class_targets_batch = [gt_class_targets]
            anchors_boxlist = box_list.BoxList(anchor_means)

            multiclass_target_assigner = self._get_target_assigner()
            num_classes = 3
            unmatched_class_label = tf.constant([1] + num_classes * [0],
                                                tf.float32)
            (cls_targets, cls_weights, reg_targets, reg_weights,
             _) = targetassigner.batch_assign_targets(
                 multiclass_target_assigner, anchors_boxlist, gt_box_batch,
                 gt_class_targets_batch, unmatched_class_label)
            return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #16
0
 def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2,
              class_targets1, class_targets2):
   box_list1 = box_list.BoxList(groundtruth_boxlist1)
   box_list2 = box_list.BoxList(groundtruth_boxlist2)
   gt_box_batch = [box_list1, box_list2]
   gt_class_targets = [class_targets1, class_targets2]
   anchors_boxlist = box_list.BoxList(anchor_means)
   multiclass_target_assigner = self._get_target_assigner()
   target_dimensions = (2, 3)
   unmatched_class_label = tf.constant(np.zeros(target_dimensions),
                                       tf.float32)
   (cls_targets, cls_weights, reg_targets, reg_weights,
    _) = targetassigner.batch_assign_targets(
        multiclass_target_assigner, anchors_boxlist, gt_box_batch,
        gt_class_targets, unmatched_class_label)
   return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #17
0
 def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2,
              class_targets1, class_targets2):
     box_list1 = box_list.BoxList(groundtruth_boxlist1)
     box_list2 = box_list.BoxList(groundtruth_boxlist2)
     gt_box_batch = [box_list1, box_list2]
     gt_class_targets = [class_targets1, class_targets2]
     anchors_boxlist = box_list.BoxList(anchor_means)
     multiclass_target_assigner = self._get_target_assigner()
     target_dimensions = (2, 3)
     unmatched_class_label = tf.constant(np.zeros(target_dimensions),
                                         tf.float32)
     (cls_targets, cls_weights, reg_targets, reg_weights,
      _) = targetassigner.batch_assign_targets(
          multiclass_target_assigner, anchors_boxlist, gt_box_batch,
          gt_class_targets, unmatched_class_label)
     return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #18
0
 def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2,
              class_targets1, class_targets2, groundtruth_weights1,
              groundtruth_weights2):
   box_list1 = box_list.BoxList(groundtruth_boxlist1)
   box_list2 = box_list.BoxList(groundtruth_boxlist2)
   gt_box_batch = [box_list1, box_list2]
   gt_class_targets = [class_targets1, class_targets2]
   gt_weights = [groundtruth_weights1, groundtruth_weights2]
   anchors_boxlist = box_list.BoxList(anchor_means)
   multiclass_target_assigner = self._get_target_assigner()
   num_classes = 3
   unmatched_class_label = tf.constant([1] + num_classes * [0], tf.float32)
   (cls_targets, cls_weights, reg_targets, reg_weights,
    _) = targetassigner.batch_assign_targets(
        multiclass_target_assigner, anchors_boxlist, gt_box_batch,
        gt_class_targets, unmatched_class_label, gt_weights)
   return (cls_targets, cls_weights, reg_targets, reg_weights)
예제 #19
0
 def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2,
              class_targets1, class_targets2, groundtruth_weights1,
              groundtruth_weights2):
     box_list1 = box_list.BoxList(groundtruth_boxlist1)
     box_list2 = box_list.BoxList(groundtruth_boxlist2)
     gt_box_batch = [box_list1, box_list2]
     gt_class_targets = [class_targets1, class_targets2]
     gt_weights = [groundtruth_weights1, groundtruth_weights2]
     anchors_boxlist = box_list.BoxList(anchor_means)
     multiclass_target_assigner = self._get_target_assigner()
     num_classes = 3
     unmatched_class_label = tf.constant([1] + num_classes * [0],
                                         tf.float32)
     (cls_targets, cls_weights, reg_targets, reg_weights,
      _) = targetassigner.batch_assign_targets(
          multiclass_target_assigner, anchors_boxlist, gt_box_batch,
          gt_class_targets, unmatched_class_label, gt_weights)
     return (cls_targets, cls_weights, reg_targets, reg_weights)
  def test_batch_assign_empty_groundtruth(self):
    box_coords_expanded = tf.zeros((1, 4), tf.float32)
    box_coords = tf.slice(box_coords_expanded, [0, 0], [0, 4])
    box_list1 = box_list.BoxList(box_coords)
    gt_box_batch = [box_list1]

    prior_means = tf.constant([[0, 0, .25, .25],
                               [0, .25, 1, 1]])
    prior_stddevs = tf.constant([[.1, .1, .1, .1],
                                 [.1, .1, .1, .1]])
    priors = box_list.BoxList(prior_means)
    priors.add_field('stddev', prior_stddevs)

    exp_reg_targets = [[[0, 0, 0, 0],
                        [0, 0, 0, 0]]]
    exp_cls_weights = [[1, 1]]
    exp_cls_targets = [[[1, 0, 0, 0],
                        [1, 0, 0, 0]]]
    exp_reg_weights = [[0, 0]]
    exp_match_0 = []

    num_classes = 3
    pad = 1
    gt_class_targets = tf.zeros((0, num_classes + pad))
    gt_class_targets_batch = [gt_class_targets]

    multiclass_target_assigner = self._get_multi_class_target_assigner(
        num_classes=3)

    (cls_targets, cls_weights, reg_targets, reg_weights,
     match_list) = targetassigner.batch_assign_targets(
         multiclass_target_assigner, priors,
         gt_box_batch, gt_class_targets_batch)
    self.assertTrue(isinstance(match_list, list) and len(match_list) == 1)
    with self.test_session() as sess:
      (cls_targets_out, cls_weights_out, reg_targets_out, reg_weights_out,
       match_out_0) = sess.run([
           cls_targets, cls_weights, reg_targets, reg_weights] + [
               match.matched_column_indices() for match in match_list])
      self.assertAllClose(cls_targets_out, exp_cls_targets)
      self.assertAllClose(cls_weights_out, exp_cls_weights)
      self.assertAllClose(reg_targets_out, exp_reg_targets)
      self.assertAllClose(reg_weights_out, exp_reg_weights)
      self.assertAllClose(match_out_0, exp_match_0)
    def test_batch_assign_empty_groundtruth(self):
        box_coords_expanded = tf.zeros((1, 4), tf.float32)
        box_coords = tf.slice(box_coords_expanded, [0, 0], [0, 4])
        box_list1 = box_list.BoxList(box_coords)
        gt_box_batch = [box_list1]

        prior_means = tf.constant([[0, 0, .25, .25],
                                   [0, .25, 1, 1]])
        prior_stddevs = tf.constant([[.1, .1, .1, .1],
                                     [.1, .1, .1, .1]])
        priors = box_list.BoxList(prior_means)
        priors.add_field('stddev', prior_stddevs)

        exp_reg_targets = [[[0, 0, 0, 0],
                            [0, 0, 0, 0]]]
        exp_cls_weights = [[1, 1]]
        exp_cls_targets = [[[1, 0, 0, 0],
                            [1, 0, 0, 0]]]
        exp_reg_weights = [[0, 0]]
        exp_match_0 = []

        num_classes = 3
        pad = 1
        gt_class_targets = tf.zeros((0, num_classes + pad))
        gt_class_targets_batch = [gt_class_targets]

        multiclass_target_assigner = self._get_multi_class_target_assigner(
            num_classes=3)

        (cls_targets, cls_weights, reg_targets, reg_weights,
         match_list) = targetassigner.batch_assign_targets(
            multiclass_target_assigner, priors,
            gt_box_batch, gt_class_targets_batch)
        self.assertTrue(isinstance(match_list, list) and len(match_list) == 1)
        with self.test_session() as sess:
            (cls_targets_out, cls_weights_out, reg_targets_out, reg_weights_out,
             match_out_0) = sess.run([
                                         cls_targets, cls_weights, reg_targets, reg_weights] + [
                                         match.matched_column_indices() for match in match_list])
            self.assertAllClose(cls_targets_out, exp_cls_targets)
            self.assertAllClose(cls_weights_out, exp_cls_weights)
            self.assertAllClose(reg_targets_out, exp_reg_targets)
            self.assertAllClose(reg_weights_out, exp_reg_weights)
            self.assertAllClose(match_out_0, exp_match_0)
예제 #22
0
  def _assign_targets(self, groundtruth_boxes_list, groundtruth_classes_list,
                    height, width, stride):
    """Assign groundtruth targets.

    Adds a background class to each one-hot encoding of groundtruth classes
    and uses target assigner to obtain regression and classification targets.

    Args:
      groundtruth_boxes_list: a list of 2-D tensors of shape [num_boxes, 4]
        containing coordinates of the groundtruth boxes.
          Groundtruth boxes are provided in [y_min, x_min, y_max, x_max]
          format and assumed to be normalized and clipped
          relative to the image window with y_min <= y_max and x_min <= x_max.
      groundtruth_classes_list: a list of 2-D one-hot (or k-hot) tensors of
        shape [num_boxes, num_classes] containing the class targets with the 0th
        index assumed to map to the first non-background class.

    Returns:
      batch_cls_targets: a tensor with shape [batch_size, num_anchors,
        num_classes],
      batch_cls_weights: a tensor with shape [batch_size, num_anchors],
      batch_reg_targets: a tensor with shape [batch_size, num_anchors,
        box_code_dimension]
      batch_reg_weights: a tensor with shape [batch_size, num_anchors],
      match_list: a list of matcher.Match objects encoding the match between
        anchors and groundtruth boxes for each image of the batch,
        with rows of the Match objects corresponding to groundtruth boxes
        and columns corresponding to anchors.
    """
    mask = tf
    groundtruth_boxlists = [
        box_list.BoxList(boxes) for boxes in groundtruth_boxes_list
    ]
    groundtruth_classes_with_background_list = [
        tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT')
        for one_hot_encoding in groundtruth_classes_list
    ]
    return target_assigner.batch_assign_targets(
        self._target_assigner, self.anchors, groundtruth_boxlists,
        groundtruth_classes_with_background_list)
예제 #23
0
  def _assign_targets(self, groundtruth_boxes_list, groundtruth_classes_list):  #assigning the targents 
    """Assign groundtruth targets.

    Adds a background class to each one-hot encoding of groundtruth classes
    and uses target assigner to obtain regression and classification targets.

    Args:
      groundtruth_boxes_list: a list of 2-D tensors of shape [num_boxes, 4]
        containing coordinates of the groundtruth boxes.
          Groundtruth boxes are provided in [y_min, x_min, y_max, x_max]
          format and assumed to be normalized and clipped
          relative to the image window with y_min <= y_max and x_min <= x_max.
      groundtruth_classes_list: a list of 2-D one-hot (or k-hot) tensors of
        shape [num_boxes, num_classes] containing the class targets with the 0th
        index assumed to map to the first non-background class.

    Returns:
      batch_cls_targets: a tensor with shape [batch_size, num_anchors,
        num_classes],
      batch_cls_weights: a tensor with shape [batch_size, num_anchors],
      batch_reg_targets: a tensor with shape [batch_size, num_anchors,
        box_code_dimension]
      batch_reg_weights: a tensor with shape [batch_size, num_anchors],
      match_list: a list of matcher.Match objects encoding the match between
        anchors and groundtruth boxes for each image of the batch,
        with rows of the Match objects corresponding to groundtruth boxes
        and columns corresponding to anchors.
    """
    groundtruth_boxlists = [
        box_list.BoxList(boxes) for boxes in groundtruth_boxes_list
    ]
    groundtruth_classes_with_background_list = [
        tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT')
        for one_hot_encoding in groundtruth_classes_list
    ]
    return target_assigner.batch_assign_targets(       #assign targets to the ll classifcations things and assigns regression targets to positve anchors 
        self._target_assigner, self.anchors, groundtruth_boxlists,    #Also the weight set for each example 
        groundtruth_classes_with_background_list)
예제 #24
0
	  	I: flattened_proposal_feature_maps = self._postprocess_rpn()
      'Very complicate function!!!!!!'
        i: self._format_groundtruth_data(): 
        ii: decoded_boxes = self._box_coder.decode(rpn_box_encodings, box_list.BoxList(anchors))
                            --> faster_rcnn_box_coder.FasterRcnnBoxCoder._decode()
            objectness_scores = tf.nn.softmax(rpn_objectness_predictions_with_background)
        iii:proposal_boxlist = post_processing.multiclass_non_max_suppression()
        iv: padded_proposals = box_list_ops.pad_or_clip_box_list()
	  	II: self._compute_second_stage_input_feature_maps()
	  	III: box_classifier_features = self._feature_extractor.extract_box_classifier_features()
	  	IV: box_predictions = self._mask_rcnn_box_predictor.predict()
	  	V: absolute_proposal_boxes = ops.normalized_to_image_coordinates()

B: losses_dict = detection_model.loss
	a. _loss_rpn
	  1. target_assigner.batch_assign_targets()
      I: target_assigner.assign()
        i: match_quality_matrix = self._similarity_calc.compare(groundtruth_boxes,anchors)
                -->sim_calc.IouSimilarity()-->box_list_ops.iou()
        ii: match = self._matcher.match(match_quality_matrix, **params)
                -->argmax_matcher.ArgMaxMatcher._match()
        iii: reg_targets = self._create_regression_targets(anchors,groundtruth_boxes,match)
        iv: cls_targets = self._create_classification_targets(groundtruth_labels,match)
        v: reg_weights = self._create_regression_weights(match)
        vi: cls_weights = self._create_classification_weights(
                        match, self._positive_class_weight, self._negative_class_weight)
    2. localization_losses = self._first_stage_localization_loss
          -->losses.WeightedSmoothL1LocalizationLoss._compute_loss()
	  3. objectness_losses = self._first_stage_objectness_loss
          -->losses.WeightedSoftmaxClassificationLoss._compute_loss()
	b. _loss_box_classifier
    def loss(self, prediction_dict, scope=None):
        """Compute scalar loss tensors with respect to provided groundtruth.

    Calling this function requires that groundtruth tensors have been
    provided via the provide_groundtruth function.

    Args:
      prediction_dict: a dictionary holding prediction tensors with
        1) box_encodings: 4-D float tensor of shape [batch_size, num_anchors,
          box_code_dimension] containing predicted boxes.
        2) class_predictions_with_background: 2-D float tensor of shape
          [batch_size, num_anchors, num_classes+1] containing class predictions
          (logits) for each of the anchors.  Note that this tensor *includes*
          background class predictions.
      scope: Optional scope name.

    Returns:
      a dictionary mapping loss keys (`localization_loss` and
        `classification_loss`) to scalar tensors representing corresponding loss
        values.
    """
        with tf.name_scope(scope, 'Loss', prediction_dict.values()):
            (groundtruth_boxlists, groundtruth_classes_with_background_list
             ) = self._format_groundtruth_data(with_background=True)
            (batch_cls_targets, batch_cls_weights, batch_reg_targets,
             batch_reg_weights,
             match_list) = target_assigner.batch_assign_targets(
                 self._target_assigner, self.anchors, groundtruth_boxlists,
                 groundtruth_classes_with_background_list)

            if self._add_summaries:
                self._summarize_input(
                    self.groundtruth_lists(fields.BoxListFields.boxes),
                    match_list)
            num_matches = tf.stack(
                [match.num_matched_columns() for match in match_list])
            location_losses = self._localization_loss(
                prediction_dict['box_encodings'],
                batch_reg_targets,
                weights=batch_reg_weights)
            cls_losses = self._classification_loss(
                prediction_dict['class_predictions_with_background'],
                batch_cls_targets,
                weights=batch_cls_weights)

            # Optionally apply hard mining on top of loss values
            localization_loss = tf.reduce_sum(location_losses)
            classification_loss = tf.reduce_sum(cls_losses)
            if self._hard_example_miner:
                (localization_loss,
                 classification_loss) = self._apply_hard_mining(
                     location_losses, cls_losses, prediction_dict, match_list)
                if self._add_summaries:
                    self._hard_example_miner.summarize()

            # Optionally normalize by number of positive matches
            normalizer = tf.constant(1.0, dtype=tf.float32)
            if self._normalize_loss_by_num_matches:
                normalizer = tf.maximum(
                    tf.to_float(tf.reduce_sum(num_matches)), 1.0)

            loss_dict = {
                'localization_loss':
                (self._localization_loss_weight / normalizer) *
                localization_loss,
                'classification_loss':
                (self._classification_loss_weight / normalizer) *
                classification_loss
            }
        return loss_dict
예제 #26
0
    model.ssd.similarity_calculator)
anchor_generator = anchor_generator_builder.build(model.ssd.anchor_generator)
(classification_loss, localization_loss, classification_weight,
 localization_weight,
 hard_example_miner) = losses_builder.build(model.ssd.loss)
image_resizer_fn = image_resizer_builder.build(model.ssd.image_resizer)
non_max_suppression_fn, score_conversion_fn = post_processing_builder.build(
    model.ssd.post_processing)
(classification_loss, localization_loss, classification_weight,
 localization_weight,
 hard_example_miner) = losses_builder.build(model.ssd.loss)
normalize_loss_by_num_matches = model.ssd.normalize_loss_by_num_matches
matcher = matcher_builder.build(model.ssd.matcher)
unmatched_cls_target = tf.constant([1] + num_classes * [0], tf.float32)
_target_assigner = target_assigner.TargetAssigner(
    region_similarity_calculator,
    matcher,
    box_coder,
    positive_class_weight=1.0,
    negative_class_weight=1.0,
    unmatched_cls_target=unmatched_cls_target)

anchors = anchor_generator.generate([(i, i) for i in range(9, 3, -1)])
pass

a = target_assigner.batch_assign_targets(
    _target_assigner, anchors, groundtruth_boxlists,
    groundtruth_classes_with_background_list)
1
2
pass