本程序

(1)mnist的图片转换成TFrecords格式

(2) 读取TFrecords格式

  1. # coding:utf-8
  2. # 将MNIST输入数据转化为TFRecord的格式
  3. # http://blog.csdn.net/u014182497/article/details/74376224
  4.  
  5. import tensorflow as tf
  6. from tensorflow.examples.tutorials.mnist import input_data
  7. import numpy as np
  8. from PIL import Image
  9.  
  10. #把传入的value转化为整数型的属性,int64_list对应着 tf.train.Example 的定义
  11. def _int64_feature(value):
  12. return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
  13.  
  14. #把传入的value转化为字符串型的属性,bytes_list对应着 tf.train.Example 的定义
  15. def _bytes_feature(value):
  16. return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
  17.  
  18. def genmnsit_tfreords():
  19. #读取MNIST数据
  20. mnist = input_data.read_data_sets("./mnist_data", dtype=tf.uint8, one_hot=True)
  21. #训练数据的图像,可以作为一个属性来存储
  22. images = mnist.train.images
  23. #训练数据所对应的正确答案,可以作为一个属性来存储
  24. labels = mnist.train.labels
  25. #训练数据的图像分辨率,可以作为一个属性来存储
  26. pixels = images.shape[0]
  27. #训练数据的个数
  28. num_examples = mnist.train.num_examples
  29. #指定要写入TFRecord文件的地址
  30. filename = "./output.tfrecords"
  31. #创建一个write来写TFRecord文件
  32. writer = tf.python_io.TFRecordWriter(filename)
  33. for index in range(num_examples):
  34. #把图像矩阵转化为字符串
  35. image_raw = images[index].tostring()
  36. #将一个样例转化为Example Protocol Buffer,并将所有的信息写入这个数据结构
  37. example = tf.train.Example(features=tf.train.Features(feature={
  38. #'pixels': _int64_feature(pixels),
  39. 'label': _int64_feature(np.argmax(labels[index])),
  40. 'image_raw': _bytes_feature(image_raw)}))
  41. #将 Example 写入TFRecord文件
  42. writer.write(example.SerializeToString())
  43.  
  44. writer.close()
  45.  
  46. #读取TFRecord文件中的数据
  47. def read_tfrecords():
  48. #创建一个reader来读取TFRecord文件中的样例
  49. reader = tf.TFRecordReader()
  50. #通过 tf.train.string_input_producer 创建输入队列
  51. filename_queue = tf.train.string_input_producer(["./output.tfrecords"])
  52. #从文件中读取一个样例
  53. _, serialized_example = reader.read(filename_queue)
  54. #解析读入的一个样例
  55. features = tf.parse_single_example(
  56. serialized_example,
  57. features={
  58. #这里解析数据的格式需要和上面程序写入数据的格式一致
  59. 'image_raw': tf.FixedLenFeature([], tf.string),
  60. 'label': tf.FixedLenFeature([], tf.int64),
  61. })
  62. #tf.decode_raw可以将字符串解析成图像对应的像素数组
  63. images = tf.decode_raw(features['image_raw'], tf.uint8)
  64. images = tf.reshape(images, [28, 28, 1])
  65. #tf.cast可以将传入的数据转化为想要改成的数据类型
  66. labels = tf.cast(features['label'], tf.int32)
  67.  
  68. sess = tf.Session()
  69. #启动多线程处理输入数据
  70. coord = tf.train.Coordinator()
  71. threads = tf.train.start_queue_runners(sess=sess, coord=coord)
  72.  
  73. num_preprocess_threads = 1
  74. batch_size = 1
  75. min_queue_examples = 50
  76. images_batch, label_batch = tf.train.shuffle_batch(
  77. [images, labels],
  78. batch_size=batch_size,
  79. num_threads=num_preprocess_threads,
  80. capacity=min_queue_examples + 3 * batch_size,
  81. min_after_dequeue=min_queue_examples)
  82.  
  83. image = tf.reshape(images_batch, [28, 28])
  84. with tf.Session() as sess:
  85. init = tf.global_variables_initializer()
  86. sess.run(init)
  87. coord = tf.train.Coordinator()
  88. threads = tf.train.start_queue_runners(sess=sess, coord=coord)
  89. # 每次运行可以读取TFRecord文件中的一个样例。当所有样例都读完之后,再次样例中的程序会重头读取
  90. for i in range(5):
  91. data, label = sess.run([image, label_batch])
  92. result = Image.fromarray(data)
  93. result.save(str(i) + '.png')
  94. pass
  95. pass
  96. coord.request_stop()
  97. coord.join(threads)
  98. if __name__ == '__main__':
  99. genmnsit_tfreords()
  100. read_tfrecords()

