Tensorflow--图像数据处理
2018-01-29 00:43
162 查看
一、TFRecord输入数据格式
Tensorflow提供了同一的数据格式TFRecord来存储数据,TFRecord文件中的数据都是通过tf.train.example Protocol Buffer的格式来存储的。其包含一个从属性名称到取值的字典,属性名称为字符串,取值可以为字符串(BytesList)、实数列表(FloatList)、或者是整数列表(Int64List)。以下为TFRecord的样例程序:
以上程序实现了将MNIST的所有数据存到一个TFRecord的文件里面。下面将介绍如何读取TFRecord文件:
Tensorflow提供了同一的数据格式TFRecord来存储数据,TFRecord文件中的数据都是通过tf.train.example Protocol Buffer的格式来存储的。其包含一个从属性名称到取值的字典,属性名称为字符串,取值可以为字符串(BytesList)、实数列表(FloatList)、或者是整数列表(Int64List)。以下为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])) mnist = input_data.read_data_sets("C:/path/to/MNIST_data",dtype=tf.uint8,one_hot=True) images = mnist.train.images #训练数据所对应的正确答案可以作为一个属性保存在TFRecord中 labels = mnist.train.labels #训练图像的分辨率还可以作为example的一个属性 pixels = images.shape[1] num_examples = mnist.train.num_examples #输出TFRecord文件的地址 filename = "path/to/output.tfrecords" #创建一个writer来写TFRecord文件 writer = tf.python_io.TFRecordWriter(filename) for index in range(num_examples): image_raw = images[index].tostring() example = tf.train.Example(features=tf.train.Features(feature={ 'pixels': _int64_feature(pixels), 'label': _int64_feature(np.argmax(labels[index])), 'image_raw': _bytes_feature(image_raw) })) writer.write(example.SerializeToString()) writer.close() print("TFRecord文件已保存。")
以上程序实现了将MNIST的所有数据存到一个TFRecord的文件里面。下面将介绍如何读取TFRecord文件:
import tensorflow as tf #创建一个reader来读取TFRecord中的样例 reader = tf.TFRecordReader() #创建一个队列来维护输入文件列表 #后续会更详细介绍tf.train.string_input_producer函数 filename_queue = tf.train.string_input_producer(["path/to/output.tfrecords"]) #从文件中读出一个样例。也可以用read_up_to函数一次性读取多个样例 _,serialized_example = reader.read(filename_queue) #解析读入的一个样例。如果需要解析多个样例,也可以用parse_example函数 features = tf.parse_single_example( serialized_example, features={ 'image_raw':tf.FixedLenFeature([],tf.string), 'pixels':tf.FixedLenFeature([],tf.int64), 'label':tf.FixedLenFeature([],tf.int64), }) #tf.decode_raw可以将字符串解析成图像对应的像素数组 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) #每次运行读取TFRecord文件的一个样例。当所有样例读完之后,在此样例中的程序会从头读取 for i in range(10): image,label,pixel = sess.run([images,labels,pixels]) print(image,'\n',label,'\n',pixel)
相关文章推荐
- tensorflow 图像数据处理(二)
- TensorFlow:实战Google深度学习框架(六)图像数据处理
- Tensorflow图像预处理,Numpy读取数据踩坑
- tensorflow 图像数据处理
- tensorflow 图像数据处理(一)
- 深度学习框架Tensorflow学习与应用 图像数据处理之一
- tensorflow处理自己的图像数据(不使用队列)
- TensorFlow图像数据处理
- 深度学习框架Tensorflow学习与应用 图像数据处理之二
- tensorflow图像数据处理
- MaxCompute与OSS非结构化数据读写互通(及图像处理实例)
- C/C++ BMP(24位真彩色)图像处理(1)------图像の打开与数据区处理
- Delphi图像处理 -- 获取窗口或设备的图像数据
- 利用tensorflow训练自己的图片数据(2)——输入图片处理
- 使用GDI+位图数据扫描线处理图像的小技巧 from http://blog.csdn.net/maozefa/article/details/4533770
- 图像处理中像素点的问题:double、long、unsigned、int、char类型数据所占字节数
- 使用Linux的V4L2读取摄像头数据+Opencv图像处理
- 数据图像处理程序代写
- 图像数据处理(3)
- 【TensorFlow】数据处理(对图像的处理)