使用 Keras ImageDataGenerator 时预处理操作的顺序是怎样的?
What is the sequence of preprocessing operations when using Keras ImageDataGenerator?
当你使用ImageDataGenerator
时,提供了很多预处理函数,我们也可以传递自己的预处理输入。假设我们使用 MobileNet
作为预训练模型,我们将使用相应的 preprocesss_input
函数。假设我们 init
class 如下:
tf.keras.preprocessing.image.ImageDataGenerator(
featurewise_center=True,featurewise_std_normalization=True,zca_whitening=True,
rotation_range=13, width_shift_range=0.13,height_shift_range=0.2, shear_range=0.06, zoom_range=0.1,
horizontal_flip=True, vertical_flip=True,
rescale=1./255.,preprocessing_function=preprocess_input)
这里的转换顺序是什么?重新缩放是先发生还是 preprocess_function
之类的。如果我们使用ZCA
,协方差偏移会导致范围改变,那么使用rescale
就没有意义了。 std_norm
也会发生同样的情况。此外,如果我们使用 preprocess_function
,不同的模型使用不同的范围,例如 -1 to 1
或 0-1
,因此重新缩放不会产生影响。不同的序列组合将导致图像的不同统计。
对于缩放、裁剪、旋转等其他变换,
- 这些会一下子发生吗?或者 class 将 select 一次转换?
- 如果是select一次全部缩放,缩放->翻转->移动->旋转->剪切会导致图像中出现异物。以随机顺序执行一项或多项对案例有意义。
有人可以阐明函数顺序的内部工作原理吗?
Whether the rescaling will happen first or the preprocess_function or
such
转换完成后将应用重新缩放。 preprocess_function
增强后
对于 featurewise
功能,您需要使生成器适合数据。来自 docs:
datagen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(x_train)
Will these happen all at once? OR the class will select one
transformation at a time?
变换随机应用于每张图像。 preprocess_function
将在增强完成后应用于 每张图像 。
If it is selecting all at once, zooming -> flipping -> Shifting ->
rotating -> shearing will lead to an alien object in the image. Where
doing one or more in random order makes sense for the cases.
没错,他们会显得很尴尬。但是您在使用 ImageDataGenerator 时指定 ranges。因此,转换将随机应用于该范围。
示例:
generator = ImageDataGenerator(
zoom_range=0.9, horizontal_flip=True, vertical_flip=True, rotation_range=13, width_shift_range=0.13,height_shift_range=0.2, shear_range=0.6,
)
将生成:
很明显,并非每张图片都缩放了 0.9 倍,反之亦然。
当你使用ImageDataGenerator
时,提供了很多预处理函数,我们也可以传递自己的预处理输入。假设我们使用 MobileNet
作为预训练模型,我们将使用相应的 preprocesss_input
函数。假设我们 init
class 如下:
tf.keras.preprocessing.image.ImageDataGenerator(
featurewise_center=True,featurewise_std_normalization=True,zca_whitening=True,
rotation_range=13, width_shift_range=0.13,height_shift_range=0.2, shear_range=0.06, zoom_range=0.1,
horizontal_flip=True, vertical_flip=True,
rescale=1./255.,preprocessing_function=preprocess_input)
这里的转换顺序是什么?重新缩放是先发生还是 preprocess_function
之类的。如果我们使用ZCA
,协方差偏移会导致范围改变,那么使用rescale
就没有意义了。 std_norm
也会发生同样的情况。此外,如果我们使用 preprocess_function
,不同的模型使用不同的范围,例如 -1 to 1
或 0-1
,因此重新缩放不会产生影响。不同的序列组合将导致图像的不同统计。
对于缩放、裁剪、旋转等其他变换,
- 这些会一下子发生吗?或者 class 将 select 一次转换?
- 如果是select一次全部缩放,缩放->翻转->移动->旋转->剪切会导致图像中出现异物。以随机顺序执行一项或多项对案例有意义。
有人可以阐明函数顺序的内部工作原理吗?
Whether the rescaling will happen first or the preprocess_function or such
转换完成后将应用重新缩放。 preprocess_function
增强后
对于 featurewise
功能,您需要使生成器适合数据。来自 docs:
datagen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(x_train)
Will these happen all at once? OR the class will select one transformation at a time?
变换随机应用于每张图像。 preprocess_function
将在增强完成后应用于 每张图像 。
If it is selecting all at once, zooming -> flipping -> Shifting -> rotating -> shearing will lead to an alien object in the image. Where doing one or more in random order makes sense for the cases.
没错,他们会显得很尴尬。但是您在使用 ImageDataGenerator 时指定 ranges。因此,转换将随机应用于该范围。
示例:
generator = ImageDataGenerator(
zoom_range=0.9, horizontal_flip=True, vertical_flip=True, rotation_range=13, width_shift_range=0.13,height_shift_range=0.2, shear_range=0.6,
)
将生成:
很明显,并非每张图片都缩放了 0.9 倍,反之亦然。