예제 #1
0
    def test_first_rows_close_as_set(self):
        a = [1, 2, 3, 0, 0]
        b = [3, 2, 1, 0, 0]
        k = 3
        self.assertTrue(test_utils.first_rows_close_as_set(a, b, k))

        a = [[1, 2], [1, 4], [0, 0]]
        b = [[1, 4 + 1e-9], [1, 2], [0, 0]]
        k = 2
        self.assertTrue(test_utils.first_rows_close_as_set(a, b, k))

        a = [[1, 2], [1, 4], [0, 0]]
        b = [[1, 4 + 1e-9], [2, 2], [0, 0]]
        k = 2
        self.assertFalse(test_utils.first_rows_close_as_set(a, b, k))
예제 #2
0
  def test_first_rows_close_as_set(self):
    a = [1, 2, 3, 0, 0]
    b = [3, 2, 1, 0, 0]
    k = 3
    self.assertTrue(test_utils.first_rows_close_as_set(a, b, k))

    a = [[1, 2], [1, 4], [0, 0]]
    b = [[1, 4 + 1e-9], [1, 2], [0, 0]]
    k = 2
    self.assertTrue(test_utils.first_rows_close_as_set(a, b, k))

    a = [[1, 2], [1, 4], [0, 0]]
    b = [[1, 4 + 1e-9], [2, 2], [0, 0]]
    k = 2
    self.assertFalse(test_utils.first_rows_close_as_set(a, b, k))
예제 #3
0
  def test_postprocess_results_are_correct_static(self, use_keras):
    with tf.Graph().as_default():
      _, _, _, _ = self._create_model(use_keras=use_keras)
    def graph_fn(input_image):
      model, _, _, _ = self._create_model(use_static_shapes=True,
                                          nms_max_size_per_class=4)
      preprocessed_inputs, true_image_shapes = model.preprocess(input_image)
      prediction_dict = model.predict(preprocessed_inputs,
                                      true_image_shapes)
      detections = model.postprocess(prediction_dict, true_image_shapes)
      return (detections['detection_boxes'], detections['detection_scores'],
              detections['detection_classes'], detections['num_detections'],
              detections['detection_multiclass_scores'])

    batch_size = 2
    image_size = 2
    channels = 3
    input_image = np.random.rand(batch_size, image_size, image_size,
                                 channels).astype(np.float32)
    expected_boxes = [
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0]
        ],  # padding
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0]
        ]
    ]  # padding
    expected_scores = [[0, 0, 0, 0], [0, 0, 0, 0]]
    expected_multiclass_scores = [[[0, 0], [0, 0], [0, 0], [0, 0]],
                                  [[0, 0], [0, 0], [0, 0], [0, 0]]]
    expected_classes = [[0, 0, 0, 0], [0, 0, 0, 0]]
    expected_num_detections = np.array([3, 3])

    (detection_boxes, detection_scores, detection_classes,
     num_detections, detection_multiclass_scores) = self.execute(graph_fn,
                                                                 [input_image])
    for image_idx in range(batch_size):
      self.assertTrue(test_utils.first_rows_close_as_set(
          detection_boxes[image_idx][
              0:expected_num_detections[image_idx]].tolist(),
          expected_boxes[image_idx][0:expected_num_detections[image_idx]]))
      self.assertAllClose(
          detection_scores[image_idx][0:expected_num_detections[image_idx]],
          expected_scores[image_idx][0:expected_num_detections[image_idx]])
      self.assertAllClose(
          detection_multiclass_scores[image_idx]
          [0:expected_num_detections[image_idx]],
          expected_multiclass_scores[image_idx]
          [0:expected_num_detections[image_idx]])
      self.assertAllClose(
          detection_classes[image_idx][0:expected_num_detections[image_idx]],
          expected_classes[image_idx][0:expected_num_detections[image_idx]])
    self.assertAllClose(num_detections,
                        expected_num_detections)
