python - 将 tfrecords 中的原始字节解码为 tf.feature_column.numeric_column 特征

标签 python tensorflow tfrecord

我有一个 tfrecords 文件,将图像存储为字节串。我想将其定义为 tf.feature_column.numeric_column("image", shape=[64, 64], dtype=tf.float32) 的特征列但由于它没有作为 float_list 存储在 tfrecords 文件中,因此不起作用。

然后我尝试使用我定义为的 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))

然而,第一个问题是解析规范 FixedLenFeature(shape=(28, 28), dtype=tf.float32, default_value=None)此 feature_column 生成的内容表示当实际存储为字符串时解析 float32,这会导致错误。所以没有使用decode函数。

使用 tf.feature_column 时除了将图像存储为 tfrecord 中的 float_list 之外,还有其他方法可以解决此问题吗?

似乎拥有静态类型系统可以很好地保证映射函数中功能的正确类型。

最佳答案

也许你可以将图像存储为字符串字节,并按照常见的方式读取图像?

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, ...)

关于python - 将 tfrecords 中的原始字节解码为 tf.feature_column.numeric_column 特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46807125/

相关文章:

python - 避免自动填充 pd.MultiIndex 中的缺失值

c++ - 如何切片或访问 Tensor 的一行?

tensorflow - 使用 tensorflow 实现 RBM

python - 如何写入tfrecord文件并读取它?错误是: truncated record at 0' failed with Read less bytes than requested [Op:IteratorGetNext]

tensorflow - 有没有办法在内存中保存一个 Tensorflow 记录文件?

Python SpaCy Regex 不提取包含单词的标记

python - 从 10-K -- 提取 SIC、CIK,创建元数据表

python - rpy2代码可以并行运行吗?

python - Keras:如何为验证集随机抽样?

python - 如何检查 Tensorflow .tfrecord 文件?