python - tf.reshape 尺寸与 tf.decode_raw 的输出不匹配
问题描述
import cv2
img = cv2.imread(cat_in_snow)
height, width, channels = img.shape
print (height, width, channels)
上述代码片段的输出是 [213 320 3]
raw_image_dataset = tf.data.TFRecordDataset('images.tfrecords')
# Create a dictionary describing the features.
image_feature_description = {
'height': tf.io.FixedLenFeature([], tf.int64),
'width': tf.io.FixedLenFeature([], tf.int64),
'depth': tf.io.FixedLenFeature([], tf.int64),
'label': tf.io.FixedLenFeature([], tf.int64),
'image_raw': tf.io.FixedLenFeature([], tf.string),
}
def _parse_image_function(example_proto):
# Parse the input tf.Example proto using the dictionary above.
return tf.io.parse_single_example(example_proto, image_feature_description)
parsed_image_dataset = raw_image_dataset.map(_parse_image_function)
for image_features in parsed_image_dataset:
print(image_features['image_raw'])
image_raw = image_features['image_raw'].numpy()
dec_img = tf.io.decode_raw(image_features['image_raw'], tf.uint8)
img = tf.reshape(dec_img,[213 ,320, 3])
InvalidArgumentError:reshape 的输入是具有 17858 个值的张量,但请求的形状有 204480 [Op:Reshape]
上述文件包含与 opencv 读取中使用的相同图像,但 decode_raw 函数给出不同的输出。有人可以帮我解决这个问题吗?
解决方案
我遇到了同样的问题。
我发现如果你将图像字符串保存到 tfrecords 中,你可以使用 `tf.io.decode_raw' 方法。
image_data = matplotlib.image.imread(img_path)
# Convert image to string data
image_str = image_data.tostring()
但对我来说,我以字节为单位读取和存储图像数据,所以我改用 'tf.image.decode_image' 方法。
with tf.compat.v1.gfile.FastGFile(img_path, 'rb') as fid:
image_data = fid.read()
希望这个答案对您有所帮助。
推荐阅读
- ubuntu - 使用 remove 卸载 logstash 在 ubuntu 上不起作用
- java - 线程“主”org.openqa.selenium.StaleElementReferenceException 中的异常:androidx.test.uiautomator.StaleObjectException
- apache-kafka-streams - 带有 EOS 的流中的拦截器中的 Kafka 事务
- postgresql - 无法访问 Chainlink 浏览器 UI
- apache-spark - 如何在 Apache Spark 中实现递归算法?
- security - 如何检测 TCP 数据包中的 TLS 数据?
- sql - SQL 如何将 M/DD/YYYY (1/31/1960) 格式化为 DD/MM/YYYY (31/01/1969)
- javascript - 仅特定数据集的图例 - chart.js
- c++ - 如何使用 exrtk 将方程评估为布尔值?
- python - 将 GeoDataFrame 折线绘制为箭头