Beispiel #1
0
    def test_multiclass_nms_select_with_clip_window_change_coordinate_frame(
            self):
        boxes = tf.constant([[[0, 0, 10, 10]], [[1, 1, 11, 11]]], tf.float32)
        scores = tf.constant([[.9], [.75]])
        clip_window = tf.constant([5, 4, 8, 7], tf.float32)
        score_thresh = 0.0
        iou_thresh = 0.5
        max_output_size = 100

        exp_nms_corners = [[0, 0, 1, 1]]
        exp_nms_scores = [.9]
        exp_nms_classes = [0]

        nms, _ = post_processing.multiclass_non_max_suppression(
            boxes,
            scores,
            score_thresh,
            iou_thresh,
            max_output_size,
            clip_window=clip_window,
            change_coordinate_frame=True)
        with self.test_session() as sess:
            nms_corners_output, nms_scores_output, nms_classes_output = sess.run(
                [
                    nms.get(),
                    nms.get_field(fields.BoxListFields.scores),
                    nms.get_field(fields.BoxListFields.classes)
                ])
            self.assertAllClose(nms_corners_output, exp_nms_corners)
            self.assertAllClose(nms_scores_output, exp_nms_scores)
            self.assertAllClose(nms_classes_output, exp_nms_classes)
  def test_multiclass_nms_select_with_separate_boxes(self):
    boxes = tf.constant([[[0, 0, 1, 1], [0, 0, 4, 5]],
                         [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]],
                         [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]],
                         [[0, 10, 1, 11], [0, 10, 1, 11]],
                         [[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]],
                         [[0, 100, 1, 101], [0, 100, 1, 101]],
                         [[0, 1000, 1, 1002], [0, 999, 2, 1004]],
                         [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]],
                        tf.float32)
    scores = tf.constant([[.9, 0.01], [.75, 0.05],
                          [.6, 0.01], [.95, 0],
                          [.5, 0.01], [.3, 0.01],
                          [.01, .85], [.01, .5]])
    score_thresh = 0.1
    iou_thresh = .5
    max_output_size = 4

    exp_nms_corners = [[0, 10, 1, 11],
                       [0, 0, 1, 1],
                       [0, 999, 2, 1004],
                       [0, 100, 1, 101]]
    exp_nms_scores = [.95, .9, .85, .3]
    exp_nms_classes = [0, 0, 1, 0]

    nms = post_processing.multiclass_non_max_suppression(
        boxes, scores, score_thresh, iou_thresh, max_output_size)
    with self.test_session() as sess:
      nms_corners_output, nms_scores_output, nms_classes_output = sess.run(
          [nms.get(), nms.get_field(fields.BoxListFields.scores),
           nms.get_field(fields.BoxListFields.classes)])
      self.assertAllClose(nms_corners_output, exp_nms_corners)
      self.assertAllClose(nms_scores_output, exp_nms_scores)
      self.assertAllClose(nms_classes_output, exp_nms_classes)
    def nms_single_class(self, boxes, scores, input_metadata):
        """ see NmsPerformerBase """
        # define nms function params
        # This is not supposed to be in the request! we insert a default value for safety.
        score_thresh = SCORE_THRESH
        iou_thresh = input_metadata.get('nmsThresh', 0.6)
        max_output_size = boxes.shape[0]

        # expand to adapt to expected input dimensions
        boxes = boxes[:, np.newaxis, :]
        scores = scores[:, np.newaxis]

        # convert to tensors
        boxes = tf.constant(boxes, tf.float32)
        scores = tf.constant(scores, tf.float32)

        # create nms graph node
        nms_node = multiclass_non_max_suppression(boxes, scores, score_thresh,
                                                  iou_thresh, max_output_size)

        # run nms evaluation
        self.sess.run(nms_node.get())
        nms_boxes = nms_node.data['boxes'].eval(session=self.sess)
        nms_scores = nms_node.data['scores'].eval(session=self.sess)

        return nms_boxes, nms_scores
