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.
"""