[英]Decode raw bytes in tfrecords into tf.feature_column.numeric_column feature
I have a tfrecords file storing images as bytestrings. 我有一个tfrecords文件,将图像存储为字节串。 I want to define the feature column for this as tf.feature_column.numeric_column("image", shape=[64, 64], dtype=tf.float32)
but since it's not stored as a float_list in the tfrecords file this doesn't work. 我想为此定义特征列为tf.feature_column.numeric_column("image", shape=[64, 64], dtype=tf.float32)
但是由于它没有作为float_list存储在tfrecords文件中,因此不会工作。
I then tried to use the normalizer_fn argument of numeric_column which I defined as. 然后,我尝试使用我定义为的numeric_column的normalizer_fn参数。
def decode(image_bytestring):
img = tf.reshape(tf.decode_raw(image_bytestring, tf.uint8), [28, 28])
img = tf.cast(img, tf.float32)
return img
...
examples = tf.parse_example(
serialized_batch,
tf.feature_column.make_parse_example_spec(feature_columns))
The first problem however is that the parse spec FixedLenFeature(shape=(28, 28), dtype=tf.float32, default_value=None)
generated by this feature_column says to parse for a float32 when it's actually stored as a string which causes an error. 但是,第一个问题是此feature_column生成的解析规范FixedLenFeature(shape=(28, 28), dtype=tf.float32, default_value=None)
表示要在float32实际存储为字符串时解析该float32,这会导致错误。 So the decode function is not used. 因此不使用解码功能。
Is there a way around this when using tf.feature_column other than storing the image as a float_list in the tfrecord instead? 使用tf.feature_column时,除了将图像存储为tfrecord中的float_list之外,还有其他方法吗?
Seems like having a static type system would have been nice to guarantee the correct type of feature from the mapping function. 似乎拥有静态类型系统似乎可以从映射功能中保证要素的正确类型。
Maybe you can just store images as string bytes, and follow the common way to read images? 也许您可以将图像存储为字符串字节,然后按照常见的方式读取图像?
feature_map = { 'image': tf.FixedLenFeature([], dtype=tf.string,default_value='') }
features = tf.parse_single_example(example_serialized, feature_map)
image_buffer = features['image']
image = tf.image.decode_image(image_buffer, ...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.