python如何读取tfrecord文件_tensorflow-TFRecord 文件详解

TFRecord 是 tensorflow 内置的文件格式,它是一种二进制文件,具有以下优点:

1. 统一各种输入文件的操作

2. 更好的利用内存,方便复制和移动

3. 将二进制数据和标签(label)存储在同一个文件中

引言

在了解如下操作后进一步详细讲解TFRecord

tf.train.Int64List(value=list_data)

它的作用是 把 list 中每个元素转换成 key-value 形式,

注意,输入必须是 list,且 list 中元素类型要相同,且与 Int 保持一致;

#value = tf.constant([1, 2]) ### 这会报错的

ss = 1 ### Int64List 对应的元素只能是 int long,其他同理

tt = 2out1= tf.train.Int64List(value =[ss, tt])print(out1)#value: 1#value: 2

ss= [1 ,2]

out2= tf.train.Int64List(value =ss)print(out2)#value: 1#value: 2

同类型的 方法还有 2 个

tf.train.FloatList

tf.train.BytesList

tf.train.Feature(int64_list=)

它的作用是 构建 一种类型的特征集,比如 整型

out = tf.train.Feature(int64_list=tf.train.Int64List(value=[33, 22]))print(out)#int64_list {#value: 33#value: 22#}

也可以是其他类型

tf.train.Feature(float_list=tf.train.FloatList())

tf.train.Feature(bytes_list=tf.train.BytesList())

tf.train.Features(feature=dict_data)

它的作用是 构建 多种类型 的特征集,可以 dict 格式表达 多种类型

ut = tf.train.Features(feature={"suibian": tf.train.Feature(int64_list=tf.train.Int64List(value=[1, 2, 4])),"a": tf.train.Feature(float_list=tf.train.FloatList(value=[5., 7.]))

})print(out)#feature {#key: "a"#value {#float_list {#value: 5.0#value: 7.0#}#}#}#feature {#key: "suibian"#value {#int64_list {#va

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python读取TFRecord文件的方法如下: ```python import tensorflow as tf # 创建一个TFRecordDataset对象 dataset = tf.data.TFRecordDataset('data.tfrecord') # 定义读取函数 def parser(record): features = { 'image': tf.io.FixedLenFeature([], dtype=tf.string), 'label': tf.io.FixedLenFeature([], dtype=tf.int64) } parsed = tf.io.parse_single_example(record, features) image = tf.io.decode_jpeg(parsed['image'], channels=3) label = parsed['label'] return image, label # 应用读取函数到每个record dataset = dataset.map(parser) # 创建迭代器 iterator = dataset.make_one_shot_iterator() # 获取数据 image, label = iterator.get_next() ``` 以上代码演示了如何读取名为`data.tfrecord`的TFRecord文件,并解析其中的图像和标签信息。在解析函数`parser`中,我们先定义了TFRecord文件中包含的特征信息,然后使用`tf.io.parse_single_example`函数解析单个record,并对图像数据进行解码。最后,我们使用`map`函数将解析函数应用到每个record上。 当然,如果您使用的是PyTorch,也可以使用以下代码读取TFRecord文件: ```python import torch import torchvision.datasets as datasets import torchvision.transforms as transforms # 定义解析函数 def parser(record): features = { 'image': tf.io.FixedLenFeature([], dtype=tf.string), 'label': tf.io.FixedLenFeature([], dtype=tf.int64) } parsed = tf.io.parse_single_example(record, features) image = tf.io.decode_jpeg(parsed['image'], channels=3) label = parsed['label'] return image, label # 创建数据集对象 dataset = datasets.DatasetFolder( 'data.tfrecord', loader=lambda x: torch.load(x), extensions=('tfrecord') ) # 应用解析函数到每个record dataset.transform = transforms.Compose([ parser ]) # 创建数据加载器 dataloader = torch.utils.data.DataLoader( dataset, batch_size=32, shuffle=True ) # 获取数据 for images, labels in dataloader: # 使用数据进行训练或预测 pass ``` 以上代码演示了如何使用PyTorch的`DatasetFolder`读取TFRecord文件,并使用解析函数`parser`解析图像和标签信息。最后,我们创建了一个数据加载器,并使用其中的数据进行训练或预测。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值