1.     利用TFRecord 格式   读、存 取    Mnist数据集的方法

存取   Mnist数据集的方法     (TFRecord格式

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np def _float32_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value])) 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])) mnist=input_data.read_data_sets('./data', dtype=tf.uint8, one_hot=True)
"""
print(mnist.train.images)
print(mnist.train.labels)
print(mnist.test.images)
print(mnist.test.labels)
"""
train_images=mnist.train.images
train_labels=mnist.train.labels
#test_images=mnist.test.images
#test_labels=mnist.test.labels train_num=mnist.train.num_examples
#test_num=mnist.test.num_examples pixels=train_images.shape[1] # 784 = 28*28 file_out='./data/output.tfrecords'
writer=tf.python_io.TFRecordWriter(file_out) for index in range(train_num):
image_raw=train_images[index].tostring() #转换为bytes序列 example=tf.train.Example(features=tf.train.Features(feature={
'pixels': _int64_feature(pixels),
'label':_int64_feature(np.argmax(train_labels[index])),
'x':_float32_feature(0.1),
'image_raw':_bytes_feature(image_raw)})) writer.write(example.SerializeToString())
writer.close()

读取   Mnist数据集的方法     (TFRecord格式

import tensorflow as tf

reader=tf.TFRecordReader()

files=tf.train.match_filenames_once('./data/output.*')

#filename_queue=tf.train.string_input_producer(['./data/output.tfrecords'])
filename_queue=tf.train.string_input_producer(files) _, 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),
'x':tf.FixedLenFeature([], tf.float32)
}) #print(features['image_raw']) # tensor string (bytes tensor string tensor) # necessary operation
# bytes_list to uint8_list
image=tf.decode_raw(features['image_raw'], tf.uint8)
#print(image) # tensor uint8 label=tf.cast(features['label'], tf.int32)
pixels=tf.cast(features['pixels'], tf.int32)
#image.set_shape([pixels**0.5, pixels**0.5])
image.set_shape([784]) batch_size=128
image_batch, label_batch, pixels_batch=tf.train.batch([image, label, pixels], batch_size=batch_size, capacity=1000+3*batch_size) coord=tf.train.Coordinator() with tf.Session() as sess:
sess.run(tf.local_variables_initializer())
threads=tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(3):
print(sess.run([image_batch, label_batch, pixels_batch])) coord.request_stop()
coord.join(threads)

==================================================================

2.     利用TFRecord 格式    存取   文件夹内图片的方法

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np def _float32_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value])) 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])) file_out='./data/output1.tfrecords'
writer=tf.python_io.TFRecordWriter(file_out) files = tf.gfile.Glob('./data/*.jpg')
sess=tf.Session()
for file in files:
image_raw_data = tf.gfile.FastGFile(file,'rb').read() img_data = tf.image.decode_jpeg(image_raw_data) # tensor
img_data = sess.run(img_data) # np.array int
resized = img_data.tostring() # np.array string uint8 example=tf.train.Example(features=tf.train.Features(feature={
'y':_int64_feature(1),
'x':_float32_feature(0.1),
'image_raw':_bytes_feature(resized)})) writer.write(example.SerializeToString())
writer.close()

文件读取过程使用   输入队列 :

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np def _float32_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value])) def _int64_feature(value):
#value类型应为:int,long,float
#return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) #value类型应为:[int],[long],[float], 这里为int的list类型
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])) file_out='./data/output2.tfrecords'
writer=tf.python_io.TFRecordWriter(file_out) files = tf.train.match_filenames_once('./data/*.jpg') # string_input_producer会产生一个文件名队列
filename_queue = tf.train.string_input_producer(files, shuffle=False, num_epochs=3)
# reader从文件名队列中读数据。对应的方法是reader.read
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue) img_data = tf.image.decode_jpeg(value) # np.array 转换为 tensor
#print(sess.run([key, img_data]))
#print(img_data.get_shape())
img_data.set_shape([None, None, 3]) img_float = tf.image.convert_image_dtype(img_data, tf.float32)
img_float = tf.image.resize_images(img_float, [300, 300], method=0) with tf.Session() as sess:
# tf.train.string_input_producer定义了一个epoch变量,要对它进行初始化
tf.local_variables_initializer().run() x=np.array([[1,1,1,1],[1,1,1,1]]) coord = tf.train.Coordinator() # 使用start_queue_runners之后,才会开始填充队列
threads = tf.train.start_queue_runners(sess=sess, coord=coord) for _ in range(3):
resized=sess.run(img_data)
resized = resized.tostring() example=tf.train.Example(features=tf.train.Features(feature={
'x':_int64_feature(x.reshape(x.size).tolist()),
'x2':_int64_feature([1,1]),
'y':_float32_feature(0.1),
'image_raw':_bytes_feature(resized)})) writer.write(example.SerializeToString())
writer.close() coord.request_stop()
coord.join(threads)

