当前位置:   article > 正文

TensorFlow中的数据增强_数据增强tensorflow csdn

数据增强tensorflow csdn

TensorFlow中的数据增强

总结TensorFlow中常用的一些数据增强方法,包括图片的缩放、裁剪、翻转等等
首先导入需要用到的包

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
  • 1
  • 2
  • 3

任选选一张图片作为测试,放到项目中,读取图片

image_raw_data = tf.gfile.FastGFile('longzhu.jpg', 'rb').read()
  • 1

在这里插入图片描述
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())
  • 1
  • 2
  • 3
  • 4

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述
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()
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述
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()
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述
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()
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述
这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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在这里插入图片描述
在这里插入图片描述
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
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

三、图像翻转

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述
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
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

四、图像简单处理

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在这里插入图片描述
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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/255267
推荐阅读
相关标签
  

闽ICP备14008679号