我正在使用 Keras ImageDataGenerator 类训练 CNN。我的代码看起来像这样:
from keras.callbacks import LearningRateScheduler
from keras.callbacks import EarlyStopping
from keras.preprocessing.image import ImageDataGenerator
data_generator = ImageDataGenerator(
rotation_range = 15,
width_shift_range = 0.1,
height_shift_range = 0.1,
horizontal_flip = True
)
data_generator.fit(xtrain)
es = EarlyStopping(monitor = 'val_loss', mode = 'min', verbose = 1, patience = 100)
history = CNN_model.fit(data_generator.flow(xtrain, ytrain, batch_size = batch_size), \
epochs = num_epochs, \
validation_data = (xval, yval), \
callbacks = [LearningRateScheduler(learning_rate_schedule), es])
现在我已经查看了文档,但我不完全明白它是否对每批中的每个图像进行图像增强,或者它是随机选择其中的一部分?我假设它会对每个图像进行增强,但我正在尝试理解。
如果它对我的数据集中的每个图像执行此操作,这意味着我根本没有在任何训练时期使用未增强的图像,对吗?
Thanks!
好吧,我做了测试,它是随机应用的每张图像,尽管这些图片来自同一批次。
根据我对文档的阅读,我没有理由认为只有部分图像被转换,而其他图像则保持不变。
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from skimage.data import chelsea
import matplotlib.pyplot as plt
import numpy as np
imgs = np.stack([chelsea() for i in range(4*4)], axis=0)
data_gen = ImageDataGenerator(
rotation_range = 90,
width_shift_range = 0.1,
height_shift_range = 0.1,
horizontal_flip = True,
preprocessing_function=lambda x: x[..., np.random.permutation([0, 1, 2])]
)
fig = plt.figure()
plt.subplots_adjust(wspace=.2, hspace=.2)
for index, image in enumerate(next(data_gen.flow(imgs)).astype(int)):
ax = plt.subplot(4, 4, index + 1)
ax.set_xticks([])
ax.set_yticks([])
ax.imshow(image)
plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)