ImageDataGenerator使用

最近发现了一个好用的类ImageDataGenerator,可以使用它完成以下工作:

  1. Accepting a batch of images used for training.
  2. Taking this batch and applying a series of random transformations to each image in the batch (including random rotation, resizing, shearing, etc.).
  3. Replacing the original batch with the new, randomly transformed batch.
  4. Training the CNN on this randomly transformed batch (i.e., the original data itself is not used for training).

简单的说就是可以使用它读入一批图片,它会根据我们设置的属性值自动的进行图像增强(如旋转,水平翻转,截取等),方便我们克服过拟合,学习到更多的特征。

使用前我们需要对ImageDataGenerator进行初始化:

#Updated to do image augmentation
train_datagen = ImageDataGenerator(
      rotation_range=40,   
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest')
  • rotation_range is a value in degrees (0–180), a range within which to randomly rotate pictures.
  • width_shift and height_shift are ranges (as a fraction of total width or height) within which to randomly translate pictures vertically or horizontally.
  • shear_range is for randomly applying shearing transformations.
  • zoom_range is for randomly zooming inside pictures.
  • horizontal_flip is for randomly flipping half of the images horizontally. This is relevant when there are no assumptions of horizontal assymmetry (e.g. real-world pictures).
  • fill_mode is the strategy used for filling in newly created pixels, which can appear after a rotation or a width/height shift.

关于ImageDataGenerator的更多属性可以查看keras文档


接下来就可以用ImageDataGenerator读入图片了:

# Flow training images in batches of 20 using train_datagen generator
train_generator = train_datagen.flow_from_directory(
        train_dir,  # This is the source directory for training images
        target_size=(150, 150),  # All images will be resized to 150x150
        batch_size=20,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

history = model.fit(
      train_generator,
      steps_per_epoch=100,  # 2000 images = batch_size * steps
      epochs=100,
      verbose=2)

使用ImageDataGeneratorflow_from_directory方法读入图片时有个非常“神奇”的一点,ImageDataGenerator会自动帮我们的图片进行分类!这里的train_dir的目录结构如下:
在这里插入图片描述
那么ImageDataGenerator会自动帮我们将图片1,2,3.jpg分为cat类,4,5,6分为dog类。

target_size参数会将读入图片转为指定大小,我们这里是resize成150*150像素大小。

然后我们训练时就直接传train_generator即可,连y标签都不用传,非常方便。

Reference

  1. https://www.pyimagesearch.com/2019/07/08/keras-imagedatagenerator-and-data-augmentation/
  2. 吴恩达的课程练习
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值