tensorflow: Result of tf.reshape() with dynamic shape does not work with tf.train.shuffle_batch()

I wrote the images into TensorFlow format file, but when I read it like this:

features = tf.parse_single_example(
            serialized_example,
            features = {
                'height': tf.FixedLenFeature([], tf.int64),
                'width': tf.FixedLenFeature([], tf.int64),
                'channel': tf.FixedLenFeature([], tf.int64),
                'image': tf.FixedLenFeature([], tf.string),
                'gt': tf.FixedLenFeature([], tf.string),
            })

first, the data has no shape infomation, only the batch size, so I reshape it, but parameter shape of tf.reshape() only support list of integers, not a tensor, I don’t know why?

second, the data of gt is list, maybe length(gt) > 2, because no shape information, so I can’t unpack it

so, why is not shape information available

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (6 by maintainers)

Most upvoted comments

Ah, so the tf.reshape() works as intended, but you then use the result in a call to tf.train.shuffle_batch() and that fails? I’m afraid that this is expected behavior.

As the docs for tf.train.shuffle_batch() say, you must either have fully-defined (i.e. static) shapes for all of the tensors you pass in the tensor_list argument (or you must specify explicit shapes for them). For TensorFlow to batch your images (or other tensors) together, they must all have the same shape. You cannot pass the result of a dynamic tf.reshape(), because each element might have a different shape, and TensorFlow cannot batch them.

Your best option is to resize, crop, or pad the input images to a static shape before passing them to tf.train.shuffle_batch(). For example, the Inception input pipeline uses tf.image.resize_images() to resize images to a standard size, so that they can be batched (using tf.train.batch_join() in this case).

If you know that image has a static shape [h, w, c], where h, w, and c are Python int values, you can simply call:

image.set_shape([h, w, c])

…and you don’t need the tf.reshape() because it should be a no-op. Then your code will work with tf.train.shuffle_batch().