keras的图像预处理全攻略(三)—— ImageDataGenerator 类的辅助类

 

上一篇文章介绍了keras图像预处理的核心类—— ImageDataGenerator 类 ,其实关于keras的图像预处理与图像generator都是通过这个类来实现的,第一篇文章介绍的相关方法都是为这个类服务的辅助方法,本文要介绍的几个类都是为 ImageDataGenerator 类服务的辅助类,所以在实际应用中,一般不需要用到辅助方法与辅助类,只需要使用ImageDataGenerator 类 即可,但是对于了解源码的架构,去了解一下这些辅助方法也是很有好处的。本篇文章来看一看那几个重要的辅助类。

一、image.py的借口框架图

主要使用的ImageDataGenerator 类 以及与之相关的辅助函数与辅助类之间的关系如下:

二、几个关键辅助类的介绍

在前面介绍ImageDataGenerator类的时候,里面有三个非常核心的方法,它们分别是flow、flow_from_directory、flow_from_dataframe,实际上它们每一个方法的实现都是通过下面的三个辅助类去实现的,对应关系如下:

  • flow()方法。由NumpyArrayIterator类去实现;
  • flow_from_directory()方法。由DirectoryIterator类去实现;
  • flow_from_dataframe方法。由DataFrameIterator类去实现

而后面的三个辅助类又是继承了基类Iterator基类的。

2.1 Iterator基类型

class Iterator(IteratorType):
    """实现图像数据迭代的基类型.

    没一个继承自Iterator基类的子类型都必须实现 _get_batches_of_transformed_samples方法。
    # 构造函数参数
        n: Integer, 需要迭代的数据的总数
        batch_size: Integer, 没一个迭代轮次的batch的大小
        shuffle: Boolean, 在每一次迭代某个批次的数据的时候,是否需要混洗
        seed: shuffle的随机种子
    """

2.2 NumpyArrayIterator类

ImageDataGenerator类里面的flow方法就是通过这个类来实现的,类的定义如下:

class NumpyArrayIterator(Iterator):
    """Iterator yielding data from a Numpy array.

    # 构造函数参数
        x: Numpy array of input data or tuple.
            If tuple, the second elements is either
            another numpy array or a list of numpy arrays,
            each of which gets passed
            through as an output without any modifications.
        y: Numpy array of targets data.
        image_data_generator: Instance of `ImageDataGenerator`
            to use for random transformations and normalization.
        batch_size: Integer, size of a batch.
        shuffle: Boolean, whether to shuffle the data between epochs.
        sample_weight: Numpy array of sample weights.
        seed: Random seed for data shuffling.
        data_format: String, one of `channels_first`, `channels_last`.
        save_to_dir: Optional directory where to save the pictures
            being yielded, in a viewable format. This is useful
            for visualizing the random transformations being
            applied, for debugging purposes.
        save_prefix: String prefix to use for saving sample
            images (if `save_to_dir` is set).
        save_format: Format to use for saving sample images
            (if `save_to_dir` is set).
        subset: Subset of data (`"training"` or `"validation"`) if
            validation_split is set in ImageDataGenerator.
        dtype: Dtype to use for the generated arrays.
    """

2.3 DirectoryIterator类

ImageDataGenerator类里面的flow_from_directory方法就是通过这个类来实现的,类的定义如下:

class DirectoryIterator(Iterator):
    """Iterator capable of reading images from a directory on disk.

    # Arguments
        directory: Path to the directory to read images from.
            Each subdirectory in this directory will be
            considered to contain images from one class,
            or alternatively you could specify class subdirectories
            via the `classes` argument.
        image_data_generator: Instance of `ImageDataGenerator`
            to use for random transformations and normalization.
        target_size: tuple of integers, dimensions to resize input images to.
        color_mode: One of `"rgb"`, `"rgba"`, `"grayscale"`.
            Color mode to read images.
        classes: Optional list of strings, names of subdirectories
            containing images from each class (e.g. `["dogs", "cats"]`).
            It will be computed automatically if not set.
        class_mode: Mode for yielding the targets:
            `"binary"`: binary targets (if there are only two classes),
            `"categorical"`: categorical targets,
            `"sparse"`: integer targets,
            `"input"`: targets are images identical to input images (mainly
                used to work with autoencoders),
            `None`: no targets get yielded (only input images are yielded).
        batch_size: Integer, size of a batch.
        shuffle: Boolean, whether to shuffle the data between epochs.
        seed: Random seed for data shuffling.
        data_format: String, one of `channels_first`, `channels_last`.
        save_to_dir: Optional directory where to save the pictures
            being yielded, in a viewable format. This is useful
            for visualizing the random transformations being
            applied, for debugging purposes.
        save_prefix: String prefix to use for saving sample
            images (if `save_to_dir` is set).
        save_format: Format to use for saving sample images
            (if `save_to_dir` is set).
        subset: Subset of data (`"training"` or `"validation"`) if
            validation_split is set in ImageDataGenerator.
        interpolation: Interpolation method used to resample the image if the
            target size is different from that of the loaded image.
            Supported methods are "nearest", "bilinear", and "bicubic".
            If PIL version 1.1.3 or newer is installed, "lanczos" is also
            supported. If PIL version 3.4.0 or newer is installed, "box" and
            "hamming" are also supported. By default, "nearest" is used.
        dtype: Dtype to use for generated arrays.
    """

