Dave
Dave

Reputation: 564

Possibility of choosing imagenet_utils.preprocess_input modes from ImageDataGenerator

There is a preprocessing technique where we can preprocess image with respect to ImageNet dataset using the following:

from keras.applications import imagenet_utils
imagenet_utils.preprocess_input(image, mode='caffe')

You see I choose mode='caffe'. In fact, there are three modes as explained by Keras.applications API:

mode: One of "caffe", "tf" or "torch".
  - caffe: will convert the images from RGB to BGR,
      then will zero-center each color channel with
      respect to the ImageNet dataset,
      without scaling.
  - tf: will scale pixels between -1 and 1,
      sample-wise.
  - torch: will scale pixels between 0 and 1 and then
      will normalize each channel with respect to the
      ImageNet dataset.

Since I use ImageDataGenerator from keras.preprocessing.image in my code, is there a workaround to allow such modes to be set from ImageDataGenerator since these three modes are applied with respect to the ImageNet dataset?

Thank you

Upvotes: 0

Views: 309

Answers (1)

Raphael Meudec
Raphael Meudec

Reputation: 741

ImageDataGenerator has a preprocessing_function argument in which you can pass a function to be applied to the images. To adapt the mode, you can do the following:

from functools import partial
from keras.applications import imagenet_utils

imagenet_caffe_preprocessing_function = partial(imagenet_utils.preprocess_input, mode="caffe")

data_generator = ImageDataGenerator(preprocess_function=imagenet_caffe_preprocessing_function, ...)

Upvotes: 2

Related Questions