예제 #4
0
  def test_postprocess_results_are_correct(self, use_keras):
    batch_size = 2
    image_size = 2
    input_shapes = [(batch_size, image_size, image_size, 3),
                    (None, image_size, image_size, 3),
                    (batch_size, None, None, 3),
                    (None, None, None, 3)]

    expected_boxes = [
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0],  # pruned prediction
            [0, 0, 0, 0]
        ],  # padding
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0],  # pruned prediction
            [0, 0, 0, 0]
        ]
    ]  # padding
    expected_scores = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
    expected_classes = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
    expected_num_detections = np.array([3, 3])

    for input_shape in input_shapes:
      tf_graph = tf.Graph()
      with tf_graph.as_default():
        model, _, _, _ = self._create_model(use_keras=use_keras)
        input_placeholder = tf.placeholder(tf.float32, shape=input_shape)
        preprocessed_inputs, true_image_shapes = model.preprocess(
            input_placeholder)
        prediction_dict = model.predict(preprocessed_inputs,
                                        true_image_shapes)
        detections = model.postprocess(prediction_dict, true_image_shapes)
        self.assertIn('detection_boxes', detections)
        self.assertIn('detection_scores', detections)
        self.assertIn('detection_classes', detections)
        self.assertIn('num_detections', detections)
        init_op = tf.global_variables_initializer()
      with self.test_session(graph=tf_graph) as sess:
        sess.run(init_op)
        detections_out = sess.run(detections,
                                  feed_dict={
                                      input_placeholder:
                                      np.random.uniform(
                                          size=(batch_size, 2, 2, 3))})
      for image_idx in range(batch_size):
        self.assertTrue(
            test_utils.first_rows_close_as_set(
                detections_out['detection_boxes'][image_idx].tolist(),
                expected_boxes[image_idx]))
      self.assertAllClose(detections_out['detection_scores'], expected_scores)
      self.assertAllClose(detections_out['detection_classes'], expected_classes)
      self.assertAllClose(detections_out['num_detections'],
                          expected_num_detections)
예제 #5
0
  def test_postprocess_results_are_correct(self, use_keras):
    batch_size = 2
    image_size = 2
    input_shapes = [(batch_size, image_size, image_size, 3),
                    (None, image_size, image_size, 3),
                    (batch_size, None, None, 3),
                    (None, None, None, 3)]

    expected_boxes = [
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0],  # pruned prediction
            [0, 0, 0, 0]
        ],  # padding
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0],  # pruned prediction
            [0, 0, 0, 0]
        ]
    ]  # padding
    expected_scores = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
    expected_classes = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
    expected_num_detections = np.array([3, 3])

    for input_shape in input_shapes:
      tf_graph = tf.Graph()
      with tf_graph.as_default():
        model, _, _, _ = self._create_model(use_keras=use_keras)
        input_placeholder = tf.placeholder(tf.float32, shape=input_shape)
        preprocessed_inputs, true_image_shapes = model.preprocess(
            input_placeholder)
        prediction_dict = model.predict(preprocessed_inputs,
                                        true_image_shapes)
        detections = model.postprocess(prediction_dict, true_image_shapes)
        self.assertIn('detection_boxes', detections)
        self.assertIn('detection_scores', detections)
        self.assertIn('detection_classes', detections)
        self.assertIn('num_detections', detections)
        init_op = tf.global_variables_initializer()
      with self.test_session(graph=tf_graph) as sess:
        sess.run(init_op)
        detections_out = sess.run(detections,
                                  feed_dict={
                                      input_placeholder:
                                      np.random.uniform(
                                          size=(batch_size, 2, 2, 3))})
      for image_idx in range(batch_size):
        self.assertTrue(
            test_utils.first_rows_close_as_set(
                detections_out['detection_boxes'][image_idx].tolist(),
                expected_boxes[image_idx]))
      self.assertAllClose(detections_out['detection_scores'], expected_scores)
      self.assertAllClose(detections_out['detection_classes'], expected_classes)
      self.assertAllClose(detections_out['num_detections'],
                          expected_num_detections)