2.4 DateFrameIterator类

ImageDataGenerator类里面的flow_from_dateframe方法就是通过这个类来实现的,类的定义如下:

class DataFrameIterator(Iterator):
    """Iterator capable of reading images from a directory on disk
        through a dataframe.

    # Arguments
        dataframe: Pandas dataframe containing the filenames of the
                   images in a column and classes in another or column/s
                   that can be fed as raw target data.
        directory: Path to the directory to read images from.
            Each subdirectory in this directory will be
            considered to contain images from one class,
            or alternatively you could specify class subdirectories
            via the `classes` argument.
            if used with dataframe,this will be the directory to under which
            all the images are present.
        image_data_generator: Instance of `ImageDataGenerator`
            to use for random transformations and normalization.
        x_col: Column in dataframe that contains all the filenames.
        y_col: Column/s in dataframe that has the target data.
        has_ext: bool, Whether the filenames in x_col has extensions or not.
        target_size: tuple of integers, dimensions to resize input images to.
        color_mode: One of `"rgb"`, `"rgba"`, `"grayscale"`.
            Color mode to read images.
        classes: Optional list of strings, names of
            each class (e.g. `["dogs", "cats"]`).
            It will be computed automatically if not set.
        class_mode: Mode for yielding the targets:
            `"binary"`: binary targets (if there are only two classes),
            `"categorical"`: categorical targets,
            `"sparse"`: integer targets,
            `"input"`: targets are images identical to input images (mainly
                used to work with autoencoders),
            `"other"`: targets are the data(numpy array) of y_col data
            `None`: no targets get yielded (only input images are yielded).
        batch_size: Integer, size of a batch.
        shuffle: Boolean, whether to shuffle the data between epochs.
        seed: Random seed for data shuffling.
        data_format: String, one of `channels_first`, `channels_last`.
        save_to_dir: Optional directory where to save the pictures
            being yielded, in a viewable format. This is useful
            for visualizing the random transformations being
            applied, for debugging purposes.
        save_prefix: String prefix to use for saving sample
            images (if `save_to_dir` is set).
        save_format: Format to use for saving sample images
            (if `save_to_dir` is set).
        subset: Subset of data (`"training"` or `"validation"`) if
            validation_split is set in ImageDataGenerator.
        interpolation: Interpolation method used to resample the image if the
            target size is different from that of the loaded image.
            Supported methods are "nearest", "bilinear", and "bicubic".
            If PIL version 1.1.3 or newer is installed, "lanczos" is also
            supported. If PIL version 3.4.0 or newer is installed, "box" and
            "hamming" are also supported. By default, "nearest" is used.
    """

 

  • 3
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
ImageDataGeneratorKeras中一个非常方便的图像数据生成器,主要用于数据增强(data augmentation)和实时数据扩充(real-time data augmentation)。它可以自动将一批原始图像转换为训练所需的随机数据,比如随机旋转、缩放、翻转等操作,从而扩大训练数据集,提高模型的泛化能力。 ImageDataGenerator可以通过定义不同的参数来实现各种图像增强的操作,例如: - rotation_range:旋转角度范围; - width_shift_range、height_shift_range:图像水平、垂直方向的平移范围; - shear_range:剪切强度; - zoom_range:随机缩放范围; - horizontal_flip、vertical_flip:是否随机水平、垂直翻转图像; - fill_mode:填充模式。 使用ImageDataGenerator时,需要先通过fit()方法计算出数据集的统计信息,然后可以通过flow()方法进行数据生成。例如: ```python from keras.preprocessing.image import ImageDataGenerator datagen = ImageDataGenerator( rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) datagen.fit(x_train) model.fit_generator(datagen.flow(x_train, y_train, batch_size=32), steps_per_epoch=len(x_train)/32, epochs=50) ``` 这段代码中,我们先定义了一个ImageDataGenerator对象,然后定义了各种图像增强的参数。接着,我们通过fit()方法计算出数据集的统计信息,最后通过flow()方法生成扩充后的数据集,用于训练模型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值