Beispiel #4
0
    def test_multiclass_nms_select_with_separate_boxes(self):
        boxes = tf.constant([[[0, 0, 1, 1], [0, 0, 4, 5]],
                             [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]],
                             [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]],
                             [[0, 10, 1, 11], [0, 10, 1, 11]],
                             [[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]],
                             [[0, 100, 1, 101], [0, 100, 1, 101]],
                             [[0, 1000, 1, 1002], [0, 999, 2, 1004]],
                             [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]],
                            tf.float32)
        scores = tf.constant([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0],
                              [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]])
        score_thresh = 0.1
        iou_thresh = .5
        max_output_size = 4

        exp_nms_corners = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 999, 2, 1004],
                           [0, 100, 1, 101]]
        exp_nms_scores = [.95, .9, .85, .3]
        exp_nms_classes = [0, 0, 1, 0]

        nms, _ = post_processing.multiclass_non_max_suppression(
            boxes, scores, score_thresh, iou_thresh, max_output_size)
        with self.test_session() as sess:
            nms_corners_output, nms_scores_output, nms_classes_output = sess.run(
                [
                    nms.get(),
                    nms.get_field(fields.BoxListFields.scores),
                    nms.get_field(fields.BoxListFields.classes)
                ])
            self.assertAllClose(nms_corners_output, exp_nms_corners)
            self.assertAllClose(nms_scores_output, exp_nms_scores)
            self.assertAllClose(nms_classes_output, exp_nms_classes)
  def test_multiclass_nms_select_with_clip_window_change_coordinate_frame(self):
    boxes = tf.constant([[[0, 0, 10, 10]],
                         [[1, 1, 11, 11]]], tf.float32)
    scores = tf.constant([[.9], [.75]])
    clip_window = tf.constant([5, 4, 8, 7], tf.float32)
    score_thresh = 0.0
    iou_thresh = 0.5
    max_output_size = 100

    exp_nms_corners = [[0, 0, 1, 1]]
    exp_nms_scores = [.9]
    exp_nms_classes = [0]

    nms, _ = post_processing.multiclass_non_max_suppression(
        boxes,
        scores,
        score_thresh,
        iou_thresh,
        max_output_size,
        clip_window=clip_window,
        change_coordinate_frame=True)
    with self.test_session() as sess:
      nms_corners_output, nms_scores_output, nms_classes_output = sess.run(
          [nms.get(), nms.get_field(fields.BoxListFields.scores),
           nms.get_field(fields.BoxListFields.classes)])
      self.assertAllClose(nms_corners_output, exp_nms_corners)
      self.assertAllClose(nms_scores_output, exp_nms_scores)
      self.assertAllClose(nms_classes_output, exp_nms_classes)
  def test_multiclass_nms_with_shared_boxes_given_keypoint_heatmaps(self):
    boxes = tf.constant([[[0, 0, 1, 1]],
                         [[0, 0.1, 1, 1.1]],
                         [[0, -0.1, 1, 0.9]],
                         [[0, 10, 1, 11]],
                         [[0, 10.1, 1, 11.1]],
                         [[0, 100, 1, 101]],
                         [[0, 1000, 1, 1002]],
                         [[0, 1000, 1, 1002.1]]], tf.float32)

    scores = tf.constant([[.9, 0.01], [.75, 0.05],
                          [.6, 0.01], [.95, 0],
                          [.5, 0.01], [.3, 0.01],
                          [.01, .85], [.01, .5]])

    num_boxes = tf.shape(boxes)[0]
    heatmap_height = 5
    heatmap_width = 5
    num_keypoints = 17
    keypoint_heatmaps = tf.ones(
        [num_boxes, heatmap_height, heatmap_width, num_keypoints],
        dtype=tf.float32)

    score_thresh = 0.1
    iou_thresh = .5
    max_output_size = 4
    exp_nms_corners = [[0, 10, 1, 11],
                       [0, 0, 1, 1],
                       [0, 1000, 1, 1002],
                       [0, 100, 1, 101]]

    exp_nms_scores = [.95, .9, .85, .3]
    exp_nms_classes = [0, 0, 1, 0]
    exp_nms_keypoint_heatmaps = np.ones(
        (4, heatmap_height, heatmap_width, num_keypoints), dtype=np.float32)

    nms, _ = post_processing.multiclass_non_max_suppression(
        boxes,
        scores,
        score_thresh,
        iou_thresh,
        max_output_size,
        additional_fields={
            fields.BoxListFields.keypoint_heatmaps: keypoint_heatmaps
        })

    with self.test_session() as sess:
      (nms_corners_output,
       nms_scores_output,
       nms_classes_output,
       nms_keypoint_heatmaps) = sess.run(
           [nms.get(),
            nms.get_field(fields.BoxListFields.scores),
            nms.get_field(fields.BoxListFields.classes),
            nms.get_field(fields.BoxListFields.keypoint_heatmaps)])

      self.assertAllClose(nms_corners_output, exp_nms_corners)
      self.assertAllClose(nms_scores_output, exp_nms_scores)
      self.assertAllClose(nms_classes_output, exp_nms_classes)
      self.assertAllEqual(nms_keypoint_heatmaps, exp_nms_keypoint_heatmaps)
  def test_multiclass_nms_with_shared_boxes_given_keypoint_heatmaps(self):
    boxes = tf.constant([[[0, 0, 1, 1]],
                         [[0, 0.1, 1, 1.1]],
                         [[0, -0.1, 1, 0.9]],
                         [[0, 10, 1, 11]],
                         [[0, 10.1, 1, 11.1]],
                         [[0, 100, 1, 101]],
                         [[0, 1000, 1, 1002]],
                         [[0, 1000, 1, 1002.1]]], tf.float32)

    scores = tf.constant([[.9, 0.01], [.75, 0.05],
                          [.6, 0.01], [.95, 0],
                          [.5, 0.01], [.3, 0.01],
                          [.01, .85], [.01, .5]])

    num_boxes = tf.shape(boxes)[0]
    heatmap_height = 5
    heatmap_width = 5
    num_keypoints = 17
    keypoint_heatmaps = tf.ones(
        [num_boxes, heatmap_height, heatmap_width, num_keypoints],
        dtype=tf.float32)

    score_thresh = 0.1
    iou_thresh = .5
    max_output_size = 4
    exp_nms_corners = [[0, 10, 1, 11],
                       [0, 0, 1, 1],
                       [0, 1000, 1, 1002],
                       [0, 100, 1, 101]]

    exp_nms_scores = [.95, .9, .85, .3]
    exp_nms_classes = [0, 0, 1, 0]
    exp_nms_keypoint_heatmaps = np.ones(
        (4, heatmap_height, heatmap_width, num_keypoints), dtype=np.float32)

    nms, _ = post_processing.multiclass_non_max_suppression(
        boxes,
        scores,
        score_thresh,
        iou_thresh,
        max_output_size,
        additional_fields={
            fields.BoxListFields.keypoint_heatmaps: keypoint_heatmaps
        })

    with self.test_session() as sess:
      (nms_corners_output,
       nms_scores_output,
       nms_classes_output,
       nms_keypoint_heatmaps) = sess.run(
           [nms.get(),
            nms.get_field(fields.BoxListFields.scores),
            nms.get_field(fields.BoxListFields.classes),
            nms.get_field(fields.BoxListFields.keypoint_heatmaps)])

      self.assertAllClose(nms_corners_output, exp_nms_corners)
      self.assertAllClose(nms_scores_output, exp_nms_scores)
      self.assertAllClose(nms_classes_output, exp_nms_classes)
      self.assertAllEqual(nms_keypoint_heatmaps, exp_nms_keypoint_heatmaps)
 def graph_fn(boxes, scores):
     score_thresh = 0.1
     iou_thresh = .5
     max_output_size = 4
     nms, _ = post_processing.multiclass_non_max_suppression(
         boxes, scores, score_thresh, iou_thresh, max_output_size)
     return (nms.get(), nms.get_field(fields.BoxListFields.scores),
             nms.get_field(fields.BoxListFields.classes))
 def graph_fn(boxes, scores):
     nms, nms_valid = post_processing.multiclass_non_max_suppression(
         boxes,
         scores,
         score_thresh,
         iou_thresh,
         max_output_size,
         pad_to_max_output_size=True)
     return nms.get(), nms_valid