예제 #6
0
  def test_postprocess_results_are_correct_static(self, use_keras):
    with tf.Graph().as_default():
      _, _, _, _ = self._create_model(use_keras=use_keras)
    def graph_fn(input_image):
      model, _, _, _ = self._create_model(use_static_shapes=True,
                                          nms_max_size_per_class=4)
      preprocessed_inputs, true_image_shapes = model.preprocess(input_image)
      prediction_dict = model.predict(preprocessed_inputs,
                                      true_image_shapes)
      detections = model.postprocess(prediction_dict, true_image_shapes)
      return (detections['detection_boxes'], detections['detection_scores'],
              detections['detection_classes'], detections['num_detections'])

    batch_size = 2
    image_size = 2
    channels = 3
    input_image = np.random.rand(batch_size, image_size, image_size,
                                 channels).astype(np.float32)
    expected_boxes = [
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0]
        ],  # padding
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0]
        ]
    ]  # padding
    expected_scores = [[0, 0, 0, 0], [0, 0, 0, 0]]
    expected_classes = [[0, 0, 0, 0], [0, 0, 0, 0]]
    expected_num_detections = np.array([3, 3])

    (detection_boxes, detection_scores, detection_classes,
     num_detections) = self.execute(graph_fn, [input_image])
    for image_idx in range(batch_size):
      self.assertTrue(test_utils.first_rows_close_as_set(
          detection_boxes[image_idx][
              0:expected_num_detections[image_idx]].tolist(),
          expected_boxes[image_idx][0:expected_num_detections[image_idx]]))
      self.assertAllClose(
          detection_scores[image_idx][0:expected_num_detections[image_idx]],
          expected_scores[image_idx][0:expected_num_detections[image_idx]])
      self.assertAllClose(
          detection_classes[image_idx][0:expected_num_detections[image_idx]],
          expected_classes[image_idx][0:expected_num_detections[image_idx]])
    self.assertAllClose(num_detections,
                        expected_num_detections)
예제 #7
0
  def test_postprocess_results_are_correct_with_calibration(self, use_keras):
    batch_size = 2
    image_size = 2
    input_shapes = [(batch_size, image_size, image_size, 3),
                    (None, image_size, image_size, 3),
                    (batch_size, None, None, 3),
                    (None, None, None, 3)]

    expected_boxes = [
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0],  # pruned prediction
            [0, 0, 0, 0]
        ],  # padding
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0],  # pruned prediction
            [0, 0, 0, 0]
        ]
    ]  # padding
    # Calibration mapping value below is set to map all scores to 0.5, except
    # for the last two detections in each batch (see expected number of
    # detections below.
    expected_scores = [[0.5, 0.5, 0.5, 0., 0.], [0.5, 0.5, 0.5, 0., 0.]]
    expected_classes = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
    expected_num_detections = np.array([3, 3])

    raw_detection_boxes = [[[0., 0., 0.5, 0.5], [0., 0.5, 0.5, 1.],
                            [0.5, 0., 1., 0.5], [1., 1., 1.5, 1.5]],
                           [[0., 0., 0.5, 0.5], [0., 0.5, 0.5, 1.],
                            [0.5, 0., 1., 0.5], [1., 1., 1.5, 1.5]]]
    raw_detection_scores = [[[0.5, 0.5], [0.5, 0.5], [0.5, 0.5], [0.5, 0.5]],
                            [[0.5, 0.5], [0.5, 0.5], [0.5, 0.5], [0.5, 0.5]]]

    for input_shape in input_shapes:
      tf_graph = tf.Graph()
      with tf_graph.as_default():
        model, _, _, _ = self._create_model(use_keras=use_keras,
                                            calibration_mapping_value=0.5)
        input_placeholder = tf.placeholder(tf.float32, shape=input_shape)
        preprocessed_inputs, true_image_shapes = model.preprocess(
            input_placeholder)
        prediction_dict = model.predict(preprocessed_inputs,
                                        true_image_shapes)
        detections = model.postprocess(prediction_dict, true_image_shapes)
        self.assertIn('detection_boxes', detections)
        self.assertIn('detection_scores', detections)
        self.assertIn('detection_classes', detections)
        self.assertIn('num_detections', detections)
        self.assertIn('raw_detection_boxes', detections)
        self.assertIn('raw_detection_scores', detections)
        init_op = tf.global_variables_initializer()
      with self.test_session(graph=tf_graph) as sess:
        sess.run(init_op)
        detections_out = sess.run(detections,
                                  feed_dict={
                                      input_placeholder:
                                      np.random.uniform(
                                          size=(batch_size, 2, 2, 3))})
      for image_idx in range(batch_size):
        self.assertTrue(
            test_utils.first_rows_close_as_set(
                detections_out['detection_boxes'][image_idx].tolist(),
                expected_boxes[image_idx]))
      self.assertAllClose(detections_out['detection_scores'], expected_scores)
      self.assertAllClose(detections_out['detection_classes'], expected_classes)
      self.assertAllClose(detections_out['num_detections'],
                          expected_num_detections)
      self.assertAllEqual(detections_out['raw_detection_boxes'],
                          raw_detection_boxes)
      self.assertAllEqual(detections_out['raw_detection_scores'],
                          raw_detection_scores)
