File size: 12,328 Bytes
9b33fca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
"""Photometric transforms."""

from __future__ import annotations

from collections.abc import Callable

import numpy as np
import torch
import torchvision.transforms.v2.functional as TF
from torch import Tensor

from vis4d.common.imports import OPENCV_AVAILABLE
from vis4d.common.typing import NDArrayF32
from vis4d.data.const import CommonKeys as K

from .base import Transform

if OPENCV_AVAILABLE:
    import cv2
else:
    raise ImportError("cv2 is not installed.")


@Transform(K.images, K.images)
class RandomGamma:
    """Apply Gamma transformation to images.

    Args:
        gamma_range (tuple[float, float]): Range of gamma values.
        image_channel_mode (str, optional): Image channel mode. Defaults to
            "RGB".
    """

    def __init__(
        self,
        gamma_range: tuple[float, float] = (1.0, 1.0),
        image_channel_mode: str = "RGB",
    ) -> None:
        """Init function for Gamma."""
        self.gamma_range = gamma_range
        self.image_channel_mode = image_channel_mode
        assert image_channel_mode in {"RGB", "BGR"}, (
            "image_channel_mode should be 'RGB' or 'BGR', "
            f"got {image_channel_mode}."
        )

    def __call__(self, images: list[NDArrayF32]) -> list[NDArrayF32]:
        """Call function for Gamma transformation."""
        factor = np.random.uniform(self.gamma_range[0], self.gamma_range[1])
        return _adjust_images(
            images, TF.adjust_gamma, factor, self.image_channel_mode
        )


@Transform(K.images, K.images)
class RandomBrightness:
    """Apply Brightness transformation to images.

    Args:
        brightness_range (tuple[float, float]): Range of brightness values.
        image_channel_mode (str, optional): Image channel mode. Defaults to
            "RGB".
    """

    def __init__(
        self,
        brightness_range: tuple[float, float] = (1.0, 1.0),
        image_channel_mode: str = "RGB",
    ) -> None:
        """Init function for Brightness."""
        self.brightness_range = brightness_range
        self.image_channel_mode = image_channel_mode
        assert image_channel_mode in {"RGB", "BGR"}, (
            "image_channel_mode should be 'RGB' or 'BGR', "
            f"got {image_channel_mode}."
        )

    def __call__(self, images: list[NDArrayF32]) -> list[NDArrayF32]:
        """Call function for Brightness transformation."""
        factor = np.random.uniform(
            self.brightness_range[0], self.brightness_range[1]
        )
        return _adjust_images(
            images, TF.adjust_brightness, factor, self.image_channel_mode
        )


@Transform(K.images, K.images)
class RandomContrast:
    """Apply Contrast transformation to images.

    Args:
        contrast_range (tuple[float, float]): Range of contrast values.
        image_channel_mode (str, optional): Image channel mode. Defaults to
            "RGB".
    """

    def __init__(
        self,
        contrast_range: tuple[float, float] = (1.0, 1.0),
        image_channel_mode: str = "RGB",
    ):
        """Init function for Contrast."""
        self.contrast_range = contrast_range
        self.image_channel_mode = image_channel_mode
        assert image_channel_mode in {"RGB", "BGR"}, (
            "image_channel_mode should be 'RGB' or 'BGR', "
            f"got {image_channel_mode}."
        )

    def __call__(self, images: list[NDArrayF32]) -> list[NDArrayF32]:
        """Call function for Contrast transformation."""
        factor = np.random.uniform(
            self.contrast_range[0], self.contrast_range[1]
        )
        return _adjust_images(
            images, TF.adjust_contrast, factor, self.image_channel_mode
        )


