我看到 imageDataGenerator 允许我指定不同样式的数据规范化,例如 featurewise_center、samplewise_center 等。
我从示例中看到,如果我指定了这些选项之一,那么我需要在生成器上调用 fit 方法,以允许生成器计算统计数据,例如生成器上的平均图像。
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
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)
# fits the model on batches with real-time data augmentation:
model.fit_generator(datagen.flow(X_train, Y_train, batch_size=32),
samples_per_epoch=len(X_train), nb_epoch=nb_epoch)
我的问题是,如果我在训练期间指定了数据规范化,预测将如何工作?我看不出在框架中我什至会如何通过训练集均值/标准偏差的知识来预测以允许我自己规范化我的测试数据,但我也没有在训练代码中看到这些信息在哪里存储。
标准化所需的图像统计数据是否存储在模型中,以便在预测期间使用它们?
原文由 Alex Taylor 发布,翻译遵循 CC BY-SA 4.0 许可协议
是的 - 这是
Keras.ImageDataGenerator
的一个非常大的缺点,你无法自己提供标准化统计数据。但是 - 有一种简单的方法可以解决这个问题。假设您有一个函数
normalize(x)
正在规范化图像 批次(请记住,生成器提供的不是简单图像而是图像数组 - 形状为(nr_of_examples_in_batch, image_dims ..)
的 批次,您可以制作您的自己的生成器通过使用标准化:那么你可以简单地使用
gen_with_norm(datagen.flow, normalize)
而不是datagen.flow
。Moreover - you might recover the
mean
andstd
computed by afit
method by getting it from appropriate fields in datagen (egdatagen.mean
anddatagen.std
)。