Beispiel #10
0
def model_fn(features, labels, mode, params, config):
    """
  Model function for estimator
  :param features:
  :param labels:
  :param mode:
  :param params:
  :param config:
  :return:
  """
    image = features['image']

    # Init network.
    ssdnet = ssd_resnet_50.init(params['class_num'], params['weight_decay'],
                                params['is_training'])

    # Compute output.
    logits, locations, endpoints = ssdnet(image)

    if mode == tf.estimator.ModeKeys.TRAIN:
        # Compute SSD loss and put it to global loss.
        ssd_resnet_50.ssdLoss(logits, locations, labels, params['alpha'])
        total_loss = tf.losses.get_total_loss()

        # Create train op
        optimazer = tf.train.GradientDescentOptimizer(
            learning_rate=params['learning_rate'])
        train_op = optimazer.minimize(
            total_loss, global_step=tf.train.get_or_create_global_step())
        return tf.estimator.EstimatorSpec(mode,
                                          loss=total_loss,
                                          train_op=train_op)

    if mode == tf.estimator.ModeKeys.EVAL:
        plogits = tf.unstack(logits, axis=0)
        probs = tf.nn.softmax(plogits, axis=1)
        pbboxes = tf.unstack(locations, axis=0)

        # Remove all background bboxes
        pbboxes, probs = evalUtil.rmBackgroundBox(pbboxes, probs)

        #TODO  Apply non maximum suppression.
        pbboxes_list = multiclass_non_max_suppression(
            pbboxes,
            probs,
        )

        eval_metrics = {}
        eval_metrics.update(
            evalUtil.get_evaluate_ops(probs,
                                      pbboxes_list,
                                      labels,
                                      categories=labels['category']))
        return eval_metrics

    if mode == tf.estimator.ModeKeys.PREDICT:
        return logits, locations
  def test_multiclass_nms_select_with_shared_boxes_given_keypoints(self):
    boxes = tf.constant([[[0, 0, 1, 1]],
                         [[0, 0.1, 1, 1.1]],
                         [[0, -0.1, 1, 0.9]],
                         [[0, 10, 1, 11]],
                         [[0, 10.1, 1, 11.1]],
                         [[0, 100, 1, 101]],
                         [[0, 1000, 1, 1002]],
                         [[0, 1000, 1, 1002.1]]], tf.float32)
    scores = tf.constant([[.9, 0.01], [.75, 0.05],
                          [.6, 0.01], [.95, 0],
                          [.5, 0.01], [.3, 0.01],
                          [.01, .85], [.01, .5]])
    num_keypoints = 6
    keypoints = tf.tile(
        tf.reshape(tf.range(8), [8, 1, 1]),
        [1, num_keypoints, 2])
    score_thresh = 0.1
    iou_thresh = .5
    max_output_size = 4

    exp_nms_corners = [[0, 10, 1, 11],
                       [0, 0, 1, 1],
                       [0, 1000, 1, 1002],
                       [0, 100, 1, 101]]
    exp_nms_scores = [.95, .9, .85, .3]
    exp_nms_classes = [0, 0, 1, 0]
    exp_nms_keypoints_tensor = tf.tile(
        tf.reshape(tf.constant([3, 0, 6, 5], dtype=tf.float32), [4, 1, 1]),
        [1, num_keypoints, 2])

    nms, _ = post_processing.multiclass_non_max_suppression(
        boxes,
        scores,
        score_thresh,
        iou_thresh,
        max_output_size,
        additional_fields={fields.BoxListFields.keypoints: keypoints})

    with self.test_session() as sess:
      (nms_corners_output,
       nms_scores_output,
       nms_classes_output,
       nms_keypoints,
       exp_nms_keypoints) = sess.run([
           nms.get(),
           nms.get_field(fields.BoxListFields.scores),
           nms.get_field(fields.BoxListFields.classes),
           nms.get_field(fields.BoxListFields.keypoints),
           exp_nms_keypoints_tensor
       ])
      self.assertAllClose(nms_corners_output, exp_nms_corners)
      self.assertAllClose(nms_scores_output, exp_nms_scores)
      self.assertAllClose(nms_classes_output, exp_nms_classes)
      self.assertAllEqual(nms_keypoints, exp_nms_keypoints)
  def test_multiclass_nms_select_with_shared_boxes_given_keypoints(self):
    boxes = tf.constant([[[0, 0, 1, 1]],
                         [[0, 0.1, 1, 1.1]],
                         [[0, -0.1, 1, 0.9]],
                         [[0, 10, 1, 11]],
                         [[0, 10.1, 1, 11.1]],
                         [[0, 100, 1, 101]],
                         [[0, 1000, 1, 1002]],
                         [[0, 1000, 1, 1002.1]]], tf.float32)
    scores = tf.constant([[.9, 0.01], [.75, 0.05],
                          [.6, 0.01], [.95, 0],
                          [.5, 0.01], [.3, 0.01],
                          [.01, .85], [.01, .5]])
    num_keypoints = 6
    keypoints = tf.tile(
        tf.reshape(tf.range(8), [8, 1, 1]),
        [1, num_keypoints, 2])
    score_thresh = 0.1
    iou_thresh = .5
    max_output_size = 4

    exp_nms_corners = [[0, 10, 1, 11],
                       [0, 0, 1, 1],
                       [0, 1000, 1, 1002],
                       [0, 100, 1, 101]]
    exp_nms_scores = [.95, .9, .85, .3]
    exp_nms_classes = [0, 0, 1, 0]
    exp_nms_keypoints_tensor = tf.tile(
        tf.reshape(tf.constant([3, 0, 6, 5], dtype=tf.float32), [4, 1, 1]),
        [1, num_keypoints, 2])

    nms, _ = post_processing.multiclass_non_max_suppression(
        boxes,
        scores,
        score_thresh,
        iou_thresh,
        max_output_size,
        additional_fields={fields.BoxListFields.keypoints: keypoints})

    with self.test_session() as sess:
      (nms_corners_output,
       nms_scores_output,
       nms_classes_output,
       nms_keypoints,
       exp_nms_keypoints) = sess.run([
           nms.get(),
           nms.get_field(fields.BoxListFields.scores),
           nms.get_field(fields.BoxListFields.classes),
           nms.get_field(fields.BoxListFields.keypoints),
           exp_nms_keypoints_tensor
       ])
      self.assertAllClose(nms_corners_output, exp_nms_corners)
      self.assertAllClose(nms_scores_output, exp_nms_scores)
      self.assertAllClose(nms_classes_output, exp_nms_classes)
      self.assertAllEqual(nms_keypoints, exp_nms_keypoints)
 def graph_fn(boxes, scores):
   nms, num_valid_nms_boxes = post_processing.multiclass_non_max_suppression(
       boxes,
       scores,
       score_thresh,
       iou_thresh,
       max_size_per_class,
       max_total_size=max_output_size,
       pad_to_max_output_size=True)
   return [nms.get(), nms.get_field(fields.BoxListFields.scores),
           nms.get_field(fields.BoxListFields.classes), num_valid_nms_boxes]
