forked from tensorflow/transform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_proto_coder_test.py
More file actions
408 lines (378 loc) · 16.6 KB
/
example_proto_coder_test.py
File metadata and controls
408 lines (378 loc) · 16.6 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# Copyright 2017 Google Inc. 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.
"""Tensorflow-transform ExampleProtoCoder tests."""
import copy
import os
import pickle
import sys
from absl import flags
# Note that this needs to happen before any non-python imports, so we do it
# pretty early on.
if any(arg == '--proto_implementation_type=python' for arg in sys.argv):
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'
elif any(arg == '--proto_implementation_type=cpp' for arg in sys.argv):
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION'] = '2'
elif any(arg.startswith('--proto_implementation_type') for arg in sys.argv):
raise ValueError('Unexpected value for --proto_implementation_type')
# pylint: disable=g-import-not-at-top
import numpy as np
import tensorflow as tf
from tensorflow_transform.coders import example_proto_coder
from tensorflow_transform import test_case
from tensorflow_transform.tf_metadata import schema_utils
from google.protobuf.internal import api_implementation
from google.protobuf import text_format
# pylint: enable=g-import-not-at-top
flags.DEFINE_string(
'proto_implementation_type', 'cpp',
'The implementation type of python proto to use when exercising this test')
_FEATURE_SPEC = {
'scalar_feature_1':
tf.io.FixedLenFeature([], tf.int64),
'scalar_feature_2':
tf.io.FixedLenFeature([], tf.int64),
'scalar_feature_3':
tf.io.FixedLenFeature([], tf.float32),
'varlen_feature_1':
tf.io.VarLenFeature(tf.float32),
'varlen_feature_2':
tf.io.VarLenFeature(tf.string),
'1d_vector_feature':
tf.io.FixedLenFeature([1], tf.string),
'2d_vector_feature':
tf.io.FixedLenFeature([2, 2], tf.float32),
'sparse_feature':
tf.io.SparseFeature('sparse_idx', 'sparse_val', tf.float32, 10),
'2d_sparse_feature':
tf.io.SparseFeature(['2d_sparse_idx0', '2d_sparse_idx1'],
'2d_sparse_val', tf.float32, [2, 10]),
'ragged_feature':
tf.io.RaggedFeature(
tf.float32,
value_key='ragged_val',
partitions=[tf.io.RaggedFeature.RowLengths('ragged_row_lengths1')]),
'2d_ragged_feature':
tf.io.RaggedFeature(
tf.string,
value_key='2d_ragged_val',
partitions=[
tf.io.RaggedFeature.RowLengths('2d_ragged_row_lengths1'),
tf.io.RaggedFeature.RowLengths('2d_ragged_row_lengths2')
]),
'ragged_uniform_feature':
tf.io.RaggedFeature(
tf.int64,
value_key='ragged_uniform_val',
partitions=[tf.io.RaggedFeature.UniformRowLength(2)]),
'2d_ragged_uniform_feature':
tf.io.RaggedFeature(
tf.int64,
value_key='2d_ragged_uniform_val',
partitions=[
tf.io.RaggedFeature.RowLengths(
'2d_ragged_uniform_row_lengths1'),
tf.io.RaggedFeature.UniformRowLength(2)
]),
}
_ENCODE_CASES = {
'unicode':
dict(
testcase_name='unicode',
feature_spec={
'unicode_feature': tf.io.FixedLenFeature([], tf.string)
},
ascii_proto="""\
features {
feature { key: "unicode_feature"
value { bytes_list { value: [ "Hello κόσμε" ] } } }
}""",
instance={'unicode_feature': u'Hello κόσμε'}),
'scalar_string_to_varlen':
dict(
testcase_name='scalar_string_to_varlen',
feature_spec={'varlen_string': tf.io.VarLenFeature(tf.string)},
ascii_proto="""\
features {
feature { key: "varlen_string" value { bytes_list { value: [ "foo" ] } } }
}""",
instance={'varlen_string': 'foo'}),
'scalar_int_to_varlen':
dict(
testcase_name='scalar_int_to_varlen',
feature_spec={'varlen_int': tf.io.VarLenFeature(tf.int64)},
ascii_proto="""\
features {
feature { key: "varlen_int" value { int64_list { value: [ 123 ] } } }
}""",
instance={'varlen_int': 123}),
'multiple_columns':
dict(
testcase_name='multiple_columns',
feature_spec=_FEATURE_SPEC,
ascii_proto="""\
features {
feature { key: "scalar_feature_1" value { int64_list { value: [ 12 ] } } }
feature { key: "varlen_feature_1"
value { float_list { value: [ 89.0 ] } } }
feature { key: "scalar_feature_2" value { int64_list { value: [ 12 ] } } }
feature { key: "scalar_feature_3"
value { float_list { value: [ 1.0 ] } } }
feature { key: "1d_vector_feature"
value { bytes_list { value: [ 'this is a ,text' ] } } }
feature { key: "2d_vector_feature"
value { float_list { value: [ 1.0, 2.0, 3.0, 4.0 ] } } }
feature { key: "varlen_feature_2"
value { bytes_list { value: [ 'female' ] } } }
feature { key: "sparse_val" value { float_list { value: [ 12.0, 20.0 ] } } }
feature { key: "sparse_idx" value { int64_list { value: [ 1, 4 ] } } }
feature { key: "2d_sparse_idx0" value { int64_list { value: [ 1, 1 ]} } }
feature { key: "2d_sparse_idx1" value { int64_list { value: [ 3, 7 ]} } }
feature { key: "2d_sparse_val"
value { float_list { value: [ 13.0, 23.0 ] } } }
}""",
ragged_ascii_proto="""
feature { key: "ragged_val"
value { float_list { value: [ 7.0, 13.0, 21.0 ] } } }
feature { key: "ragged_row_lengths1"
value { int64_list { value: [ 1, 2 ] } } }
feature { key: "2d_ragged_val"
value { bytes_list { value: [ "aa a", "abc", "hi" ] } } }
feature { key: "2d_ragged_row_lengths1"
value { int64_list { value: [ 0, 3 ] } } }
feature { key: "2d_ragged_row_lengths2"
value { int64_list { value: [ 1, 0, 2 ] } } }
feature { key: "ragged_uniform_val"
value { int64_list { value: [ 1, -1, 2, 1, -1, 2] } } }
feature { key: "2d_ragged_uniform_val"
value { int64_list { value: [ 1, -1, 2, 1, -1, 2] } } }
feature { key: "2d_ragged_uniform_row_lengths1"
value { int64_list { value: [ 1, 0, 2 ] } } }
}
""",
instance={
'scalar_feature_1': 12,
'scalar_feature_2': 12,
'scalar_feature_3': 1.0,
'varlen_feature_1': [89.0],
'1d_vector_feature': [b'this is a ,text'],
'2d_vector_feature': [[1.0, 2.0], [3.0, 4.0]],
'varlen_feature_2': [b'female'],
'sparse_idx': [1, 4],
'sparse_val': [12.0, 20.0],
'2d_sparse_idx0': [1, 1],
'2d_sparse_idx1': [3, 7],
'2d_sparse_val': [13.0, 23.0],
},
ragged_instance={
'ragged_val': [7.0, 13.0, 21.0],
'ragged_row_lengths1': [1, 2],
'2d_ragged_val': [b'aa a', b'abc', b'hi'],
'2d_ragged_row_lengths1': [0, 3],
'2d_ragged_row_lengths2': [1, 0, 2],
'ragged_uniform_val': [1, -1, 2, 1, -1, 2],
'2d_ragged_uniform_val': [1, -1, 2, 1, -1, 2],
'2d_ragged_uniform_row_lengths1': [1, 0, 2],
}),
'multiple_columns_ndarray':
dict(
testcase_name='multiple_columns_ndarray',
feature_spec=_FEATURE_SPEC,
ascii_proto="""\
features {
feature { key: "scalar_feature_1" value { int64_list { value: [ 13 ] } } }
feature { key: "varlen_feature_1" value { float_list { } } }
feature { key: "scalar_feature_2"
value { int64_list { value: [ 214 ] } } }
feature { key: "scalar_feature_3"
value { float_list { value: [ 2.0 ] } } }
feature { key: "1d_vector_feature"
value { bytes_list { value: [ 'this is another ,text' ] } } }
feature { key: "2d_vector_feature"
value { float_list { value: [ 9.0, 8.0, 7.0, 6.0 ] } } }
feature { key: "varlen_feature_2"
value { bytes_list { value: [ 'male' ] } } }
feature { key: "sparse_val" value { float_list { value: [ 13.0, 21.0 ] } } }
feature { key: "sparse_idx" value { int64_list { value: [ 2, 5 ] } } }
feature { key: "2d_sparse_idx0" value { int64_list { value: [ 1, 1 ]} } }
feature { key: "2d_sparse_idx1" value { int64_list { value: [ 3, 7 ]} } }
feature { key: "2d_sparse_val"
value { float_list { value: [ 13.0, 23.0 ] } } }
}""",
ragged_ascii_proto="""
feature { key: "ragged_val"
value { float_list { value: [ 22.0, 22.0, 21.0 ] } } }
feature { key: "ragged_row_lengths1"
value { int64_list { value: [ 0, 2, 1 ] } } }
feature { key: "2d_ragged_val"
value { bytes_list { value: [ "oh", "hello ", "" ] } } }
feature { key: "2d_ragged_row_lengths1"
value { int64_list { value: [ 1, 2 ] } } }
feature { key: "2d_ragged_row_lengths2"
value { int64_list { value: [ 0, 0, 3 ] } } }
feature { key: "ragged_uniform_val"
value { int64_list { value: [ 12, -11, 2, 1, -1, 12] } } }
feature { key: "2d_ragged_uniform_val"
value { int64_list { value: [ 1, -1, 23, 1, -1, 32] } } }
feature { key: "2d_ragged_uniform_row_lengths1"
value { int64_list { value: [ 1, 0, 2 ] } } }
}
""",
instance={
'scalar_feature_1': np.array(13),
'scalar_feature_2': np.int32(214),
'scalar_feature_3': np.array(2.0),
'varlen_feature_1': np.array([]),
'1d_vector_feature': np.array([b'this is another ,text']),
'2d_vector_feature': np.array([[9.0, 8.0], [7.0, 6.0]]),
'varlen_feature_2': np.array([b'male']),
'sparse_idx': np.array([2, 5]),
'sparse_val': np.array([13.0, 21.0]),
'2d_sparse_idx0': np.array([1, 1]),
'2d_sparse_idx1': np.array([3, 7]),
'2d_sparse_val': np.array([13.0, 23.0]),
},
ragged_instance={
'ragged_val': np.array([22.0, 22.0, 21.0]),
'ragged_row_lengths1': np.array([0, 2, 1]),
'2d_ragged_val': np.array([b'oh', b'hello ', b'']),
'2d_ragged_row_lengths1': np.array([1, 2]),
'2d_ragged_row_lengths2': np.array([0, 0, 3]),
'ragged_uniform_val': np.array([12, -11, 2, 1, -1, 12]),
'2d_ragged_uniform_val': np.array([1, -1, 23, 1, -1, 32]),
'2d_ragged_uniform_row_lengths1': np.array([1, 0, 2]),
}),
'multiple_columns_with_missing':
dict(
testcase_name='multiple_columns_with_missing',
feature_spec={'varlen_feature': tf.io.VarLenFeature(tf.string)},
ascii_proto="""\
features { feature { key: "varlen_feature" value {} } }""",
instance={'varlen_feature': None}),
'multivariate_string_to_varlen':
dict(
testcase_name='multivariate_string_to_varlen',
feature_spec={'varlen_string': tf.io.VarLenFeature(tf.string)},
ascii_proto="""\
features {
feature { key: "varlen_string" value { bytes_list { value: [ "foo", "bar" ] } } }
}""",
instance={'varlen_string': [b'foo', b'bar']}),
}
_ENCODE_ERROR_CASES = [
dict(
testcase_name='to_few_values',
feature_spec={
'2d_vector_feature': tf.io.FixedLenFeature([2, 2], tf.int64),
},
instance={'2d_vector_feature': [1, 2, 3]},
error_msg='got wrong number of values'),
dict(
testcase_name='unsupported_ragged_partition_sequence',
feature_spec={
'2d_ragged_feature':
tf.io.RaggedFeature(
tf.string,
value_key='2d_ragged_val',
partitions=[
tf.io.RaggedFeature.UniformRowLength(4),
tf.io.RaggedFeature.RowLengths('2d_ragged_row_lengths1')
]),
},
instance={'2d_ragged_val': [b'not', b'necessary']},
error_msg='Encountered ragged dimension after uniform'),
]
def _maybe_extend_encode_case_with_ragged(encode_case):
result = copy.deepcopy(encode_case)
ragged_ascii_proto = result.pop('ragged_ascii_proto', '}')
ragged_instance = result.pop('ragged_instance', {})
result['ascii_proto'] = (encode_case['ascii_proto'][:-1] + ragged_ascii_proto)
result['instance'].update(ragged_instance)
return result
def _maybe_extend_encode_cases_with_ragged(encode_cases):
for case in encode_cases.values():
yield _maybe_extend_encode_case_with_ragged(case)
def _ascii_to_example(ascii_proto):
return text_format.Merge(ascii_proto, tf.train.Example())
def _ascii_to_binary(ascii_proto):
return _ascii_to_example(ascii_proto).SerializeToString()
def _binary_to_example(serialized_proto):
return tf.train.Example.FromString(serialized_proto)
class ExampleProtoCoderTest(test_case.TransformTestCase):
def setUp(self):
super().setUp()
# Verify that the implementation we requested via the Flag is honored.
if any(arg.startswith('--proto_implementation_type') for arg in sys.argv):
assert (
api_implementation.Type() == flags.FLAGS.proto_implementation_type
), (
'Expected proto implementation type '
f'"{flags.FLAGS.proto_implementation_type}", got: '
f'"{api_implementation.Type()}"'
)
def assertSerializedProtosEqual(self, a, b):
np.testing.assert_equal(_binary_to_example(a), _binary_to_example(b))
@test_case.named_parameters(
*_maybe_extend_encode_cases_with_ragged(_ENCODE_CASES))
def test_encode(self, feature_spec, ascii_proto, instance, **kwargs):
schema = schema_utils.schema_from_feature_spec(feature_spec)
coder = example_proto_coder.ExampleProtoCoder(schema, **kwargs)
serialized_proto = _ascii_to_binary(ascii_proto)
self.assertSerializedProtosEqual(coder.encode(instance), serialized_proto)
@test_case.named_parameters(
*_maybe_extend_encode_cases_with_ragged(_ENCODE_CASES))
def test_encode_non_serialized(self, feature_spec, ascii_proto, instance,
**kwargs):
schema = schema_utils.schema_from_feature_spec(feature_spec)
coder = example_proto_coder.ExampleProtoCoder(
schema, serialized=False, **kwargs)
proto = _ascii_to_example(ascii_proto)
self.assertProtoEquals(coder.encode(instance), proto)
@test_case.named_parameters(*_ENCODE_ERROR_CASES)
def test_encode_error(self,
feature_spec,
instance,
error_msg,
error_type=ValueError,
**kwargs):
schema = schema_utils.schema_from_feature_spec(feature_spec)
with self.assertRaisesRegex(error_type, error_msg):
coder = example_proto_coder.ExampleProtoCoder(schema, **kwargs)
coder.encode(instance)
def test_example_proto_coder_picklable(self):
encode_case = _maybe_extend_encode_case_with_ragged(
_ENCODE_CASES['multiple_columns'])
schema = schema_utils.schema_from_feature_spec(encode_case['feature_spec'])
coder = example_proto_coder.ExampleProtoCoder(schema)
ascii_proto = encode_case['ascii_proto']
instance = encode_case['instance']
serialized_proto = _ascii_to_binary(ascii_proto)
for _ in range(2):
coder = pickle.loads(pickle.dumps(coder))
self.assertSerializedProtosEqual(coder.encode(instance), serialized_proto)
def test_example_proto_coder_cache(self):
"""Test that the cache remains valid after reading/writing None."""
schema = schema_utils.schema_from_feature_spec({
'varlen': tf.io.VarLenFeature(tf.int64),
})
coder = example_proto_coder.ExampleProtoCoder(schema)
ascii_protos = [
'features {feature {key: "varlen" value {int64_list {value: [5] }}}}',
'features {feature {key: "varlen" value {}}}',
'features {feature {key: "varlen" value {int64_list {value: [6] }}}}',
]
instances = [{'varlen': [5]}, {'varlen': None}, {'varlen': [6]}]
serialized_protos = map(_ascii_to_binary, ascii_protos)
for instance, serialized_proto in zip(instances, serialized_protos):
self.assertSerializedProtosEqual(coder.encode(instance), serialized_proto)