我在Keras上阅读了一些关于数据增加的材料,但它对我来说仍然有点模糊。是否有任何参数控制数据增加步骤中从每个输入图像创建的图像数量?在this example中,我看不到任何控制从每个图像创建的图像数量的参数。Keras数据增加参数
例如,在下面的代码中,我可以有一个参数(num_imgs
),用于控制从每个输入图像创建并存储在名为preview的文件夹中的图像数量;但在实时数据增强中,此目的没有任何参数。
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
num_imgs = 20
datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
img = load_img('data/train/cats/cat.0.jpg') # this is a PIL image
x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150)
x = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, 3, 150, 150)
# the .flow() command below generates batches of randomly transformed images
# and saves the results to the `preview/` directory
i = 0
for batch in datagen.flow(x, batch_size=1,
save_to_dir='preview', save_prefix='cat', save_format='jpeg'):
i += 1
if i > num_imgs:
break # otherwise the generator would loop indefinitely
感谢您的有用评论。所以,如果我总共有1000张图像,那么在每个时期,从原始图像中生成1000张新图像,并馈入模型进行训练。然后,在下一个时期,从原始图像生成1000个新图像并馈入模型中以进行训练,等等。所以,模型可能永远不会看到原始数据,对吧? – SaraG
另一种方法是生成和存储新图像,然后使用它来训练我们的模型。例如,如果我从每个输入图像生成10个图像,那么我将有10,000个新图像。所以,我完全有11,000张图像用于训练。哪一种方法更好? – SaraG
是的,你是对的,模型可能永远不会看到原始数据。应用的转换越多,模型将看到原始数据的概率就越小。转换参数的范围也影响该概率。你在第二条评论中提出的方法似乎也很合理。很难说哪一个更好,我会尝试两种替代方案,并选择能够产生最佳结果的方案! –