我有一个 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/