【问题标题】:tf.reshape dimensions are not matching the output of tf.decode_rawtf.reshape 尺寸与 tf.decode_raw 的输出不匹配
【发布时间】:2019-11-12 09:39:28
【问题描述】:
import cv2
img = cv2.imread(cat_in_snow)
height, width, channels = img.shape
print (height, width, channels)

上述代码 sn-p 的输出为 [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 文件中的图像进行编码的?
  • 是的。我将图像的字符串传递给它被转换为字节列表的功能可能
  • 张量必须是以下类型之一:int32、tf.reshape 的 int64。
  • 有趣。为什么不 uint8 ?有什么想法吗?

标签: python tensorflow image-processing deep-learning


【解决方案1】:

我遇到了同样的问题。

我发现如果你将图片字符串保存到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()

希望这个答案对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多