当前位置:   article > 正文

数据增强(from tensorflow.python.keras.preprocessing.image import ImageDataGenerator)_from tensorflow.keras.preprocessing.image import i

from tensorflow.keras.preprocessing.image import imagedatagenerator
  1. keras.preprocessing.image.ImageDataGenerator(featurewise_center=False,
  2. samplewise_center=False,
  3. featurewise_std_normalization = False,
  4. samplewise_std_normalization = False,
  5. zca_whitening = False,
  6. rotation_range = 0.,
  7. width_shift_range = 0.,
  8. height_shift_range = 0.,
  9. shear_range = 0.,
  10. zoom_range = 0.,
  11. channel_shift_range = 0.,
  12. fill_mode = 'nearest',
  13. cval = 0.0,
  14. horizontal_flip = False,
  15. vertical_flip = False,
  16. rescale = None,
  17. preprocessing_function = None,
  18. data_format = K.image_data_format(),
  19. )

参数的介绍:

1.featurewise_center:布尔值,使输入数据集去中心化(均值为0), 按feature执行。

2.samplewise_center:布尔值,使输入数据的每个样本均值为0 。

3.featurewise_std_normalization:布尔值,将输入除以数据集的标准差以完成标准化, 按feature执行。

4.samplewise_std_normalization:布尔值,将输入的每个样本除以其自身的标准差。

5. zca_whitening:布尔值,对输入数据施加ZCA白化。

6. rotation_range:整数,数据提升时图片随机转动的角度。随机选择图片的角度,是一个0~180的度数,取值为0~180。 7.width_shift_range:浮点数,图片宽度的某个比例,数据提升时图片随机水平偏移的幅度。

8.height_shift_range:浮点数,图片高度的某个比例,数据提升时图片随机竖直偏移的幅度。 height_shift_range和width_shift_range是用来指定水平和竖直方向随机移动的程度,这是两个0~1之间的比例。

9: shear_range:浮点数,剪切强度(逆时针方向的剪切变换角度)。是用来进行剪切变换的程度。

10:zoom_range:浮点数或形如[lower,upper]的列表,随机缩放的幅度,若为浮点数,则相当于[lower,upper] = [1 - zoom_range, 1+zoom_range]。用来进行随机的放大。

11:channel_shift_range:浮点数,随机通道偏移的幅度。

12: fill_mode:‘constant’,‘nearest’,‘reflect’或‘wrap’之一,当进行变换时超出边界的点将根据本参数给定的方法进行处理 cval:浮点数或整数,当fill_mode=constant时,指定要向超出边界的点填充的值。

13:cval:浮点数或整数,当fill_mode=constant时,指定要向超出边界的点填充的值。

14:horizontal_flip:布尔值,进行随机水平翻转。随机的对图片进行水平翻转,这个参数适用于水平翻转不影响图片语义的时候。

15:vertical_flip:布尔值,进行随机竖直翻转。 rescale: 值将在执行其他处理前乘到整个图像上,我们的图像在RGB通道都是0~255的整数,这样的操作可能使图像的值过高或过低,所以我们将这个值定为0~1之间的数。

16:rescale: 值将在执行其他处理前乘到整个图像上,我们的图像在RGB通道都是0~255的整数,这样的操作可能使图像的值过高或过低,所以我们将这个值定为0~1之间的数

17:preprocessing_function: 将被应用于每个输入的函数。该函数将在任何其他修改之前运行。该函数接受一个参数,为一张图片(秩为3的numpy array),并且输出一个具有相同shape的numpy array

18:data_format:字符串,“channel_first”或“channel_last”之一,代表图像的通道维的位置。该参数是Keras 1.x中的image_dim_ordering,“channel_last”对应原本的“tf”,“channel_first”对应原本的“th”。以128x128的RGB图像为例,“channel_first”应将数据组织为(3,128,128),而“channel_last”应将数据组织为(128,128,3)。该参数的默认值是~/.keras/keras.json中设置的值,若从未设置过,则为“channel_last” 

下面举一个例子:

我们先来一张图片:

  1. # from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
  2. from tensorflow.python.keras.preprocessing.image import ImageDataGenerator,array_to_img,img_to_array,load_img
  3. datagen = ImageDataGenerator(
  4. rotation_range=30,
  5. width_shift_range=0.2,
  6. height_shift_range=0.2,
  7. shear_range=0.2,
  8. zoom_range=0.2,
  9. horizontal_flip=True,
  10. fill_mode='nearest')
  11. img = load_img('D:\\cat1.jpg') # this is a PIL image
  12. x = img_to_array(img) # this is a Numpy array with shape (536, 536, 3)
  13. x = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, 536, 536, 3)
  14. # the .flow() command below generates batches of randomly transformed images
  15. i = 0
  16. for batch in datagen.flow(x, batch_size=1, # save_to_dir 要保存的文件夹 prefix图片名字 format图片的格式
  17. save_to_dir='./Model', save_prefix='cat', save_format='jpeg'):
  18. i += 1
  19. if i > 10:
  20. break

我们可以看到生成了十一张图片

关于 ImageDataGenerator生成器的flow,flow_from_directory用法

flow:
flow(self, X, y, batch_size=32, shuffle=True, seed=None, save_to_dir=None, save_prefix='', save_format='png'):接收numpy数组和标签为参数,生成经过数据提升或标准化后的batch数据,并在一个无限循环中不断的返回batch数据

x:样本数据,秩应为4.在黑白图像的情况下channel轴的值为1,在彩色图像情况下值为3

y:标签

batch_size:整数,默认32

shuffle:布尔值,是否随机打乱数据,默认为True

save_to_dir:None或字符串,该参数能让你将提升后的图片保存起来,用以可视化

save_prefix:字符串,保存提升后图片时使用的前缀, 仅当设置了save_to_dir时生效

save_format:"png"或"jpeg"之一,指定保存图片的数据格式,默认"jpeg"

yields:形如(x,y)的tuple,x是代表图像数据的numpy数组.y是代表标签的numpy数组.该迭代器无限循环.

seed: 整数,随机数种子
 

  1. from tensorflow.python.keras.preprocessing.image import ImageDataGenerator,array_to_img,img_to_array,load_img
  2. import numpy as np
  3. x=np.random.rand(2,536,536,3)
  4. y=np.array([
  5. [1],
  6. [0]
  7. ],dtype=np.float64)
  8. print(datagen.flow(x,y,shuffle=True, seed=None, save_to_dir=None, save_prefix='', save_format='png')[0][0].shape)

我放进去了两个样本,形状是[1,536,536,3],然后两个标签y,然后返回得到了一个随即输出:

(2, 536, 536, 3)

datagen.flow(x,y,shuffle=True, seed=None, save_to_dir=None, save_prefix='', save_format='png')返回的是元组tupple,

datagen.flow(x,y,shuffle=True, seed=None, save_to_dir=None, save_prefix='', save_format='png')[0][0]返回的是x,类型numpy

datagen.flow(x,y,shuffle=True, seed=None, save_to_dir=None, save_prefix='', save_format='png')[0][1]返回的是这个x对应的y,类型numpy

由于是无限循环的,所以取之不尽用之不竭,也就是说将上面代码里的print行复制一下,输出的也是不一样的两个结果。

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

闽ICP备14008679号