1# Copyright 2016 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"""Tests for ExampleParserConfiguration.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21from google.protobuf import text_format 22 23from tensorflow.core.example import example_parser_configuration_pb2 24from tensorflow.python.client import session 25from tensorflow.python.framework import dtypes 26from tensorflow.python.ops import array_ops 27from tensorflow.python.ops import parsing_ops 28from tensorflow.python.platform import test 29from tensorflow.python.util.example_parser_configuration import extract_example_parser_configuration 30 31EXPECTED_CONFIG_V1 = """ 32feature_map { 33 key: "x" 34 value { 35 fixed_len_feature { 36 dtype: DT_FLOAT 37 shape { 38 dim { 39 size: 1 40 } 41 } 42 default_value { 43 dtype: DT_FLOAT 44 tensor_shape { 45 dim { 46 size: 1 47 } 48 } 49 float_val: 33.0 50 } 51 values_output_tensor_name: "ParseExample/ParseExample:3" 52 } 53 } 54} 55feature_map { 56 key: "y" 57 value { 58 var_len_feature { 59 dtype: DT_STRING 60 values_output_tensor_name: "ParseExample/ParseExample:1" 61 indices_output_tensor_name: "ParseExample/ParseExample:0" 62 shapes_output_tensor_name: "ParseExample/ParseExample:2" 63 } 64 } 65} 66""" 67 68 69EXPECTED_CONFIG_V2 = EXPECTED_CONFIG_V1.replace( 70 'ParseExample/ParseExample:', 'ParseExample/ParseExampleV2:') 71 72 73class ExampleParserConfigurationTest(test.TestCase): 74 75 def getExpectedConfig(self, op_type): 76 expected = example_parser_configuration_pb2.ExampleParserConfiguration() 77 if op_type == 'ParseExampleV2': 78 text_format.Parse(EXPECTED_CONFIG_V2, expected) 79 else: 80 text_format.Parse(EXPECTED_CONFIG_V1, expected) 81 return expected 82 83 def testBasic(self): 84 with session.Session() as sess: 85 examples = array_ops.placeholder(dtypes.string, shape=[1]) 86 feature_to_type = { 87 'x': parsing_ops.FixedLenFeature([1], dtypes.float32, 33.0), 88 'y': parsing_ops.VarLenFeature(dtypes.string) 89 } 90 result = parsing_ops.parse_example(examples, feature_to_type) 91 parse_example_op = result['x'].op 92 config = extract_example_parser_configuration(parse_example_op, sess) 93 expected = self.getExpectedConfig(parse_example_op.type) 94 self.assertProtoEquals(expected, config) 95 96 97if __name__ == '__main__': 98 test.main() 99