예제 #8
0
    def test_postprocess_results_are_correct(self):

        with test_utils.GraphContextOrNone() as g:
            model, _, _, _ = self._create_model()

        def graph_fn():
            size = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32)
            batch = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32)
            shape = tf.stack([batch, size, size, 3])
            image = tf.random.uniform(shape)
            preprocessed_inputs, true_image_shapes = model.preprocess(image)
            prediction_dict = model.predict(preprocessed_inputs,
                                            true_image_shapes)
            detections = model.postprocess(prediction_dict, true_image_shapes)
            return [
                batch, detections['detection_boxes'],
                detections['detection_scores'],
                detections['detection_classes'],
                detections['detection_multiclass_scores'],
                detections['num_detections'],
                detections['raw_detection_boxes'],
                detections['raw_detection_scores'],
                detections['detection_anchor_indices']
            ]

        expected_boxes = [
            [
                [0, 0, .5, .5],
                [0, .5, .5, 1],
                [.5, 0, 1, .5],
                [0, 0, 0, 0],  # pruned prediction
                [0, 0, 0, 0]
            ],  # padding
            [
                [0, 0, .5, .5],
                [0, .5, .5, 1],
                [.5, 0, 1, .5],
                [0, 0, 0, 0],  # pruned prediction
                [0, 0, 0, 0]
            ]
        ]  # padding
        expected_scores = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
        expected_multiclass_scores = [[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]],
                                      [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]]

        expected_classes = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
        expected_num_detections = np.array([3, 3])

        expected_raw_detection_boxes = [[[0., 0., 0.5,
                                          0.5], [0., 0.5, 0.5, 1.],
                                         [0.5, 0., 1., 0.5],
                                         [1., 1., 1.5, 1.5]],
                                        [[0., 0., 0.5,
                                          0.5], [0., 0.5, 0.5, 1.],
                                         [0.5, 0., 1., 0.5],
                                         [1., 1., 1.5, 1.5]]]
        expected_raw_detection_scores = [[[0, 0], [0, 0], [0, 0], [0, 0]],
                                         [[0, 0], [0, 0], [0, 0], [0, 0]]]
        expected_detection_anchor_indices = [[0, 1, 2], [0, 1, 2]]
        (batch, detection_boxes, detection_scores, detection_classes,
         detection_multiclass_scores, num_detections, raw_detection_boxes,
         raw_detection_scores,
         detection_anchor_indices) = self.execute_cpu(graph_fn, [], graph=g)
        for image_idx in range(batch):
            self.assertTrue(
                test_utils.first_rows_close_as_set(
                    detection_boxes[image_idx].tolist(),
                    expected_boxes[image_idx]))
            self.assertSameElements(
                detection_anchor_indices[image_idx],
                expected_detection_anchor_indices[image_idx])
        self.assertAllClose(detection_scores, expected_scores)
        self.assertAllClose(detection_classes, expected_classes)
        self.assertAllClose(detection_multiclass_scores,
                            expected_multiclass_scores)
        self.assertAllClose(num_detections, expected_num_detections)
        self.assertAllEqual(raw_detection_boxes, expected_raw_detection_boxes)
        self.assertAllEqual(raw_detection_scores,
                            expected_raw_detection_scores)
