【TensorFlow2.0】ImageDataGenerator使用

一、作用

  ImageDataGenerator()是keras.preprocessing.image模块中的图片生成器,同时也可以在batch中进行数据增强。
  例:可以自动为训练数据生成标签。下图中训练数据包含两个文件夹,使用ImageDataGenerator可以自动将horses里面的图片的标签自动设置为horses类标签,将humans里面的图片的标签自动设置为humans的标签。
                在这里插入图片描述

二、使用步骤

  1.创建对象:datagen=keras.preprocessing.image.ImageDataGenerator(参数)

datagen= keras.preprocessing.image.ImageDataGenerator(featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, zca_epsilon=1e-06, rotation_range=0.0, width_shift_range=0.0, height_shift_range=0.0, brightness_range=None, shear_range=0.0, zoom_range=0.0, channel_shift_range=0.0, fill_mode='nearest', cval=0.0, horizontal_flip=False, vertical_flip=False, rescale=None, preprocessing_function=None, data_format=None, validation_split=0.0)

  参数:

  • featurewise_center: Boolean. 对输入的图片每个通道减去每个通道对应均值。
  • samplewise_center: Boolan. 每张图片减去样本均值, 使得每个样本均值为0。
  • featurewise_std_normalization(): Boolean()
  • samplewise_std_normalization(): Boolean()
  • zca_epsilon(): Default 12-6
  • zca_whitening: Boolean. 去除样本之间的相关性
  • rotation_range(): 旋转范围
  • width_shift_range(): 水平平移范围
  • height_shift_range(): 垂直平移范围
  • shear_range(): float, 透视变换的范围
  • zoom_range(): 缩放范围
  • fill_mode: 填充模式, constant, nearest, reflect
  • cval: fill_mode == 'constant’的时候填充值
  • horizontal_flip(): 水平反转
  • vertical_flip(): 垂直翻转
  • preprocessing_function(): user提供的处理函数
  • data_format(): channels_first或者channels_last
  • validation_split(): 多少数据用于验证集
      2.对象调用方法:generator=datagen.方法()
    方法:
  • apply_transform(x, transform_parameters):根据参数对x进行变换
  • fit(x, augment=False, rounds=1, seed=None): 将生成器用于数据x,从数据x中获得样本的统计参数, 只有featurewise_center, featurewise_std_normalization或者zca_whitening为True才需要
  • flow(x, y=None, batch_size=32, shuffle=True, sample_weight=None, seed=None, save_to_dir=None, save_prefix=’’, save_format=‘png’, subset=None) ):按batch_size大小从x,y生成增强数据
  • flow_from_directory()从路径生成增强数据,和flow方法相比最大的优点在于不用一次将所有的数据读入内存当中,这样减小内存压力。
  • get_random_transform(img_shape, seed=None): 返回包含随机图像变换参数的字典
    random_transform(x, seed=None): 进行随机图像变换, 通过设置seed可以达到同步变换。
    standardize(x): 对x进行归一化

三、代码案例

  数据:
                    在这里插入图片描述

import os
import zipfile
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
############################################构建模型############################################################################
model=tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(16,(3,3),activation=tf.nn.relu,input_shape=(300,300,3)),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(32,(3,3),activation=tf.nn.relu),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(64,(3,3),activation=tf.nn.relu),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(64,(3,3),activation=tf.nn.relu),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(64, (3, 3), activation=tf.nn.relu),
    tf.keras.layers.MaxPooling2D(2, 2),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512,activation=tf.nn.relu),
    tf.keras.layers.Dense(1,activation=tf.nn.sigmoid)
])

model.summary()#模型可视化
# Model: "sequential"
# _________________________________________________________________
# Layer (type)                 Output Shape              Param #
# =================================================================
# conv2d (Conv2D)              (None, 298, 298, 16)      448
# _________________________________________________________________
# max_pooling2d (MaxPooling2D) (None, 149, 149, 16)      0
# _________________________________________________________________
# conv2d_1 (Conv2D)            (None, 147, 147, 32)      4640
# _________________________________________________________________
# max_pooling2d_1 (MaxPooling2 (None, 73, 73, 32)        0
# _________________________________________________________________
# conv2d_2 (Conv2D)            (None, 71, 71, 64)        18496
# _________________________________________________________________
# max_pooling2d_2 (MaxPooling2 (None, 35, 35, 64)        0
# _________________________________________________________________
# conv2d_3 (Conv2D)            (None, 33, 33, 64)        36928
# _________________________________________________________________
# max_pooling2d_3 (MaxPooling2 (None, 16, 16, 64)        0
# _________________________________________________________________
# conv2d_4 (Conv2D)            (None, 14, 14, 64)        36928
# _________________________________________________________________
# max_pooling2d_4 (MaxPooling2 (None, 7, 7, 64)          0
# _________________________________________________________________
# flatten (Flatten)            (None, 3136)              0
# _________________________________________________________________
# dense (Dense)                (None, 512)               1606144
# _________________________________________________________________
# dense_1 (Dense)              (None, 1)                 513
# =================================================================
# Total params: 1,704,097
# Trainable params: 1,704,097
# Non-trainable params: 0
# _________________________________________________________________


############################################编译模型############################################################################
model.compile(opitimizer=tf.keras.optimizers.RMSprop(lr=0.001),loss=tf.keras.losses.binary_crossentropy)


############################################使用图像生成器生成带标签的数据数据###################################################

from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen=ImageDataGenerator(rescale=1/255)#创建一个对象,并将图片的大小规范到0-1之间

# 生成训练集的带标签的数据
train_generator=train_datagen.flow_from_directory(
    '数据集\horse-or-man',#数据路径
     target_size=(300,300),
     class_mode='binary'
)

############################################训练模型#############################################################################
history=model.fit_generator(
    train_generator,
    steps_per_epoch=8,
    epochs=15,
    verbose=1
)

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值