Inspect a TFRecordReader entry without launching a Session
<p>Lets assume I write a TFRecords file with MNIST examples (<a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/convert_to_records.py" rel="nofollow">https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/convert_to_records.py</a>)
This is done like so:</p>

<pre><code>writer = tf.python_io.TFRecordWriter(filename)
  for index in range(num_examples):
    image_raw = images[index].tostring()
    example = tf.train.Example(features=tf.train.Features(feature={
        'height': _int64_feature(rows),
        'width': _int64_feature(cols),
        'depth': _int64_feature(depth),
        'label': _int64_feature(int(labels[index])),
        'image_raw': _bytes_feature(image_raw)}))
    writer.write(example.SerializeToString())
  writer.close()
</code></pre>

<p>Then in some other script I load it in. But the only way I have found is to run it as a tensor and extract the data, in which <code>r</code> is one record from an iterator <code>record_iter = tf.python_io.tf_record_iterator(db_path)</code></p>

<pre><code>    with tf.Session() as sess_tmp:
        single_ex = (sess_tmp.run(tf.parse_single_example(r,features={
            'height': tf.FixedLenFeature([], tf.int64),
            'width': tf.FixedLenFeature([], tf.int64),
            'depth': tf.FixedLenFeature([], tf.int64),
            'image_raw': tf.FixedLenFeature([], tf.string),
            'label': tf.FixedLenFeature([], tf.int64),
        })))
</code></pre>

<p>The data can then be retrieved with <code>single_ex['height']</code> for example.
However, it seems to me that there must be an easier way. I cannot seem to find a corresponding .proto to retrieve the data. And the data is definately there. here is a dump of <code>r</code>:</p>

<pre><code>?
?
    image_raw?
?
?&amp;00&gt;a?????????????(??Y??aC\?z??;??\????\e?\i???
                                                ??)L???^
?y????~??a???4??G??&lt;????.M???n???t????VBÑ?&lt;???Ø§Z???\?????,I?Å

depth


label


width


height
</code></pre>

<p>The <code>tf.train.Example.ParseFromString()</code> can be used to transform the string into a protobuf object:</p>

<pre><code>r = ...  # String object from `tf.python_io.tf_record_iterator()`.
example_proto = tf.train.Example()
example_proto.ParseFromString(r)
</code></pre>

<p>The schema for this protocol buffer can be found in <a href="https://github.com/tensorflow/tensorflow/blob/28166c086204c5ca4134086f250469a3802546ea/tensorflow/core/example/example.proto" rel="nofollow"><code>tensorflow/core/example/example.proto</code></a>.</p>

python tensorflow