예제 #9
0
  def test_postprocess_results_are_correct_with_calibration(self, use_keras):
    batch_size = 2
    image_size = 2
    input_shapes = [(batch_size, image_size, image_size, 3),
                    (None, image_size, image_size, 3),
                    (batch_size, None, None, 3),
                    (None, None, None, 3)]

    expected_boxes = [
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0],  # pruned prediction
            [0, 0, 0, 0]
        ],  # padding
        [
            [0, 0, .5, .5],
            [0, .5, .5, 1],
            [.5, 0, 1, .5],
            [0, 0, 0, 0],  # pruned prediction
            [0, 0, 0, 0]
        ]
    ]  # padding
    # Calibration mapping value below is set to map all scores to 0.5, except
    # for the last two detections in each batch (see expected number of
    # detections below.
    expected_scores = [[0.5, 0.5, 0.5, 0., 0.], [0.5, 0.5, 0.5, 0., 0.]]
    expected_classes = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
    expected_num_detections = np.array([3, 3])

    raw_detection_boxes = [[[0., 0., 0.5, 0.5], [0., 0.5, 0.5, 1.],
                            [0.5, 0., 1., 0.5], [1., 1., 1.5, 1.5]],
                           [[0., 0., 0.5, 0.5], [0., 0.5, 0.5, 1.],
                            [0.5, 0., 1., 0.5], [1., 1., 1.5, 1.5]]]
    raw_detection_scores = [[[0, 0], [0, 0], [0, 0], [0, 0]],
                            [[0, 0], [0, 0], [0, 0], [0, 0]]]

    for input_shape in input_shapes:
      tf_graph = tf.Graph()
      with tf_graph.as_default():
        model, _, _, _ = self._create_model(use_keras=use_keras,
                                            calibration_mapping_value=0.5)
        input_placeholder = tf.placeholder(tf.float32, shape=input_shape)
        preprocessed_inputs, true_image_shapes = model.preprocess(
            input_placeholder)
        prediction_dict = model.predict(preprocessed_inputs,
                                        true_image_shapes)
        detections = model.postprocess(prediction_dict, true_image_shapes)
        self.assertIn('detection_boxes', detections)
        self.assertIn('detection_scores', detections)
        self.assertIn('detection_classes', detections)
        self.assertIn('num_detections', detections)
        self.assertIn('raw_detection_boxes', detections)
        self.assertIn('raw_detection_scores', detections)
        init_op = tf.global_variables_initializer()
      with self.test_session(graph=tf_graph) as sess:
        sess.run(init_op)
        detections_out = sess.run(detections,
                                  feed_dict={
                                      input_placeholder:
                                      np.random.uniform(
                                          size=(batch_size, 2, 2, 3))})
      for image_idx in range(batch_size):
        self.assertTrue(
            test_utils.first_rows_close_as_set(
                detections_out['detection_boxes'][image_idx].tolist(),
                expected_boxes[image_idx]))
      self.assertAllClose(detections_out['detection_scores'], expected_scores)
      self.assertAllClose(detections_out['detection_classes'], expected_classes)
      self.assertAllClose(detections_out['num_detections'],
                          expected_num_detections)
      self.assertAllEqual(detections_out['raw_detection_boxes'],
                          raw_detection_boxes)
      self.assertAllEqual(detections_out['raw_detection_scores'],
                          raw_detection_scores)