在不启动会话的情况下检查TFRecordReader条目

Tim*_*man 2 python tensorflow

让我们假设我写了一个带有MNIST示例的TFRecords文件(https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/convert_to_records.py)这样做是这样的:

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()
Run Code Online (Sandbox Code Playgroud)

然后在其他一些脚本中加载它.但我找到的唯一方法是将它作为张量运行并提取数据,其中r一个是来自迭代器的记录record_iter = tf.python_io.tf_record_iterator(db_path)

    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),
        })))
Run Code Online (Sandbox Code Playgroud)

然后可以single_ex['height']例如检索数据.但是,在我看来,必须有一个更简单的方法.我似乎无法找到相应的.proto来检索数据.而数据肯定在那里.这是一个转储r:

?
?
    image_raw?
?
?&00>a?????????????(??Y??aC\?z??;??\????\e?\i???
                                                ??)L???^
?y????~??a???4??G??<????.M???n???t????VB??<????Z???\?????,I??

depth


label


width


height
Run Code Online (Sandbox Code Playgroud)

mrr*_*rry 7

所述tf.train.Example.ParseFromString()可用于将字符串转换成protobuf的对象:

r = ...  # String object from `tf.python_io.tf_record_iterator()`.
example_proto = tf.train.Example()
example_proto.ParseFromString(r)
Run Code Online (Sandbox Code Playgroud)

可以在中找到此协议缓冲区的模式tensorflow/core/example/example.proto.