tensorflow 读、存取 图像 数据的 TFRecord 方法 (示例)的更多相关文章

  1. TensorFlow中读取图像数据的三种方式

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

  2. Tensorflow 处理libsvm格式数据生成TFRecord (parse libsvm data to TFRecord)

    #写libsvm格式 数据 write libsvm     #!/usr/bin/env python #coding=gbk # ================================= ...

  3. Oracle在本地调试成功读取数据,但是把代码放到服务器读不出数据的解决方法。

    用MVC EF框架开发项目,数据库用的是Oracle,本地调试的时候一切正常,但是把代码编译之后放到服务器就会读不出数据. 原因:本地调试环境与服务器环境不一致. 办法:在服务器上装ODT.NET组件 ...

  4. 快速遍历OpenCV Mat图像数据的多种方法和性能分析 | opencv mat for loop

    本文首发于个人博客https://kezunlin.me/post/61d55ab4/,欢迎阅读! opencv mat for loop Series Part 1: compile opencv ...

  5. Delphi存取图像完整解决方案

    http://blog.sina.com.cn/s/blog_693cf1cf0100plkq.html 对于涉及图像数据的数据库应用程序,图像数据的存取技术是一个关键.由于缺少技术文档及DEMO例程 ...

  6. Inter IPP 处理图像数据的方法

    Inter IPP没有读取图片和保存图片的函数,需要结合opencv完成这个功能. opencv读到图片以后逐个像素点赋值给IPP显然是不可取的,方法如下: int main(int argc, ch ...

  7. vc/mfc获取rgb图像数据后动态显示及保存图片的方法

    vc/mfc获取rgb图像数据后动态显示及保存图片的方法 该情况可用于视频通信中获取的位图数据回放显示或显示摄像头捕获的本地图像 第一种方法 #include<vfw.h> 加载 vfw3 ...

  8. [转]MFC子线程更改图像数据后更新主窗口图像显示方法

    程序思路是由外部的输入输出控制卡发出采集图像信号,之后相机采集图像得到图像数据指针,接收图像数据指针创建成图像最后显示到MFC对话框应用程序的Picture Control控件上,同时,为了标定相机位 ...

  9. (转)原始图像数据和PDF中的图像数据

    比较原始图像数据和PDF中的图像数据,结果见表1.1.表1.1中各种“解码器”的解释见本文后续的“PDF支持的图像格式”部分,“PDF中的图像数据”各栏中的数据来自开源的PdfView.如果您有兴趣查 ...

  10. 基于FPGA的Uart接收图像数据至VGA显示

    系统框图 前面我们设计了基于FPGA的静态图片显示,接下来我们来做做基于FPGA的动态图片显示,本实验内容为:由PC端上位机软件通过串口发送一幅图像数据至FPGA,FPGA内部将图像数据存储,最后扫描 ...

随机推荐

  1. kettle从入门到精通 第五十二课 ETL之kettle Avro output

    1.上一节课我们学习了avro input,本节课我们一起学习下avro out步骤. 本节课通过json input 加载json文件,通过avro out 生成avro二进制文件,写日志步骤打印日 ...

  2. Winform绘制圆形图

    1 private void Form_Load(object sender, EventArgs e) 2 { 3 PictureBox pic = new PictureBox(); 4 pic. ...

  3. 怎么实现鼠标移入第i个li则对应显示第i个div,默认显示第一个LI

    html 部分 <ul> <li>菜单1</li> <li>菜单2</li> <li>菜单3</li> <li ...

  4. undefined,null 和 undeclared 有什么区别?

    a.null表示"没有对象",即该处不应该有值,转为数值时为0.典型用法是:作为函数的参数,表示该函数的参数不是对象.作       为对象原型链的终点. b.undefined表 ...

  5. Kafka多维度调优

    优化金字塔 应用程序层面 框架层面(Broker层面) JVM层面 操作系统层面 应用程序层面:应当优化业务代码合理使用kafka,合理规划主题,合理规划分区,合理设计数据结构: 框架层面:在不改动源 ...

  6. java.sql.SQLException: Connection is read-only. Queries leading to data modification are not

    java.sql.SQLException: Connection is read-only. Queries leading to data modification are not 产生的原因:事 ...

  7. Excel poi 设置单元格格式 发现不可读内容 已修复的记录: /xl/worksheets/sheet1.xml 部分的问题(巨坑)

    Excel poi 设置单元格格式 发现不可读内容 已修复的记录: /xl/worksheets/sheet1.xml 部分的问题(巨坑) 1.先设置值,后设置样式. 正确的是:先设置样式,后设置值. ...

  8. 【解决方案】Java 互联网项目中消息通知系统的设计与实现(上)

    目录 前言 一.需求分析 1.1发送通知 1.2撤回通知 1.3通知消息数 1.4通知消息列表 二.数据模型设计 2.1概念模型 2.2逻辑模型 三.关键流程设计 本篇小结 前言 消息通知系统(not ...

  9. 我又学会了使用Range实现网络文件下载的断点续传

    目录 前言 1.Range请求头 1.1.概述 1.2.使用限制 1.3.范围请求 1.4.预防资源变更 2.断点续传下载实现 2.1.流程设计 2.2.代码实现 2.3.运行结果 3.RandomA ...

  10. Bike Sharing Analysis(二)- 假设检验方法

    假设检验 假设检验是推论统计学(inferential statistics)的一个分支,也就是对一个较小的.有代表性的数据组(例如样本集合)进行分析与评估,并依此推断出一个大型的数据组(例如人口)的 ...