Dataset之图片数据增强:基于TF实现图片数据增强(原始的训练图片reshaped_image→数据增强→distorted_image(训练时直接使用))

 

 

目录

数据增强步骤

数据增强实现代码


 

 

 

数据增强步骤

1、对reshaped_image随机裁剪图片:从原始32×32裁剪到24×24小块进行训练,因为小块可以取在图像的任何位置,所以仅此一步就可以大大增加训练、集的样本数目。
2、对裁剪后的小块进行水平翻转:随机翻转图片。每张图片有50%的概率被水平左右翻转,另有50%的概率保持不变
3、随机改变亮度和对比度:对得到的图片进行亮度和对比度的随机改变。

 

数据增强实现代码

#TF实现数据增强。原始的训练图片是reshaped_image ,最后会得到一个数据增强后的训练样本distorted_image。
  #训练时,直接使用distorted_image 进行训练即可。
  
  # Randomly crop a [height, width] section of the image.
  #1、对reshaped_image随机裁剪图片,从原始32×32裁剪到24×24小块进行训练,因为小块可以取在图像的任何位置,所以仅此一步就可以大大增加训练、集的样本数目。
  distorted_image = tf.random_crop(reshaped_image, [height, width, 3])

  # Randomly flip the image horizontally.
  #2、对裁剪后的小块进行水平翻转。随机翻转图片。每张图片有50%的概率被水平左右翻转,另有50%的概率保持不变
  distorted_image = tf.image.random_flip_left_right(distorted_image)

  # Because these operations are not commutative, consider randomizing
  # the order their operation.
  #3、随机改变亮度和对比度:对得到的图片进行亮度和对比度的随机改变。
  distorted_image = tf.image.random_brightness(distorted_image,
                                               max_delta=63)
  distorted_image = tf.image.random_contrast(distorted_image,
                                             lower=0.2, upper=1.8)

 

 

 

 

 

更多文章请关注《万象专栏》