[TFRecord格式数据]利用TFRecords存储与读取带标签的图片

时间:2022-12-30 13:59:14

利用TFRecords存储与读取带标签的图片

觉得有用的话,欢迎一起讨论相互学习~

[TFRecord格式数据]利用TFRecords存储与读取带标签的图片

[TFRecord格式数据]利用TFRecords存储与读取带标签的图片[TFRecord格式数据]利用TFRecords存储与读取带标签的图片[TFRecord格式数据]利用TFRecords存储与读取带标签的图片

TFRecords其实是一种二进制文件,虽然它不如其他格式好理解,但是它能更好的利用内存,更方便复制和移动,并且不需要单独的标签文件

TFRecords文件包含了tf.train.Example 协议内存块(protocol buffer)(协议内存块包含了字段 Features)。我们可以写一段代码获取你的数据, 将数据填入到Example协议内存块(protocol buffer),将协议内存块序列化为一个字符串, 并且通过tf.python_io.TFRecordWriter 写入到TFRecords文件。

从TFRecords文件中读取数据, 可以使用tf.TFRecordReader的tf.parse_single_example解析器。这个操作可以将Example协议内存块(protocol buffer)解析为张量。

我们使用tf.train.Example来定义我们要填入的数据格式,然后使用tf.python_io.TFRecordWriter来写入。

基本的,一个Example中包含Features,Features里包含Feature(这里没s)的字典。最后,Feature里包含有一个 FloatList, 或者ByteList,或者Int64List

示例代码

