tensorflow(一):图片处理
一、图片处理
  1、图片存取 tf.gfile
复制代码

import tensorflow as tf
 import matplotlib.pyplot as pltimage_bytes = tf.gfile.FastGFile(“dog.jpg”, ‘rb’).read() # 字节
 with tf.Session() as session:
 # 2.图片解码
 img = tf.image.decode_jpeg(image_bytes)
 # print(img) # tensor(‘DecodePnng:0’, shape=(?,?,?),dtype=uint8)
 img_array = img.eval() # 将tensor对象转成数组
 # 3.图片显示
 plt.imshow(img_array)
 plt.show()
 # 4.图片数据类型转化(整形)
 # img = tf.image.convert_image_dtype(img, dtype=tf.float32)
 # print(img)
 # 5.图像重编码
 encode_image = tf.image.encode_jpeg(img)
 new_img = encode_image.eval() # 数组
 # 6.图片保存
 with tf.gfile.GFile(“dog_new.png”, “wb”) as f:
 f.write(new_img)


复制代码
  2、图片修改 tf.image
复制代码

import tensorflow as tf
 import matplotlib.pyplot as pltimage_bytes = tf.gfile.FastGFile(“dog.jpg”, ‘rb’).read() # 字节
 with tf.Session() as session:
 img = tf.image.decode_jpeg(image_bytes)
 # 翻转图片
 img_flipped = tf.image.flip_up_down(img) # 上下反转
 img_flipped = tf.image.flip_left_right(img_flipped) # 左右反转
 img_flipped = tf.image.transpose_image(img_flipped) # 对角线反转
 img_flipped = tf.image.random_flip_up_down(img_flipped) # 随机上下反转
 img_flipped = tf.image.random_flip_left_right(img_flipped) # 随机左右反转
 # 亮度设置
 img_adjust = tf.image.adjust_brightness(img_flipped, -0.5) # 增加亮度
 img_adjust = tf.image.adjust_brightness(img_adjust, +0.5) # 降低亮度
 img_adjust = tf.image.random_brightness(img_adjust, max_delta=0.3) # 随机调整亮度,亮度在[-max_delta, +max_delta]]
 # 色度
 img_saturation = tf.image.adjust_saturation(img_adjust, 1.5) # 支持random
    # 饱和度
 img_hue = tf.image.adjust_hue(img_saturation, delta=0.2)
    # 对比度
 img_contrast = tf.image.adjust_contrast(img_hue, 0.5)
 # 图片标准化
 img_standard = tf.image.per_image_standardization(img_adjust)
 img_standard = tf.clip_by_value(img_standard, 0.0, 10)
 # 转成数组
 img_array = img_standard.eval()
 plt.imshow(img_array)
 plt.show()


复制代码
  3、图像标注框
复制代码

import tensorflow as tf
 import matplotlib.pyplot as pltimage_bytes = tf.gfile.FastGFile(“dog.jpg”, ‘rb’).read() # 字节
 with tf.Session() as session:
 img = tf.image.decode_jpeg(image_bytes)
 # 调整图片大小
 img_resize = tf.image.resize_image_with_crop_or_pad(img, 300, 300)
 # 按比例截取图片
 boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38], [0.38, 0.53, 0.53, 0.71]]]) # 两个标注框
 # boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38]]]) # 设置一个RGB,设置四个角的比例位置
 # 给原始图片添加一个图层
 batched = tf.expand_dims(tf.image.convert_image_dtype(img_resize, tf.float32), 0)
 # 把boxes标注的框画到原始图片上
 image_with_boxes = tf.image.draw_bounding_boxes(batched, boxes)
 # 重新将原始图片设置为RGB
 image_with_boxes = tf.reshape(image_with_boxes, [300, 300, 3])
 img_array = image_with_boxes.eval()
 plt.imshow(img_array)
 plt.show()


复制代码
  4、图片随机截取
复制代码

