title: Tensorflow record: how to read and plot image values?
body: <p>I have data in a tensorflow record file (data.record), and I seem to be able to read that data. I want to do something simple: just display the (png-encoded) image for a given example. But I can't get the image as a numpy array and simply show it. I mean, the data are in there how hard can it be to just pull it out and show it? I imagine I am missing something really obvious.</p>
  
  <pre><code>height = 700 # Image height
  width = 500 # Image width
  
  file_path = r'/home/train.record'
  with tf.Session() as sess:
      feature = {'image/encoded': tf.FixedLenFeature([], tf.string),
                 'image/object/class/label': tf.FixedLenFeature([], tf.int64)}
      filename_queue = tf.train.string_input_producer([data_path], num_epochs=1)
      reader = tf.TFRecordReader()
      _, serialized_example = reader.read(filename_queue)
      parsed_example = tf.parse_single_example(serialized_example, features=feature)
      image_raw = parsed_example['image/encoded']
      image = tf.decode_raw(image_raw, tf.uint8)
      image = tf.cast(image, tf.float32)
      image = tf.reshape(image, (height, width))
  </code></pre>
  
  <p>This seems to have extracted an image from <code>train.record</code>, with the right dimensions, but it is of type <code>tensorflow.python.framework.ops.Tensor</code>, and when I try to plot it with something like:</p>
  
  <pre><code>cv2.imshow("image", image)
  </code></pre>
  
  <p>I just get an error: <code>TypeError: Expected cv::UMat for argument 'mat'</code>.</p>
  
  <p>I have tried using <code>eval</code>, as recommended at a link below:</p>
  
  <pre><code>array = image.eval(session = sess)
  </code></pre>
  
  <p>But it did not work. The program just hangs at that point (for instance if I put it after the last line above). </p>
  
  <p>More generally, it seems I am just missing something, for even when I try to get the class label:</p>
  
  <pre><code>label = parsed_example['label']
  </code></pre>
  
  <p>I get the same thing: not the value, but an object of type <code>tensorflow.python.framework.ops.Tensor</code>. I can literally see the value is there when I type the name in my ipython notebook, but am not sure how to <em>access</em> it as an int (or whatever). </p>
  
  <p>Note I tried this, which has some methods that seem to directly convert to a numpy array but they did not work: <a href="https://github.com/yinguobing/tfrecord_utility/blob/master/view_record.py" rel="nofollow noreferrer">https://github.com/yinguobing/tfrecord_utility/blob/master/view_record.py</a></p>
  
  <p>I just got the error <code>there is no numpy method for a tensor object</code>.</p>
  
  <p>Note I am using tensorflow 1.13, Python 3.7, working in Ubuntu 18. I get the same results whether I run from Spyder or the command line.</p>
  
  <p><strong>Related questions</strong><br>
  - <a href="https://stackoverflow.com/questions/33633370/how-to-print-the-value-of-a-tensor-object-in-tensorflow">How to print the value of a Tensor object in TensorFlow?</a><br>
  - <a href="https://github.com/aymericdamien/TensorFlow-Examples/issues/40" rel="nofollow noreferrer">https://github.com/aymericdamien/TensorFlow-Examples/issues/40</a> </p>