As an example, consider fine-tuning a Resnet50 model in Keras. For example here:
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
train_datagen = ImageDataGenerator()
train_generator = train_datagen.flow_from_directory(
"./data/train",
target_size=(299, 299),
batch_size=50,
class_mode='binary')
model.fit_generator(train_generator, steps_per_epoch=100)
What confuses me is why the ImageDataGenerator isn't given a preprocessing_function specification that aligns with what Resnet50 expects. Specifically Resnet50.preprocess_input() is supplied in the ResNet50 package. ImageDataGenerator's input looks like:
keras.preprocessing.image.ImageDataGenerator(featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zca_epsilon=1e-6,
rotation_range=0.,
width_shift_range=0.,
height_shift_range=0.,
shear_range=0.,
zoom_range=0.,
channel_shift_range=0.,
fill_mode='nearest',
cval=0.,
horizontal_flip=False,
vertical_flip=False,
rescale=None,
preprocessing_function=None,
data_format=K.image_data_format())
So I'm confused what the correct initialization of ImageDataGenerator would be. I could set preprocessing_function=resnet50.Resnet50.preprocess_input, but then I'm not sure what to set for the rest of the ImageDataGenerator parameters, as some of them are nonzero, like for zca.
Note: I'm not just interested in Resnet50, but for ANY model in general. There seem to be some defaults in Keras, like defaulting to 'caffe' or 'inception' normalizations.
You can pass the name of the preprocessing function to the preprocessing argument. If you do not want data augmentation, you do not need to pass anything else.
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
You can also write your own custom preprocessing function and pass it as an argument. Make sure that the argument and the return value of your custom function are numpy arrays.
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