Issue
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?
- https://github.com/aymericdamien/TensorFlow-Examples/issues/40
Solution
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
plt.imshow(image_np)
plt.show()
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.