@Transform(K.images, K.images)
class RandomSaturation:
    """Apply saturation transformation to images.

    Args:
        saturation_range (tuple[float, float]): Range of saturation values.
        image_channel_mode (str, optional): Image channel mode. Defaults to
            "RGB".
    """

    def __init__(
        self,
        saturation_range: tuple[float, float] = (1.0, 1.0),
        image_channel_mode: str = "RGB",
    ):
        """Init function for saturation."""
        self.saturation_range = saturation_range
        self.image_channel_mode = image_channel_mode
        assert image_channel_mode in {"RGB", "BGR"}, (
            "image_channel_mode should be 'RGB' or 'BGR', "
            f"got {image_channel_mode}."
        )

    def __call__(self, images: list[NDArrayF32]) -> list[NDArrayF32]:
        """Call function for saturation transformation."""
        factor = np.random.uniform(
            self.saturation_range[0], self.saturation_range[1]
        )
        return _adjust_images(
            images, TF.adjust_saturation, factor, self.image_channel_mode
        )


@Transform(K.images, K.images)
class RandomHue:
    """Apply hue transformation to images.

    Args:
        hue_range (tuple[float, float]): Range of hue values.
        image_channel_mode (str, optional): Image channel mode. Defaults to
            "RGB".
    """

    def __init__(
        self,
        hue_range: tuple[float, float] = (0.0, 0.0),
        image_channel_mode: str = "RGB",
    ):
        """Init function for hue."""
        self.hue_range = hue_range
        self.image_channel_mode = image_channel_mode
        assert image_channel_mode in {"RGB", "BGR"}, (
            "image_channel_mode should be 'RGB' or 'BGR', "
            f"got {image_channel_mode}."
        )

    def __call__(self, images: list[NDArrayF32]) -> list[NDArrayF32]:
        """Call function for Hue transformation."""
        factor = np.random.uniform(self.hue_range[0], self.hue_range[1])
        return _adjust_images(
            images, TF.adjust_hue, factor, self.image_channel_mode
        )


@Transform(K.images, K.images)
class ColorJitter:
    """Apply color jitter to images.

    Args:
        brightness_range (tuple[float, float]): Range of brightness values.
        contrast_range (tuple[float, float]): Range of contrast values.
        saturation_range (tuple[float, float]): Range of saturation values.
        hue_range (tuple[float, float]): Range of hue values.
        image_channel_mode (str, optional): Image channel mode. Defaults to
            "RGB".
    """

    def __init__(
        self,
        brightness_range: tuple[float, float] = (0.875, 1.125),
        contrast_range: tuple[float, float] = (0.5, 1.5),
        saturation_range: tuple[float, float] = (0.5, 1.5),
        hue_range: tuple[float, float] = (-0.05, 0.05),
        image_channel_mode: str = "RGB",
    ):
        """Init function for color jitter."""
        self.brightness_range = brightness_range
        self.contrast_range = contrast_range
        self.saturation_range = saturation_range
        self.hue_range = hue_range
        self.image_channel_mode = image_channel_mode
        assert image_channel_mode in {"RGB", "BGR"}, (
            "image_channel_mode should be 'RGB' or 'BGR', "
            f"got {image_channel_mode}."
        )

    def __call__(self, images: list[NDArrayF32]) -> list[NDArrayF32]:
        """Call function for Hue transformation."""
        transform_order = np.random.permutation(4)
        for transform in transform_order:
            # apply photometric transforms in a random order
            if transform == 0:
                # random brightness
                bfactor = np.random.uniform(
                    self.brightness_range[0], self.brightness_range[1]
                )
                images = _adjust_images(
                    images,
                    TF.adjust_brightness,
                    bfactor,
                    self.image_channel_mode,
                )
            elif transform == 1:
                # random contrast
                cfactor = np.random.uniform(
                    self.contrast_range[0], self.contrast_range[1]
                )
                images = _adjust_images(
                    images,
                    TF.adjust_contrast,
                    cfactor,
                    self.image_channel_mode,
                )
            elif transform == 2:
                # random saturation
                sfactor = np.random.uniform(
                    self.saturation_range[0], self.saturation_range[1]
                )
                images = _adjust_images(
                    images,
                    TF.adjust_saturation,
                    sfactor,
                    self.image_channel_mode,
                )
            elif transform == 3:
                # random hue
                hfactor = np.random.uniform(
                    self.hue_range[0], self.hue_range[1]
                )
                images = _adjust_images(
                    images, TF.adjust_hue, hfactor, self.image_channel_mode
                )
        return images