Beispiel #14
0
    def test_multiclass_nms_with_additional_fields(self):
        boxes = tf.constant(
            [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]],
             [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]],
             [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], tf.float32)

        scores = tf.constant([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0],
                              [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]])

        coarse_boxes_key = 'coarse_boxes'
        coarse_boxes = tf.constant(
            [[0.1, 0.1, 1.1, 1.1], [0.1, 0.2, 1.1, 1.2], [0.1, -0.2, 1.1, 1.0],
             [0.1, 10.1, 1.1, 11.1], [0.1, 10.2, 1.1, 11.2],
             [0.1, 100.1, 1.1, 101.1], [0.1, 1000.1, 1.1, 1002.1],
             [0.1, 1000.1, 1.1, 1002.2]], tf.float32)

        score_thresh = 0.1
        iou_thresh = .5
        max_output_size = 4

        exp_nms_corners = np.array([[0, 10, 1, 11], [0, 0, 1, 1],
                                    [0, 1000, 1, 1002], [0, 100, 1, 101]],
                                   dtype=np.float32)

        exp_nms_coarse_corners = np.array(
            [[0.1, 10.1, 1.1, 11.1], [0.1, 0.1, 1.1, 1.1],
             [0.1, 1000.1, 1.1, 1002.1], [0.1, 100.1, 1.1, 101.1]],
            dtype=np.float32)

        exp_nms_scores = [.95, .9, .85, .3]
        exp_nms_classes = [0, 0, 1, 0]

        nms, _ = post_processing.multiclass_non_max_suppression(
            boxes,
            scores,
            score_thresh,
            iou_thresh,
            max_output_size,
            additional_fields={coarse_boxes_key: coarse_boxes})

        with self.test_session() as sess:
            (nms_corners_output, nms_scores_output, nms_classes_output,
             nms_coarse_corners) = sess.run([
                 nms.get(),
                 nms.get_field(fields.BoxListFields.scores),
                 nms.get_field(fields.BoxListFields.classes),
                 nms.get_field(coarse_boxes_key)
             ])

            self.assertAllClose(nms_corners_output, exp_nms_corners)
            self.assertAllClose(nms_scores_output, exp_nms_scores)
            self.assertAllClose(nms_classes_output, exp_nms_classes)
            self.assertAllEqual(nms_coarse_corners, exp_nms_coarse_corners)
 def graph_fn(boxes, scores):
     nms, _ = post_processing.multiclass_non_max_suppression(
         boxes,
         scores,
         score_thresh,
         iou_thresh,
         max_size_per_class=max_output_size,
         max_total_size=max_output_size,
         soft_nms_sigma=0.5)
     return [
         nms.get(),
         nms.get_field(fields.BoxListFields.scores),
         nms.get_field(fields.BoxListFields.classes)
     ]
 def graph_fn(boxes, scores, clip_window):
     nms, nms_valid = post_processing.multiclass_non_max_suppression(
         boxes,
         scores,
         score_thresh,
         iou_thresh,
         max_output_size,
         pad_to_max_output_size=True,
         clip_window=clip_window)
     return [
         nms.get(),
         nms.get_field(fields.BoxListFields.scores),
         nms.get_field(fields.BoxListFields.classes), nms_valid
     ]
 def graph_fn(boxes, scores, keypoints):
     nms, nms_valid = post_processing.multiclass_non_max_suppression(
         boxes,
         scores,
         score_thresh,
         iou_thresh,
         max_output_size,
         pad_to_max_output_size=True,
         additional_fields={fields.BoxListFields.keypoints: keypoints})
     return [
         nms.get(),
         nms.get_field(fields.BoxListFields.scores),
         nms.get_field(fields.BoxListFields.classes),
         nms.get_field(fields.BoxListFields.keypoints), nms_valid
     ]
