首页 > 解决方案 > TensorFlow TFDV 不适用于图像

问题描述

我试图让 TFDV 使用 RGB 图像作为特征输入,从 TFRecords 文件中读取。我可以很好地将图像数据读/写到 TFRecord 文件中。这是编写的相关代码片段,其中 img 是一个 numpy [32,32,3] 数组:

feature = {'train/label': _int64_feature(y_train[i]),
           'train/image': _bytes_feature(tf.compat.as_bytes(img.tostring()))
          }

回读:

read_features = {'train/label': tf.FixedLenFeature([], tf.int64),
             'train/image': tf.FixedLenFeature([], tf.string)}

然后我可以使用 frombuffer 和 reshape 来恢复我的图像正确性。

问题是当我使用该 TFRecords 文件运行 tfdv.generate_statistics_from_tfrecord() 时。它抛出一个错误:

ValueError: '\xff ...... \x87' has type str, but isn't valid UTF-8 encoding. Non-UTF-8 strings must be converted to unicode objects before being added. [while running 'GenerateStatistics/RunStatsGenerators/TopKStatsGenerator/TopK_ConvertToSingleFeatureStats']

我已经尝试过使用 astype(unicode) 等各种不同的方式来编写图像,但我无法让它工作。

请问有什么想法吗?

谢谢,保罗

标签: tensorflowtensorflow-data-validation

解决方案


尝试以下操作:

image_string = open(image_location, 'rb').read()
feature = {'train/label': _int64_feature(y_train[i]),
           'train/image': _bytes_feature(image_string)
          }

参考官方教程


推荐阅读