|
| 1 | +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +r"""Tests for detection_inference.py.""" |
| 16 | + |
| 17 | +import os |
| 18 | +import StringIO |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +from PIL import Image |
| 22 | +import tensorflow as tf |
| 23 | + |
| 24 | +from object_detection.core import standard_fields |
| 25 | +from object_detection.inference import detection_inference |
| 26 | +from object_detection.utils import dataset_util |
| 27 | + |
| 28 | + |
| 29 | +def get_mock_tfrecord_path(): |
| 30 | + return os.path.join(tf.test.get_temp_dir(), 'mock.tfrec') |
| 31 | + |
| 32 | + |
| 33 | +def create_mock_tfrecord(): |
| 34 | + pil_image = Image.fromarray(np.array([[[123, 0, 0]]], dtype=np.uint8), 'RGB') |
| 35 | + image_output_stream = StringIO.StringIO() |
| 36 | + pil_image.save(image_output_stream, format='png') |
| 37 | + encoded_image = image_output_stream.getvalue() |
| 38 | + |
| 39 | + feature_map = { |
| 40 | + 'test_field': |
| 41 | + dataset_util.float_list_feature([1, 2, 3, 4]), |
| 42 | + standard_fields.TfExampleFields.image_encoded: |
| 43 | + dataset_util.bytes_feature(encoded_image), |
| 44 | + } |
| 45 | + |
| 46 | + tf_example = tf.train.Example(features=tf.train.Features(feature=feature_map)) |
| 47 | + with tf.python_io.TFRecordWriter(get_mock_tfrecord_path()) as writer: |
| 48 | + writer.write(tf_example.SerializeToString()) |
| 49 | + |
| 50 | + |
| 51 | +def get_mock_graph_path(): |
| 52 | + return os.path.join(tf.test.get_temp_dir(), 'mock_graph.pb') |
| 53 | + |
| 54 | + |
| 55 | +def create_mock_graph(): |
| 56 | + g = tf.Graph() |
| 57 | + with g.as_default(): |
| 58 | + in_image_tensor = tf.placeholder( |
| 59 | + tf.uint8, shape=[1, None, None, 3], name='image_tensor') |
| 60 | + tf.constant([2.0], name='num_detections') |
| 61 | + tf.constant( |
| 62 | + [[[0, 0.8, 0.7, 1], [0.1, 0.2, 0.8, 0.9], [0.2, 0.3, 0.4, 0.5]]], |
| 63 | + name='detection_boxes') |
| 64 | + tf.constant([[0.1, 0.2, 0.3]], name='detection_scores') |
| 65 | + tf.identity( |
| 66 | + tf.constant([[1.0, 2.0, 3.0]]) * |
| 67 | + tf.reduce_sum(tf.cast(in_image_tensor, dtype=tf.float32)), |
| 68 | + name='detection_classes') |
| 69 | + graph_def = g.as_graph_def() |
| 70 | + |
| 71 | + with tf.gfile.Open(get_mock_graph_path(), 'w') as fl: |
| 72 | + fl.write(graph_def.SerializeToString()) |
| 73 | + |
| 74 | + |
| 75 | +class InferDetectionsTests(tf.test.TestCase): |
| 76 | + |
| 77 | + def test_simple(self): |
| 78 | + create_mock_graph() |
| 79 | + create_mock_tfrecord() |
| 80 | + |
| 81 | + serialized_example_tensor, image_tensor = detection_inference.build_input( |
| 82 | + [get_mock_tfrecord_path()]) |
| 83 | + self.assertAllEqual(image_tensor.get_shape().as_list(), [1, None, None, 3]) |
| 84 | + |
| 85 | + (detected_boxes_tensor, detected_scores_tensor, |
| 86 | + detected_labels_tensor) = detection_inference.build_inference_graph( |
| 87 | + image_tensor, get_mock_graph_path()) |
| 88 | + |
| 89 | + with self.test_session(use_gpu=False) as sess: |
| 90 | + sess.run(tf.global_variables_initializer()) |
| 91 | + sess.run(tf.local_variables_initializer()) |
| 92 | + tf.train.start_queue_runners() |
| 93 | + |
| 94 | + tf_example = detection_inference.infer_detections_and_add_to_example( |
| 95 | + serialized_example_tensor, detected_boxes_tensor, |
| 96 | + detected_scores_tensor, detected_labels_tensor, False) |
| 97 | + |
| 98 | + self.assertProtoEquals(r""" |
| 99 | + features { |
| 100 | + feature { |
| 101 | + key: "image/detection/bbox/ymin" |
| 102 | + value { float_list { value: [0.0, 0.1] } } } |
| 103 | + feature { |
| 104 | + key: "image/detection/bbox/xmin" |
| 105 | + value { float_list { value: [0.8, 0.2] } } } |
| 106 | + feature { |
| 107 | + key: "image/detection/bbox/ymax" |
| 108 | + value { float_list { value: [0.7, 0.8] } } } |
| 109 | + feature { |
| 110 | + key: "image/detection/bbox/xmax" |
| 111 | + value { float_list { value: [1.0, 0.9] } } } |
| 112 | + feature { |
| 113 | + key: "image/detection/label" |
| 114 | + value { int64_list { value: [123, 246] } } } |
| 115 | + feature { |
| 116 | + key: "image/detection/score" |
| 117 | + value { float_list { value: [0.1, 0.2] } } } |
| 118 | + feature { |
| 119 | + key: "image/encoded" |
| 120 | + value { bytes_list { value: |
| 121 | + "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\001\000\000" |
| 122 | + "\000\001\010\002\000\000\000\220wS\336\000\000\000\022IDATx" |
| 123 | + "\234b\250f`\000\000\000\000\377\377\003\000\001u\000|gO\242" |
| 124 | + "\213\000\000\000\000IEND\256B`\202" } } } |
| 125 | + feature { |
| 126 | + key: "test_field" |
| 127 | + value { float_list { value: [1.0, 2.0, 3.0, 4.0] } } } } |
| 128 | + """, tf_example) |
| 129 | + |
| 130 | + def test_discard_image(self): |
| 131 | + create_mock_graph() |
| 132 | + create_mock_tfrecord() |
| 133 | + |
| 134 | + serialized_example_tensor, image_tensor = detection_inference.build_input( |
| 135 | + [get_mock_tfrecord_path()]) |
| 136 | + (detected_boxes_tensor, detected_scores_tensor, |
| 137 | + detected_labels_tensor) = detection_inference.build_inference_graph( |
| 138 | + image_tensor, get_mock_graph_path()) |
| 139 | + |
| 140 | + with self.test_session(use_gpu=False) as sess: |
| 141 | + sess.run(tf.global_variables_initializer()) |
| 142 | + sess.run(tf.local_variables_initializer()) |
| 143 | + tf.train.start_queue_runners() |
| 144 | + |
| 145 | + tf_example = detection_inference.infer_detections_and_add_to_example( |
| 146 | + serialized_example_tensor, detected_boxes_tensor, |
| 147 | + detected_scores_tensor, detected_labels_tensor, True) |
| 148 | + |
| 149 | + self.assertProtoEquals(r""" |
| 150 | + features { |
| 151 | + feature { |
| 152 | + key: "image/detection/bbox/ymin" |
| 153 | + value { float_list { value: [0.0, 0.1] } } } |
| 154 | + feature { |
| 155 | + key: "image/detection/bbox/xmin" |
| 156 | + value { float_list { value: [0.8, 0.2] } } } |
| 157 | + feature { |
| 158 | + key: "image/detection/bbox/ymax" |
| 159 | + value { float_list { value: [0.7, 0.8] } } } |
| 160 | + feature { |
| 161 | + key: "image/detection/bbox/xmax" |
| 162 | + value { float_list { value: [1.0, 0.9] } } } |
| 163 | + feature { |
| 164 | + key: "image/detection/label" |
| 165 | + value { int64_list { value: [123, 246] } } } |
| 166 | + feature { |
| 167 | + key: "image/detection/score" |
| 168 | + value { float_list { value: [0.1, 0.2] } } } |
| 169 | + feature { |
| 170 | + key: "test_field" |
| 171 | + value { float_list { value: [1.0, 2.0, 3.0, 4.0] } } } } |
| 172 | + """, tf_example) |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == '__main__': |
| 176 | + tf.test.main() |
0 commit comments