0

I have noticed there is a preprocess_input function that is different according to the model you wanna use in tensorflow.keras.applications.

I am using ImageDataGenerator class to augment my data. More specificaly, I am using a CustomDataGenerator, that extend from the ImageDataGenerator class and adds a color transformation.

This is how it looks like:

class CustomDataGenerator(ImageDataGenerator):
    def __init__(self, color=False, **kwargs):
        super().__init__(preprocessing_function=self.augment_color, **kwargs)

        self.hue = None

        if color:
            self.hue = random.random()

    def augment_color(self, img):
        if not self.hue or random.random() < 1/3:
            return img

        img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        img_hsv[:, :, 0] = self.hue

        return cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR)

I was using rescale=1./255 on the ImageDataGenerator, but some models need different preprocessing.

So when I try

CustomDataGenerator(preprocessing_function=tf.keras.applications.xception.preprocess_input)

I get this error:

__init__() got multiple values for keyword argument 'preprocessing_function'
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Azazel
  • 183
  • 1
  • 1
  • 10

1 Answers1

0

The issue is, you are already passing preprocessing_function once here

super().__init__(preprocessing_function=self.augment_color, **kwargs)

Then again passing it from

CustomDataGenerator(preprocessing_function=tf.keras.applications.xception.preprocess_input)

So now it is like

super().__init__(preprocessing_function=self.augment_color, preprocessing_function=tf.keras.applications.xception.preprocess_input)

Remove one of them and you would be good to go.

Edit 1: In case you want to keep both , better merge them into a single preprocess method and pass that as preprocessing_function instead

Add following method to CustomDataGenerator

    def preprocess(self, image):
        image = self.augment_color(image)
        return tf.keras.applications.xception.preprocess_input(image)

Use that as preprocessing function

super().__init__(preprocessing_function=self.preprocess, **kwargs)
Khairul
  • 386
  • 1
  • 7
  • Mmmm, why would i remove any of them? I mean, I still want the color transformation to be done – Azazel Jun 09 '21 at 15:19
  • So i guess you want multiple preprocessing ? Something similar is here https://stackoverflow.com/questions/52458017/use-keras-imagedatagenerator-with-multiple-preprocessing-functions and https://stackoverflow.com/questions/57890259/how-do-i-add-mulitipile-preprocessing-functions-to-a-datagenerator-in-keras – Khairul Jun 09 '21 at 15:21
  • Mmmm basically i wanted to preprocess my dataset according to the preprocess_input funtion from the current model. But i also wanted to apply a color transformation – Azazel Jun 09 '21 at 15:23
  • then its best to merge those preprocessings in a single method and only pass that as preprocessing_function – Khairul Jun 09 '21 at 15:43
  • But docs say the preprocessing_function function is applied after all the transformations are done. So the color transformation will undo the pixels value rescale/shift – Azazel Jun 09 '21 at 15:46