Sar*_*raG 5 python theano deep-learning keras
我在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
Run Code Online (Sandbox Code Playgroud)
数据增强的工作原理如下:在每个学习时期,在指定范围内随机选择的参数的变换应用于训练集中的所有原始图像.在完成一个时期之后,即在将学习算法暴露于整组训练数据之后,开始下一个学习时期,并且通过将指定的变换应用于原始训练数据再次增强训练数据.
以这种方式,每个图像被增强的次数等于学习时期的数量.回想一下您链接的示例:
# Fit the model on the batches generated by datagen.flow().
model.fit_generator(datagen.flow(X_train, Y_train,
batch_size=batch_size),
samples_per_epoch=X_train.shape[0],
nb_epoch=nb_epoch,
validation_data=(X_test, Y_test))
Run Code Online (Sandbox Code Playgroud)
这里的datagen
对象会将训练集暴露给model
nb_epoch
时间,因此每个图像都会被扩充nb_epoch
一次.以这种方式,学习算法几乎从不会看到两个完全相同的训练示例,因为在每个时期训练示例是随机变换的.
归档时间: |
|
查看次数: |
4710 次 |
最近记录: |