这篇文章主要讲一下如何用Tensorflow中的标准数据读取方式简单的实现对自己数据的读取操作.

主要分为以下两个步骤:(1)将自己的数据集转化为 xx.tfrecords的形式;(2):在自己的程序中读取并使用.tfrecords进行操作.

数据集转换:为了便于讲解,我们简单制作了一个数据,如下图所示:

程序:

[python] view plaincopy
  1. import tensorflow as tf
  2. import numpy as np
  3. import os
  4. from PIL import Image
  5. def _int64_feature(value):
  6. return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
  7. def _bytes_feature(value):
  8. return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
  9. def img_to_tfrecord(data_path):
  10. rows = 256
  11. cols = 256
  12. depth = 3
  13. writer = tf.python_io.TFRecordWriter('test.tfrecords')
  14. labelfile=open("random.txt")
  15. lines=labelfile.readlines()
  16. for line in lines:
  17. #print line
  18. img_name = line.split(" ")[0]#name
  19. label = line.split(" ")[1]#label
  20. img_path = data_path+img_name
  21. img = Image.open(img_path)
  22. img = img.resize((rows,cols))
  23. #img_raw = img.tostring()
  24. img_raw = img.tobytes()
  25. example = tf.train.Example(features = tf.train.Features(feature = {
  26. 'height': _int64_feature(rows),
  27. 'weight': _int64_feature(cols),
  28. 'depth': _int64_feature(depth),
  29. 'image_raw': _bytes_feature(img_raw),
  30. 'label': _bytes_feature(label)}))
  31. writer.write(example.SerializeToString())
  32. writer.close()
  33. if __name__ == '__main__':
  34. current_dir = os.getcwd()
  35. data_path = current_dir + '/data/'
  36. #name = current_dir + '/data'
  37. print('Convert start')
  38. img_to_tfrecord(data_path)
  39. print('done!')
运行该段程序可以看到在dataset_tfrecord文件夹下面有test.tfrecord文件生成。
在TF的Session中调用这个生成的文件
[python] view plaincopy
  1. #encoding=utf-8
  2. # 设置utf-8编码,方便在程序中加入中文注释.
  3. import os
  4. import scipy.misc
  5. import tensorflow as tf
  6. import numpy as np
  7. from test import *
  8. import matplotlib.pyplot as plt
  9. def read_and_decode(filename_queue):
  10. reader = tf.TFRecordReader()
  11. _, serialized_example = reader.read(filename_queue)
  12. features = tf.parse_single_example(serialized_example,features = {
  13. 'image_raw':tf.FixedLenFeature([], tf.string)})
  14. image = tf.decode_raw(features['image_raw'], tf.uint8)
  15. image = tf.reshape(image, [OUTPUT_SIZE, OUTPUT_SIZE, 3])
  16. image = tf.cast(image, tf.float32)
  17. #image = image / 255.0
  18. return image
  19. data_dir = '/home/sanyuan/dataset_animal/dataset_tfrecords/'
  20. filenames = [os.path.join(data_dir,'train%d.tfrecords' % ii) for ii in range(1)] #如果有多个文件,直接更改这里即可
  21. filename_queue = tf.train.string_input_producer(filenames)
  22. image = read_and_decode(filename_queue)
  23. with tf.Session() as sess:
  24. coord = tf.train.Coordinator()
  25. threads = tf.train.start_queue_runners(coord=coord)
  26. for i in xrange(2):
  27. img = sess.run([image])
  28. print(img[0].shape)  # 设置batch_size等于1.每次读出来只有一张图
  29. plt.imshow(img[0])
  30. plt.show()
  31. coord.request_stop()
  32. coord.join(threads)
程序到这里就已经处理完成了,当然在decorde的过程中也是可以进行一些预处理操作的,不过建议还是在制作数据集的时候进行,TFrecord使用的是队列的方式进行读取数据,这个对于多线程操作来说还是很方便的,只需要设置好格式,每次直接读取就可以了.

