import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data # 定义函数转化变量类型。
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])) # 将数据转化为tf.train.Example格式。
def _make_example(pixels, label, image):
image_raw = image.tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'pixels': _int64_feature(pixels),
'label': _int64_feature(np.argmax(label)),
'image_raw': _bytes_feature(image_raw)
}))
return example # 读取mnist训练数据。
mnist = input_data.read_data_sets("E:\\MNIST_data\\",dtype=tf.uint8, one_hot=True)
images = mnist.train.images
labels = mnist.train.labels
pixels = images.shape[1]
num_examples = mnist.train.num_examples # 输出包含训练数据的TFRecord文件。
with tf.python_io.TFRecordWriter("E:\\MNIST_data\\output.tfrecords") as writer:
for index in range(num_examples):
example = _make_example(pixels, labels[index], images[index])
writer.write(example.SerializeToString())
print("TFRecord训练文件已保存。") # 读取mnist测试数据。
images_test = mnist.test.images
labels_test = mnist.test.labels
pixels_test = images_test.shape[1]
num_examples_test = mnist.test.num_examples # 输出包含测试数据的TFRecord文件。
with tf.python_io.TFRecordWriter("E:\\MNIST_data\\output_test.tfrecords") as writer:
for index in range(num_examples_test):
example = _make_example(pixels_test, labels_test[index], images_test[index])
writer.write(example.SerializeToString())
print("TFRecord测试文件已保存。")

# 读取文件。
reader = tf.TFRecordReader()
filename_queue = tf.train.string_input_producer(["E:\\MNIST_data\\output.tfrecords"])
_,serialized_example = reader.read(filename_queue) # 解析读取的样例。
features = tf.parse_single_example(
serialized_example,
features={
'image_raw':tf.FixedLenFeature([],tf.string),
'pixels':tf.FixedLenFeature([],tf.int64),
'label':tf.FixedLenFeature([],tf.int64)
}) images = tf.decode_raw(features['image_raw'],tf.uint8)
labels = tf.cast(features['label'],tf.int32)
pixels = tf.cast(features['pixels'],tf.int32) sess = tf.Session() # 启动多线程处理输入数据。
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coord) for i in range(10):
image, label, pixel = sess.run([images, labels, pixels])

吴裕雄 python 神经网络——TensorFlow TFRecord样例程序的更多相关文章

  1. 吴裕雄 python 神经网络TensorFlow实现LeNet模型处理手写数字识别MNIST数据集

    import tensorflow as tf tf.reset_default_graph() # 配置神经网络的参数 INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE ...

  2. 吴裕雄 python 神经网络——TensorFlow 输入数据处理框架

    import tensorflow as tf files = tf.train.match_filenames_once("E:\\MNIST_data\\output.tfrecords ...

  3. 吴裕雄 python 神经网络——TensorFlow 输入文件队列

    import tensorflow as tf def _int64_feature(value): return tf.train.Feature(int64_list=tf.train.Int64 ...

  4. 吴裕雄 python 神经网络——TensorFlow 图像预处理完整样例

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def distort_color(image, ...

  5. 吴裕雄 python 神经网络——TensorFlow 完整神经网络样例程序

    import tensorflow as tf from numpy.random import RandomState batch_size = 8 w1= tf.Variable(tf.rando ...

  6. 吴裕雄 python 神经网络——TensorFlow variables_to_restore函数的使用样例

    import tensorflow as tf v = tf.Variable(0, dtype=tf.float32, name="v") ema = tf.train.Expo ...

  7. 吴裕雄 python 神经网络——TensorFlow训练神经网络:卷积层、池化层样例

    import numpy as np import tensorflow as tf M = np.array([ [[1],[-1],[0]], [[-1],[2],[1]], [[0],[2],[ ...

  8. 吴裕雄 python 神经网络——TensorFlow 数据集高层操作

    import tempfile import tensorflow as tf train_files = tf.train.match_filenames_once("E:\\output ...

  9. 吴裕雄 python 神经网络——TensorFlow 数据集基本使用方法

    import tempfile import tensorflow as tf input_data = [1, 2, 3, 5, 8] dataset = tf.data.Dataset.from_ ...

随机推荐

  1. Mongo查询list数组中一个字段大于特定条数

    由于刚刚接触mongodb,很多语法还不是很了解,写的不好的地方请大佬指出 查询的demo数据 { "_id":Object("xxxxxxxx"), &quo ...

  2. 素问 - IC跨期套利

    摘自<小韭的学习圈> Q 我现在是长持ic.我观察到IC1907和IC1909的贴水差会有波动.有时候,IC 1907涨的多,有时候IC1909涨的多.而在某一天这个趋势相对是稳定的. 那 ...

  3. 安装proxmox VE (PVE 安装教程)

    1. 下载PVE, 笔者下载的版本是proxmox-ve_6.1-1.iso 版本,官网地址:https://www.proxmox.com/ 2. mac电脑dd命令刻录安装镜像 MacBook-P ...

  4. codeforces 1245D(最小生成树)

    题面链接:https://codeforces.com/problemset/problem/1245/D 题意大概是给你一些城市的坐标,可以在城市中建立发电站,也可以让某个城市和已经建好发电站的城市 ...

  5. 左偏树(p1456) 比较模板的一道题

    题意:有n只猴子,m个操作,每一个操作,会让这两堆猴子里的最大的两只打架,打完之后,自身权值减半,然后他们会成为朋友 也就是会属于同一棵树,细节:如果选出的猴子在同一堆,就输出-1,然后下一个操作,不 ...

  6. windows 动态库导出

    以下内容来自博客:https://blog.csdn.net/fengbingchun/article/details/78825004 __declspec是Microsoft VC中专用的关键字, ...

  7. 【音乐欣赏】《JINGO JUNGLE》 - MYTH & ROID

    歌名:JINGO JUNGLE 作者:MYTH & ROID [00:19.82]Don’t go away 逃げ場はない [00:27.12]Oh why? You’re crying 嗤え ...

  8. Python面向对象基础语法

    目标 dir 内置函数 定义简单的类(只包含方法) 方法中的 self 参数 初始化方法 内置方法和属性 01. dir 内置函数(知道) 在 Python 中 对象几乎是无所不在的,我们之前学习的  ...

  9. jmeter-BeanShell PreProcessor的使用

    BeanShell简介 BeanShell是一个小型嵌入式Java源代码解释器,具有对象脚本语言特性,能够动态地执行标准JAVA语法.在BeanShell中,我们可以使用java语言自定义函数来处理特 ...

  10. win api 音频可视化

    暂时记录,改天有时间再完善...其实写好好久了,但以前的代码丢了,重新写一遍.. 原理和 python 的一样,获取输入设备,然后把数据读取到 buffer 中,在绘制出来. 这里要注意两点: 1. ...