TensorFlow - Read all examples from a TFRecords at once?

前端 未结 6 1530
野的像风
野的像风 2020-12-13 02:29

How do you read all examples from a TFRecords at once?

I\'ve been using tf.parse_single_example to read out individual examples using code similar to th

6条回答
  •  半阙折子戏
    2020-12-13 03:07

    If you need to read all the data from TFRecord at once, you can write way easier solution just in a few lines of code using tf_record_iterator:

    An iterator that read the records from a TFRecords file.

    To do this, you just:

    1. create an example
    2. iterate over records from the iterator
    3. parse each record and read each feature depending on its type

    Here is an example with explanation how to read each type.

    example = tf.train.Example()
    for record in tf.python_io.tf_record_iterator():
        example.ParseFromString(record)
        f = example.features.feature
        v1 = f['int64 feature'].int64_list.value[0]
        v2 = f['float feature'].float_list.value[0]
        v3 = f['bytes feature'].bytes_list.value[0]
        # for bytes you might want to represent them in a different way (based on what they were before saving)
        # something like `np.fromstring(f['img'].bytes_list.value[0], dtype=np.uint8
        # Now do something with your v1/v2/v3
    

提交回复
热议问题