class_agnostic_nms_test.py 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for google3.third_party.tensorflow_models.object_detection.core.class_agnostic_nms."""
P
pkulzc 已提交
16
from absl.testing import parameterized
17 18 19 20 21 22
import tensorflow as tf
from object_detection.core import post_processing
from object_detection.core import standard_fields as fields
from object_detection.utils import test_case


P
pkulzc 已提交
23 24
class ClassAgnosticNonMaxSuppressionTest(test_case.TestCase,
                                         parameterized.TestCase):
25 26

  def test_class_agnostic_nms_select_with_shared_boxes(self):
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    def graph_fn():
      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]])
      score_thresh = 0.1
      iou_thresh = .5
      max_classes_per_detection = 1
      max_output_size = 4
      nms, _ = post_processing.class_agnostic_non_max_suppression(
          boxes, scores, score_thresh, iou_thresh, max_classes_per_detection,
          max_output_size)
      return (nms.get(), nms.get_field(fields.BoxListFields.scores),
              nms.get_field(fields.BoxListFields.classes))
43 44 45 46 47 48

    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]

49 50 51 52 53
    (nms_corners_output, nms_scores_output,
     nms_classes_output) = self.execute_cpu(graph_fn, [])
    self.assertAllClose(nms_corners_output, exp_nms_corners)
    self.assertAllClose(nms_scores_output, exp_nms_scores)
    self.assertAllClose(nms_classes_output, exp_nms_classes)
54

P
pkulzc 已提交
55

56
  def test_class_agnostic_nms_select_with_per_class_boxes(self):
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    def graph_fn():
      boxes = tf.constant(
          [[[4, 5, 9, 10], [0, 0, 1, 1]],
           [[0, 0.1, 1, 1.1], [4, 5, 9, 10]],
           [[0, -0.1, 1, 0.9], [4, 5, 9, 10]],
           [[0, 10, 1, 11], [4, 5, 9, 10]],
           [[0, 10.1, 1, 11.1], [4, 5, 9, 10]],
           [[0, 100, 1, 101], [4, 5, 9, 10]],
           [[4, 5, 9, 10], [0, 1000, 1, 1002]],
           [[4, 5, 9, 10], [0, 1000, 1, 1002.1]]], tf.float32)
      scores = tf.constant([[.01, 0.9],
                            [.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_classes_per_detection = 1
      max_output_size = 4
      nms, _ = post_processing.class_agnostic_non_max_suppression(
          boxes, scores, score_thresh, iou_thresh, max_classes_per_detection,
          max_output_size)
      return (nms.get(), nms.get_field(fields.BoxListFields.scores),
              nms.get_field(fields.BoxListFields.classes))
    (nms_corners_output, nms_scores_output,
     nms_classes_output) = self.execute_cpu(graph_fn, [])
86 87 88 89 90 91
    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, 1, 1, 0]
92 93 94
    self.assertAllClose(nms_corners_output, exp_nms_corners)
    self.assertAllClose(nms_scores_output, exp_nms_scores)
    self.assertAllClose(nms_classes_output, exp_nms_classes)
95

P
pkulzc 已提交
96 97 98 99 100 101 102 103
  # Two cases will be tested here: using / not using static shapes.
  # Named the two test cases for easier control during testing, with a flag of
  # '--test_filter=ClassAgnosticNonMaxSuppressionTest.test_batch_classagnostic_nms_with_batch_size_1'
  # or
  # '--test_filter=ClassAgnosticNonMaxSuppressionTest.test_batch_classagnostic_nms_with_batch_size_1_use_static_shapes'.
  @parameterized.named_parameters(('', False), ('_use_static_shapes', True))
  def test_batch_classagnostic_nms_with_batch_size_1(self,
                                                     use_static_shapes=False):
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    def graph_fn():
      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]]])
      score_thresh = 0.1
      iou_thresh = .5
      max_output_size = 4
      max_classes_per_detection = 1
      use_class_agnostic_nms = True
      (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks,
       nmsed_additional_fields,
       num_detections) = post_processing.batch_multiclass_non_max_suppression(
           boxes,
           scores,
           score_thresh,
           iou_thresh,
           max_size_per_class=max_output_size,
           max_total_size=max_output_size,
           use_class_agnostic_nms=use_class_agnostic_nms,
           use_static_shapes=use_static_shapes,
           max_classes_per_detection=max_classes_per_detection)
      self.assertIsNone(nmsed_masks)
      self.assertIsNone(nmsed_additional_fields)
      return (nmsed_boxes, nmsed_scores, nmsed_classes, num_detections)
131 132 133 134
    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]]
135 136 137 138 139 140
    (nmsed_boxes, nmsed_scores, nmsed_classes,
     num_detections) = self.execute_cpu(graph_fn, [])
    self.assertAllClose(nmsed_boxes, exp_nms_corners)
    self.assertAllClose(nmsed_scores, exp_nms_scores)
    self.assertAllClose(nmsed_classes, exp_nms_classes)
    self.assertEqual(num_detections, [4])
141 142 143 144


if __name__ == '__main__':
  tf.test.main()