TensorFlow中的数据增强

TensorFlow中的数据增强

总结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对象转化为有效的表达式参与求值运算返回计算结果
打印出的三维矩阵:
在这里插入图片描述

一、图像缩放:tf.image.resize_images(images,new_height,new_width,method)

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()

在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值