1
$\begingroup$

I use keras for training an image classification problem as follows:

datagen = ImageDataGenerator(
    featurewise_center=False,
    featurewise_std_normalization=False,
    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)


# alternative to model.fit_generator
for e in range(epochs):
    print('Epoch', e)
    batches = 0
    for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=32):
        model.fit(x_batch, y_batch)
        batches += 1
        if batches >= len(x_train) / 32:
            # we need to break the loop by hand because
            # the generator loops indefinitely
            break

I would like to know if datagen.fit is required if I do not apply any data normalization?

$\endgroup$

1 Answer 1

1
$\begingroup$

According to the comment of the documentation in the code:

# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(x_train)

It does the normalization, reducing mean and dividing by standard deviation, and more things like PCA. So it seems that you don't need to do normalization. That method does that, and normalizing features is required for accelerating training process and caring about all features with different scales the same.

$\endgroup$
8
  • $\begingroup$ one more question friend, if I add the following for validation : ValidScore = model.evaluate(x_val, y_val, batch_size=32,verbose =1) directly after model.fit is applied, would I have to apply datagen.flow to my (x_val, y_val) ? $\endgroup$
    – user121
    Feb 16, 2018 at 15:44
  • $\begingroup$ @unknown121 yes, definitely. Your validation data should be normalized as well. Because your network has learned the normalized data, so the validation data should be normalized too. $\endgroup$ Feb 16, 2018 at 15:51
  • $\begingroup$ i) But if I do not normalize and only apply rotation, is it still necessary to do the datagen.flow? ii)I am passing the valid. data this way but it is not allowing me to : ` TrainScore =model.fit(x_batch,y_batch,callbacks=[lr_reduce,model_checkpoint],verbose =1,validation_data=datagen.flow(x_val, y_val ))` $\endgroup$
    – user121
    Feb 16, 2018 at 16:03
  • $\begingroup$ @unknown121 data normalization is different from transformation and translation. Are your familiar with that? $\endgroup$ Feb 16, 2018 at 16:06
  • $\begingroup$ ok yes so datagen.fit is not needed when transformations are applied then. Also how can I apply datagen.flow to validation data. I am trying this but giving an error of improper arguments: TrainScore=model.fit(x_batch,y_batch,callbacks=[lr_reduce,model_checkpoint],verbose =1,validation_data=datagen.flow(x_val, y_val )) $\endgroup$
    – user121
    Feb 16, 2018 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.