TensorFlow图像数据处理

TensorFlow图像数据处理


作者大树先生
博客http://blog.csdn.net/koala_tree
GitHubhttps://github.com/KoalaTree
2017 年 09 月 18 日


–自《TensorFlow实战Google》

在卷积神经网络的训练过程中,图片的很多因素都会对训练数据产生很大的影响,进而影响神经网络模型的训练;同时也需要在。对图片进行预处理的过程中,减小预处理对于训练速度的影响。

一、TFRecord输入数据格式

TensorFlow读取数据,总共有三种方法:

  • 供给数据(Feeding):在TensorFlow程序运行的过程中,使用Python代码来供给数据;
  • 从文件读取数据:在TensorFlow图的起始,让一个输入管线从文件中读取数据;
  • 预加载数据:在TensorFlow图中定义常量或者变量来保存所有数据(仅适用于数据量比较小的情况)。

对于数据量较小的情况下,一般选择直接将数据加载进内存,然后再分batch输入网络进行训练。但是,如果数据量较大,这样的方法太耗内存,这时最好使用tensorflow提供的队列queue,也就是第二种方法从文件读取数据

1. 输入保存为TFRecord文件

TFRecords文件包含了tf.train.Example 协议内存块(protocol buffer)(协议内存块包含了字段 Features)。

过程:

  • 获取需要保存的数据;
  • 将数据填入到Example协议内存块(protocol buffer);
  • 将协议内存块序列化为一个字符串;
  • 通过tf.python_io.TFRecordWriter写入到TFRecords文件。

其中,一个Example中包含FeaturesFeatures里包含Feature的字典,Feature里包含有一个FloatListByteListInt64List

例程:

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数据。
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]
num_examples = mnist.train.num_examples

# 输出TFRecord文件的地址。
filename = "Records/output.tfrecords"
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文件已保存。"

2. 读取TFRecord文件

在生成了TFRecords文件后,为了高效地读取数据,在TensorFlow中使用队列(queue)读取数据。

过程:

  • 使用tf.train.string_input_producer获取tfrecords文件;
  • 使用tf.TFRecordReader对文件进行读取;
  • 利用tf.parse_single_example解析器对数据例进行解析;
  • 对feature进行解析;
  • 启动多线程处理输入数据

这个操作可以将Example协议内存块(protocol buffer)解析为张量。

例程:

# 读取文件。
reader = tf.TFRecordReader()
filename_queue = tf.train.string_input_producer(["Records/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])

二、图像预处理

1. 图像编码处理

彩色图片不是直接存储三维矩阵的数字,而是记录经过压缩编码后的结果,所以如果想要将一张图像还原成一个三维矩阵,需要解码的过程。

TensorFlow提供了对jpeg和png格式图像的编码/解码函数。

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

image_raw_data = tf.gfile.FastGFile("datasets/cat.jpg", 'r').read()

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw_data)

    # 输出解码之后的三维矩阵
    print(img_data.eval())

    # 打印图片
    plt.imshow(img_data.eval())
    plt.show()

这里写图片描述

图片转存

将一张图片的三维矩阵重新按照jpeg格式编码并存入指定的文件中。

with tf.Session() as sess:
    # 将图像数据重新编码存储
    encoded_image = tf.image.encode_jpeg(img_data)
    with tf.gfile.GFile("output/1restore_cat.jpg", "wb") as f:
        f.write(encoded_image.eval())

2. 图片大小调整

将载入的图片调整为制定的大小。

第一种:使用算法调整图片大小,并尽量保存原始图像上的所有信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值