当前位置:网站首页>数据集类型转换—TFRecords文件
数据集类型转换—TFRecords文件
2022-08-04 03:07:00 【Peyzhang】
TFRecord 是 TensorFlow 中的数据集存储格式。当我们将数据集整理成 TFRecord 格式后,TensorFlow 就可以高效地读取和处理这些数据集,从而帮助我们更高效地进行大规模的模型训练。
格式:TFRecord 可以理解为一系列序列化的 tf.train.Example 元素所组成的列表文件,而每一个 tf.train.Example 又由若干个 tf.train.Feature 的字典组成。形式如下:
[
{
# example 1 (tf.train.Example)
'feature_1': tf.train.Feature,
...
'feature_k': tf.train.Feature
},
...
{
# example N (tf.train.Example)
'feature_1': tf.train.Feature,
...
'feature_k': tf.train.Feature
}
]
# 字典结构如
feature = {
'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image])),
'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))
}
- 保存TFRecord
- 为了将形式各样的数据集整理为 TFRecord 格式,我们需要对数据集中的每个元素进行以下步骤:
读取该数据元素到内存 - 将该元素转换为 tf.train.Example 对象(每一个 tf.train.Example 由若干个 tf.train.Feature 的字典组成,因此需要先建立 Feature 的字典);
- 将该 tf.train.Example 对象序列化为字符串,并通过一个预先定义的 tf.io.TFRecordWriter 写入 TFRecord 文件。
- 为了将形式各样的数据集整理为 TFRecord 格式,我们需要对数据集中的每个元素进行以下步骤:
- 读取 TFRecord 数据
- 通过 tf.data.TFRecordDataset 读入原始的 TFRecord 文件(此时文件中的 tf.train.Example 对象尚未被反序列化),获得一个 tf.data.Dataset 数据集对象;
- 通过 Dataset.map 方法,对该数据集对象中的每一个序列化的 tf.train.Example 字符串执行 tf.io.parse_single_example 函数,从而实现反序列化。
代码:
# -*- coding: utf-8 -*-
""" @Time : 2022-08-03 22:02 @Author : peyzhang @File : tfRecode.py @Software: PyCharm """
import tensorflow as tf
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
train_cars_dir = 'F:/AI/data//train/car/'
train_human_dir = 'F:/AI/data//train/dog/'
tfrecord_file = 'F:/AI/data//train.tfrecords'
def main():
train_car_filenames = [train_cars_dir + filename for filename in os.listdir(train_cars_dir)]
train_dog_filenames = [train_human_dir + filename for filename in os.listdir(train_human_dir)]
train_filename = train_car_filenames + train_dog_filenames
train_labels = [0] * len(train_car_filenames) + [1] *len(train_dog_filenames)
print(train_filename)
print(train_labels)
with tf.io.TFRecordWriter(tfrecord_file) as writer:
for filename , label in zip(train_filename, train_labels):
image = open(filename,"rb").read()
feature = {
"image": tf.train.Feature(bytes_list=tf.train.BytesList(value=[image])),
"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))
}
example = tf.train.Example(features=tf.train.Features(feature=feature))
writer.write(example.SerializeToString())
def readTFrecord():
raw_dataset = tf.data.TFRecordDataset(tfrecord_file)
feature_description = {
'image': tf.io.FixedLenFeature([], tf.string),
'label': tf.io.FixedLenFeature([], tf.int64),
}
def decoding(example_string):
fdict = tf.io.parse_single_example(example_string, feature_description)
fdict['image'] = tf.io.decode_jpeg(fdict['image'])
return fdict['image'], fdict['label']
dataseate = raw_dataset.map(decoding)
for iamge , label in dataseate:
print(iamge, label)
if __name__ == '__main__':
# main()
readTFrecord()
边栏推荐
- STM8S project creation (STVD creation) --- use COSMIC to create a C language project
- [QNX Hypervisor 2.2 User Manual] 10.3 vdev gic
- Good bosses, please ask the flink CDC oracle to Doris, found that the CPU is unusual, a run down
- 【Playwright测试教程】5分钟上手
- 创新互融|华秋赋能助力OpenHarmony生态硬件开发落地
- Detailed analysis of scaffolding content
- Mockito单元测试
- Deep Learning (3) Classification Theory Part
- View mysql deadlock syntax
- In a more general sense, calculating the displacement distance and assumptions
猜你喜欢
Utilities of Ruineng Micrometer Chip RN2026
6口全千兆二层网管型工业以太网交换机千兆2光4电光纤自愈ERPS环网交换机
Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
Dong mingzhu live cold face away, when employees frequency low-level mistakes, no one can understand their products
三分建设,七分管理!产品、系统、组织三管齐下节能降耗
《nlp入门+实战:第八章:使用Pytorch实现手写数字识别》
复制带随机指针的链表
The general SQL injection flow (sample attached)
随机推荐
2022支付宝C2C现金红包PHP源码DEMO/兼容苹果/安卓浏览器和扫码形式
自定义通用分页标签01
在更一般意义上验算移位距离和假设
移动端响应式适配的方法
FPGA解析B码----连载3
《nlp入门+实战:第八章:使用Pytorch实现手写数字识别》
【观察】超聚变:首提“算网九阶”评估模型,共建开放繁荣的算力网络
MySQL 查询练习(1)
Pine Script | How to display and typeset a plot switch?
自制蓝牙手机app控制stm8/stm32/C51板载LED
说说数据治理中常见的20个问题
SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
函数,递归以及dom简单操作
安装postgis时报找不到“POSTGIS_VERSION”这个函数
单片机C语言->的用法,和意思
[Original] Start the XPS/OXPS reader that comes with Windows 10
Sfdp 超级表单开发平台 V6.0.5 正式发布
How to drop all tables under database in MySQL
There are n steps in total, and you can go up to 1 or 2 steps each time. How many ways are there?
sqoop ETL tool