TFRecord 的使用

什么是 TFRecord

PS:这段内容摘自 http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/reading_data.html

一种保存记录的方法可以允许你讲任意的数据转换为TensorFlow所支持的格式, 这种方法可以使TensorFlow的数据集更容易与网络应用架构相匹配。这种建议的方法就是使用TFRecords文件,TFRecords文件包含了tf.train.Example 协议内存块(protocol buffer)(协议内存块包含了字段 Features)。你可以写一段代码获取你的数据, 将数据填入到Example协议内存块(protocolbuffer),将协议内存块序列化为一个字符串, 并且通过tf.python_io.TFRecordWriterclass写入到TFRecords文件。tensorflow/g3doc/how_tos/reading_data/convert_to_records.py就是这样的一个例子。

从TFRecords文件中读取数据,
可以使用tf.TFRecordReader的tf.parse_single_example解析器。这个parse_single_example操作可以将Example协议内存块(protocolbuffer)解析为张量。
MNIST的例子就使用了convert_to_records 所构建的数据。
请参看tensorflow/g3doc/how_tos/reading_data/fully_connected_reader.py,

代码

adjust_pic.py

单纯的转换图片大小

[python] view plain copy

  1. # -*- coding: utf-8 -*-
  2. import tensorflow as tf
  3. def resize(img_data, width, high, method=0):
  4. return tf.image.resize_images(img_data,[width, high], method)

pic2tfrecords.py

将图片保存成TFRecord

[python] view plain copy

  1. # -*- coding: utf-8 -*-
  2. # 将图片保存成 TFRecord
  3. import os.path
  4. import matplotlib.image as mpimg
  5. import tensorflow as tf
  6. import adjust_pic as ap
  7. from PIL import Image
  8. SAVE_PATH = ‘data/dataset.tfrecords‘
  9. def _int64_feature(value):
  10. return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
  11. def _bytes_feature(value):
  12. return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
  13. def load_data(datafile, width, high, method=0, save=False):
  14. train_list = open(datafile,‘r‘)
  15. # 准备一个 writer 用来写 TFRecord 文件
  16. writer = tf.python_io.TFRecordWriter(SAVE_PATH)
  17. with tf.Session() as sess:
  18. for line in train_list:
  19. # 获得图片的路径和类型
  20. tmp = line.strip().split(‘ ‘)
  21. img_path = tmp[0]
  22. label = int(tmp[1])
  23. # 读取图片
  24. image = tf.gfile.FastGFile(img_path, ‘r‘).read()
  25. # 解码图片(如果是 png 格式就使用 decode_png)
  26. image = tf.image.decode_jpeg(image)
  27. # 转换数据类型
  28. # 因为为了将图片数据能够保存到 TFRecord 结构体中,所以需要将其图片矩阵转换成 string,所以为了在使用时能够转换回来,这里确定下数据格式为 tf.float32
  29. image = tf.image.convert_image_dtype(image, dtype=tf.float32)
  30. # 既然都将图片保存成 TFRecord 了,那就先把图片转换成希望的大小吧
  31. image = ap.resize(image, width, high)
  32. # 执行 op: image
  33. image = sess.run(image)
  34. # 将其图片矩阵转换成 string
  35. image_raw = image.tostring()
  36. # 将数据整理成 TFRecord 需要的数据结构
  37. example = tf.train.Example(features=tf.train.Features(feature={
  38. ‘image_raw‘: _bytes_feature(image_raw),
  39. ‘label‘: _int64_feature(label),
  40. }))
  41. # 写 TFRecord
  42. writer.write(example.SerializeToString())
  43. writer.close()
  44. load_data(‘train_list.txt_bak‘, 224, 224)

tfrecords2data.py

从TFRecord中读取并保存成图片

[python] view plain copy

  1. # -*- coding: utf-8 -*-
  2. # 从 TFRecord 中读取并保存图片
  3. import tensorflow as tf
  4. import numpy as np
  5. SAVE_PATH = ‘data/dataset.tfrecords‘
  6. def load_data(width, high):
  7. reader = tf.TFRecordReader()
  8. filename_queue = tf.train.string_input_producer([SAVE_PATH])
  9. # 从 TFRecord 读取内容并保存到 serialized_example 中
  10. _, serialized_example = reader.read(filename_queue)
  11. # 读取 serialized_example 的格式
  12. features = tf.parse_single_example(
  13. serialized_example,
  14. features={
  15. ‘image_raw‘: tf.FixedLenFeature([], tf.string),
  16. ‘label‘: tf.FixedLenFeature([], tf.int64),
  17. })
  18. # 解析从 serialized_example 读取到的内容
  19. images = tf.decode_raw(features[‘image_raw‘], tf.uint8)
  20. labels = tf.cast(features[‘label‘], tf.int64)
  21. with tf.Session() as sess:
  22. # 启动多线程
  23. coord = tf.train.Coordinator()
  24. threads = tf.train.start_queue_runners(sess=sess, coord=coord)
  25. # 因为我这里只有 2 张图片,所以下面循环 2 次
  26. for i in range(2):
  27. # 获取一张图片和其对应的类型
  28. label, image = sess.run([labels, images])
  29. # 这里特别说明下:
  30. #   因为要想把图片保存成 TFRecord,那就必须先将图片矩阵转换成 string,即:
  31. #       pic2tfrecords.py 中 image_raw = image.tostring() 这行
  32. #   所以这里需要执行下面这行将 string 转换回来,否则会无法 reshape 成图片矩阵,请看下面的小例子:
  33. #       a = np.array([[1, 2], [3, 4]], dtype=np.int64) # 2*2 的矩阵
  34. #       b = a.tostring()
  35. #       # 下面这行的输出是 32,即: 2*2 之后还要再乘 8
  36. #       # 如果 tostring 之后的长度是 2*2=4 的话,那可以将 b 直接 reshape([2, 2]),但现在的长度是 2*2*8 = 32,所以无法直接 reshape
  37. #       # 同理如果你的图片是 500*500*3 的话,那 tostring() 之后的长度是 500*500*3 后再乘上一个数
  38. #       print len(b)
  39. #
  40. #   但在网上有很多提供的代码里都没有下面这一行,你们那真的能 reshape ?
  41. image = np.fromstring(image, dtype=np.float32)
  42. # reshape 成图片矩阵
  43. image = tf.reshape(image, [224, 224, 3])
  44. # 因为要保存图片,所以将其转换成 uint8
  45. image = tf.image.convert_image_dtype(image, dtype=tf.uint8)
  46. # 按照 jpeg 格式编码
  47. image = tf.image.encode_jpeg(image)
  48. # 保存图片
  49. with tf.gfile.GFile(‘pic_%d.jpg‘ % label, ‘wb‘) as f:
  50. f.write(sess.run(image))
  51. load_data(224, 224)

