Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (19k points)

I see that the imageDataGenerator allows me to specify different styles of data normalization, e.g. featurewise_center, samplewise_center, etc.

I see from the examples that if I specify one of these options, then I need to call the fit method on the generator in order to allow the generator to compute statistics like the mean image on the generator.

(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)

My question is, how does prediction work if I have specified data normalization during training? I can't see how in the framework I would even pass the knowledge of the training set mean/std deviation along to predict to allow me to normalize my test data myself, but I also don't see in the training code where this information is stored.

Are the image statistics needed for normalization stored in the model so that they can be used during prediction?

1 Answer

0 votes
by (33.1k points)

In Keras, you can't provide the standardization statistics manually, but, there is an easy method to solve this.

Let’s say you have a function normalize(x) which is normalizing an image batch. You can make your own generator with normalization by using:

def gen_with_norm(gen, normalize):

    for x, y in gen:

        yield normalize(x), y

Simply use gen_with_norm(datagen.flow, normalize) instead of datagen.flow. The mean and std computed by a fit method by getting it from appropriate fields in datagen.

For more details to master Keras, get certified in Machine Learning Certification. Studying Artificial Intelligence would also give one an optimum amount of knowledge about the aforementioned topic.

Hope this answer helps you!

Browse Categories

...