Tensorflow record: how to read and plot image values?


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.

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

This seems to have extracted an image from train.record, with the right dimensions, but it is of type tensorflow.python.framework.ops.Tensor, and when I try to plot it with something like:

cv2.imshow("image", image)

I just get an error: TypeError: Expected cv::UMat for argument 'mat'.

I have tried using eval, as recommended at a link below:

array = image.eval(session = sess)

But it did not work. The program just hangs at that point (for instance if I put it after the last line above).

More generally, it seems I am just missing something, for even when I try to get the class label:

label = parsed_example['label']

I get the same thing: not the value, but an object of type tensorflow.python.framework.ops.Tensor. I can literally see the value is there when I type the name in my ipython notebook, but am not sure how to access it as an int (or whatever).

Note I tried this, which has some methods that seem to directly convert to a numpy array but they did not work: https://github.com/yinguobing/tfrecord_utility/blob/master/view_record.py

I just got the error there is no numpy method for a tensor object.

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.

Related questions
How to print the value of a Tensor object in TensorFlow?


To visualize a single image from the TFRecord file, you could do something along the lines of:

import tensorflow as tf
import matplotlib.pyplot as plt

def parse_fn(data_record):
    feature = {'image/encoded': tf.FixedLenFeature([], tf.string),
               'image/object/class/label': tf.FixedLenFeature([], tf.int64)}
    sample = tf.parse_single_example(data_record, feature)
    return sample

file_path = r'/home/train.record'
dataset = tf.data.TFRecordDataset([file_path])
record_iterator = dataset.make_one_shot_iterator().get_next()

with tf.Session() as sess:
    # Read and parse record
    parsed_example = parse_fn(record_iterator)

    # Decode image and get numpy array
    encoded_image = parsed_example['image/encoded']
    decoded_image = tf.image.decode_jpeg(encoded_image, channels=3)
    image_np = sess.run(decoded_image)

    # Display image

This assumes that the image is JPEG-encoded. You should use the appropriate decoding function (e.g. for PNG images, use tf.image.decode_png).

NOTE: Not tested.

Answered By – rvinas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published