Beispiel #18
0
        def single_image_nms_fn(args):
            """Runs NMS on a single image and returns padded output."""
            (per_image_boxes, per_image_scores, per_image_masks,
             per_image_num_valid_boxes) = args
            per_image_boxes = tf.reshape(
                tf.slice(per_image_boxes, 3 * [0],
                         tf.stack([per_image_num_valid_boxes, -1, -1])),
                [-1, q, 4])
            per_image_scores = tf.reshape(
                tf.slice(per_image_scores, [0, 0],
                         tf.stack([per_image_num_valid_boxes, -1])),
                [-1, num_classes])

            per_image_masks = tf.reshape(
                tf.slice(per_image_masks, 4 * [0],
                         tf.stack([per_image_num_valid_boxes, -1, -1, -1])), [
                             -1, q, per_image_masks.shape[2].value,
                             per_image_masks.shape[3].value
                         ])
            nmsed_boxlist = multiclass_non_max_suppression(
                per_image_boxes,
                per_image_scores,
                score_thresh,
                iou_thresh,
                max_size_per_class,
                max_total_size,
                masks=per_image_masks,
                clip_window=clip_window,
                change_coordinate_frame=change_coordinate_frame)

            num_detections = nmsed_boxlist.num_boxes()
            nmsed_boxes = nmsed_boxlist.get()
            nmsed_scores = nmsed_boxlist.get_field(fields.BoxListFields.scores)
            nmsed_classes = nmsed_boxlist.get_field(
                fields.BoxListFields.classes)
            nmsed_masks = nmsed_boxlist.get_field(fields.BoxListFields.masks)

            resampling_inds = util.topk_or_pad_inds_with_resampling(
                tf.ones((num_detections, ), dtype=tf.bool), nmsed_scores,
                max_total_size)

            tensor_list = [
                nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks
            ]
            tensor_list = [
                tf.gather(tensor, resampling_inds) for tensor in tensor_list
            ]
            return tensor_list + [num_detections, max_total_size]
