How to augment gamma correction randomly?

The document https://pytorch.org/docs/stable/torchvision/transforms.html#torchvision.transforms.functional.adjust_gamma us how to add gamma correction, but it seems to use a constant gain for the transformation. Is there any way to adjust randommly?

You could write a custom transformation using the functional API es described here.
You could pass the desired ranges for gamma and gain, sample these values randomly in the __call__ method, and apply them on your images.

class RandomGammaCorrection(object):
“”"
Apply Gamma Correction to the images
“”"

def __init__(self, gamma = None):
    self.gamma = gamma
    
    
def __call__(self,image):
    
    if self.gamma == None:
        # more chances of selecting 0 (original image)
        gammas = [0,0,0,0.5,1,1.5]
        self.gamma = random.choice(gammas)
    
    print(self.gamma)
    if self.gamma == 0:
        return image
    
    else:
        return TF.adjust_gamma(image, self.gamma, gain=1)
1 Like