featurewise std normalization can not fit for each batch in flow from directory

Viewed 134

I want to normalize and standardize the images of each batch from Dataset. The formula is: Xi = (Xi - mean) / std (pixel-level global mean (SD))

I use ImageDataGenerator and flow_from_directory as below:

datagen = ImageDataGenerator(featurewise_center=True, 
                             featurewise_std_normalization=True, 
                             vertical_flip=True,              #data augmentation 
                             validation_split= 0.1,           #validation
                             rescale=1./255,                  #Scaling
                             horizontal_flip=True)            #data augmentation 

train_gen = datagen.flow_from_directory(
            train_data_dir,
            target_size=(img_height,img_width),
            batch_size=batch_size,
            class_mode='binary',
            subset='training',
            shuffle=True)

valid_gen = datagen.flow_from_directory(...)

model = Model(...) #I use ResNet and some fully connected layer
model.compile(...)

trainer= model.fit(train_gen,
                   validation_data= valid_gen,
                   epochs=16,
                   verbose=2)

As what I need, I use featurewise_center and featurewise_std_normalization in ImageDataGenerator. but it seems that these options do NOT work with flow_from_directory ! also I should use flow_from_directory because my data is heavy (I don't have enough RAM for processing whole the dataset at the same time).

Can you give me some recommendations or advice for solving this problem,. i.e how to implement pixel-level global mean (SD) for pictures in this method?

1 Answers

In order to achieve the effect you must run train_generator.fit before you train your model. Documentation for this is here.

Related