tensorflow数据增强

    相信大家都听说过数据增强(Data Augmentation),这是在做神经网络时非常极其重要的trick, 因为数据是宝贵的,稀有的,通过数据增强我们能让我们的数据量迅速增大,并且能使训练的模型具有一定抗噪能力。这篇文章主要探讨一下 tensorflow 关于数据增强的API.
    先读取图片数据, 并输出我们的图片信息

import numpy as np
from PIL import Image

# #***************案例1*numpy读取图片数据**********************#
img = Image.open("5.jpg", 'r')
img.show()
a = np.asarray(img, dtype=float)
print(img.mode)
# output RGB (1601, 1002)
没错,就是一个漂亮的小姐姐,hh

        再先来个题外话,叫做 图片数据的编码, tensorflow能支持的格式不多, jpg,png,gif,bmp这些常用格式倒是可以的,如果你的图片格式是很特殊的格式的话, 恐怕你就需要用其他处理方式先进行预处理了, 比如常用的医学图像 .nii格式 你就需要用nibable, 一些tiff,DICOM,等你可能可以用 PIL,cv2,openslide , libvips等等。

    

# ***# 读取图片,进行图片解码
# 读取图像的原始数据 返回值:<class 'bytes'>
# 也就是读取图片,将其转换成一串二进制串
image_raw_data = tf.gfile.FastGFile("5.jpg", 'rb').read()

with tf.Session() as sess:
    # img_data--> <class 'tensorflow.python.framework.ops.Tensor'>
    img_data = tf.image.decode_jpeg(image_raw_data)

    print(img_data.eval())  # RGB模式输出一个三维数组
    # py_plot展示图片
  plt.imshow(img_data.eval())
    plt.show()
    # 将数据类型转化为uint8
    img_data = tf.image.convert_image_dtype(img_data, dtype=tf.uint8)

# ***# 读取数据,进行图片编码
  encoded_image = tf.image.encode_png(img_data)
    with tf.gfile.GFile("6.png", 'wb') as f:
        f.write(encoded_image.eval())

        接下来我们就一个个来看看,首先读取图片数据,并转化成float32

image_raw_data = tf.gfile.FastGFile("5.jpg", 'rb').read()  # 500x500
with tf.Session() as sess:
    # img_data--> <class 'tensorflow.python.framework.ops.Tensor'>
    img_data = tf.image.decode_jpeg(image_raw_data)
    img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
 
print(type(img_data) # class 'tensorflow.python.framework.ops.Tensor'>

(一)调整图片大小resize

    一般来说网络上的图像大小不确定,但是神经网络的输入节点的个数是固定的。所以与处理是需要统一图片大小

    调整大小方法有以下几个:
    # 1、双线性插值法
    # 2、最近邻居法
    # 3、双三次插值法
    # 4、面积插值法

    

resize_img = tf.image.resize_images(img_data, [300, 300], method=0)  # 一共提供了四种方法
# <class 'tensorflow.python.framework.ops.Tensor'>
plt.imshow(resize_img.eval())
plt.show()

# 0代表ResizeMethod.BILINEAR,依次类推

  输出结果:

   

(二)图片剪切填充

 

# 放大图片就自动周围填充黑色;缩小图片就自动从图片中间剪切
resize_img = tf.image.resize_image_with_crop_or_pad(img_data, 600, 600)
plt.imshow(resize_img.eval())
plt.show()



    输出结果:

   


    (三)图片按比例大小缩小图片(也是一种剪切,类似上一种方式)

    

central_cropped = tf.image.central_crop(img_data, 0.5)  
plt.imshow(central_cropped.eval())
plt.show()

    输出结果:

    

    (四)方框剪切

    

# # bounding_box_crop
# # similar func:tf.image.pad_to_bounding_box
# 图片(0 0)位置在左上角, (50,50)指heightwidth在图片左上角的偏移量
resize_img = tf.image.crop_to_bounding_box(img_data, 50, 50, 300, 300)

   

    (五)翻转以及随机翻转

# #图像翻转# 数据增强
    # 上下翻转、左右翻转、对角线翻转
    # tf.image.transpose_image(img_data)
    # tf.image.flip_left_right(img_data)
    flip_img = tf.image.flip_up_down(img_data)
    plt.imshow(flip_img.eval())
    plt.show()
    # 随机翻转 推荐应用这个方法
    # 随机上下左右、亮度、对比度、色相、饱和度
    # tf.image.random_flip_up_down(img_data)
    # tf.image.random_brightness()
    # tf.image.random_contrast()
    # tf.image.random_hue()
    # tf.image.random_saturation()
    rand_flip_img = tf.image.random_flip_left_right(img_data, seed=1)
    plt.imshow(rand_flip_img.eval())
    plt.show()

    (六)图像色彩调整(五 中包含了一些色彩调整函数,不过是随机的就直接列在上面了,方便大家总结理解)

    

# 调整图片亮度、对比度、gamma、色相、饱和度
# tf.image.adjust_contrast()
# tf.image.adjust_gamma()
# tf.image.adjust_hue()
# tf.image.adjust_saturation()
adjust_img = tf.image.adjust_brightness(img_data, -0.5)
# 将图片亮度变为均值为0,方差为1
adjust_img = tf.image.per_image_standardization(img_data)
print(img_data.eval())

    (七)图像标注

    

# # tf.image.draw_bounding_boxes函数的输入是一个batch的数据,也就是
#   多张图像组成的四维矩阵
#   第一个输入参数img_data中数据类型应该是实数,前面最初已经转换成了tf.float32
batched = tf.expand_dims(img_data, 0)
#  [0.2, 0.3, 0.5, 0.8]给出的是图像中的相对位置 [y_min, x_min, y_max, x_max]
boxes = tf.constant([[[0.2, 0.3, 0.48, 0.65]]])
res = tf.image.draw_bounding_boxes(batched, boxes, name='bounding_box')
plt.subplot(121), plt.imshow(img_data.eval()), plt.title('original')
plt.subplot(122), plt.imshow(np.asarray(res.eval())[0]), plt.title('result')
# plt.imsave(fname="save.jpg", arr=np.asarray(res.eval())[0])  # 保存图片
plt.show()
    上面图片做标注不是很明显,换张女神图片来码代码~

    

    (八)截取标记部分

    

# 随机截取图像上有信息含量的部分,也可以提高模型健壮性
# 此函数为图像生成单个随机变形的边界框。函数输出的是可用于裁剪原始图像的单个边框。
# 返回值为3个张量:beginsize bboxes。前2个张量用于 tf.slice 剪裁图像。
# 后者可以用于 tf.image.draw_bounding_boxes 函数来画出边界框。
boxes = tf.constant([[[0.2, 0.3, 0.48, 0.65]]])
print(np.asarray(img_data).shape)
begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(tf.shape(img_data),
                                                                    bounding_boxes=boxes,
                                                                    min_object_covered=0.1)
# batched = tf.expand_dims(tf.image.convert_image_dtype(img_data, tf.float32), 0)
# image_with_box = tf.image.draw_bounding_boxes(batched, bbox_for_draw)
distorted_image = tf.slice(img_data, begin, size)
plt.imshow(distorted_image.eval())
plt.show()


 tensorflow 数据增强处理基本上就这些啦~用起来,训练神经网络才是关键。


  • 9
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值