tfrecords 文件读取|学习笔记

本文涉及的产品
云解析DNS-重点域名监控,免费拨测 20万次(价值200元)
简介: 快速学习 tfrecords 文件读取

开发者学堂课程【深度学习框架TensorFlow入门tfrecords 文件读取学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址https://developer.aliyun.com/learning/course/773/detail/13557


tfrecords 文件读取


内容介绍:

一、读取 TFRecords 文件API

二、案例:读取 CIFAR 的 TFRecords 文件


一、读取TFRecords文件API

1)构造文件名队列

2)读取

解析example

tf.parse_single_example(value,features={

“image”:tf.FixedLenFeature([],tf.string),

“label”:tf.FixedLenFeature([],tf.int64)

})

image = feature[“iamge”]

lebel = feature[“lebel”]

解码

tf.decode_raw()

3)构造批处理队列

读取这种文件整个过程与其他问价一样,只不过需要有个解析 Example 的步骤。从 TFRecords 文件中读取数据,可以使用 tf.TFRecordReader 的 tf.parse_single_example 解析器。这个操作可以将 Example 协议内存块 (protocol buffer) 解析为张量。

tf.parse_single_example(serialized,features=None,name=None)

解析一个单一的 Example 原型

serialized :标量字符串 Tensor ,一个序列化的 Example

features : dict 字典数据,键为读取的名字,值为 FixedLenFeature

return :一个键值对组成的字典,键为读取的名字

tf.FixedLenFeature(shape,dtype)

shape :输入数据的形状,一般不指定,为空列表

dtype :输入数据类型,与存储进文件的类型要一致

类型只能是 float32 , int34 , string


二、案例:读取 CIFAR 的 TFRecords 文件

import tensorflow as tf

class Cifar(object):

def __init__(self):

# 初始化操作

self.height = 32

self.width = 32

self.channels = 3

# 设置图像字节数

self.image_bytes = self.height * self.width * self.channels

self.label_bytes = 1

self.all_bytes = self.label_bytes + self.image_bytes

def read_and_decode(self):

"""

读取二进制文件

:return:

"""

# 1、构造文件名队列

file_queue = tf.train.string_input_producer(file_list)

# 2、读取与解码

# 读取阶段

reader = tf.FixedLengthRecordReader(self.all_bytes)

# key 文件名 value 一个样本

key,value = reader,read(file_queue)

print("key:\n",key)

print("value:\n",value)

# 解码阶段

decoded = tf.decode_raw(value,tf.uint8)

print("decoded:\n",decoded)

# 将目标值和特征值切片切开

label=tf.slice(decoded,[0],[self.label_bytes])

tf.slice(decoded,[self.label_bytes],[self.image_bytes])

print("label:\n",label)

print("image:\n",image)

# 调整图片形状

image_reshaped=tf.reshaped(image,shape=[self,channels,self.heighy,self.width])

print("image_reshaped:\n",image_reshaped)

# 转置,将图片的顺序转为 height,width,channels

image_transposed=tf.transpose(image_reshaped,[1,2,0])

print("image_transposed:\n",image_transposed)

# 调整图像类型

image_cast=tf.cast(image_transposed,tf.float32)

# 3、批处理

label_batch,image_batch=tf.train.batch([label,image_cast],batch_size=100,num_threads=1,capacity=100)

# 开启会话

with tf.Session() as sess:

# 开启线程

coord = tf.train.Coordinator()

threads=tf.train.start_queue_runners(sess=sess,coord=coord)

key_new,value_new,decoded_new,label_new,image_new,image_reshaped_new,image_transposed_new=sess.run([key,value,decoded,label,image_reshaped,image_transposed])

label_value,image_value=sess.run(label_batch,image_batch)

print("key_new:\n",key_new)

print("value_new:\n",value_new)

print("decoded_new:\n",decoded_new)

print("label_new:\n",label_new)

print("image_new:\n",image_new)

print("image_reshaped_new:\n",image_reshaped_new)

print("image_transposed_new:\n",image_transposed_new)

print("label_value:\n",label_value)

print("image_value:\n",image_value)

# 回收线程

coord.request_stop()

coord.join(threads)

return image_value,label_value

#写入

def write_to_tfrecords(self,image_batch,label_batch):

"""

将样本的特征值和目标值一起写入 tfrecords 文件

:param image:

:param label:

:return:

"""

with tf.python_io.TFRecordWriter("cifar10.tfrecords") as writer:

# 循环构造 example 对象,并序列化写入文件

for i in range(100):

image = image_batch[i].tostring()

label = label_batch[i][0]

# print("tfrecords_image:\n",image)

# print("tfrecords_label:\n",label)

example=tf.train.Example(features=tf.train.Features(features={

“image”:tf.train.Feature(bytes_list=tf.train.BytesList(value=[image]),

“label”:tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),

}))

# example.SerializeToString

# 将序列化后的 example 写入文件

writer.write(example.SerializeToString())

return None

#读取

def read_tfrecords(self):

"""

读取 TFRecords 文件

:return:

"""

# 1、构造文件名队列

file_queue = tf.train.string_input_producer(["cifar10.tfrecords])

# 2、读取与解码

# 读取

reader = tf.TFRecordReader()

key,value = reader.read(file_queue)

# 解析 example

tf.parse_single_example(value,features={

“image”:tf.FixedLenFeature([],tf.string),

“label”:tf.FixedLenFeature([],tf.int64)

})

image = feature[“iamge”]

lebel = feature[“lebel”]

print("read_tf_image:\n",image)

print("read_tf_label:\n",label)

# 解码

tf.decode_raw(image,tf.uint8)

print("image_decoded:\n",image_decoded)

# 图像形状调整

image_reshaped=tf.reshape(image_decoded,[self.height,self.width,self.channel])

print("image_reshaped:\n",image_reshaped)

# 3、构造批处理队列

image_batch,label_batch=tf.train.batch([image_reshaped,label],batch_size=100,num_threads=2,capacity=100)

print("image_batch:\n",image_batch)

print("label_batch:\n",label_batch)

# 开启会话

with tf.Session() as sess:

#开启线程

coord = tf.train.Coordinator()

threads=tf.train.start_queue_runners(sess=sess,coord=coord)

image_value,label_value=sess.run([image_batch,label_batch])

print("image_value:\n",image_value)

print("label_value:\n",label_value)

#回收资源

coord.request_stop()

cooed.join(threads)

return None

if __name__ == "__main__":

cifar = Cifar()

# image_value,label_value = cifar.read_binary()

# cifar.write_to_tfrecords(image_value,label_value)

cifar.read_tfrecords()

相关文章
学术规范与论文写作(雨课堂)(研究生)期末考试 正确顺序
学术规范与论文写作(雨课堂)(研究生)期末考试 正确顺序
835 0
学术规范与论文写作(雨课堂)(研究生)期末考试 正确顺序
|
4天前
|
云安全 人工智能 自然语言处理
|
8天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
793 17
|
11天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
801 59
Meta SAM3开源:让图像分割,听懂你的话
|
2天前
|
人工智能 安全 小程序
阿里云无影云电脑是什么?最新收费价格个人版、企业版和商业版无影云电脑收费价格
阿里云无影云电脑是运行在云端的虚拟电脑,分企业版和个人版。企业版适用于办公、设计等场景,4核8G配置低至199元/年;个人版适合游戏、娱乐,黄金款14元/月起。支持多端接入,灵活按需使用。
235 164
|
9天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
334 116
|
2天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
350 3

热门文章

最新文章