File size: 7,791 Bytes
0b8359d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Copyright 2017 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 post_processing_builder."""

import tensorflow.compat.v1 as tf
from google.protobuf import text_format
from object_detection.builders import post_processing_builder
from object_detection.protos import post_processing_pb2
from object_detection.utils import test_case


class PostProcessingBuilderTest(test_case.TestCase):

  def test_build_non_max_suppressor_with_correct_parameters(self):
    post_processing_text_proto = """
      batch_non_max_suppression {
        score_threshold: 0.7
        iou_threshold: 0.6
        max_detections_per_class: 100
        max_total_detections: 300
        soft_nms_sigma: 0.4
      }
    """
    post_processing_config = post_processing_pb2.PostProcessing()
    text_format.Merge(post_processing_text_proto, post_processing_config)
    non_max_suppressor, _ = post_processing_builder.build(
        post_processing_config)
    self.assertEqual(non_max_suppressor.keywords['max_size_per_class'], 100)
    self.assertEqual(non_max_suppressor.keywords['max_total_size'], 300)
    self.assertAlmostEqual(non_max_suppressor.keywords['score_thresh'], 0.7)
    self.assertAlmostEqual(non_max_suppressor.keywords['iou_thresh'], 0.6)
    self.assertAlmostEqual(non_max_suppressor.keywords['soft_nms_sigma'], 0.4)

  def test_build_non_max_suppressor_with_correct_parameters_classagnostic_nms(
      self):
    post_processing_text_proto = """
      batch_non_max_suppression {
        score_threshold: 0.7
        iou_threshold: 0.6
        max_detections_per_class: 10
        max_total_detections: 300
        use_class_agnostic_nms: True
        max_classes_per_detection: 1
      }
    """
    post_processing_config = post_processing_pb2.PostProcessing()
    text_format.Merge(post_processing_text_proto, post_processing_config)
    non_max_suppressor, _ = post_processing_builder.build(
        post_processing_config)
    self.assertEqual(non_max_suppressor.keywords['max_size_per_class'], 10)
    self.assertEqual(non_max_suppressor.keywords['max_total_size'], 300)
    self.assertEqual(non_max_suppressor.keywords['max_classes_per_detection'],
                     1)
    self.assertEqual(non_max_suppressor.keywords['use_class_agnostic_nms'],
                     True)
    self.assertAlmostEqual(non_max_suppressor.keywords['score_thresh'], 0.7)
    self.assertAlmostEqual(non_max_suppressor.keywords['iou_thresh'], 0.6)

  def test_build_identity_score_converter(self):
    post_processing_text_proto = """
      score_converter: IDENTITY
    """
    post_processing_config = post_processing_pb2.PostProcessing()
    text_format.Merge(post_processing_text_proto, post_processing_config)
    _, score_converter = post_processing_builder.build(
        post_processing_config)
    self.assertEqual(score_converter.__name__, 'identity_with_logit_scale')
    def graph_fn():
      inputs = tf.constant([1, 1], tf.float32)
      outputs = score_converter(inputs)
      return outputs
    converted_scores = self.execute_cpu(graph_fn, [])
    self.assertAllClose(converted_scores, [1, 1])

  def test_build_identity_score_converter_with_logit_scale(self):
    post_processing_text_proto = """
      score_converter: IDENTITY
      logit_scale: 2.0
    """
    post_processing_config = post_processing_pb2.PostProcessing()
    text_format.Merge(post_processing_text_proto, post_processing_config)
    _, score_converter = post_processing_builder.build(post_processing_config)
    self.assertEqual(score_converter.__name__, 'identity_with_logit_scale')

    def graph_fn():
      inputs = tf.constant([1, 1], tf.float32)
      outputs = score_converter(inputs)
      return outputs
    converted_scores = self.execute_cpu(graph_fn, [])
    self.assertAllClose(converted_scores, [.5, .5])

  def test_build_sigmoid_score_converter(self):
    post_processing_text_proto = """
      score_converter: SIGMOID
    """
    post_processing_config = post_processing_pb2.PostProcessing()
    text_format.Merge(post_processing_text_proto, post_processing_config)
    _, score_converter = post_processing_builder.build(post_processing_config)
    self.assertEqual(score_converter.__name__, 'sigmoid_with_logit_scale')

  def test_build_softmax_score_converter(self):
    post_processing_text_proto = """
      score_converter: SOFTMAX
    """
    post_processing_config = post_processing_pb2.PostProcessing()
    text_format.Merge(post_processing_text_proto, post_processing_config)
    _, score_converter = post_processing_builder.build(post_processing_config)
    self.assertEqual(score_converter.__name__, 'softmax_with_logit_scale')

  def test_build_softmax_score_converter_with_temperature(self):
    post_processing_text_proto = """
      score_converter: SOFTMAX
      logit_scale: 2.0
    """
    post_processing_config = post_processing_pb2.PostProcessing()
    text_format.Merge(post_processing_text_proto, post_processing_config)
    _, score_converter = post_processing_builder.build(post_processing_config)
    self.assertEqual(score_converter.__name__, 'softmax_with_logit_scale')

  def test_build_calibrator_with_nonempty_config(self):
    """Test that identity function used when no calibration_config specified."""
    # Calibration config maps all scores to 0.5.
    post_processing_text_proto = """
      score_converter: SOFTMAX
      calibration_config {
        function_approximation {
          x_y_pairs {
              x_y_pair {
                x: 0.0
                y: 0.5
              }
              x_y_pair {
                x: 1.0
                y: 0.5
              }}}}"""
    post_processing_config = post_processing_pb2.PostProcessing()
    text_format.Merge(post_processing_text_proto, post_processing_config)
    _, calibrated_score_conversion_fn = post_processing_builder.build(
        post_processing_config)
    self.assertEqual(calibrated_score_conversion_fn.__name__,
                     'calibrate_with_function_approximation')

    def graph_fn():
      input_scores = tf.constant([1, 1], tf.float32)
      outputs = calibrated_score_conversion_fn(input_scores)
      return outputs
    calibrated_scores = self.execute_cpu(graph_fn, [])
    self.assertAllClose(calibrated_scores, [0.5, 0.5])

  def test_build_temperature_scaling_calibrator(self):
    post_processing_text_proto = """
      score_converter: SOFTMAX
      calibration_config {
        temperature_scaling_calibration {
          scaler: 2.0
          }}"""
    post_processing_config = post_processing_pb2.PostProcessing()
    text_format.Merge(post_processing_text_proto, post_processing_config)
    _, calibrated_score_conversion_fn = post_processing_builder.build(
        post_processing_config)
    self.assertEqual(calibrated_score_conversion_fn.__name__,
                     'calibrate_with_temperature_scaling_calibration')

    def graph_fn():
      input_scores = tf.constant([1, 1], tf.float32)
      outputs = calibrated_score_conversion_fn(input_scores)
      return outputs
    calibrated_scores = self.execute_cpu(graph_fn, [])
    self.assertAllClose(calibrated_scores, [0.5, 0.5])

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