Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras ImageDataGenerator setting mean and std

I have a pretrained keras model and I want to use it on new dataset. I have weights, mean and std file from the pretrained model and I want to use flow_from_directory from Image Data Generator to load the new dataset. The problem is how can I set the mean and std file for normalization explicitly?

Thanks

like image 396
ali khodadadi Avatar asked Nov 06 '16 12:11

ali khodadadi


2 Answers

I think you can just use the 'featurewise_center' and 'featurewise_std_normalization' of ImageDataGenerator to handle that. Reference: https://keras.io/preprocessing/image/#imagedatagenerator-class

Say, the mean [R, G, B] value of your pre-trained dataset is [123.68, 116.779, 103.939], and the std is 64.0. You can then use the example code below: (using Keras 2 with TF backend, image_data_format='channels_last')

from keras.preprocessing import image

datagen = image.ImageDataGenerator(featurewise_center=True,
                                   featurewise_std_normalization=True)
datagen.mean = np.array([123.68, 116.779, 103.939], dtype=np.float32).reshape((1,1,3)) # ordering: [R, G, B]
datagen.std = 64.
batches = datagen.flow_from_directory(DATASET_PATH + '/train',
                                      target_size=(224,224),
                                      color_mode='rgb',
                                      class_mode='categorical',
                                      shuffle=True,
                                      batch_size=BATCH_SIZE)
like image 78
jkjung13 Avatar answered Sep 22 '22 14:09

jkjung13


I think the best way to achieve this is writing your own method to process the samples generated by flow_from_directory. It could be something like:

def custom_normilze_generator(directory, mean):
    for img in flow_from_directory(directory):
        yield (img - mean)
like image 27
S.Mohsen sh Avatar answered Sep 23 '22 14:09

S.Mohsen sh