|
| 1 | +import keras.src.layers.preprocessing.image_preprocessing.random_brightness as random_brightness # noqa: E501 |
| 2 | +import keras.src.layers.preprocessing.image_preprocessing.random_contrast as random_contrast # noqa: E501 |
| 3 | +import keras.src.layers.preprocessing.image_preprocessing.random_hue as random_hue # noqa: E501 |
| 4 | +import keras.src.layers.preprocessing.image_preprocessing.random_saturation as random_saturation # noqa: E501 |
| 5 | +from keras.src.api_export import keras_export |
| 6 | +from keras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layer import ( # noqa: E501 |
| 7 | + BaseImagePreprocessingLayer, |
| 8 | +) |
| 9 | +from keras.src.random.seed_generator import SeedGenerator |
| 10 | +from keras.src.utils import backend_utils |
| 11 | + |
| 12 | + |
| 13 | +@keras_export("keras.layers.RandomColorJitter") |
| 14 | +class RandomColorJitter(BaseImagePreprocessingLayer): |
| 15 | + """RandomColorJitter class randomly apply brightness, contrast, saturation |
| 16 | + and hue image processing operation sequentially and randomly on the |
| 17 | + input. |
| 18 | +
|
| 19 | + Args: |
| 20 | + value_range: the range of values the incoming images will have. |
| 21 | + Represented as a two number tuple written [low, high]. |
| 22 | + This is typically either `[0, 1]` or `[0, 255]` depending |
| 23 | + on how your preprocessing pipeline is set up. |
| 24 | + brightness_factor: Float or a list/tuple of 2 floats between -1.0 |
| 25 | + and 1.0. The factor is used to determine the lower bound and |
| 26 | + upper bound of the brightness adjustment. A float value will |
| 27 | + be chosen randomly between the limits. When -1.0 is chosen, |
| 28 | + the output image will be black, and when 1.0 is chosen, the |
| 29 | + image will be fully white. When only one float is provided, |
| 30 | + eg, 0.2, then -0.2 will be used for lower bound and 0.2 will |
| 31 | + be used for upper bound. |
| 32 | + contrast_factor: a positive float represented as fraction of value, |
| 33 | + or a tuple of size 2 representing lower and upper bound. When |
| 34 | + represented as a single float, lower = upper. The contrast |
| 35 | + factor will be randomly picked between `[1.0 - lower, 1.0 + |
| 36 | + upper]`. For any pixel x in the channel, the output will be |
| 37 | + `(x - mean) * factor + mean` where `mean` is the mean value |
| 38 | + of the channel. |
| 39 | + saturation_factor: A tuple of two floats or a single float. `factor` |
| 40 | + controls the extent to which the image saturation is impacted. |
| 41 | + `factor=0.5` makes this layer perform a no-op operation. |
| 42 | + `factor=0.0` makes the image fully grayscale. `factor=1.0` |
| 43 | + makes the image fully saturated. Values should be between |
| 44 | + `0.0` and `1.0`. If a tuple is used, a `factor` is sampled |
| 45 | + between the two values for every image augmented. If a single |
| 46 | + float is used, a value between `0.0` and the passed float is |
| 47 | + sampled. To ensure the value is always the same, pass a tuple |
| 48 | + with two identical floats: `(0.5, 0.5)`. |
| 49 | + hue_factor: A single float or a tuple of two floats. `factor` |
| 50 | + controls the extent to which the image hue is impacted. |
| 51 | + `factor=0.0` makes this layer perform a no-op operation, |
| 52 | + while a value of `1.0` performs the most aggressive contrast |
| 53 | + adjustment available. If a tuple is used, a `factor` is |
| 54 | + sampled between the two values for every image augmented. |
| 55 | + If a single float is used, a value between `0.0` and the |
| 56 | + passed float is sampled. In order to ensure the value is |
| 57 | + always the same, please pass a tuple with two identical |
| 58 | + floats: `(0.5, 0.5)`. |
| 59 | + seed: Integer. Used to create a random seed. |
| 60 | + """ |
| 61 | + |
| 62 | + def __init__( |
| 63 | + self, |
| 64 | + value_range=(0, 255), |
| 65 | + brightness_factor=None, |
| 66 | + contrast_factor=None, |
| 67 | + saturation_factor=None, |
| 68 | + hue_factor=None, |
| 69 | + seed=None, |
| 70 | + data_format=None, |
| 71 | + **kwargs, |
| 72 | + ): |
| 73 | + super().__init__(data_format=data_format, **kwargs) |
| 74 | + self.value_range = value_range |
| 75 | + self.brightness_factor = brightness_factor |
| 76 | + self.contrast_factor = contrast_factor |
| 77 | + self.saturation_factor = saturation_factor |
| 78 | + self.hue_factor = hue_factor |
| 79 | + self.seed = seed |
| 80 | + self.generator = SeedGenerator(seed) |
| 81 | + |
| 82 | + self.random_brightness = None |
| 83 | + self.random_contrast = None |
| 84 | + self.random_saturation = None |
| 85 | + self.random_hue = None |
| 86 | + |
| 87 | + if self.brightness_factor is not None: |
| 88 | + self.random_brightness = random_brightness.RandomBrightness( |
| 89 | + factor=self.brightness_factor, |
| 90 | + value_range=self.value_range, |
| 91 | + seed=self.seed, |
| 92 | + ) |
| 93 | + |
| 94 | + if self.contrast_factor is not None: |
| 95 | + self.random_contrast = random_contrast.RandomContrast( |
| 96 | + factor=self.contrast_factor, |
| 97 | + value_range=self.value_range, |
| 98 | + seed=self.seed, |
| 99 | + ) |
| 100 | + |
| 101 | + if self.saturation_factor is not None: |
| 102 | + self.random_saturation = random_saturation.RandomSaturation( |
| 103 | + factor=self.saturation_factor, |
| 104 | + value_range=self.value_range, |
| 105 | + seed=self.seed, |
| 106 | + ) |
| 107 | + |
| 108 | + if self.hue_factor is not None: |
| 109 | + self.random_hue = random_hue.RandomHue( |
| 110 | + factor=self.hue_factor, |
| 111 | + value_range=self.value_range, |
| 112 | + seed=self.seed, |
| 113 | + ) |
| 114 | + |
| 115 | + def transform_images(self, images, transformation, training=True): |
| 116 | + if training: |
| 117 | + if backend_utils.in_tf_graph(): |
| 118 | + self.backend.set_backend("tensorflow") |
| 119 | + images = self.backend.cast(images, self.compute_dtype) |
| 120 | + if self.brightness_factor is not None: |
| 121 | + if backend_utils.in_tf_graph(): |
| 122 | + self.random_brightness.backend.set_backend("tensorflow") |
| 123 | + transformation = ( |
| 124 | + self.random_brightness.get_random_transformation( |
| 125 | + images, |
| 126 | + seed=self._get_seed_generator(self.backend._backend), |
| 127 | + ) |
| 128 | + ) |
| 129 | + images = self.random_brightness.transform_images( |
| 130 | + images, transformation |
| 131 | + ) |
| 132 | + if self.contrast_factor is not None: |
| 133 | + if backend_utils.in_tf_graph(): |
| 134 | + self.random_contrast.backend.set_backend("tensorflow") |
| 135 | + transformation = self.random_contrast.get_random_transformation( |
| 136 | + images, seed=self._get_seed_generator(self.backend._backend) |
| 137 | + ) |
| 138 | + transformation["contrast_factor"] = self.backend.cast( |
| 139 | + transformation["contrast_factor"], dtype=self.compute_dtype |
| 140 | + ) |
| 141 | + images = self.random_contrast.transform_images( |
| 142 | + images, transformation |
| 143 | + ) |
| 144 | + if self.saturation_factor is not None: |
| 145 | + if backend_utils.in_tf_graph(): |
| 146 | + self.random_saturation.backend.set_backend("tensorflow") |
| 147 | + transformation = ( |
| 148 | + self.random_saturation.get_random_transformation( |
| 149 | + images, |
| 150 | + seed=self._get_seed_generator(self.backend._backend), |
| 151 | + ) |
| 152 | + ) |
| 153 | + images = self.random_saturation.transform_images( |
| 154 | + images, transformation |
| 155 | + ) |
| 156 | + if self.hue_factor is not None: |
| 157 | + if backend_utils.in_tf_graph(): |
| 158 | + self.random_hue.backend.set_backend("tensorflow") |
| 159 | + transformation = self.random_hue.get_random_transformation( |
| 160 | + images, seed=self._get_seed_generator(self.backend._backend) |
| 161 | + ) |
| 162 | + images = self.random_hue.transform_images( |
| 163 | + images, transformation |
| 164 | + ) |
| 165 | + images = self.backend.cast(images, self.compute_dtype) |
| 166 | + return images |
| 167 | + |
| 168 | + def transform_labels(self, labels, transformation, training=True): |
| 169 | + return labels |
| 170 | + |
| 171 | + def transform_bounding_boxes( |
| 172 | + self, |
| 173 | + bounding_boxes, |
| 174 | + transformation, |
| 175 | + training=True, |
| 176 | + ): |
| 177 | + return bounding_boxes |
| 178 | + |
| 179 | + def transform_segmentation_masks( |
| 180 | + self, segmentation_masks, transformation, training=True |
| 181 | + ): |
| 182 | + return segmentation_masks |
| 183 | + |
| 184 | + def compute_output_shape(self, input_shape): |
| 185 | + return input_shape |
| 186 | + |
| 187 | + def get_config(self): |
| 188 | + config = { |
| 189 | + "value_range": self.value_range, |
| 190 | + "brightness_factor": self.brightness_factor, |
| 191 | + "contrast_factor": self.contrast_factor, |
| 192 | + "saturation_factor": self.saturation_factor, |
| 193 | + "hue_factor": self.hue_factor, |
| 194 | + "seed": self.seed, |
| 195 | + } |
| 196 | + base_config = super().get_config() |
| 197 | + return {**base_config, **config} |
0 commit comments