tensorflow - 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) 等各种不同的方式来编写图像,但我无法让它工作。
请问有什么想法吗?
谢谢,保罗
解决方案
尝试以下操作:
image_string = open(image_location, 'rb').read()
feature = {'train/label': _int64_feature(y_train[i]),
'train/image': _bytes_feature(image_string)
}
参考官方教程
推荐阅读
- design-patterns - 轮询数据仓库设计
- python - 如何将两个列表与第三个列表进行比较,如果存在匹配项,则将它们分组到 Python 中的嵌套列表中?
- swift - 将字典映射到 Swift 中的结构数组
- django - Django:从不同的网络访问
- c# - creating a list of anonymous type and passing to view
- javascript - 在 XML 文件中搜索
- php - Display json api data in html
- python - Python Requests Module Not Getting Latest Data from Web Server
- apache-spark - 如何使用 scala 在 Spark 中评估 minHashLSH?
- c++ - How can I make clang-format put the first arg/param on its own line when arguments are split into lines?