train_list.txt_bak 中的内容如下:

image_1093.jpg 13

image_0805.jpg 10

时间: 2024-08-28 21:06:47

TFRecord 的使用的相关文章

学习笔记TF016:CNN实现、数据集、TFRecord、加载图像、模型、训练、调试

AlexNet(Alex Krizhevsky,ILSVRC2012冠军)适合做图像分类.层自左向右.自上向下读取,关联层分为一组,高度.宽度减小,深度增加.深度增加减少网络计算量. 训练模型数据集 Stanford计算机视觉站点Stanford Dogs http://vision.stanford.edu/aditya86/ImageNetDogs/ .数据下载解压到模型代码同一路径imagenet-dogs目录下.包含的120种狗图像.80%训练,20%测试.产品模型需要预留原始数据交叉验

image和TFRecord互相转换

关说不练假把式.手上正好有车牌字符的数据集,想把他们写成TFRecord格式,然后读进来,构建一个简单的cnn训练看看.然后发现准确率只有0.0x.随机猜也比这要好点吧.只能一步步检查整个过程.暂时想到问题可能出现的地方: 数据编码解码错误 网络构建问题 学习步长问题 数据量太小 label设置 不确定是不是这些问题先检查下,tensorboard能给我们很多信息.今天先检查了图片解码编码问题.在读取数据的时候为image增加一个summary,这样就能在tensorboard上看到图片了. i

Tensorflow 处理libsvm格式数据生成TFRecord (parse libsvm data to TFRecord)

#写libsvm格式 数据 write libsvm ? ? #!/usr/bin/env python #coding=gbk # ============================================================================== # \file gen-records.py # \author chenghuige # \date 2016-08-12 11:52:01.952044 # \Description # ========

3 TFRecord样例程序实战

将图片数据写入Record文件 # 定义函数转化变量类型. def _int64_feature(value): return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) # 读取mnist数据. mnist = input

将图片数据转化为TFRecord格式与读取

将图片数据转化为TFRecord格式与读取 一.问题情景描述 目录下有一个叫做"Original"的文件夹,文件夹里有十个子文件,分别命名为1,2···一直到10(为了做10轮取平均),这10个子文件夹里还有四个子文件夹,分别命名为"train0","train1","test0","test1".其中含义如其命名所示.这四个子文件夹里一共有若干张JPG格式图像数据.现欲将这份图像数据转化为TFRecord

读取TFRecord文件报错

读取保存有多个样例的TFRecord文件时报错: InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 14410143 values, but the requested shape has 230400 [[Node: Reshape = Reshape[T=DT_UINT8, Tshape=DT_INT32, _device="/job:localhost/replica:0/ta

TFRecord文件的读写

前言在跑通了官网的mnist和cifar10数据之后,笔者尝试着制作自己的数据集,并保存,读入,显示. TensorFlow可以支持cifar10的数据格式, 也提供了标准的TFRecord 格式,而关于 tensorflow 读取数据, 官网提供了3中方法 1 Feeding: 在tensorflow程序运行的每一步, 用python代码在线提供数据 2 Reader : 在一个计算图(tf.graph)的开始前,将文件读入到流(queue)中 3 在声明tf.variable变量或numpy

TFRecord读写简介+Demo

简介: TFRecord是TensorFlow官方推荐使用的数据格式化存储工具. 它规范了数据的读写方式. 只要生成一次TFRecord,之后的数据读取和加工处理的效率都会得到提高. 参考了:https://blog.csdn.net/gg_18826075157/article/details/78449104 原文地址:https://www.cnblogs.com/xbit/p/10071848.html

目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练

将目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练. import xml.etree.ElementTree as ET import numpy as np import os import tensorflow as tf from PIL import Image classes = ["aeroplane", "bicycle", "bird", "boat", &quo