[Python] Tensorflow: Getting scalar tensor value as int for pass to set_shape()

Discussão em 'Python' iniciado por Stack, Abril 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'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.

    Each image has a different height, width, and depth value, so when reshaping the array I need to know these values. However, I'm getting an error when I try to use set_shape, as somewhere down the line int() is being used, and it doesn't accept Tensor values.

    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])


    Error trace:

    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'


    I originally thought this was because the Tensor did not have a value until it was evaluated in a session, but loss is being evaluated in a sess.run(), which is what requires the call to tower_loss(). My training code is identical in structure to cifar10_multi_gpu_train.py, and the overall file structure is also very similar.

    The question then is: Is it actually being evaluated in a session, or is the graph not built yet? Do I need to somehow extract a value from the zero-dimensional Tensor? More generally, what am I misunderstanding about Tensors and sessions that is making my code not work as I expect it to?

    Continue reading...

Compartilhe esta Página