Python Tensorflow:将标量张量值获取为int,以便通过设置_shape()进行传递
我试图将3D图像及其标签从numpy数组加载到TensorFlow记录,然后在训练网络时从队列中读取它们。转换代码基于TensorFlow的转换 每个图像都有不同的高度、宽度和深度值,因此在重塑阵列时,我需要知道这些值。但是,当我尝试使用set_shape时,我遇到了一个错误,因为在使用int()时,它不接受张量值Python Tensorflow:将标量张量值获取为int,以便通过设置_shape()进行传递,python,tensorflow,Python,Tensorflow,我试图将3D图像及其标签从numpy数组加载到TensorFlow记录,然后在训练网络时从队列中读取它们。转换代码基于TensorFlow的转换 每个图像都有不同的高度、宽度和深度值,因此在重塑阵列时,我需要知道这些值。但是,当我尝试使用set_shape时,我遇到了一个错误,因为在使用int()时,它不接受张量值 reader = tf.TFRecordReader() _, value = reader.read(filename_queue) # Features in Example
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'
回溯(最近一次呼叫最后一次):
文件“dsb17_multi_gpu_train.py”,第227行,在
tf.app.run()
文件“/usr/local/lib/python3.4/dist-packages/tensorflow/python/platform/app.py”,第44行,正在运行
_系统出口(主(_sys.argv[:1]+标志_passthrough))
文件“dsb17_multi_gpu_train.py”,第223行,主目录
列车()
文件“dsb17_multi_gpu_train.py”,第129行,列车中
损耗=塔架损耗(范围)
文件“dsb17_multi_gpu_train.py”,第34行,塔楼中
图像、标签=dsb17.输入(假)
文件“/home/ubuntu/dsb17/model/dsb17.py”,第104行,输入
批次大小=标志。批次大小)
文件“/home/ubuntu/dsb17/model/dsb17_input.py”,第161行,输入
读取输入=读取数据(文件名队列)
文件“/home/ubuntu/dsb17/model/dsb17_input.py”,第62行,读取数据
result.image.set_形状([result.height,result.width,result.depth,1])
文件“/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py”,第425行,呈集合形状
self.\u shape=self.\u shape.与(shape)合并
文件“/usr/local/lib/python3.4/dist packages/tensorflow/python/framework/tensor\u shape.py”,第573行,与合并
其他=形状相同(其他)
文件“/usr/local/lib/python3.4/dist packages/tensorflow/python/framework/tensor_shape.py”,第821行,as_形
返回张量形状(形状)
文件“/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_-shape.py”,第457行,在u-init中__
self._dims=[作为dims中d的尺寸(d)]
文件“/usr/local/lib/python3.4/dist packages/tensorflow/python/framework/tensor_shape.py”,第457行,在
self._dims=[作为dims中d的尺寸(d)]
文件“/usr/local/lib/python3.4/dist packages/tensorflow/python/framework/tensor_shape.py”,第378行,as_维度
返回维度(值)
文件“/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_-shape.py”,第33行,在初始化中__
self.\u value=int(值)
TypeError:int()参数必须是字符串、类似对象的字节或数字,而不是“张量”
我最初认为这是因为在会话中计算张量之前,张量没有值,但是在sess.run()中计算损耗,这就需要调用tower_loss()。我的培训代码在结构上与相同,并且总体文件结构也非常相似
接下来的问题是:它实际上是在会话中被评估的,还是图形还没有构建?我需要从零维张量中提取一个值吗?更一般地说,我对张量和会话有什么误解,导致我的代码无法按预期工作?根据TensorFlow的说法,tf.cast返回一个张量
您的错误是,当使用set_shape()
时,不能使用张量作为参数,而应使用int
您可以尝试强制Tensorflow来评估演员阵容。这个简单的例子适用于我:
a = tf.constant(2.0)
b = tf.constant([1.0,2.0])
b.set_shape(a.eval())
如果不调用
eval()
,我会得到与您相同的错误。通常,您无法使用执行此操作,因为该方法需要静态形状。张量result.height
、result.width
、result.depth
表示从文件中读取的值,在运行时,它们可以计算为许多不同的整数(取决于文件中的内容),因此没有可以传递给它们的单个int
。在这种情况下,您当前所能做的最好方法是将这些维度表示为静态未知,对未知维度使用None
:
result.image.set_shape([None, None, None, 1])
请注意,此语句不应改变任何内容,因为TensorFlow应该已经能够推断形状为4-D,最后一个维度的大小为1
有关静态和动态形状的详细信息,请参见。实际上,您可以将图像形状传递给
重塑功能,但还需要一个步骤。换句话:
image = tf.reshape(image, [result.depth, result.height, result.width])
致:
这是一个很好的建议,但是如果我在代码的该部分中创建一个会话(使用allow_soft_placement=True,否则它将根本不起作用),然后使用.eval(),代码将运行,但不会发生任何事情。如果它工作正常,我会收到一条消息说图像正在添加到队列中,但不知道代码在做什么。
image_shape = tf.stack([result.depth, result.height, result.width])
image = tf.reshape(image, image_shape)