欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tf 解析tfrecord

程序员文章站 2024-01-19 14:03:58
...

对于获取的一个tfrecord,若未知对应的key,则可参考tf.train.Example.FromString解析tfrecord

 

现在给出例子解析cifar10,下载链接:https://download.csdn.net/download/u014426939/13619698

从上面链接,得出cifar10键值:

{'label': 'tf.FixedLenFeature([1], tf.int64, default_value=0', 
'id': "tf.FixedLenFeature([], tf.string, default_value=''", 
'image': "tf.FixedLenFeature([], tf.string, default_value=''"}

解析cifar10:利用

tf.io.parse_example和tf.io.parse_single_example分别解析
import tensorflow as tf
import glob
tf.enable_eager_execution()

path='cifar/cifar10*.tfrecord*'

files=glob.glob(path)

keys_to_features = {
        "id": tf.FixedLenFeature([], tf.string, default_value=''),
        "image":tf.FixedLenFeature([],tf.string,default_value=''),
        "label":tf.FixedLenFeature([1],tf.int64,default_value=0),
    }

'''
def _parse_record(example):
    features = tf.io.parse_single_example(
       example,
       features=keys_to_features)
    id=features['id']
    image=features['image']
    label=features['label']
    image=tf.image.decode_image(image)
    #tf.image.resize(image)
    return id,image,label
'''
data = tf.data.TFRecordDataset(files)

data2 = data.map(lambda x : tf.io.parse_example(tf.reshape(x,[1]), features = keys_to_features))
iter2=data2.__iter__()
iter2.next()
#
#
data3 = data.map(lambda x : tf.io.parse_single_example(x, features = keys_to_features))
iter3=data3.__iter__()
iter3.next()