def get_params(brightness, contrast, saturation, hue): """Get a randomized transform to be applied on image. Arguments are same as that of __init__. Returns: Transform which randomly adjusts brightness, contrast and saturation in a random order. """ transforms = [] if brightness is not None: brightness_factor = random.uniform(brightness[0], brightness[1]) transforms.append( Lambda( lambda img: F.adjust_brightness(img, brightness_factor))) if contrast is not None: contrast_factor = random.uniform(contrast[0], contrast[1]) transforms.append( Lambda(lambda img: F.adjust_contrast(img, contrast_factor))) if saturation is not None: saturation_factor = random.uniform(saturation[0], saturation[1]) transforms.append( Lambda( lambda img: F.adjust_saturation(img, saturation_factor))) if hue is not None: hue_factor = random.uniform(hue[0], hue[1]) transforms.append( Lambda(lambda img: F.adjust_hue(img, hue_factor))) random.shuffle(transforms) transform = Compose(transforms) return transform
def get_params(brightness, contrast, saturation, hue): """Get a randomized transform to be applied on image. Arguments are same as that of __init__. Returns: Transform which randomly adjusts brightness, contrast and saturation in a random order. """ transforms = [] if brightness > 0: brightness_factor = np.random.uniform(max(0, 1 - brightness), 1 + brightness) transforms.append(Lambda(lambda sample: F.adjust_brightness(sample, brightness_factor))) if contrast > 0: contrast_factor = np.random.uniform(max(0, 1 - contrast), 1 + contrast) transforms.append(Lambda(lambda sample: F.adjust_contrast(sample, contrast_factor))) if saturation > 0: saturation_factor = np.random.uniform(max(0, 1 - saturation), 1 + saturation) transforms.append(Lambda(lambda sample: F.adjust_saturation(sample, saturation_factor))) if hue > 0: hue_factor = np.random.uniform(-hue, hue) transforms.append(Lambda(lambda sample: F.adjust_hue(sample, hue_factor))) np.random.shuffle(transforms) transform = Compose(transforms) return transform
def __call__(self, img): transforms = [] if self.brightness > 0: brightness_factor = random.uniform(max(0, 1 - self.brightness), 1 + self.brightness) transforms.append( Lambda( lambda img: F.adjust_brightness(img, brightness_factor))) if self.contrast > 0: contrast_factor = random.uniform(max(0, 1 - self.contrast), 1 + self.contrast) transforms.append( Lambda(lambda img: F.adjust_contrast(img, contrast_factor))) if self.saturation > 0: saturation_factor = random.uniform(max(0, 1 - self.saturation), 1 + self.saturation) transforms.append( Lambda( lambda img: F.adjust_saturation(img, saturation_factor))) if self.hue > 0: hue_factor = random.uniform(-self.hue, self.hue) transforms.append( Lambda(lambda img: F.adjust_hue(img, hue_factor))) random.shuffle(transforms) transform = Compose(transforms) return transform(img)