赞
踩
总结TensorFlow中常用的一些数据增强方法,包括图片的缩放、裁剪、翻转等等
首先导入需要用到的包
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
任选选一张图片作为测试,放到项目中,读取图片
image_raw_data = tf.gfile.FastGFile('longzhu.jpg', 'rb').read()
tf.image.decode_jpeg(image_raw_data)
对图像进行解码,得到图像对应的三维矩阵
with tf.Session() as sess:
#对图像进行jpg格式解码从而得到图像对应的三维矩阵
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval())
eval()官方文档里面给出来的功能解释是:将字符串string对象转化为有效的表达式参与求值运算返回计算结果
打印出的三维矩阵:
1、双线性插值法ResizeMethod.BILINEAR(默认设置),对应method=0
注:TensorFlow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片
with tf.Session() as sess:
#双线性插值法将图像缩放为制定尺寸
resize1 = tf.image.resize_images(img_data, [256, 256], method=0)
#TensorFlow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片
resize1 = np.asarray(resize1.eval(), dtype='uint8')
plt.imshow(resize1)
plt.show()
2、最近邻插值法NEAREST_NEIGHBOR,对应method=1
with tf.Session() as sess:
resize2 = tf.image.resize_images(img_data, [256, 256], method=1)
resize2 = np.asarray(resize2.eval(), dtype='uint8')
plt.imshow(resize2)
plt.show()
3、双立方插值法BICUBIC,对应method=2
with tf.Session() as sess:
resize3 = tf.image.resize_images(img_data, [256, 256], method=2)
resize3 = np.asarray(resize3.eval(), dtype='uint8')
plt.imshow(resize3)
plt.show()
4、像素区域插值法AREA,对应method=3
with tf.Session() as sess:
resize4 = tf.image.resize_images(img_data, [256, 256], method=3)
resize4 = np.asarray(resize4.eval(), dtype='uint8')
plt.imshow(resize4)
plt.show()
这4中缩放方法最终得到的结果并没有差别。
1、如果目标图像尺寸小于原始图像尺寸,则在中心位置剪裁,反之则用黑色像素进行填充。
tf.image.resize_image_with_crop_or_pad(image, target_height, target_width)
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
croped = tf.image.resize_image_with_crop_or_pad(img_data, 400, 400)
plt.imshow(croped.eval())
plt.show()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
padded = tf.image.resize_image_with_crop_or_pad(img_data, 2000, 2000)
plt.imshow(padded, eval())
plt.show()
2、随机裁剪
tf.image.random_crop(image, size, seed=None, name=None)
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
random_crop1 = tf.random_crop(img_data, [100, 100, 3])
plt.imshow(random_crop1.eval())
plt.show()
1、水平翻转
tf.image.flip_left_right(img_data)
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
#plt.imshow(img_data.eval())
#plt.axis('off')
#plt.show()
flip_left_right = tf.image.flip_left_right(img_data)
plt.imshow(flip_left_right.eval())
plt.axis('off')
plt.show()
2、上下翻转
tf.image.flip_up_down(img_data)
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
flip_up_down = tf.image.flip_up_down(img_data)
plt.imshow(flip_up_down.eval())
plt.axis('off')
plt.show()
1、改变对比度
tf.image.random_contrast
通过不同的参数设置来调整对比度
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
#将图像的对比度将至原来的二分之一
contrast = tf.image.adjust_contrast(img_data, 0.5)
#将图像的对比度提高至原来的5倍
# contrast = tf.image.adjust_contrast(img_data, 5)
#在[lower,upper]范围随机调整图像对比度
# contrast = tf.image.random_contrast(img_data, lower=0.2, upper=3)
plt.imshow(contrast.eval())
plt.show()
2、标准化
将图像的像素值转化成零均值和单位方差
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
#对图像进行标准化(归一化)
standardization = tf.image.per_image_standardization(img_data)
plt.imshow(np.asarray(standardization.eval(), dtype='uint8'))
plt.show()
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。