Tensorflow TFDV不适用于图像

Tensorflow TFDV不适用于图像,tensorflow,tensorflow-data-validation,Tensorflow,Tensorflow Data Validation,我试图让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())) } 回过头

我试图让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和重塑来恢复正确的图像

问题是,当我运行tfdv.generate_statistics_时,使用该tfrecord文件从_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)等各种不同的方式来编写图像,但我可以;我不能让它工作

有什么想法吗

谢谢, 保罗

尝试以下方法:

image_string = open(image_location, 'rb').read()
feature = {'train/label': _int64_feature(y_train[i]),
           'train/image': _bytes_feature(image_string)
          }
如前所述,记录应采用TF示例格式。所以,您可以使用example gen将其转换为tf示例,然后将这些tf示例提供给tfdv吗。这应该能解决你的问题。请试试这个,如果有帮助请告诉我。