tensorflow学习笔记(10) mnist格式数据转换为TFrecords的更多相关文章

  1. 深度学习-tensorflow学习笔记(2)-MNIST手写字体识别

    深度学习-tensorflow学习笔记(2)-MNIST手写字体识别超级详细版 这是tf入门的第一个例子.minst应该是内置的数据集. 前置知识在学习笔记(1)里面讲过了 这里直接上代码 # -*- ...

  2. 深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识

    深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识 在tf第一个例子的时候需要很多预备知识. tf基本知识 香农熵 交叉熵代价函数cross-entropy 卷积神经网络 s ...

  3. tensorflow学习笔记三:实例数据下载与读取

    一.mnist数据 深度学习的入门实例,一般就是mnist手写数字分类识别,因此我们应该先下载这个数据集. tensorflow提供一个input_data.py文件,专门用于下载mnist数据,我们 ...

  4. tensorflow学习笔记————分类MNIST数据集

    在使用tensorflow分类MNIST数据集中,最容易遇到的问题是下载MNIST样本的问题. 一般是通过使用tensorflow内置的函数进行下载和加载, from tensorflow.examp ...

  5. TensorFlow学习笔记(MNIST报错修正 适用Tensorflow1.3)

    在Tensorflow实战Google框架下的深度学习这本书的MNIST的图像识别例子中,每次都要报错   错误如下: Only call `sparse_softmax_cross_entropy_ ...

  6. tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)

    tensorflow学习笔记——使用TensorFlow操作MNIST数据(1) 一:神经网络知识点整理 1.1,多层:使用多层权重,例如多层全连接方式 以下定义了三个隐藏层的全连接方式的神经网络样例 ...

  7. tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)

    续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...

  8. Tensorflow学习笔记No.10

    多输出模型 使用函数式API构建多输出模型完成多标签分类任务. 数据集下载链接:https://pan.baidu.com/s/1JtKt7KCR2lEqAirjIXzvgg 提取码:2kbc 1.读 ...

  9. TensorFlow学习笔记——LeNet-5(训练自己的数据集)

    在之前的TensorFlow学习笔记——图像识别与卷积神经网络(链接:请点击我)中了解了一下经典的卷积神经网络模型LeNet模型.那其实之前学习了别人的代码实现了LeNet网络对MNIST数据集的训练 ...

随机推荐

  1. du和df命令的区别

    du和df命令都被用于获得文件系统大小的信息:df用于报告文件系统的总块数及剩余块数,du -s /<filesystem>用于报告文件系统使用的块数.但是,我们可以发现从df命令算出的文 ...

  2. LWIP移植

  3. winfrom 窗口起始位置为屏幕中央

    窗口起始位置为屏幕中央 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 获取鼠标触发事件光标位置 t ...

  4. mysql数据库以加索引方式提高性能

    数据库查询速率慢的情况下可以给对应的表加上对应的索引,能够有效的提高查询效率,mysql数据库添加索引的SQL入下: ALTER TABLE `table_name` ADD INDEX index_ ...

  5. Android开发7——android.database.CursorIndexOutOfBoundsException:Index -1 requested

    android中数据库处理使用cursor时,游标不是放在为0的下标,而是放在为-1的下标处开始的. 也就是说返回给cursor查询结果时,不能够马上从cursor中提取值. 下面的代码会返回错误Us ...

  6. Xilinx IP核的根目录地址,有datasheet 和仿真相关的资料

    C:\Xilinx\14.7\ISE_DS\ISE\coregen\ip\xilinx\dsp\com\xilinx\ip Xilinx IP核的根目录地址,有datasheet 和仿真相关的资料

  7. [na]win PPTP场景与搭建

    这也是在不成熟时期的一种对windows远程访问的好奇. 现在看来没啥用了. 现在一般都用linux的openvpn+gg方案了 远程访问方案: ,端口映射 ,vpn 实现这种远程访问的协议:pptp ...

  8. C#判断窗体是否存在重复打开

    foreach (Form f in Application.OpenForms) { f.Name //是打开窗体的Text //以下判断....... } Form2 F2 ; if(F2 == ...

  9. 在 Chrome 开发者工具中调试 node.js

    命令行工具 devtool ,它可以在 Chrome 的开发者工具中运行 Node.js 程序. 下面的记录显示了在一个 HTTP 服务器中设置断点的情况. 该工具基于 Electron 将 Node ...

  10. CCEaseElasticOut调整速度和振幅

    pSprite->setAnchorPoint(CCPoint(,)); pSprite->setPosition(CCPoint(,)); CCFiniteTimeAction* pAc ...