tensorflow中tfrecords使用介绍相关推荐

  1. TensorFlow 中文文档 介绍

    介绍 本章的目的是让你了解和运行 TensorFlow 在开始之前, 先看一段使用 Python API 撰写的 TensorFlow 示例代码, 对将要学习的内容有初步的印象. 这段很短的 Pyth ...

  2. tensorflow中的命令行参数介绍

    1.tensorflow中的tf.flags参数介绍 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-01-20 21:39: ...

  3. TF:tensorflow框架中常用函数介绍—tf.Variable()和tf.get_variable()用法及其区别

    TF:tensorflow框架中常用函数介绍-tf.Variable()和tf.get_variable()用法及其区别 目录 tensorflow框架 tensorflow.Variable()函数 ...

  4. TensorFlow Hub介绍:TensorFlow中可重用的机器学习模块库

    摘要: 本文对TensorFlow Hub库的介绍,并举例说明其用法. 在软件开发中,最常见的失误就是容易忽视共享代码库,而库则能够使软件开发具有更高的效率.从某种意义上来说,它改变了编程的过程.我们 ...

  5. python读取图像数据流_浅谈TensorFlow中读取图像数据的三种方式

    本文面对三种常常遇到的情况,总结三种读取数据的方式,分别用于处理单张图片.大量图片,和TFRecorder读取方式.并且还补充了功能相近的tf函数. 1.处理单张图片 我们训练完模型之后,常常要用图片 ...

  6. tensorflow中tfrecord数据操作

    前言: 为了更加展示tfrecord数据的相关操作,笔者后续又写了一个实践的简单例子进一步解释,具体可以看: TFrecords 制作数据集小例子(多标签)_爱吃火锅的博客-CSDN博客 正文: tf ...

  7. TensorFlow中的计算图

    作者 | stephenDC 来源 | 大数据与人工智能(ID:ai-big-data) 1 什么是计算图? 一个机器学习任务的核心是模型的定义以及模型的参数求解方式,对这两者进行抽象之后,可以确定一 ...

  8. 如何使用TensorFlow中的Dataset API

    翻译 | AI科技大本营 参与 | zzq 审校 | reason_W 本文已更新至TensorFlow1.5版本 我们知道,在TensorFlow中可以使用feed-dict的方式输入数据信息,但是 ...

  9. TensorFlow中设置学习率的方式

    目录 1. 指数衰减 2. 分段常数衰减 3. 自然指数衰减 4. 多项式衰减 5. 倒数衰减 6. 余弦衰减 6.1 标准余弦衰减 6.2 重启余弦衰减 6.3 线性余弦噪声 6.4 噪声余弦衰减 ...

  10. 中tile函数_HelpGirlFriend 系列 --- tensorflow 中的张量运算思想

    GirlFriend 在复现论文的时候,我发现她不太会将通用数学公式转化为张量运算公式,导致 tensorflow 无法通过并行的方式优化其论文复现代码的运行速率. 这里对给 GirlFriend 讲 ...

最新文章

  1. SVO中 Inverse Compositional Image Alignment方法的学习笔记
  2. 自定义html页面鼠标右键,javascript鼠标右键菜单自定义效果
  3. visionmaster视觉软件说明书_测试策略与软件需求层次
  4. VC中实现弹出CEdit的气泡提示框
  5. 受大厂们青睐的Web前端工程师需要掌握的3项能力!
  6. 有三角形的即时通讯源码?
  7. 扫地机器人石头爬坡_都这么强了,还要怎么升级:真实评测石头T6扫地机器人...
  8. 拓端tecdat|用Python粒度分析及其在沉积学中应用研究
  9. html微信怎么转发,微信朋友圈怎么转发别人的文章(链接、视频、图片、文字)
  10. 基于随机森林实现特征选择降维及回归预测(Matlab代码实现)
  11. TestNG教程三:TestNG中的监听
  12. spring mvc +maven 集成 quartz实现定时任务
  13. ocpc php,oCPC匹配词很乱怎么办?| SEM问答
  14. 前端效果 -- 实现折叠、展开动画效果
  15. 剑指Offer题目详解(CPP、JAVA)
  16. 主板电容损坏导致台式机开机风扇转无显示信号输出
  17. 最近看中的几款Limitless的家具
  18. python学习笔记:python类和对象,文件操作,网络编程
  19. 阿里副总裁人设“翻车”:30 岁成 AI 顶尖科学家,但我很懒
  20. 上海交通大学python实验二_20193207 实验二《Python程序设计》实验报告

热门文章

  1. Remap 后的 USART1 不能发送数据
  2. java7-3 继承
  3. 【转】“正由另一进程使用,因此该进程无法访问该文件”的问题解决方法
  4. 关于System.identityHashCode(obj) 与 obj.hashcode()
  5. .NET 应用从 Visual Studio 迁移到 Eclipse
  6. Java中的for循环和JavaScript中的for循环差别初探(02)
  7. 深入理解Yii2.0 (3)行为(Behavior)
  8. SAX方式解析XML文档
  9. 47. Use traits for information about types.
  10. 02. 实现Singleton模式(C++版本)