Beispiel #19
0
 def test_with_invalid_scores_size(self):
     boxes = tf.constant(
         [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]],
          [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]]],
         tf.float32)
     scores = tf.constant([[.9], [.75], [.6], [.95], [.5]])
     iou_thresh = .5
     score_thresh = 0.6
     max_output_size = 3
     nms = post_processing.multiclass_non_max_suppression(
         boxes, scores, score_thresh, iou_thresh, max_output_size)
     with self.test_session() as sess:
         with self.assertRaisesWithPredicateMatch(
                 tf.errors.InvalidArgumentError,
                 'Incorrect scores field length'):
             sess.run(nms.get())
Beispiel #20
0
    def test_multiclass_nms_threshold_then_select_with_shared_boxes(self):
        boxes = tf.constant(
            [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]],
             [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]],
             [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], tf.float32)
        scores = tf.constant([[.9], [.75], [.6], [.95], [.5], [.3], [.01],
                              [.01]])
        score_thresh = 0.1
        iou_thresh = .5
        max_output_size = 3

        exp_nms = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 100, 1, 101]]
        nms, _ = post_processing.multiclass_non_max_suppression(
            boxes, scores, score_thresh, iou_thresh, max_output_size)
        with self.test_session() as sess:
            nms_output = sess.run(nms.get())
            self.assertAllClose(nms_output, exp_nms)
 def test_with_invalid_scores_size(self):
   boxes = tf.constant([[[0, 0, 1, 1]],
                        [[0, 0.1, 1, 1.1]],
                        [[0, -0.1, 1, 0.9]],
                        [[0, 10, 1, 11]],
                        [[0, 10.1, 1, 11.1]],
                        [[0, 100, 1, 101]]], tf.float32)
   scores = tf.constant([[.9], [.75], [.6], [.95], [.5]])
   iou_thresh = .5
   score_thresh = 0.6
   max_output_size = 3
   nms = post_processing.multiclass_non_max_suppression(
       boxes, scores, score_thresh, iou_thresh, max_output_size)
   with self.test_session() as sess:
     with self.assertRaisesWithPredicateMatch(
         tf.errors.InvalidArgumentError, 'Incorrect scores field length'):
       sess.run(nms.get())
  def test_multiclass_nms_threshold_then_select_with_shared_boxes(self):
    boxes = tf.constant([[[0, 0, 1, 1]],
                         [[0, 0.1, 1, 1.1]],
                         [[0, -0.1, 1, 0.9]],
                         [[0, 10, 1, 11]],
                         [[0, 10.1, 1, 11.1]],
                         [[0, 100, 1, 101]],
                         [[0, 1000, 1, 1002]],
                         [[0, 1000, 1, 1002.1]]], tf.float32)
    scores = tf.constant([[.9], [.75], [.6], [.95], [.5], [.3], [.01], [.01]])
    score_thresh = 0.1
    iou_thresh = .5
    max_output_size = 3

    exp_nms = [[0, 10, 1, 11],
               [0, 0, 1, 1],
               [0, 100, 1, 101]]
    nms = post_processing.multiclass_non_max_suppression(
        boxes, scores, score_thresh, iou_thresh, max_output_size)
    with self.test_session() as sess:
      nms_output = sess.run(nms.get())
      self.assertAllClose(nms_output, exp_nms)
  def test_multiclass_nms_with_additional_fields(self):
    boxes = tf.constant([[[0, 0, 1, 1]],
                         [[0, 0.1, 1, 1.1]],
                         [[0, -0.1, 1, 0.9]],
                         [[0, 10, 1, 11]],
                         [[0, 10.1, 1, 11.1]],
                         [[0, 100, 1, 101]],
                         [[0, 1000, 1, 1002]],
                         [[0, 1000, 1, 1002.1]]], tf.float32)

    scores = tf.constant([[.9, 0.01], [.75, 0.05],
                          [.6, 0.01], [.95, 0],
                          [.5, 0.01], [.3, 0.01],
                          [.01, .85], [.01, .5]])

    coarse_boxes_key = 'coarse_boxes'
    coarse_boxes = tf.constant([[0.1, 0.1, 1.1, 1.1],
                                [0.1, 0.2, 1.1, 1.2],
                                [0.1, -0.2, 1.1, 1.0],
                                [0.1, 10.1, 1.1, 11.1],
                                [0.1, 10.2, 1.1, 11.2],
                                [0.1, 100.1, 1.1, 101.1],
                                [0.1, 1000.1, 1.1, 1002.1],
                                [0.1, 1000.1, 1.1, 1002.2]], tf.float32)

    score_thresh = 0.1
    iou_thresh = .5
    max_output_size = 4

    exp_nms_corners = np.array([[0, 10, 1, 11],
                                [0, 0, 1, 1],
                                [0, 1000, 1, 1002],
                                [0, 100, 1, 101]], dtype=np.float32)

    exp_nms_coarse_corners = np.array([[0.1, 10.1, 1.1, 11.1],
                                       [0.1, 0.1, 1.1, 1.1],
                                       [0.1, 1000.1, 1.1, 1002.1],
                                       [0.1, 100.1, 1.1, 101.1]],
                                      dtype=np.float32)

    exp_nms_scores = [.95, .9, .85, .3]
    exp_nms_classes = [0, 0, 1, 0]

    nms = post_processing.multiclass_non_max_suppression(
        boxes, scores, score_thresh, iou_thresh, max_output_size,
        additional_fields={coarse_boxes_key: coarse_boxes})

    with self.test_session() as sess:
      (nms_corners_output,
       nms_scores_output,
       nms_classes_output,
       nms_coarse_corners) = sess.run(
           [nms.get(),
            nms.get_field(fields.BoxListFields.scores),
            nms.get_field(fields.BoxListFields.classes),
            nms.get_field(coarse_boxes_key)])

      self.assertAllClose(nms_corners_output, exp_nms_corners)
      self.assertAllClose(nms_scores_output, exp_nms_scores)
      self.assertAllClose(nms_classes_output, exp_nms_classes)
      self.assertAllEqual(nms_coarse_corners, exp_nms_coarse_corners)
Beispiel #24
0
	                      box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND()
	  3. _remove_invalid_anchors_and_predictions
	    I: pruned_anchors_boxlist, keep_indices = box_list_ops.prune_outside_window()
	    II: _batch_gather_kept_indices(box_encodings)
	        _batch_gather_kept_indices(objectness_predictions_with_background)
	        'Extremely hard to get through!!!!!!'

	b: Classification (second stage)
	  1. _predict_second_stage
	  	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)