def _adjust_images(
    images: list[NDArrayF32],
    adjust_func: Callable[[Tensor, float], Tensor],
    adj_factor: float,
    image_channel_mode: str = "RGB",
) -> list[NDArrayF32]:
    """Apply color transformation to images.

    Args:
        images (list[NDArrayF32]): Image to be transformed.
        adjust_func (Callable[[Tensor, float], Tensor]): Function to apply.
        adj_factor (float): Adjustment factor.
        image_channel_mode (str, optional): Image channel mode. Defaults to
            "RGB".

    Returns:
        list[NDArrayF32]: Transformed image.
    """
    for i, image in enumerate(images):
        if image_channel_mode == "BGR":
            image = image[..., [2, 1, 0]]  # convert to RGB
        image_ = torch.from_numpy(image).permute(0, 3, 1, 2) / 255.0
        image_ = adjust_func(image_, adj_factor) * 255.0
        images[i] = image_.permute(0, 2, 3, 1).numpy()
        if image_channel_mode == "BGR":
            images[i] = images[i][..., [2, 1, 0]]  # convert back to BGR
    return images


@Transform(K.images, K.images)
class RandomHSV:
    """Apply HSV transformation to images.

    Used by YOLOX. Modifed from: https://github.com/Megvii-BaseDetection/YOLOX.

    Args:
        hue_delta (int): Delta for hue.
        saturation_delta (int): Delta for saturation.
        value_delta (int): Delta for value.
        image_channel_mode (str, optional): Image channel mode. Defaults to
            "BGR".
    """

    def __init__(
        self,
        hue_delta: int = 5,
        saturation_delta: int = 30,
        value_delta: int = 30,
        image_channel_mode: str = "BGR",
    ):
        """Init function for HSV transformation."""
        assert OPENCV_AVAILABLE, "RandomHSV requires OpenCV to be installed."
        self.hue_delta = hue_delta
        self.saturation_delta = saturation_delta
        self.value_delta = value_delta
        self.image_channel_mode = image_channel_mode
        assert image_channel_mode in {"RGB", "BGR"}, (
            "image_channel_mode should be 'RGB' or 'BGR', "
            f"got {image_channel_mode}."
        )

    # pylint: disable=no-member
    def __call__(self, images: list[NDArrayF32]) -> list[NDArrayF32]:
        """Call function for Hue transformation."""
        for i, image in enumerate(images):
            image = image[0].astype(np.uint8)
            if self.image_channel_mode == "BGR":
                image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
            else:
                image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
            image = image.astype(np.int16)
            hsv_gains = np.random.uniform(-1, 1, 3) * [
                self.hue_delta,
                self.saturation_delta,
                self.value_delta,
            ]
            # random selection of h, s, v
            hsv_gains = (hsv_gains * np.random.randint(0, 2, 3)).astype(
                np.int16
            )
            image[..., 0] = (image[..., 0] + hsv_gains[0]) % 180
            image[..., 1] = np.clip(image[..., 1] + hsv_gains[1], 0, 255)
            image[..., 2] = np.clip(image[..., 2] + hsv_gains[2], 0, 255)
            image = image.astype(np.uint8)
            if self.image_channel_mode == "BGR":
                cv2.cvtColor(image, cv2.COLOR_HSV2BGR, dst=image)
            else:
                cv2.cvtColor(image, cv2.COLOR_HSV2RGB, dst=image)
            images[i] = image[None, ...].astype(np.float32)
        return images