NVIDIA DALI: Image distortion with a probability

I would like to use NVIDIA DALI library to replace my current image pre-processing pipeline written in TensorFlow.

Currently I have the following code that applies random contrast distortion with the probability 0.5:

def random_contrast(image, lower=0.5, upper=1.5, prob=0.5, seed=None):
        random_value = random_float(seed=seed)
        image = tf.cond(tf.greater(prob, random_value),
                        lambda: tf.image.random_contrast(image, lower=lower, upper=upper, seed=seed),
                        lambda: image)
        return image

The corresponding Contrast op (https://docs.nvidia.com/deeplearning/sdk/dali-developer-guide/docs/supported_ops.html#contrast) from DALI allows me to randomly alter the image contrast:

class MyPipeline(dali.pipeline.Pipeline):
        def __init__(self, ...):
            self.input = dali.ops.TFRecordReader(...)
            ...
    
            self.random_contrast_value = dali.ops.Uniform(range=[0.5, 1.5])
            self.random_contrast = dali.ops.Contrast()
    
    
        def define_graph(self):
            # Read images and labels
            inputs = self.input(name="Reader")
            images = inputs["image/encoded"]
            labels = inputs["image/class/label"].gpu()
    
            # Decode and augmentation
            images = self.decode(images)
            images = self.resize(images)

            images = self.random_contrast(images.gpu(), contrast=self.random_contrast_value())
    
            return (images, labels)

But this will apply the distortion to every single image. How can I make the operation being called only with 50% probability?

Hi,
Sorry for the late response, we expect to get the questions on GitHub directly.
You can use the CoinFlip operator to mask the contrast, something like:

    self.random_contrast_value = dali.ops.Uniform(range=[-0.5, 0.5])
    self.coin_value = dali.ops.CoinFlip()
def define_graph(self):
    contrast=self.random_contrast_value()*self.coin_value() + 1
    images = self.random_contrast(images.gpu(), contrast=contrast)