Search code examples
pythontensorflowkerasdata-augmentation

Randomly apply Keras `PreprocessingLayer`


Imgaug has a p parameter which defines a probability for how often a certain augmentation is applied e.g. in 50% of the inputs. Is there something similar for the Keras PreprocessingLayers? One example of such a layer is the RandomFlip. In imgaug we can say that this function should be activated with a probability p, but I guess that the Keras implementation assumes that 50% of the images are to be flipped.

The current implementations of the PreprocessingLayers used for augmentation have the following structure:

If training==True apply function. Else pass input forward.

def call(self,inputs, training=None, **kwargs):
      if training is None:
          training = K.learning_phase()
      def function():
          return do_something(input)

      # comparable to K.switch(condition,true_function,else_function)
      output = control_flow_util.smart_cond(training,function,lambda: inputs)
      return output

I could implement the random apply behavior by changing

    if training is None:
          training = K.learning_phase()

to something like

    if training is None:
          training = K.learning_phase()
          # most likely 'K.switch' or 'tf.cond' must be used instead of 'if' (but 'if' is more readable) 
          if training:
              training = uniform(0,1)<self.p

But I think this should be part of every RandomFunction layer. So I am asking here, does a parameter p already exist for the preprocessing layers? Or should I open an issue on GitHub?


Solution

  • Another note about KerasCV -- it also offers keras_cv.layers.MaybeApply, which does exactly what @Innat's answer suggests.