把张量变成int_Tensorflow:将标量张量值作为int传递给set_shape()

我'm attempting to load 3D images and their labels from a numpy array to TensorFlow records, then read them from a queue while training my network. The code for conversion is based on the conversion for TensorFlow' s 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

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

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和会话的误解是什么让我的代码不能像我期望的那样工作?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值