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
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With