/
example_encoding.py
301 lines (239 loc) · 10.5 KB
/
example_encoding.py
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
# coding=utf-8
# Copyright 2020 The TF-Agents Authors.
#
# 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
#
# https://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.
"""Utilities for easily encoding nests of numpy arrays into example protos."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from collections import abc
import io
import gin
import numpy as np
from PIL import Image
import tensorflow as tf # pylint: disable=g-explicit-tensorflow-version-import
from tf_agents.utils import nest_utils
def get_example_encoder(spec, compress_image=False, image_quality=95):
"""Get example encoder function for the given spec.
Given a spec, returns an example encoder function. The example encoder
function takes a nest of np.array feature values as input and returns a
TF Example proto.
Example:
spec = {
'lidar': array_spec.ArraySpec((900,), np.float32),
'joint_positions': {
'arm': array_spec.ArraySpec((7,), np.float32),
'hand': array_spec.BoundedArraySpec((3, 3), np.int32, -1, 1)
},
}
example_encoder = get_example_encoder(spec)
serialized = example_encoder({
'lidar': np.zeros((900,), np.float32),
'joint_positions': {
'arm': np.array([0.0, 1.57, 0.707, 0.2, 0.0, -1.57, 0.0],
np.float32),
'hand': np.ones((3, 3), np.int32)
},
})
The returned example encoder function requires that the feature nest passed
has the shape and exact dtype specified in the spec. For example, it is
an error to pass an array with np.float64 dtype where np.float32 is expected.
Args:
spec: list/tuple/nest of ArraySpecs describing a single example.
compress_image: Whether to compress image. It is assumed that any uint8
tensor of rank 3 with shape (w,h,c) is an image.
image_quality: An optional int. Defaults to 95. Quality of the compression
from 0 to 100 (higher is better and slower).
Returns:
Function
```python
encoder(features_nest of np.arrays) -> tf.train.Example
```
"""
# pylint: disable=g-complex-comprehension
feature_encoders = [(path,
_get_feature_encoder(spec.shape, spec.dtype,
compress_image, image_quality))
for (path,
spec) in nest_utils.flatten_with_joined_paths(spec)]
# pylint: enable=g-complex-comprehension
def _example_encoder(features_nest):
flat_features = tf.nest.flatten(features_nest)
if len(flat_features) != len(feature_encoders):
raise ValueError(
'Encoding failed: The number of items to encode does not match the '
'number of encoders generated. Num features %d, num_encoders:%d' %
(len(flat_features), len(feature_encoders)))
feature_dict = {
path: feature_encoder(feature)
for feature, (path,
feature_encoder) in zip(flat_features, feature_encoders)
}
return tf.train.Example(features=tf.train.Features(feature=feature_dict))
return _example_encoder
def get_example_serializer(spec, compress_image=False, image_quality=95):
"""Returns string serializer of example protos."""
encoder = get_example_encoder(
spec, compress_image=compress_image, image_quality=image_quality)
return lambda features_nest: encoder(features_nest).SerializeToString()
def get_example_decoder(example_spec,
batched=False,
compress_image=False):
"""Get an example decoder function for a nested spec.
Given a spec, returns an example decoder function. The decoder function parses
string serialized example protos into tensors according to the given spec.
Args:
example_spec: list/tuple/nest of ArraySpecs describing a single example.
batched: Boolean indicating if the decoder will receive batches of
serialized data.
compress_image: Whether to decompress image. It is assumed that any uint8
tensor of rank 3 with shape (w,h,c) is an image.
If the tensor was compressed in the encoder, it needs to be decompressed.
Returns:
Function
```python
decoder(serialized_proto: tf.tensor[string]) -> example_spec nest of tensors
```
"""
features_dict = {}
parsers = []
for (path, spec) in nest_utils.flatten_with_joined_paths(example_spec):
feature, parser = _get_feature_parser(spec.shape, spec.dtype,
compress_image)
features_dict[path] = feature
parsers.append((path, parser))
def _example_decoder(serialized):
"""Parses string serialized example protos into tensors."""
if batched:
raw_features = tf.io.parse_example(
serialized=serialized, features=features_dict)
decoded_features = []
dtypes = [s.dtype for s in tf.nest.flatten(example_spec)]
if len(parsers) != len(dtypes):
raise ValueError(
'Decoding failed: Number of parsers (%d) does not match the number '
'of items in the example_spec (%d)' % (len(parsers), len(dtypes)))
for (path, parser), dtype in zip(parsers, dtypes):
decoded_features.append(
tf.map_fn(parser, raw_features[path], dtype=dtype))
return tf.nest.pack_sequence_as(example_spec, decoded_features)
else:
raw_features = tf.io.parse_single_example(
serialized=serialized, features=features_dict)
return tf.nest.pack_sequence_as(
example_spec,
[parser(raw_features[path]) for path, parser in parsers])
return _example_decoder
def _validate_shape(shape):
"""Check that shape is a valid array shape."""
if not isinstance(shape, abc.Iterable):
raise TypeError('shape must be a tuple or other iterable object, not %s' %
type(shape).__name__)
validated_shape = []
for i, dim in enumerate(shape):
if not dim or dim != int(dim) or int(dim) <= 0:
raise ValueError(
'Dimension %d is invalid in %s, expected positive int' % (i, shape))
validated_shape.append(int(dim))
return tuple(validated_shape)
def _validate_dtype(dtype):
"""Check that dtype is supported by tf.decode_raw."""
dtype = tf.as_dtype(dtype)
supported_dtypes = (tf.half, tf.float32, tf.float64, tf.uint8, tf.int8,
tf.uint16, tf.int16, tf.int32, tf.int64)
if dtype not in supported_dtypes:
raise ValueError('%s is not supported, dtype must be one of %s' %
(dtype.name, ', '.join(d.name for d in supported_dtypes)))
return dtype
def _check_shape_and_dtype(value, shape, dtype):
"""Check that `value` has expected shape and dtype."""
value_dtype = tf.as_dtype(value.dtype.newbyteorder('N'))
if shape != value.shape or dtype != value_dtype:
raise ValueError('Expected shape %s of %s, got: shape %s of %s' %
(shape, dtype.name, value.shape, value_dtype.name))
@gin.configurable
def _get_feature_encoder(shape, dtype, compress_image=False, image_quality=95):
"""Get feature encoder function for shape and dtype.
Args:
shape: An array shape
dtype: A list of dtypes.
compress_image: Whether to compress image. It is assumed that any uint8
tensor of rank 3 with shape (w,h,c) is an image.
image_quality: An optional int. Defaults to 95. Quality of the compression
from 0 to 100 (higher is better and slower).
Returns:
A tf.train.Feature encoder.
"""
shape = _validate_shape(shape)
dtype = _validate_dtype(dtype)
if compress_image and len(shape) == 3 and dtype == tf.uint8:
def _encode_to_jpeg_bytes_list(value):
if shape[-1] == 1:
im = Image.fromarray(value[:, :, 0])
else:
im = Image.fromarray(value)
out = io.BytesIO()
im.save(out, format='jpeg', quality=image_quality)
return tf.train.Feature(
bytes_list=tf.train.BytesList(value=[out.getvalue()]))
return _encode_to_jpeg_bytes_list
if dtype == tf.float32: # Serialize float32 to FloatList.
def _encode_to_float_list(value):
value = np.asarray(value)
_check_shape_and_dtype(value, shape, dtype)
return tf.train.Feature(
float_list=tf.train.FloatList(
value=value.flatten(order='C').tolist()))
return _encode_to_float_list
elif dtype == tf.int64: # Serialize int64 to Int64List.
def _encode_to_int64_list(value):
value = np.asarray(value)
_check_shape_and_dtype(value, shape, dtype)
return tf.train.Feature(
int64_list=tf.train.Int64List(
value=value.flatten(order='C').tolist()))
return _encode_to_int64_list
else: # Serialize anything else to BytesList in little endian order.
le_dtype = dtype.as_numpy_dtype(0).newbyteorder('L')
def _encode_to_bytes_list(value):
value = np.asarray(value)
_check_shape_and_dtype(value, shape, dtype)
bytes_list_value = np.require(
value, dtype=le_dtype, requirements='C').tobytes()
return tf.train.Feature(
bytes_list=tf.train.BytesList(value=[bytes_list_value]))
return _encode_to_bytes_list
@gin.configurable
def _get_feature_parser(shape, dtype, compress_image=False):
"""Get tf.train.Features entry and decoder function for parsing feature.
Args:
shape: An array shape
dtype: A list of dtypes.
compress_image: Whether to decompress image. It is assumed that any uint8
tensor of rank 3 with shape (w,h,c) is an image.
If the tensor was compressed in the encoder, it needs to be decompressed.
Returns:
A tuple containing tf.io.FixedLenFeature decoder and decode function.
"""
shape = _validate_shape(shape)
dtype = _validate_dtype(dtype)
if compress_image and len(shape) == 3 and dtype == tf.uint8:
return (tf.io.FixedLenFeature(shape=[], dtype=tf.string), tf.io.decode_jpeg)
if dtype == tf.float32:
return (tf.io.FixedLenFeature(shape=shape, dtype=tf.float32), lambda x: x)
elif dtype == tf.int64:
return (tf.io.FixedLenFeature(shape=shape, dtype=tf.int64), lambda x: x)
def decode(x):
return tf.reshape(tf.io.decode_raw(x, dtype), shape)
return (tf.io.FixedLenFeature(shape=[], dtype=tf.string), decode)
# pylint: enable=g-long-lambda