如何使用从 TFRecords 读取的值作为 tf.reshape 的参数? [英] How can I use values read from TFRecords as arguments to tf.reshape?

查看:22
本文介绍了如何使用从 TFRecords 读取的值作为 tf.reshape 的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def read_and_decode(filename_queue):
  reader = tf.TFRecordReader()
  _, serialized_example = reader.read(filename_queue)
  features = tf.parse_single_example(
      serialized_example,
      # Defaults are not specified since both keys are required.
      features={
          'image_raw': tf.FixedLenFeature([], tf.string),
          'label': tf.FixedLenFeature([], tf.int64),
          'height': tf.FixedLenFeature([], tf.int64),
          'width': tf.FixedLenFeature([], tf.int64),
          'depth': tf.FixedLenFeature([], tf.int64)
      })
  # height = tf.cast(features['height'],tf.int32)
  image = tf.decode_raw(features['image_raw'], tf.uint8)
  image = tf.reshape(image,[32, 32, 3])
  image = tf.cast(image,tf.float32)
  label = tf.cast(features['label'], tf.int32)
  return image, label

我正在使用 TFRecord 来存储我的所有数据.函数 read_and_decode 来自 TensorFlow 提供的 TFRecords 示例.目前我通过具有预定义的值来重塑:

I'm using a TFRecord to store all my data. The function read_and_decode is from the TFRecords example provided by TensorFlow. Currently I reshape by having predefined values:

image = tf.reshape(image,[32, 32, 3])

但是,我现在要使用的数据是不同维度的.例如,我可以有一个 [40, 30, 3] 的图像(缩放这不是一个选项,因为我不希望它被扭曲).我想读入不同维度的数据并在数据增强阶段使用random_crop来规避这个问题.我需要的是以下内容.

However, the data that I will be using now is of different dimensions. For example, I could have an image that is [40, 30, 3] (scaling this is not an option as I don't want it to be warped). I would like to read in data of different dimensions and use random_crop in the data augmentation stage to circumvent this problem. What I need is something like the following.

height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)
image = tf.reshape(image,[height, width, 3])

但是,我似乎无法找到一种方法来做到这一点.感谢您的帮助!

However, I can't seem to find a way to do this. Thanks for your help!

ValueError: All shapes must be fully defined: [TensorShape([Dimension(None), Dimension(None), Dimension(None)]), TensorShape([])]

image = tf.reshape(image, tf.pack([height, width, 3]))
image = tf.reshape(image, [32,32,3])

问题肯定出在这两行上.硬编码变量有效,但不是 tf.pack().

The problem is definitely with these 2 lines. The hard coded variables work, but not the one with tf.pack().

推荐答案

您即将拥有一个可行的解决方案!现在没有自动方法可以给 TensorFlow 一个由张量和数字组成的列表并从中生成一个张量,tf.reshape() 期待.答案是使用 tf.stack(),它明确地获取一个 N 维张量列表(或可转换为张量的事物)并将它们打包成一个 (N+1) 维张量.

You're very close to having a working solution! Right now there's no automatic way to give TensorFlow a list made up of tensors and numbers and make a tensor from it, which tf.reshape() is expecting. The answer is to use tf.stack(), which explicitly takes a list of N-dimensional tensors (or things convertible to tensors) and packs them into an (N+1)-dimensional tensor.

这意味着你可以写:

features = ...  # Parse from an example proto.
height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)

image = tf.reshape(image, tf.stack([height, width, 3]))

这篇关于如何使用从 TFRecords 读取的值作为 tf.reshape 的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