# Reuse the image from earlier and give it a fake label
# 复用之前的图像,并赋予一个假标签
import tensorflow as tf image_filename = "./images/chapter-05-object-recognition-and-classification/working-with-images/test-input-image.jpg"
# 获得文件名列表
filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once(image_filename))
# 生成文件名队列
image_reader = tf.WholeFileReader()
_, image_file = image_reader.read(filename_queue)
# 通过阅读器返回一个键值对,其中value表示图像
image = tf.image.decode_jpeg(image_file)
# 通过tf.image.decode_jpeg解码函数对图片进行解码,得到图像.
sess = tf.Session()
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord, sess=sess)
print('the image is:', sess.run(image))
filename_queue.close(cancel_pending_enqueues=True)
coord.request_stop()
coord.join(threads)
image_label = b'\x01'
# Assume the label data is in a one-hot representation (00000001)
# 假设标签数据位于一个独热的(one-hot)编码表示中,(00000001) 二进制8位'x01'
# Convert the tensor into bytes, notice that this will load the entire image file
# 将张量转换为字节型,注意这会加载整个图像文件。
image_loaded = sess.run(image)
image_bytes = image_loaded.tobytes() # 将张量转化为字节类型.
image_height, image_width, image_channels = image_loaded.shape # Export TFRecord 导出TFRecord
writer = tf.python_io.TFRecordWriter("./output/training-image.tfrecord") # Don't store the width, height or image channels in this Example file to save space but not required.
# 样本文件中不保存图像的宽度/高度和通道数,以便节省不要求分配的空间.
example = tf.train.Example(features=tf.train.Features(feature={
'label': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_label])),
'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_bytes]))
})) # This will save the example to a text file tfrecord
writer.write(example.SerializeToString()) # 序列化为字符串
writer.close()
# image = example.features.feature['image_bytes'].bytes_list.value
# label = example.features.feature['image_label'].int64_list.value
# 这样的方式进行读取.
"""标签的格式被称为独热编码(one-hot encoding)这是一种用于多类分类的有标签数据的常见的表示方法.
Stanford Dogs 数据集之所以被视为多类分类数据,是因为狗会被分类为单一品种,而非多个品种的混合,
在现实世界中,当预测狗的品种是,多标签解决方案通常较为有效,因为他们能够同时匹配属于多个品种的狗""" """
这段代码中,图像被加载到内存中并被转换为字节数组
image_bytes = image_loaded.tobytes()
然后通过tf.train.Example函数将values和labels以value的方式加载到example中,
example = tf.train.Example(features=tf.train.Features(feature={
'label': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_label])),
'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_bytes]))
}))
example在写入保存到磁盘之前需要先通过SerializeToString()方法将其序列化为二进制字符串.
序列化是一种将内存对象转化为可安全传输到某种文件的格式.
上面序列化的样本现在被保存为一种可被加载的格式,并可被反序列化为这里的样本格式 由于图像被保存为TFRecord文件,可以被再次从TFRecord文件加载.这样比将图像及其标签分开加载会节省一些时间
"""
# Load TFRecord
# 加载TFRecord文件,获取文件名队列
tf_record_filename_queue = tf.train.string_input_producer(["./output/training-image.tfrecord"]) # Notice the different record reader, this one is designed to work with TFRecord files which may
# have more than one example in them.
# 注意这个不同的记录读取其,它的设计意图是能够使用可能会包含多个样本的TFRecord文件
tf_record_reader = tf.TFRecordReader()
_, tf_record_serialized = tf_record_reader.read(tf_record_filename_queue)
# 通过阅读器读取value值,并保存为tf_record_serialized # The label and image are stored as bytes but could be stored as int64 or float64 values in a
# serialized tf.Example protobuf.
# 标签和图像都按字节存储,但也可按int64或float64类型存储于序列化的tf.Example protobuf文件中
tf_record_features = tf.parse_single_example( # 这是一个模板化的东西,大部分都是这么写的
tf_record_serialized,
features={
'label': tf.FixedLenFeature([], tf.string),
'image': tf.FixedLenFeature([], tf.string),
})
"""
class FixedLenFeature(collections.namedtuple(
"FixedLenFeature", ["shape", "dtype", "default_value"])):""" """Configuration for parsing a fixed-length input feature.
用于解析固定长度的输入特性的配置。
To treat sparse input as dense, provide a `default_value`; otherwise,
the parse functions will fail on any examples missing this feature.
把稀疏的输入看作是稠密的,提供一个默认值;否则,解析函数将缺少属性值的情况下报错。
Fields:
shape: Shape of input data.输入数据的形状
dtype: Data type of input.输入数据类型
default_value: Value to be used if an example is missing this feature. It
must be compatible with `dtype` and of the specified `shape`.
如果一个示例缺少属性值,那么将使用该默认值。它必须与dtype和指定的形状兼容。
"""
# 但是在实际使用的过程中这里的features的是根据原先的保存时的名字对应的,而数据类型可以自行选取. # Using tf.uint8 because all of the channel information is between 0-255
# 使用tf.uint8类型,因为所有的通道信息都处于0~255的范围内
tf_record_image = tf.decode_raw(
tf_record_features['image'], tf.uint8)
# tf.decode_raw()函数将将字符串的字节重新解释为一个数字的向量。 # Reshape the image to look like the image saved, not required
# 调整图像的尺寸,使其与保存的图像类似,但这并不是必需的
tf_record_image = tf.reshape(
tf_record_image,
[image_height, image_width, image_channels])
# Use real values for the height, width and channels of the image because it's required
# 用是指表示图像的高度,宽度和通道,因为必须对输入的形状进行调整
# to reshape the input. tf_record_label = tf.cast(tf_record_features['label'], tf.string)
sess.close()
sess = tf.InteractiveSession()
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord, sess=sess)
print("equal the image before and now", sess.run(tf.equal(image, tf_record_image))) # 检查原始图像和加载后的图像是否一致
"""首先,按照与其他文件相同的方式加载该文件,主要区别在于该文件主要有TFRecordReaader对象读取.
tf.parse_single_example对TFRecord进行解析,然后图像按原始字节(tf.decode_raw)进行读取"""
print("The lable of the image:", sess.run(tf_record_label)) # 输出图像的标签
tf_record_filename_queue.close(cancel_pending_enqueues=True)
coord.request_stop()
coord.join(threads)

Notice

如果你想要复用这段代码,请将 image_filename, tf.python_io.TFRecordWriter, tf.train.string_input_producer 等处的文件保存参数修改成你自己的图片所在位置.

test-input-image图片下载地址

大图是这样的,运行请下载小图.

[TFRecord格式数据]利用TFRecords存储与读取带标签的图片

参考资料

面向机器智能的Tensorflow实践