import matplotlib.pyplot as plt
image_bytes = tf.gfile.FastGFile(“dog.jpg”, ‘rb’).read() # 字节
 with tf.Session() as session:
 img = tf.image.decode_jpeg(image_bytes)
 # 给定截取框大小
 bounding_boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38]]]) # 设置一个RGB,设置四个角的比例位置
 # 选择相关图像截取算法截图
 # Bounding boxes are supplied and returned as [y_min, x_min, y_max, x_max].
 begin, size, bboxes = tf.image.sample_distorted_bounding_box(
 tf.shape(img), bounding_boxes=bounding_boxes, min_object_covered=0.1
 )
 # 生成概要
 # img_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(tf.image.convert_image_dtype(img, dtype=tf.float32), 0), bboxes)
 # tf.summary.image(‘img_with_box’, img_with_box)
 # print(begin.eval(), size.eval())
 # 截图
 distorted_img = tf.slice(img, begin, size)
 img_array = distorted_img.eval()
 plt.imshow(img_array)
 plt.show()


复制代码
  5、一个简单样例代码,实现随机截取图片
复制代码

import tensorflow as tf
 import numpy as np
 import matplotlib.pyplot as pltclass Sample:
 def load_jpg(self, path, mode=‘rb’):
 image_bytes = tf.gfile.FastGFile(path, mode).read()
 return tf.image.decode_jpeg(image_bytes, channels=3)
 def _distort_picture(self, image, color_ordering=0):
 if color_ordering == 0:
 image = tf.image.random_brightness(image, max_delta=32./255.) # 随机亮度
 image = tf.image.random_contrast(image, lower=0.5, upper=1.5) # 对比度
 image = tf.image.random_hue(image, max_delta=0.2) # 饱和度
 image = tf.image.random_saturation(image, lower=0.5, upper=1.5)# 色度
 if color_ordering == 1:
 image = tf.image.random_hue(image, max_delta=0.2) # 饱和度
 image = tf.image.random_saturation(image, lower=0.5, upper=1.5)# 色度
 image = tf.image.random_flip_left_right(image)
 image = tf.image.random_flip_up_down(image)
 return tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=1.0) # 归一化
 def _preprocess_for_train(self, image, height, width, bounding_boxes=None):
 if bounding_boxes is None:
 bounding_boxes = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
 if image.dtype != tf.float32:
 image = tf.image.convert_image_dtype(image, dtype=tf.float32)
 begin, size, bboxes = tf.image.sample_distorted_bounding_box(
 tf.shape(image), bounding_boxes=bounding_boxes, min_object_covered=0.1
 )
 # 随机截图
 distorted_image = tf.slice(image, begin=begin, size=size)
 # 调整随机截图的图片大小
 # distorted_image = tf.image.resize_image_with_crop_or_pad(distorted_image, height, width)
 distorted_image = tf.image.resize_images(
 distorted_image, size=[height, width], method=np.random.randint(4)
 )
 # 随机调整图片的一些设置
 distorted_image = self._distort_picture(distorted_image, np.random.randint(2))
 return distorted_image
 def get_random_picture(self, number, image, *args, **kwargs):
 with tf.Session() as session:
 for i in range(number):
 random_picture = self._preprocess_for_train(image, *args, **kwargs)
 plt.imshow(random_picture.eval())
 plt.show()def main():
 sample = Sample()
 image = sample.load_jpg(“dog.jpg”, ‘rb’)

bounding_boxes = tf.constant([0.2, 0.2, 0.8, 0.8], dtype=tf.float32, shape=[1, 1, 4])

bounding_boxes = tf.constant([[[0.2, 0.2, 0.8, 0.8]]])
height = width = 150
sample.get_random_picture(5, image, height, width, bounding_boxes)

main()
复制代码
  5、图片处理有关函数整理
函数 描述

