3

This question is based on: Tensorflow image reading & display

Following their code we have the following:

string = ['/home/user/test.jpg']
filepath_queue = tf.train.string_input_producer(string)
self.reader = tf.WholeFileReader()
key, value = self.reader.read(filepath_queue)

print(value)
# Output: Tensor("ReaderRead:1", shape=TensorShape([]), dtype=string)

my_img = tf.image.decode_jpeg(value, channels=3)
print(my_img)
# Output: Tensor("DecodeJpeg:0", shape=TensorShape([Dimension(None), Dimension(None), Dimension(3)]), dtype=uint8)

Why does my_img have no dimensions? (Dimension(3) is only because of the argument channels=3)

Does this mean that the image is not properly loaded? (img = misc.imread('/home/user/test.jpg') does load that image).

Community
  • 1
  • 1
NumesSanguis
  • 5,832
  • 6
  • 41
  • 76

1 Answers1

7

The image will be properly loaded, but TensorFlow doesn't have enough information to infer the image's shape until the op is run. This arises because tf.image.decode_jpeg() can produce tensors of different shapes (heights and widths), depending on the contents of the string tensor value. This enables you to build input pipelines using a collection of images with different sizes.

The Dimension(None) in the shape means "unknown" rather than "empty". If you happen to know that all images read by this operation will have the same size, you can use Tensor.set_shape() to provide this information, and doing so will help to validate the shapes of later parts of the graph:

my_img = tf.image.decode_jpeg(value, channels=3)    
KNOWN_HEIGHT = 28
KNOWN_WIDTH = 28
my_img.set_shape([KNOWN_HEIGHT, KNOWN_WIDTH, 3])

print(my_img)
# Output: Tensor("DecodeJpeg:0", shape=TensorShape([Dimension(28), Dimension(28), Dimension(3)]), dtype=uint8)
mrry
  • 125,488
  • 26
  • 399
  • 400