Reputation: 3
I am writing a python code to create tfrecord file which stores A and C as features, But I am having trouble to print out the A,C values from TFrecord file. Can anyone take a look at this?
#Writing TFrecord file
import tensorflow as tf
import numpy as np
writer=tf.python_io.TFRecordWriter('output.tfrecord')
A=[1,3,4]
C=[1.1, 2.1, 3.1]
feature_A=tf.train.Feature(int64_list=tf.train.Int64List(value=A))
feature_C=tf.train.Feature(float_list=tf.train.FloatList(value=C))
features={'A':feature_A, 'C':feature_C}
example=tf.train.Example(features=tf.train.Features(feature=features))
writer.write(example.SerializeToString())
writer.close()
#Read TFrecord file
import tensorflow as tf
reader=tf.TFRecordReader()
filename_queue = tf.train.string_input_producer(
["output.tfrecord"])
_, serialized_example = reader.read(filename_queue)
feature_set = { 'A': tf.FixedLenFeature([], tf.int64),
'C': tf.FixedLenFeature([], tf.float32)
}
features = tf.parse_single_example( serialized_example, features= feature_set )
A=features['A']
C=features['C']
with tf.Session() as sess:
print(sess.run([A,C])) # print out nothing
Upvotes: 0
Views: 366
Reputation: 17201
There are two issues:
FixedlenFeature has to have the size defined. So change to:
feature_set = { 'A': tf.FixedLenFeature([3], tf.int64),
'C': tf.FixedLenFeature([3], tf.float32)}
You need to start the queues for reading the inputs, so your code should look like:
with tf.Session() as sess:
# for the queues
init_op = tf.local_variables_initializer()
sess.run(init_op)
# Create a coordinator, launch the queue runner threads.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
try:
for _ in range(1):
print(sess.run([A,C]))
except tf.errors.OutOfRangeError:
# When done, ask the threads to stop.
print('')
finally:
coord.request_stop()
# Wait for threads to finish.
coord.join(threads)
Upvotes: 1