[TFRecord格式数据]利用TFRecords存储与读取带标签的图片的更多相关文章

  1. java保存json格式数据,保存字符串和读取字符串

    1.java保存json格式数据,保存字符串和读取字符串 import java.io.*; class RWJson { public void wiite(String s, String toS ...

  2. 更加清晰的TFRecord格式数据生成及读取

    TFRecords 格式数据文件处理流程 TFRecords 文件包含了 tf.train.Example 协议缓冲区(protocol buffer),协议缓冲区包含了特征 Features.Ten ...

  3. tensorflow制作tfrecord格式数据

    tf.Example msg tensorflow提供了一种统一的格式.tfrecord来存储图像数据.用的是自家的google protobuf.就是把图像数据序列化成自定义格式的二进制数据. To ...

  4. hive 压缩全解读(hive表存储格式以及外部表直接加载压缩格式数据);HADOOP存储数据压缩方案对比(LZO,gz,ORC)

    数据做压缩和解压缩会增加CPU的开销,但可以最大程度的减少文件所需的磁盘空间和网络I/O的开销,所以最好对那些I/O密集型的作业使用数据压缩,cpu密集型,使用压缩反而会降低性能. 而hive中间结果 ...

  5. tensorflow学习笔记(10) mnist格式数据转换为TFrecords

    本程序 (1)mnist的图片转换成TFrecords格式 (2) 读取TFrecords格式 # coding:utf-8 # 将MNIST输入数据转化为TFRecord的格式 # http://b ...

  6. "笨方法"学习CNN图像识别(二)—— tfrecord格式高效读取数据

    原文地址:https://finthon.com/learn-cnn-two-tfrecord-read-data/-- 全文阅读5分钟 -- 在本文中,你将学习到以下内容: 将图片数据制作成tfre ...

  7. 利用python进行数据分析之数据加载存储与文件格式

    在开始学习之前,我们需要安装pandas模块.由于我安装的python的版本是2.7,故我们在https://pypi.python.org/pypi/pandas/0.16.2/#downloads ...

  8. python多种格式数据加载、处理与存储

    多种格式数据加载.处理与存储 实际的场景中,我们会在不同的地方遇到各种不同的数据格式(比如大家熟悉的csv与txt,比如网页HTML格式,比如XML格式),我们来一起看看python如何和这些格式的数 ...

  9. Numpy用于数组数据的存储和读取

    Python的Numpy模块可用于存储和读取数据: 1.将一个数组存储为二进制文件 Numpy.save:将一个数组以.npy的格式保存为二进制文件 调用格式:numpy.save(file, arr ...

随机推荐

  1. 软件开发之路、Step 1 需求分析

    百度百科 需求分析 所谓"需求分析",是指对要解决的问题进行详细的分析,弄清楚问题的要求,包括需要输入什么数据,要得到什么结果,最后应输出什么.可以说,在软件工程当中的“需求分析” ...

  2. linux gitlab nginx 安装 配置

    更新:bitnami-gitlab 7.8版本后界面发生变化 邮件问题: cd /data/server/gitlab/apps/gitlab/htdocs/config vim environmen ...

  3. HTMLParser获取属性名

    HTMLParser获取属性名方式: 原始网页文本: <a title="美军被曝虐尸" href="http://www.sogou.com/web?query= ...

  4. nanosleep纳秒级延迟

    //函数原型 int nanosleep(struct timespec *req, struct timespec *rem) //参数列表: // req:要求的睡眠时间 // rem:剩余的睡眠 ...

  5. C&num;的逆变和协变

    out: 输出(作为结果),in:输入(作为参数) 所以如果有一个泛型参数标记为out,则代表它是用来输出的,只能作为结果返回,而如果有一个泛型参数标记为in,则代表它是用来输入的,也就是它只能作为参 ...

  6. 闲谈REST API

    REST 表述性状态传递(英文:Representational State Transfer,简称REST). 资源: 资源由URI(统一资源定位符)的来指定. 通过资源的表现形式来操作资源 对资源 ...

  7. 尚硅谷springboot学习35-启动原理

    先列出几个重要的事件回调机制 配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListen ...

  8. js解决下拉列表框互斥选项的问题

    如图不区分选项与其他选项是互斥的关系,当选择了不区分时,其他选项就要去除,当有其他选项存在时,就不能有不区分 解决办法:定义change事件,若列表发生改变,首先判断点击的是否是不区分,若是,则将其他 ...

  9. physics---hdu5826(积分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5826 题意:有n个小球在一条直线上滚动,起始位置为xi, 方向为di(-1往左走,1往右走),初始速度 ...

  10. 第一篇随笔&comma; 正在做 ESP32 &comma; STM32 &comma; 树莓派 RaspberryPi 的创客工具

    先随便写写一些思路, 以后再整理. 这段时间笔者做了一些硬件开发, 领悟了一些事情. 1 - 在常规创客的角度上, 硬件开发所需的知识面比较广, 非常广, 但不算太深. 2 - 发现硬件开发由于其特殊 ...