1. import numpy as np
  2. import tensorflow as tf
  3.  
  4. from tensorflow.examples.tutorials.mnist import input_data
  5.  
  6. # 定义函数转化变量类型。
  7. def _int64_feature(value):
  8. return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
  9.  
  10. def _bytes_feature(value):
  11. return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
  12.  
  13. # 读取mnist训练数据。
  14. mnist = input_data.read_data_sets("F:\\TensorFlowGoogle\\201806-github\\datasets\\MNIST_data\\",dtype=tf.uint8, one_hot=True)
  15. images = mnist.train.images
  16. labels = mnist.train.labels
  17. pixels = images.shape[1]#训练数据的图像分辨率,可作为Example的一个属性
  18. print(pixels)

  1. num_examples = mnist.train.num_examples
  2. print(type(num_examples))
  3. print(num_examples)#训练图片的张数

  1. print(type(images))
  2. print(images[0].shape)
  3. print(images.shape)

  1. print(type(labels))
  2. print(labels[0].shape)
  3. print(labels.shape)

  1. # 将数据转化为tf.train.Example格式。
  2. def _make_example(pixels, label, image):
  3. image_raw = image.tostring()
  4. example = tf.train.Example(features=tf.train.Features(feature={
  5. 'pixels': _int64_feature(pixels),
  6. 'label': _int64_feature(np.argmax(label)),
  7. 'image_raw': _bytes_feature(image_raw)
  8. }))
  9. return example
  10.  
  11. # 输出包含训练数据的TFRecord文件。
  12. with tf.compat.v1.python_io.TFRecordWriter("F:\\output.tfrecords") as writer:
  13. for index in range(num_examples):
  14. example = _make_example(pixels, labels[index], images[index])
  15. writer.write(example.SerializeToString())
  16. print("TFRecord训练文件已保存。")

吴裕雄--天生自然 pythonTensorFlow图形数据处理:将MNIST手写图片数据写入TFRecord文件的更多相关文章

  1. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:循环神经网络预测正弦函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 定义RNN的参数. HIDDEN_SIZE = ...

  2. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集高层操作

    import tempfile import tensorflow as tf # 1. 列举输入文件. # 输入数据生成的训练和测试数据. train_files = tf.train.match_ ...

  3. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集基本使用方法

    import tempfile import tensorflow as tf # 1. 从数组创建数据集. input_data = [1, 2, 3, 5, 8] dataset = tf.dat ...

  4. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:输入数据处理框架

    import tensorflow as tf # 1. 创建文件列表,通过文件列表创建输入文件队列 files = tf.train.match_filenames_once("F:\\o ...

  5. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:输入文件队列

    import tensorflow as tf # 1. 生成文件存储样例数据. def _int64_feature(value): return tf.train.Feature(int64_li ...

  6. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:多线程队列操作

    import tensorflow as tf #1. 定义队列及其操作. queue = tf.FIFOQueue(100,"float") enqueue_op = queue ...

  7. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:队列操作

    import tensorflow as tf #1. 创建队列,并操作里面的元素. q = tf.FIFOQueue(2, "int32") init = q.enqueue_m ...

  8. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:图像预处理完整样例

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #随机调整图片的色彩,定义两种顺序. def di ...

  9. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:TensorFlow图像处理函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #读取图片 image_raw_data = tf ...

随机推荐

  1. 数据结构顺序表中Sqlist *L,&L,Sqlist *&L

    //定义顺序表L的结构体 typedef struct { Elemtype data[MaxSize]: int length; }SqList; //建立顺序表 void CreateList(S ...

  2. java切换jdk版本

    目的:将jdk1.7切换为jdk1.6 1.原本安装了jdk1.7,环境变量也是配置的1.7相关路径,在cmd下输入[java -version]后,显示 [ C:\Users\Administrat ...

  3. 自学Java第五章——《面向对象基础》

    5.1 类与对象 1.类:一类具有相同特性的事物的抽象描述. 对象:类的一个个体,实例,具体的存在. 类是对象的设计模板. 2.如何声明类? [修饰符] class 类名{    成员列表:属性.方法 ...

  4. 洛谷-P2634 [国家集训队]聪聪可可 点分治

    Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...

  5. Day 12:枚举值、枚举类

    jdk1.5新特性之-----枚举 问题:某些方法所接收的数据必须是在固定范围之内的,  解决方案: 这时候我们的解决方案就是自定义一个类,然后是私有化构造函数,在自定义类中创建本类的对象对外使用. ...

  6. Sequence Models Week 3 Neural Machine Translation

    Neural Machine Translation Welcome to your first programming assignment for this week! You will buil ...

  7. GitHub练习——如何将本地已有项目添加到github

    刚开始开始接触,搞点简单的,看看是怎么把项目传上去,总结一下,大概是这些步骤: 创建本地仓库 将本地仓库变成git可管理的仓库:git init 把项目文件添加到缓存区:项目文件添加到已有的仓库,然后 ...

  8. 82.常用的返回QuerySet对象的方法使用详解:all,select_related

    1. all: 返回这个ORM模型的QuerySet对象. articles = Article.objects.all() print(articles) 2.select_related: 查找数 ...

  9. “帮你”校园资讯平台app使用体验

    该app由我的17级学长学姐编写而成,主要功能失物招领,二手市场,表白墙.该软件目前只是面向本校的各专业学生,为内测版本.该软件的注册流程简单,只需要学号确定身份后即可登陆,并且发布各种信息,或者与丢 ...

  10. Linux--计划任务未执行

    参考:http://blog.csdn.net/shangdiyisi/article/details/9477521 日志 /var/log/cron