简体   繁体   English

将tfrecords中的原始字节解码为tf.feature_column.numeric_column功能

[英]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.

相关问题 如何从数据框列获取TF功能列? - How can I get a TF feature column from a dataframe column? 如何通过乘以其他两个tf.feature_columns来创建tf.feature_column? - How to create a tf.feature_column by multiplying two other tf.feature_columns? tf.feature_column.input_layer返回错误的张量形状 - tf.feature_column.input_layer returning wrong shape of tensor 如何在csv列中分隔值以用作tf.contrib.learn.DNNRegressor中sparse_column_with_integerized_feature中的多价要素列 - How to seperate values in a csv column to use as multivalent feature column in sparse_column_with_integerized_feature in tf.contrib.learn.DNNRegressor TensorFlow DNNClassifier功能列 - TensorFlow DNNClassifier Feature column 一次在不同数据集中具有tf.feature_column.input_layer的自定义模型(不是tf.estimator)? - Custom model (not tf.estimator) with tf.feature_column.input_layer on different datasets at once? tf.feature_column.input_layer和tf.layers.Input有什么区别 - What's the difference between tf.feature_column.input_layer and tf.layers.Input Tensorflow Estimator:使用tf.feature_column.embedding_column作为类别变量列表时的损失不会减少 - Tensorflow Estimator: loss not decreasing when using tf.feature_column.embedding_column for a list of categorical variables Tensorflow:tf.train.Feature错误``预期之一:字节'' - Tensorflow: tf.train.Feature error 'expected one of: bytes' 结合 scikit-learn model 使用 TensorFlow 预处理(tf.feature_column) - Using TensorFlow pre-processing (tf.feature_column) in combination with scikit-learn model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM