我试图将3D图像及其标签从numpy数组加载到TensorFlow记录,然后在训练我的网络时从队列中读取它们。转化代码基于TensorFlow Inception model的转化。
每个图像都有不同的高度,宽度和深度值,因此在重塑数组时我需要知道这些值。但是,当我尝试使用set_shape时,我收到错误,因为正在使用int()的某个地方,并且它不接受Tensor值。
reader = tf.TFRecordReader()
_, value = reader.read(filename_queue)
# Features in Example proto
feature_map = {
'height': tf.VarLenFeature(dtype=tf.int64),
'width': tf.VarLenFeature(dtype=tf.int64),
'depth': tf.VarLenFeature(dtype=tf.int64),
'label': tf.VarLenFeature(dtype=tf.int64),
'image_raw': tf.VarLenFeature(dtype=tf.string)
}
features = tf.parse_single_example(value, feature_map)
result.label = tf.cast(features['label'].values[0], dtype=tf.int32)
result.height = tf.cast(features['height'].values[0], dtype=tf.int32)
result.width = tf.cast(features['width'].values[0], dtype=tf.int32)
result.depth = tf.cast(features['depth'].values[0], dtype=tf.int32)
image = tf.decode_raw(features['image_raw'].values[0], tf.int16)
image = tf.reshape(image, [result.depth, result.height, result.width])
image = tf.cast(tf.transpose(image, [1, 2, 0]), tf.float32)
result.image = tf.expand_dims(image, 3)
result.image.set_shape([result.height, result.width, result.depth, 1])
result.label = tf.expand_dims(result.label, 0)
result.label.set_shape([1])
错误追踪:
Traceback (most recent call last):
File "dsb17_multi_gpu_train.py", line 227, in <module>
tf.app.run()
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "dsb17_multi_gpu_train.py", line 223, in main
train()
File "dsb17_multi_gpu_train.py", line 129, in train
loss = tower_loss(scope)
File "dsb17_multi_gpu_train.py", line 34, in tower_loss
images, labels = dsb17.inputs(False)
File "/home/ubuntu/dsb17/model/dsb17.py", line 104, in inputs
batch_size=FLAGS.batch_size)
File "/home/ubuntu/dsb17/model/dsb17_input.py", line 161, in inputs
read_input = read_data(filename_queue)
File "/home/ubuntu/dsb17/model/dsb17_input.py", line 62, in read_data
result.image.set_shape([result.height, result.width, result.depth, 1])
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py", line 425, in set_shape
self._shape = self._shape.merge_with(shape)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 573, in merge_with
other = as_shape(other)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 821, in as_shape
return TensorShape(shape)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 457, in __init__
self._dims = [as_dimension(d) for d in dims_iter]
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 457, in <listcomp>
self._dims = [as_dimension(d) for d in dims_iter]
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 378, in as_dimension
return Dimension(value)
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 33, in __init__
self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'
我原本以为这是因为Tensor在会话中被评估之前没有值,但是在sess.run()中正在评估丢失,这需要调用tower_loss()。我的训练代码在结构上与cifar10_multi_gpu_train.py相同,整体文件结构也非常相似。
那么问题是:它实际上是在会话中进行评估,还是图形尚未构建?我是否需要以某种方式从零维张量中提取值?更一般地说,我对Tensors和会话的误解是什么让我的代码不能像我期望的那样工作?
答案 0 :(得分:0)
根据TensorFlow的tf.cast docs,tf.cast返回Tensor。
您的错误表示在使用set_shape()
时,您不能将Tensor作为参数,而是使用int。
您可以尝试强制Tensorflow评估演员阵容。这个简单的例子对我有用:
a = tf.constant(2.0)
b = tf.constant([1.0,2.0])
b.set_shape(a.eval())
如果没有调用eval()
,我会收到与您相同的错误。
答案 1 :(得分:0)
通常,您无法使用tf.Tensor.set_shape()
执行此操作,因为该方法需要静态形状。张量result.height
,result.width
,result.depth
表示从文件读取的值,在运行时它们可以评估为许多不同的整数(取决于文件中的内容),因此没有您可以为他们传递的单int
。在这种情况下,您当前可以做的最好的事情是将这些维度表示为静态未知,使用None
表示未知维度:
result.image.set_shape([None, None, None, 1])
请注意,此声明不应更改任何内容,因为TensorFlow应该已经能够推断出形状是4-D,最后一维的大小为1。
有关静态和动态形状的更多详细信息,请参阅this answer。
答案 2 :(得分:0)
实际上,您可以将图像形状传递给reshape
函数,但是您还需要一步。只需更改行:
image = tf.reshape(image, [result.depth, result.height, result.width])
收件人:
image_shape = tf.stack([result.depth, result.height, result.width])
image = tf.reshape(image, image_shape)