tf.gfile.FastGFile 读取单个图片,返回字节流数据
 tf.decode_jpeg 在图片读入操作之后,图片处理之前,对图片进行解码
 tf.encode_jpeg 在图片保存时对图片进行重编码
 tf.gfile.GFile 写出单个图片
 tf.image.convert_image_dtype 转换图片的数据类型
 tf.resize_images 剪裁图片大小
 tf.resize_image_with_crop_of_pad 剪裁单个图片大小
 tf.image.random_flip_left_right 图片随机左右反转
 tf.image.random_flip_up_down 图片随机上下反转
 tf.image.random_brightness 图片随机调整亮度
 tf.image.random_hue 图片随机调整饱和度
 tf.image.random_contrast 图片随机调整对比度
 tf.image.random_saturation 图片随机调整色度
 tf.image.per_image_standardization 单个图片标准化
 tf.image.clip_by_value 单个图片归一化,其它还有tf.image.clip_by_XXX等方法
 tf.expand_dims 给图片增加维度(图层)
 tf.image.sample_distorted_bounding_box 生成随机子图
 tf.image.draw_bounding_boxes 将标注框标注的子图取出来
 tf.image.reshape 调整图片的维度
 tf.slice 截取随机子图为单个图片


二、TFRecord
  TFRecord文件是tensorflow指定的一种文件存储格式。它由tf.train.Example和tf.train.Feature来规定和实现。

复制代码

tf.train.Example Protocol Buffer

message Example {
 Features features = 1;
 }
 message Features {
 map<string, Feature> feature = 1;
 }
 message Feature {
 oneof kind{
 BytesList bytes_list = 1;
 FloatList float_list = 2;
 Int64List int64_list = 3;
 }
 }


复制代码
  1、TFRecord文件写出
  手写字mnist数据下载地址: http://yann.lecun.com/exdb/mnist/

复制代码

import tensorflow as tf
 from tensorflow.examples.tutorials.mnist import input_data
 import numpy as np

导入训练集和测试集的图片和标签

mnist = input_data.read_data_sets(“tensorflow/mnist/”, dtype=tf.uint8, one_hot=True)

获取图片和标签

images = mnist.train.images # images.shape (55000, 784) 热独编码
 labels = mnist.train.labels # labels.shape (55000, 10)

获取图像的数量及图片的分辨率([…])

numbers, pixels = images.shape

按照tf.train.Example Protocol Buffer来定义TFRecord文件格式

def _int64(value):

return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
 def _bytes(value):
 return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
 def example_protocol_buffer(pixel, size, image):
 example = tf.train.Example(
 features=tf.train.Features(
 feature={
 ‘pixels’: _int64(pixels),
 ‘label’: _int64(size),
 ‘image’: _bytes(image)
 }
 )
 )
 return example.SerializeToString() # 序列化为字节

输出文件地址

filename = “tensorflow/test/mnist.tfrecord”

创建一个writer

writer = tf.python_io.TFRecordWriter(filename)

遍历每张图片

for index in range(numbers):
 image = images[index].tostring() # 转成字节
 serialize = example_protocol_buffer(pixels, np.argmax(labels[index]), image)
 writer.write(serialize)
 writer.close()
 print(“done.”)


复制代码
  2、TFRecord文件读入
复制代码

import tensorflow as tf
 import matplotlib.pyplot as plt

创建reader

reader = tf.TFRecordReader()

创建字节流读取队列

filename_queue = tf.train.string_input_producer(
 [“tensorflow/test/mnist.tfrecord”]
 )

从文件中读取一个样例,read_up_to函数一次性读取多个样例

key, serialized_example = reader.read(filename_queue)

解析读取的一个样例,如果需要解析多个样例,可以用parse_example

def parse_single(serialized_example):
 features = tf.parse_single_example(
 serialized_example,
 features={
 ‘image’: tf.FixedLenFeature([], tf.string),
 ‘label’: tf.FixedLenFeature([], tf.int64),
 ‘pixels’: tf.FixedLenFeature([], tf.int64)
 }
 )
 # 将读取的单个样例解码
 image = tf.decode_raw(features[‘image’], tf.uint8)
 label = tf.cast(features[‘label’], tf.int32)
 pixels = tf.cast(features[‘pixels’], tf.int32)
 return image, label, pixelssess = 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(parse_single(serialized_example))
 print(image, label, pixel)
 plt.imshow(image.reshape(28, 28))
 plt.show()