由于6.5中提出的TFRecord非常复杂,可扩展性差,所以本节换一种方式

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np # 定义函数转化变量类型。
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("./datasets/MNIST_data",dtype=tf.uint8, one_hot=True)
images = mnist.train.images
labels = mnist.train.labels
pixels = images.shape[1] # 784
num_examples = mnist.train.num_examples # 60000 # 输出包含训练数据的TFRecord文件。
with tf.python_io.TFRecordWriter("./datasets/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("./datasets/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测试文件已保存。")

读取时注意使用了多线程

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np # 定义函数转化变量类型。
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("./datasets/MNIST_data",dtype=tf.uint8, one_hot=True)
images = mnist.train.images
labels = mnist.train.labels
pixels = images.shape[1] # 784
num_examples = mnist.train.num_examples # 60000 # 输出包含训练数据的TFRecord文件。
with tf.python_io.TFRecordWriter("./datasets/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("./datasets/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测试文件已保存。")

最新文章

  1. gbk与utf-8转换
  2. Windows Azure Redis 缓存服务
  3. HTTP 缓存控制总结
  4. 实现View弹性滑动例子
  5. c# 通过配置自动附加数据库
  6. USACO奶牛赛跑(逆序对)
  7. js常会问的问题:找出字符串中出现次数最多的字符。
  8. 关于JQ中,新生成的节点on绑定事件失效的解决
  9. JS判断
  10. 斯坦福大学公开课机器学习:advice for applying machine learning - deciding what to try next(设计机器学习系统时,怎样确定最适合、最正确的方法)
  11. Leetcode 1003. 检查替换后的词是否有效
  12. 二分查找法(binary_search,lower_bound,upper_bound,equal_range)
  13. [转帖] Linux运维基础知识学习内容
  14. Web服务器指纹识别工具httprint
  15. html 空白汉字占位符
  16. 4 Dockerfile指令详解 && COPY 指令
  17. BZOJ 4945 NOI2017 游戏 搜索+2-SAT
  18. linux防火墙放行了端口,但是仍然访问不到
  19. C# Winform 窗体传值 利用委托 子窗体传值给父窗体
  20. PHP中函数的定义与使用

热门文章

  1. pandas.DataFrame对象解析
  2. Django-model更上层楼
  3. 面试6 --- 当List<String> list =new ArrayList<String>(20); 他会扩容多少次
  4. Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE
  5. core文件问题
  6. RequestBody只能读取一次的问题
  7. 编译基于obs-studio的阿里巴巴直播工具tblive的过程和常见问题解决
  8. vue 全局挂载组件
  9. Fortify漏洞之 Privacy Violation(隐私泄露)和 Null Dereference(空指针异常)
  10. Swift面试题