簡體   English   中英

tf.reshape 尺寸與 tf.decode_raw 的 output 不匹配

[英]tf.reshape dimensions are not matching the output of tf.decode_raw

import cv2
img = cv2.imread(cat_in_snow)
height, width, channels = img.shape
print (height, width, channels)

上述代碼片段的 output 為 [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 function 給出不同的 output。 有人可以幫我解決這個問題嗎?

我遇到了同樣的問題。

我發現如果將圖像字符串保存到 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()

希望這個答案對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM