Skip to content

Transforms

KeypointTransform

Bases: object

Base class for all transforms for keypoints augmnetation. All transforms subclassing it should implement call method which takes image, mask and keypoints as input and returns transformed image, mask and keypoints.

Source code in training/transforms/keypoint_transforms.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class KeypointTransform(object):
    """
    Base class for all transforms for keypoints augmnetation.
    All transforms subclassing it should implement __call__ method which takes image, mask and keypoints as input and
    returns transformed image, mask and keypoints.
    """

    @abstractmethod
    def __call__(
        self, image: np.ndarray, mask: np.ndarray, joints: np.ndarray, areas: Optional[np.ndarray], bboxes: Optional[np.ndarray]
    ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, Optional[np.ndarray], Optional[np.ndarray]]:
        """
        Apply transformation to image, mask and keypoints.

        :param image: Input image of [H,W,3] shape
        :param mask: Numpy array of [H,W] shape, where zero values are considered as ignored mask (not contributing to the loss)
        :param joints: Numpy array of [NumInstances, NumJoints, 3] shape. Last dimension contains (x,y,visibility) for each joint.
        :param areas: (Optional) Numpy array of [N] shape with area of each instance
        :param bboxes: (Optional) Numpy array of [N,4] shape with bounding box of each instance (XYWH)
        :return: (image, mask, joints)
        """
        raise NotImplementedError

__call__(image, mask, joints, areas, bboxes) abstractmethod

Apply transformation to image, mask and keypoints.

Parameters:

Name Type Description Default
image np.ndarray

Input image of [H,W,3] shape

required
mask np.ndarray

Numpy array of [H,W] shape, where zero values are considered as ignored mask (not contributing to the loss)

required
joints np.ndarray

Numpy array of [NumInstances, NumJoints, 3] shape. Last dimension contains (x,y,visibility) for each joint.

required
areas Optional[np.ndarray]

(Optional) Numpy array of [N] shape with area of each instance

required
bboxes Optional[np.ndarray]

(Optional) Numpy array of [N,4] shape with bounding box of each instance (XYWH)

required

Returns:

Type Description
Tuple[np.ndarray, np.ndarray, np.ndarray, Optional[np.ndarray], Optional[np.ndarray]]

(image, mask, joints)

Source code in training/transforms/keypoint_transforms.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@abstractmethod
def __call__(
    self, image: np.ndarray, mask: np.ndarray, joints: np.ndarray, areas: Optional[np.ndarray], bboxes: Optional[np.ndarray]
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, Optional[np.ndarray], Optional[np.ndarray]]:
    """
    Apply transformation to image, mask and keypoints.

    :param image: Input image of [H,W,3] shape
    :param mask: Numpy array of [H,W] shape, where zero values are considered as ignored mask (not contributing to the loss)
    :param joints: Numpy array of [NumInstances, NumJoints, 3] shape. Last dimension contains (x,y,visibility) for each joint.
    :param areas: (Optional) Numpy array of [N] shape with area of each instance
    :param bboxes: (Optional) Numpy array of [N,4] shape with bounding box of each instance (XYWH)
    :return: (image, mask, joints)
    """
    raise NotImplementedError

KeypointsImageNormalize

Bases: KeypointTransform

Normalize image with mean and std. Note this transform should come after KeypointsImageToTensor since it operates on torch Tensor and not numpy array.

Source code in training/transforms/keypoint_transforms.py
71
72
73
74
75
76
77
78
79
80
81
82
83
class KeypointsImageNormalize(KeypointTransform):
    """
    Normalize image with mean and std. Note this transform should come after KeypointsImageToTensor
    since it operates on torch Tensor and not numpy array.
    """

    def __init__(self, mean, std):
        self.mean = mean
        self.std = std

    def __call__(self, image: np.ndarray, mask: np.ndarray, joints: np.ndarray, areas: Optional[np.ndarray], bboxes: Optional[np.ndarray]):
        image = F.normalize(image, mean=self.mean, std=self.std)
        return image, mask, joints, areas, bboxes

KeypointsImageToTensor

Bases: KeypointTransform

Convert image from numpy array to tensor and permute axes to [C,H,W]. This function also divides image by 255.0 to convert it to [0,1] range.

Source code in training/transforms/keypoint_transforms.py
61
62
63
64
65
66
67
68
class KeypointsImageToTensor(KeypointTransform):
    """
    Convert image from numpy array to tensor and permute axes to [C,H,W].
    This function also divides image by 255.0 to convert it to [0,1] range.
    """

    def __call__(self, image: np.ndarray, mask: np.ndarray, joints: np.ndarray, areas: Optional[np.ndarray], bboxes: Optional[np.ndarray]):
        return F.to_tensor(image), mask, joints, areas, bboxes

KeypointsLongestMaxSize

Bases: KeypointTransform

Resize image, mask and joints to ensure that resulting image does not exceed max_sizes (rows, cols).

Source code in training/transforms/keypoint_transforms.py
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
class KeypointsLongestMaxSize(KeypointTransform):
    """
    Resize image, mask and joints to ensure that resulting image does not exceed max_sizes (rows, cols).
    """

    def __init__(self, max_height: int, max_width: int, interpolation: int = cv2.INTER_LINEAR, prob: float = 1.0):
        """

        :param max_sizes: (rows, cols) - Maximum size of the image after resizing
        :param interpolation: Used interpolation method for image
        :param prob: Probability of applying this transform
        """
        self.max_height = max_height
        self.max_width = max_width
        self.interpolation = interpolation
        self.prob = prob

    def __call__(self, image, mask, joints, areas: Optional[np.ndarray], bboxes: Optional[np.ndarray]):
        if random.random() < self.prob:
            height, width = image.shape[:2]
            scale = min(self.max_height / height, self.max_width / width)
            image = self.apply_to_image(image, scale, cv2.INTER_LINEAR)
            mask = self.apply_to_image(mask, scale, cv2.INTER_LINEAR)

            if image.shape[0] != self.max_height and image.shape[1] != self.max_width:
                raise RuntimeError(f"Image shape is not as expected (scale={scale}, input_shape={height, width}, resized_shape={image.shape[:2]})")

            if image.shape[0] > self.max_height or image.shape[1] > self.max_width:
                raise RuntimeError(f"Image shape is not as expected (scale={scale}, input_shape={height, width}, resized_shape={image.shape[:2]}")

            joints = self.apply_to_keypoints(joints, scale)
            if bboxes is not None:
                bboxes = self.apply_to_bboxes(bboxes, scale)

            if areas is not None:
                areas = areas * scale

        return image, mask, joints, areas, bboxes

    @classmethod
    def apply_to_image(cls, img, scale, interpolation):
        height, width = img.shape[:2]

        if scale != 1.0:
            new_height, new_width = tuple(int(dim * scale + 0.5) for dim in (height, width))
            img = cv2.resize(img, dsize=(new_width, new_height), interpolation=interpolation)
        return img

    @classmethod
    def apply_to_keypoints(cls, keypoints, scale):
        keypoints = keypoints.astype(np.float32, copy=True)
        keypoints[:, :, 0:2] *= scale
        return keypoints

    @classmethod
    def apply_to_bboxes(cls, bboxes, scale):
        return bboxes * scale

__init__(max_height, max_width, interpolation=cv2.INTER_LINEAR, prob=1.0)

Parameters:

Name Type Description Default
max_sizes

(rows, cols) - Maximum size of the image after resizing

required
interpolation int

Used interpolation method for image

cv2.INTER_LINEAR
prob float

Probability of applying this transform

1.0
Source code in training/transforms/keypoint_transforms.py
176
177
178
179
180
181
182
183
184
185
186
def __init__(self, max_height: int, max_width: int, interpolation: int = cv2.INTER_LINEAR, prob: float = 1.0):
    """

    :param max_sizes: (rows, cols) - Maximum size of the image after resizing
    :param interpolation: Used interpolation method for image
    :param prob: Probability of applying this transform
    """
    self.max_height = max_height
    self.max_width = max_width
    self.interpolation = interpolation
    self.prob = prob

KeypointsPadIfNeeded

Bases: KeypointTransform

Pad image and mask to ensure that resulting image size is not less than output_size (rows, cols). Image and mask padded from right and bottom, thus joints remains unchanged.

Source code in training/transforms/keypoint_transforms.py
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
class KeypointsPadIfNeeded(KeypointTransform):
    """
    Pad image and mask to ensure that resulting image size is not less than `output_size` (rows, cols).
    Image and mask padded from right and bottom, thus joints remains unchanged.
    """

    def __init__(self, min_height: int, min_width: int, image_pad_value: int, mask_pad_value: float):
        """

        :param output_size: Desired image size (rows, cols)
        :param image_pad_value: Padding value of image
        :param mask_pad_value: Padding value for mask
        """
        self.min_height = min_height
        self.min_width = min_width
        self.image_pad_value = tuple(image_pad_value) if isinstance(image_pad_value, Iterable) else int(image_pad_value)
        self.mask_pad_value = mask_pad_value

    def __call__(self, image, mask, joints, areas: Optional[np.ndarray], bboxes: Optional[np.ndarray]):
        height, width = image.shape[:2]

        pad_bottom = max(0, self.min_height - height)
        pad_right = max(0, self.min_width - width)

        image = cv2.copyMakeBorder(image, top=0, bottom=pad_bottom, left=0, right=pad_right, value=self.image_pad_value, borderType=cv2.BORDER_CONSTANT)

        original_dtype = mask.dtype
        mask = cv2.copyMakeBorder(
            mask.astype(np.uint8), top=0, bottom=pad_bottom, left=0, right=pad_right, value=self.mask_pad_value, borderType=cv2.BORDER_CONSTANT
        )
        mask = mask.astype(original_dtype)

        return image, mask, joints, areas, bboxes

__init__(min_height, min_width, image_pad_value, mask_pad_value)

Parameters:

Name Type Description Default
output_size

Desired image size (rows, cols)

required
image_pad_value int

Padding value of image

required
mask_pad_value float

Padding value for mask

required
Source code in training/transforms/keypoint_transforms.py
236
237
238
239
240
241
242
243
244
245
246
def __init__(self, min_height: int, min_width: int, image_pad_value: int, mask_pad_value: float):
    """

    :param output_size: Desired image size (rows, cols)
    :param image_pad_value: Padding value of image
    :param mask_pad_value: Padding value for mask
    """
    self.min_height = min_height
    self.min_width = min_width
    self.image_pad_value = tuple(image_pad_value) if isinstance(image_pad_value, Iterable) else int(image_pad_value)
    self.mask_pad_value = mask_pad_value

KeypointsRandomAffineTransform

Bases: KeypointTransform

Apply random affine transform to image, mask and joints.

Source code in training/transforms/keypoint_transforms.py
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
class KeypointsRandomAffineTransform(KeypointTransform):
    """
    Apply random affine transform to image, mask and joints.
    """

    def __init__(
        self,
        max_rotation: float,
        min_scale: float,
        max_scale: float,
        max_translate: float,
        image_pad_value: int,
        mask_pad_value: float,
        prob: float = 0.5,
    ):
        """

        :param max_rotation: Max rotation angle in degrees
        :param min_scale: Lower bound for the scale change. For +- 20% size jitter this should be 0.8
        :param max_scale: Lower bound for the scale change. For +- 20% size jitter this should be 1.2
        :param max_translate: Max translation offset in percents of image size
        """
        self.max_rotation = max_rotation
        self.min_scale = min_scale
        self.max_scale = max_scale
        self.max_translate = max_translate
        self.image_pad_value = tuple(image_pad_value) if isinstance(image_pad_value, Iterable) else int(image_pad_value)
        self.mask_pad_value = mask_pad_value
        self.prob = prob

    def _get_affine_matrix(self, img, angle, scale, dx, dy):
        """

        :param center: (x,y)
        :param scale:
        :param output_size: (rows, cols)
        :param rot:
        :return:
        """
        height, width = img.shape[:2]
        center = (width / 2 + dx * width, height / 2 + dy * height)
        matrix = cv2.getRotationMatrix2D(center, angle, scale)

        return matrix

    def __call__(self, image: np.ndarray, mask: np.ndarray, joints: np.ndarray, areas: Optional[np.ndarray], bboxes: Optional[np.ndarray]):
        """

        :param image: (np.ndarray) Image of shape [H,W,3]
        :param mask: Single-element array with mask of [H,W] shape.
        :param joints: Single-element array of joints of [Num instances, Num Joints, 3] shape. Semantics of last channel is: x, y, joint index (?)
        :param area: Area each instance occipy: [Num instances, 1]
        :return:
        """

        if random.random() < self.prob:
            angle = random.uniform(-self.max_rotation, self.max_rotation)
            scale = random.uniform(self.min_scale, self.max_scale)
            dx = random.uniform(-self.max_translate, self.max_translate)
            dy = random.uniform(-self.max_translate, self.max_translate)

            mat_output = self._get_affine_matrix(image, angle, scale, dx, dy)
            mat_output = mat_output[:2]

            mask = self.apply_to_image(mask, mat_output, cv2.INTER_NEAREST, self.mask_pad_value, cv2.BORDER_CONSTANT)
            image = self.apply_to_image(image, mat_output, cv2.INTER_LINEAR, self.image_pad_value, cv2.BORDER_CONSTANT)

            joints = self.apply_to_keypoints(joints, mat_output, image.shape)

            if bboxes is not None:
                bboxes = self.apply_to_bboxes(bboxes, mat_output)

            if areas is not None:
                areas = self.apply_to_areas(areas, mat_output)

        return image, mask, joints, areas, bboxes

    @classmethod
    def apply_to_areas(cls, areas, mat):
        det = np.linalg.det(mat[:2, :2])
        return areas * abs(det)

    @classmethod
    def apply_to_bboxes(cls, bboxes, mat: np.ndarray):
        def bbox_shift_scale_rotate(bbox, m):
            x_min, y_min, x_max, y_max = bbox[:4]

            x = np.array([x_min, x_max, x_max, x_min])
            y = np.array([y_min, y_min, y_max, y_max])
            ones = np.ones(shape=(len(x)))
            points_ones = np.vstack([x, y, ones]).transpose()

            tr_points = m.dot(points_ones.T).T

            x_min, x_max = min(tr_points[:, 0]), max(tr_points[:, 0])
            y_min, y_max = min(tr_points[:, 1]), max(tr_points[:, 1])

            return np.array([x_min, y_min, x_max, y_max])

        bboxes_xyxy = xywh_to_xyxy(bboxes, image_shape=None)
        bboxes_xyxy = np.array([bbox_shift_scale_rotate(box, mat) for box in bboxes_xyxy])
        return xyxy_to_xywh(bboxes_xyxy, image_shape=None)

    @classmethod
    def apply_to_keypoints(cls, keypoints: np.ndarray, mat: np.ndarray, image_shape):
        keypoints_with_visibility = keypoints.copy()
        keypoints = keypoints_with_visibility[:, :, 0:2]

        shape = keypoints.shape
        keypoints = keypoints.reshape(-1, 2)
        keypoints = np.dot(np.concatenate((keypoints, keypoints[:, 0:1] * 0 + 1), axis=1), mat.T).reshape(shape)

        # Update visibility status of joints that were moved outside visible area
        keypoints_with_visibility[:, :, 0:2] = keypoints
        joints_outside_image = (
            (keypoints[:, :, 0] < 0) | (keypoints[:, :, 0] >= image_shape[1]) | (keypoints[:, :, 1] < 0) | (keypoints[:, :, 1] >= image_shape[0])
        )
        keypoints_with_visibility[joints_outside_image, 2] = 0
        return keypoints_with_visibility

    @classmethod
    def apply_to_image(cls, image, mat, interpolation, padding_value, padding_mode=cv2.BORDER_CONSTANT):
        return cv2.warpAffine(
            image,
            mat,
            dsize=(image.shape[1], image.shape[0]),
            flags=interpolation,
            borderValue=padding_value,
            borderMode=padding_mode,
        )

__call__(image, mask, joints, areas, bboxes)

Parameters:

Name Type Description Default
image np.ndarray

(np.ndarray) Image of shape [H,W,3]

required
mask np.ndarray

Single-element array with mask of [H,W] shape.

required
joints np.ndarray

Single-element array of joints of [Num instances, Num Joints, 3] shape. Semantics of last channel is: x, y, joint index (?)

required
area

Area each instance occipy: [Num instances, 1]

required

Returns:

Type Description
Source code in training/transforms/keypoint_transforms.py
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
def __call__(self, image: np.ndarray, mask: np.ndarray, joints: np.ndarray, areas: Optional[np.ndarray], bboxes: Optional[np.ndarray]):
    """

    :param image: (np.ndarray) Image of shape [H,W,3]
    :param mask: Single-element array with mask of [H,W] shape.
    :param joints: Single-element array of joints of [Num instances, Num Joints, 3] shape. Semantics of last channel is: x, y, joint index (?)
    :param area: Area each instance occipy: [Num instances, 1]
    :return:
    """

    if random.random() < self.prob:
        angle = random.uniform(-self.max_rotation, self.max_rotation)
        scale = random.uniform(self.min_scale, self.max_scale)
        dx = random.uniform(-self.max_translate, self.max_translate)
        dy = random.uniform(-self.max_translate, self.max_translate)

        mat_output = self._get_affine_matrix(image, angle, scale, dx, dy)
        mat_output = mat_output[:2]

        mask = self.apply_to_image(mask, mat_output, cv2.INTER_NEAREST, self.mask_pad_value, cv2.BORDER_CONSTANT)
        image = self.apply_to_image(image, mat_output, cv2.INTER_LINEAR, self.image_pad_value, cv2.BORDER_CONSTANT)

        joints = self.apply_to_keypoints(joints, mat_output, image.shape)

        if bboxes is not None:
            bboxes = self.apply_to_bboxes(bboxes, mat_output)

        if areas is not None:
            areas = self.apply_to_areas(areas, mat_output)

    return image, mask, joints, areas, bboxes

__init__(max_rotation, min_scale, max_scale, max_translate, image_pad_value, mask_pad_value, prob=0.5)

Parameters:

Name Type Description Default
max_rotation float

Max rotation angle in degrees

required
min_scale float

Lower bound for the scale change. For +- 20% size jitter this should be 0.8

required
max_scale float

Lower bound for the scale change. For +- 20% size jitter this should be 1.2

required
max_translate float

Max translation offset in percents of image size

required
Source code in training/transforms/keypoint_transforms.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
def __init__(
    self,
    max_rotation: float,
    min_scale: float,
    max_scale: float,
    max_translate: float,
    image_pad_value: int,
    mask_pad_value: float,
    prob: float = 0.5,
):
    """

    :param max_rotation: Max rotation angle in degrees
    :param min_scale: Lower bound for the scale change. For +- 20% size jitter this should be 0.8
    :param max_scale: Lower bound for the scale change. For +- 20% size jitter this should be 1.2
    :param max_translate: Max translation offset in percents of image size
    """
    self.max_rotation = max_rotation
    self.min_scale = min_scale
    self.max_scale = max_scale
    self.max_translate = max_translate
    self.image_pad_value = tuple(image_pad_value) if isinstance(image_pad_value, Iterable) else int(image_pad_value)
    self.mask_pad_value = mask_pad_value
    self.prob = prob

KeypointsRandomHorizontalFlip

Bases: KeypointTransform

Flip image, mask and joints horizontally with a given probability.

Source code in training/transforms/keypoint_transforms.py
 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
class KeypointsRandomHorizontalFlip(KeypointTransform):
    """
    Flip image, mask and joints horizontally with a given probability.
    """

    def __init__(self, flip_index: List[int], prob: float = 0.5):
        """

        :param flip_index: Indexes of keypoints on the flipped image. When doing left-right flip, left hand becomes right hand.
                           So this array contains order of keypoints on the flipped image. This is dataset specific and depends on
                           how keypoints are defined in dataset.
        :param prob: Probability of flipping
        """
        self.flip_index = flip_index
        self.prob = prob

    def __call__(self, image, mask, joints, areas: Optional[np.ndarray], bboxes: Optional[np.ndarray]):
        if image.shape[:2] != mask.shape[:2]:
            raise RuntimeError(f"Image shape ({image.shape[:2]}) does not match mask shape ({mask.shape[:2]}).")

        if random.random() < self.prob:
            image = self.apply_to_image(image)
            mask = self.apply_to_image(mask)
            rows, cols = image.shape[:2]

            joints = self.apply_to_keypoints(joints, cols)

            if bboxes is not None:
                bboxes = self.apply_to_bboxes(bboxes, cols)

        return image, mask, joints, areas, bboxes

    def apply_to_image(self, image):
        return np.ascontiguousarray(np.fliplr(image))

    def apply_to_keypoints(self, keypoints, cols):
        keypoints = keypoints.copy()
        keypoints = keypoints[:, self.flip_index]
        keypoints[:, :, 0] = cols - keypoints[:, :, 0] - 1
        return keypoints

    def apply_to_bboxes(self, bboxes, cols):
        bboxes = bboxes.copy()
        bboxes[:, 0] = cols - (bboxes[:, 0] + bboxes[:, 2])
        return bboxes

__init__(flip_index, prob=0.5)

Parameters:

Name Type Description Default
flip_index List[int]

Indexes of keypoints on the flipped image. When doing left-right flip, left hand becomes right hand. So this array contains order of keypoints on the flipped image. This is dataset specific and depends on how keypoints are defined in dataset.

required
prob float

Probability of flipping

0.5
Source code in training/transforms/keypoint_transforms.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def __init__(self, flip_index: List[int], prob: float = 0.5):
    """

    :param flip_index: Indexes of keypoints on the flipped image. When doing left-right flip, left hand becomes right hand.
                       So this array contains order of keypoints on the flipped image. This is dataset specific and depends on
                       how keypoints are defined in dataset.
    :param prob: Probability of flipping
    """
    self.flip_index = flip_index
    self.prob = prob

KeypointsRandomVerticalFlip

Bases: KeypointTransform

Flip image, mask and joints vertically with a given probability.

Source code in training/transforms/keypoint_transforms.py
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
class KeypointsRandomVerticalFlip(KeypointTransform):
    """
    Flip image, mask and joints vertically with a given probability.
    """

    def __init__(self, prob: float = 0.5):
        self.prob = prob

    def __call__(self, image, mask, joints, areas: Optional[np.ndarray], bboxes: Optional[np.ndarray]):
        if image.shape[:2] != mask.shape[:2]:
            raise RuntimeError(f"Image shape ({image.shape[:2]}) does not match mask shape ({mask.shape[:2]}).")

        if random.random() < self.prob:
            image = self.apply_to_image(image)
            mask = self.apply_to_image(mask)

            rows, cols = image.shape[:2]
            joints = self.apply_to_keypoints(joints, rows)

            if bboxes is not None:
                bboxes = self.apply_to_bboxes(bboxes, rows)

        return image, mask, joints, areas, bboxes

    def apply_to_image(self, image):
        return np.ascontiguousarray(np.flipud(image))

    def apply_to_keypoints(self, keypoints, rows):
        keypoints = keypoints.copy()
        keypoints[:, :, 1] = rows - keypoints[:, :, 1] - 1
        return keypoints

    def apply_to_bboxes(self, bboxes, rows):
        bboxes = bboxes.copy()
        bboxes[:, 1] = rows - (bboxes[:, 1] + bboxes[:, 3]) - 1
        return bboxes

DetectionHSV

Bases: DetectionTransform

Detection HSV transform.

Attributes: prob: (float) probability to apply the transform. hgain: (float) hue gain (default=0.5) sgain: (float) saturation gain (default=0.5) vgain: (float) value gain (default=0.5) bgr_channels: (tuple) channel indices of the BGR channels- useful for images with >3 channels, or when BGR channels are in different order. (default=(0,1,2)).

Source code in training/transforms/transforms.py
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
class DetectionHSV(DetectionTransform):
    """
    Detection HSV transform.

    Attributes:
        prob: (float) probability to apply the transform.
        hgain: (float) hue gain (default=0.5)
        sgain: (float) saturation gain (default=0.5)
        vgain: (float) value gain (default=0.5)
        bgr_channels: (tuple) channel indices of the BGR channels- useful for images with >3 channels,
         or when BGR channels are in different order. (default=(0,1,2)).

    """

    def __init__(self, prob: float, hgain: float = 0.5, sgain: float = 0.5, vgain: float = 0.5, bgr_channels=(0, 1, 2)):
        super(DetectionHSV, self).__init__()
        self.prob = prob
        self.hgain = hgain
        self.sgain = sgain
        self.vgain = vgain
        self.bgr_channels = bgr_channels
        self._additional_channels_warned = False

    def __call__(self, sample: dict) -> dict:
        if sample["image"].shape[2] < 3:
            raise ValueError("HSV transform expects at least 3 channels, got: " + str(sample["image"].shape[2]))
        if sample["image"].shape[2] > 3 and not self._additional_channels_warned:
            logger.warning(
                "HSV transform received image with "
                + str(sample["image"].shape[2])
                + " channels. HSV transform will only be applied on channels: "
                + str(self.bgr_channels)
                + "."
            )
            self._additional_channels_warned = True
        if random.random() < self.prob:
            augment_hsv(sample["image"], self.hgain, self.sgain, self.vgain, self.bgr_channels)
        return sample

DetectionHorizontalFlip

Bases: DetectionTransform

Horizontal Flip for Detection

Attributes: prob: float: probability of applying horizontal flip max_targets: int: max objects in single image, padding target to this size in case of empty image.

Source code in training/transforms/transforms.py
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
class DetectionHorizontalFlip(DetectionTransform):
    """
    Horizontal Flip for Detection

    Attributes:
        prob: float: probability of applying horizontal flip
        max_targets: int: max objects in single image, padding target to this size in case of empty image.
    """

    def __init__(self, prob, max_targets: int = 120):
        super(DetectionHorizontalFlip, self).__init__()
        self.prob = prob
        self.max_targets = max_targets

    def __call__(self, sample):
        image, targets = sample["image"], sample["target"]
        boxes = targets[:, :4]
        if len(boxes) == 0:
            targets = np.zeros((self.max_targets, 5), dtype=np.float32)
            boxes = targets[:, :4]
        image, boxes = _mirror(image, boxes, self.prob)
        targets[:, :4] = boxes
        sample["target"] = targets
        sample["image"] = image
        return sample

DetectionMixup

Bases: DetectionTransform

Mixup detection transform

Attributes: input_dim: (tuple) input dimension. mixup_scale: (tuple) scale range for the additional loaded image for mixup. prob: (float) probability of applying mixup. enable_mixup: (bool) whether to apply mixup at all (regardless of prob) (default=True). flip_prob: (float) prbability to apply horizontal flip to the additional sample. border_value: value for filling borders after applying transform (default=114).

Source code in training/transforms/transforms.py
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
class DetectionMixup(DetectionTransform):
    """
    Mixup detection transform

    Attributes:
        input_dim: (tuple) input dimension.
        mixup_scale: (tuple) scale range for the additional loaded image for mixup.
        prob: (float) probability of applying mixup.
        enable_mixup: (bool) whether to apply mixup at all (regardless of prob) (default=True).
        flip_prob: (float) prbability to apply horizontal flip to the additional sample.
        border_value: value for filling borders after applying transform (default=114).

    """

    def __init__(self, input_dim, mixup_scale, prob=1.0, enable_mixup=True, flip_prob=0.5, border_value=114):
        super(DetectionMixup, self).__init__(additional_samples_count=1, non_empty_targets=True)
        self.input_dim = input_dim
        self.mixup_scale = mixup_scale
        self.prob = prob
        self.enable_mixup = enable_mixup
        self.flip_prob = flip_prob
        self.border_value = border_value

    def close(self):
        self.additional_samples_count = 0
        self.enable_mixup = False

    def __call__(self, sample: dict):
        if self.enable_mixup and random.random() < self.prob:
            origin_img, origin_labels = sample["image"], sample["target"]
            target_dim = self.input_dim if self.input_dim is not None else sample["image"].shape[:2]

            cp_sample = sample["additional_samples"][0]
            img, cp_labels = cp_sample["image"], cp_sample["target"]
            cp_boxes = cp_labels[:, :4]

            img, cp_boxes = _mirror(img, cp_boxes, self.flip_prob)
            # PLUG IN TARGET THE FLIPPED BOXES
            cp_labels[:, :4] = cp_boxes

            jit_factor = random.uniform(*self.mixup_scale)

            if len(img.shape) == 3:
                cp_img = np.ones((target_dim[0], target_dim[1], 3), dtype=np.uint8) * self.border_value
            else:
                cp_img = np.ones(target_dim, dtype=np.uint8) * self.border_value

            cp_scale_ratio = min(target_dim[0] / img.shape[0], target_dim[1] / img.shape[1])
            resized_img = cv2.resize(
                img,
                (int(img.shape[1] * cp_scale_ratio), int(img.shape[0] * cp_scale_ratio)),
                interpolation=cv2.INTER_LINEAR,
            )

            cp_img[: int(img.shape[0] * cp_scale_ratio), : int(img.shape[1] * cp_scale_ratio)] = resized_img

            cp_img = cv2.resize(
                cp_img,
                (int(cp_img.shape[1] * jit_factor), int(cp_img.shape[0] * jit_factor)),
            )
            cp_scale_ratio *= jit_factor

            origin_h, origin_w = cp_img.shape[:2]
            target_h, target_w = origin_img.shape[:2]

            if len(img.shape) == 3:
                padded_img = np.zeros((max(origin_h, target_h), max(origin_w, target_w), img.shape[2]), dtype=np.uint8)
            else:
                padded_img = np.zeros((max(origin_h, target_h), max(origin_w, target_w)), dtype=np.uint8)

            padded_img[:origin_h, :origin_w] = cp_img

            x_offset, y_offset = 0, 0
            if padded_img.shape[0] > target_h:
                y_offset = random.randint(0, padded_img.shape[0] - target_h - 1)
            if padded_img.shape[1] > target_w:
                x_offset = random.randint(0, padded_img.shape[1] - target_w - 1)
            padded_cropped_img = padded_img[y_offset : y_offset + target_h, x_offset : x_offset + target_w]

            cp_bboxes_origin_np = adjust_box_anns(cp_labels[:, :4].copy(), cp_scale_ratio, 0, 0, origin_w, origin_h)
            cp_bboxes_transformed_np = cp_bboxes_origin_np.copy()
            cp_bboxes_transformed_np[:, 0::2] = np.clip(cp_bboxes_transformed_np[:, 0::2] - x_offset, 0, target_w)
            cp_bboxes_transformed_np[:, 1::2] = np.clip(cp_bboxes_transformed_np[:, 1::2] - y_offset, 0, target_h)

            cls_labels = cp_labels[:, 4:5].copy()
            box_labels = cp_bboxes_transformed_np
            labels = np.hstack((box_labels, cls_labels))
            origin_labels = np.vstack((origin_labels, labels))
            origin_img = origin_img.astype(np.float32)
            origin_img = 0.5 * origin_img + 0.5 * padded_cropped_img.astype(np.float32)

            sample["image"], sample["target"] = origin_img.astype(np.uint8), origin_labels
        return sample

DetectionMosaic

Bases: DetectionTransform

DetectionMosaic detection transform

Attributes: input_dim: (tuple) input dimension. prob: (float) probability of applying mosaic. enable_mosaic: (bool) whether to apply mosaic at all (regardless of prob) (default=True). border_value: value for filling borders after applying transforms (default=114).

Source code in training/transforms/transforms.py
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
class DetectionMosaic(DetectionTransform):
    """
    DetectionMosaic detection transform

    Attributes:
        input_dim: (tuple) input dimension.
        prob: (float) probability of applying mosaic.
        enable_mosaic: (bool) whether to apply mosaic at all (regardless of prob) (default=True).
        border_value: value for filling borders after applying transforms (default=114).

    """

    def __init__(self, input_dim: tuple, prob: float = 1.0, enable_mosaic: bool = True, border_value=114):
        super(DetectionMosaic, self).__init__(additional_samples_count=3)
        self.prob = prob
        self.input_dim = input_dim
        self.enable_mosaic = enable_mosaic
        self.border_value = border_value

    def close(self):
        self.additional_samples_count = 0
        self.enable_mosaic = False

    def __call__(self, sample: Union[dict, list]):
        if self.enable_mosaic and random.random() < self.prob:
            mosaic_labels = []
            mosaic_labels_seg = []
            input_h, input_w = self.input_dim[0], self.input_dim[1]

            # yc, xc = s, s  # mosaic center x, y
            yc = int(random.uniform(0.5 * input_h, 1.5 * input_h))
            xc = int(random.uniform(0.5 * input_w, 1.5 * input_w))

            # 3 additional samples, total of 4
            all_samples = [sample] + sample["additional_samples"]

            for i_mosaic, mosaic_sample in enumerate(all_samples):
                img, _labels = mosaic_sample["image"], mosaic_sample["target"]
                _labels_seg = mosaic_sample.get("target_seg")

                h0, w0 = img.shape[:2]  # orig hw
                scale = min(1.0 * input_h / h0, 1.0 * input_w / w0)
                img = cv2.resize(img, (int(w0 * scale), int(h0 * scale)), interpolation=cv2.INTER_LINEAR)
                # generate output mosaic image
                (h, w, c) = img.shape[:3]
                if i_mosaic == 0:
                    mosaic_img = np.full((input_h * 2, input_w * 2, c), self.border_value, dtype=np.uint8)

                # suffix l means large image, while s means small image in mosaic aug.
                (l_x1, l_y1, l_x2, l_y2), (s_x1, s_y1, s_x2, s_y2) = get_mosaic_coordinate(i_mosaic, xc, yc, w, h, input_h, input_w)

                mosaic_img[l_y1:l_y2, l_x1:l_x2] = img[s_y1:s_y2, s_x1:s_x2]
                padw, padh = l_x1 - s_x1, l_y1 - s_y1

                labels = _labels.copy()

                # Normalized xywh to pixel xyxy format
                if _labels.size > 0:
                    labels[:, 0] = scale * _labels[:, 0] + padw
                    labels[:, 1] = scale * _labels[:, 1] + padh
                    labels[:, 2] = scale * _labels[:, 2] + padw
                    labels[:, 3] = scale * _labels[:, 3] + padh
                mosaic_labels.append(labels)

                if _labels_seg is not None:
                    labels_seg = _labels_seg.copy()
                    if _labels.size > 0:
                        labels_seg[:, ::2] = scale * labels_seg[:, ::2] + padw
                        labels_seg[:, 1::2] = scale * labels_seg[:, 1::2] + padh
                    mosaic_labels_seg.append(labels_seg)

            if len(mosaic_labels):
                mosaic_labels = np.concatenate(mosaic_labels, 0)
                np.clip(mosaic_labels[:, 0], 0, 2 * input_w, out=mosaic_labels[:, 0])
                np.clip(mosaic_labels[:, 1], 0, 2 * input_h, out=mosaic_labels[:, 1])
                np.clip(mosaic_labels[:, 2], 0, 2 * input_w, out=mosaic_labels[:, 2])
                np.clip(mosaic_labels[:, 3], 0, 2 * input_h, out=mosaic_labels[:, 3])

            if len(mosaic_labels_seg):
                mosaic_labels_seg = np.concatenate(mosaic_labels_seg, 0)
                np.clip(mosaic_labels_seg[:, ::2], 0, 2 * input_w, out=mosaic_labels_seg[:, ::2])
                np.clip(mosaic_labels_seg[:, 1::2], 0, 2 * input_h, out=mosaic_labels_seg[:, 1::2])

            sample["image"] = mosaic_img
            sample["target"] = mosaic_labels
            sample["info"] = (mosaic_img.shape[1], mosaic_img.shape[0])
            if len(mosaic_labels_seg):
                sample["target_seg"] = mosaic_labels_seg

        return sample

DetectionNormalize

Bases: DetectionTransform

Normalize image by subtracting mean and dividing by std.

Source code in training/transforms/transforms.py
940
941
942
943
944
945
946
947
948
949
950
951
952
class DetectionNormalize(DetectionTransform):
    """
    Normalize image by subtracting mean and dividing by std.
    """

    def __init__(self, mean, std):
        super().__init__()
        self.mean = np.array(list(mean)).reshape((1, 1, -1)).astype(np.float32)
        self.std = np.array(list(std)).reshape((1, 1, -1)).astype(np.float32)

    def __call__(self, sample: dict) -> dict:
        sample["image"] = (sample["image"] - self.mean) / self.std
        return sample

DetectionPaddedRescale

Bases: DetectionTransform

Preprocessing transform to be applied last of all transforms for validation.

Image- Rescales and pads to self.input_dim. Targets- pads targets to max_targets, moves the class label to first index, converts boxes format- xyxy -> cxcywh.

Attributes: input_dim: (tuple) final input dimension (default=(640,640)) swap: image axis's to be rearranged.

Source code in training/transforms/transforms.py
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
class DetectionPaddedRescale(DetectionTransform):
    """
    Preprocessing transform to be applied last of all transforms for validation.

    Image- Rescales and pads to self.input_dim.
    Targets- pads targets to max_targets, moves the class label to first index, converts boxes format- xyxy -> cxcywh.

    Attributes:
        input_dim: (tuple) final input dimension (default=(640,640))
        swap: image axis's to be rearranged.

    """

    def __init__(self, input_dim, swap=(2, 0, 1), max_targets=50, pad_value=114):
        self.swap = swap
        self.input_dim = input_dim
        self.max_targets = max_targets
        self.pad_value = pad_value

    def __call__(self, sample: Dict[str, np.array]):
        img, targets, crowd_targets = sample["image"], sample["target"], sample.get("crowd_target")
        img, r = rescale_and_pad_to_size(img, self.input_dim, self.swap, self.pad_value)

        sample["image"] = img
        sample["target"] = self._rescale_target(targets, r)
        if crowd_targets is not None:
            sample["crowd_target"] = self._rescale_target(crowd_targets, r)
        return sample

    def _rescale_target(self, targets: np.array, r: float) -> np.array:
        """SegRescale the target according to a coefficient used to rescale the image.
        This is done to have images and targets at the same scale.

        :param targets:  Targets to rescale, shape (batch_size, 6)
        :param r:        SegRescale coefficient that was applied to the image

        :return:         Rescaled targets, shape (batch_size, 6)
        """
        targets = targets.copy() if len(targets) > 0 else np.zeros((self.max_targets, 5), dtype=np.float32)
        boxes, labels = targets[:, :4], targets[:, 4]
        boxes = xyxy2cxcywh(boxes)
        boxes *= r
        boxes = cxcywh2xyxy(boxes)
        return np.concatenate((boxes, labels[:, np.newaxis]), 1)

DetectionRGB2BGR

Bases: DetectionTransform

Detection change Red & Blue channel of the image

Attributes: prob: (float) probability to apply the transform.

Source code in training/transforms/transforms.py
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
class DetectionRGB2BGR(DetectionTransform):
    """
    Detection change Red & Blue channel of the image

    Attributes:
        prob: (float) probability to apply the transform.

    """

    def __init__(self, prob: float = 0.5):
        super().__init__()
        self.prob = prob

    def __call__(self, sample: dict) -> dict:
        if sample["image"].shape[2] < 3:
            raise ValueError("DetectionRGB2BGR transform expects at least 3 channels, got: " + str(sample["image"].shape[2]))

        if random.random() < self.prob:
            sample["image"] = sample["image"][..., ::-1]
        return sample

DetectionRandomAffine

Bases: DetectionTransform

DetectionRandomAffine detection transform

Attributes: target_size: (tuple) desired output shape.

degrees: (Union[tuple, float]) degrees for random rotation, when float the random values are drawn uniformly from (-degrees, degrees)

translate: (Union[tuple, float]) translate size (in pixels) for random translation, when float the random values are drawn uniformly from (center-translate, center+translate)

scales: (Union[tuple, float]) values for random rescale, when float the random values are drawn uniformly from (1-scales, 1+scales)

shear: (Union[tuple, float]) degrees for random shear, when float the random values are drawn uniformly from (-shear, shear)

enable: (bool) whether to apply the below transform at all.

filter_box_candidates: (bool) whether to filter out transformed bboxes by edge size, area ratio, and aspect ratio (default=False).

wh_thr: (float) edge size threshold when filter_box_candidates = True. Bounding oxes with edges smaller then this values will be filtered out. (default=2)

ar_thr: (float) aspect ratio threshold filter_box_candidates = True. Bounding boxes with aspect ratio larger then this values will be filtered out. (default=20)

area_thr:(float) threshold for area ratio between original image and the transformed one, when when filter_box_candidates = True. Bounding boxes with such ratio smaller then this value will be filtered out. (default=0.1)

border_value: value for filling borders after applying transforms (default=114).

Source code in training/transforms/transforms.py
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
class DetectionRandomAffine(DetectionTransform):
    """
    DetectionRandomAffine detection transform

    Attributes:
     target_size: (tuple) desired output shape.

     degrees:  (Union[tuple, float]) degrees for random rotation, when float the random values are drawn uniformly
        from (-degrees, degrees)

     translate:  (Union[tuple, float]) translate size (in pixels) for random translation, when float the random values
        are drawn uniformly from (center-translate, center+translate)

     scales: (Union[tuple, float]) values for random rescale, when float the random values are drawn uniformly
        from (1-scales, 1+scales)

     shear: (Union[tuple, float]) degrees for random shear, when float the random values are drawn uniformly
        from (-shear, shear)

     enable: (bool) whether to apply the below transform at all.

     filter_box_candidates: (bool) whether to filter out transformed bboxes by edge size, area ratio, and aspect ratio (default=False).

     wh_thr: (float) edge size threshold when filter_box_candidates = True. Bounding oxes with edges smaller
      then this values will be filtered out. (default=2)

     ar_thr: (float) aspect ratio threshold filter_box_candidates = True. Bounding boxes with aspect ratio larger
      then this values will be filtered out. (default=20)

     area_thr:(float) threshold for area ratio between original image and the transformed one, when when filter_box_candidates = True.
      Bounding boxes with such ratio smaller then this value will be filtered out. (default=0.1)

     border_value: value for filling borders after applying transforms (default=114).


    """

    def __init__(
        self,
        degrees=10,
        translate=0.1,
        scales=0.1,
        shear=10,
        target_size: Optional[Tuple[int, int]] = (640, 640),
        filter_box_candidates: bool = False,
        wh_thr=2,
        ar_thr=20,
        area_thr=0.1,
        border_value=114,
    ):
        super(DetectionRandomAffine, self).__init__()
        self.degrees = degrees
        self.translate = translate
        self.scale = scales
        self.shear = shear
        self.target_size = target_size
        self.enable = True
        self.filter_box_candidates = filter_box_candidates
        self.wh_thr = wh_thr
        self.ar_thr = ar_thr
        self.area_thr = area_thr
        self.border_value = border_value

    def close(self):
        self.enable = False

    def __call__(self, sample: dict):
        if self.enable:
            img, target = random_affine(
                sample["image"],
                sample["target"],
                sample.get("target_seg"),
                target_size=self.target_size or tuple(reversed(sample["image"].shape[:2])),
                degrees=self.degrees,
                translate=self.translate,
                scales=self.scale,
                shear=self.shear,
                filter_box_candidates=self.filter_box_candidates,
                wh_thr=self.wh_thr,
                area_thr=self.area_thr,
                ar_thr=self.ar_thr,
                border_value=self.border_value,
            )
            sample["image"] = img
            sample["target"] = target
        return sample

DetectionRandomRotate90

Bases: DetectionTransform

Source code in training/transforms/transforms.py
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
class DetectionRandomRotate90(DetectionTransform):
    def __init__(self, prob: float = 0.5):
        super().__init__()
        self.prob = prob

    def __call__(self, sample: dict) -> dict:
        if random.random() < self.prob:
            k = random.randrange(0, 4)

            img, targets, crowd_targets = sample["image"], sample["target"], sample.get("crowd_target")

            sample["image"] = np.ascontiguousarray(np.rot90(img, k))
            sample["target"] = self.rotate_bboxes(targets, k, img.shape[:2])
            if crowd_targets is not None:
                sample["crowd_target"] = self.rotate_bboxes(crowd_targets, k, img.shape[:2])

        return sample

    @classmethod
    def rotate_bboxes(cls, targets, k: int, image_shape):
        if k == 0:
            return targets
        rows, cols = image_shape
        targets = targets.copy()
        targets[:, 0:4] = cls.xyxy_bbox_rot90(targets[:, 0:4], k, rows, cols)
        return targets

    @classmethod
    def xyxy_bbox_rot90(cls, bboxes, factor: int, rows: int, cols: int):
        """Rotates a bounding box by 90 degrees CCW (see np.rot90)

        Args:
            bbox: A bounding box tuple (x_min, y_min, x_max, y_max).
            factor: Number of CCW rotations. Must be in set {0, 1, 2, 3} See np.rot90.
            rows: Image rows.
            cols: Image cols.

        Returns:
            tuple: A bounding box tuple (x_min, y_min, x_max, y_max).

        """
        x_min, y_min, x_max, y_max = bboxes[:, 0], bboxes[:, 1], bboxes[:, 2], bboxes[:, 3]

        if factor == 0:
            bbox = x_min, y_min, x_max, y_max
        elif factor == 1:
            bbox = y_min, cols - x_max, y_max, cols - x_min
        elif factor == 2:
            bbox = cols - x_max, rows - y_max, cols - x_min, rows - y_min
        elif factor == 3:
            bbox = rows - y_max, x_min, rows - y_min, x_max
        else:
            raise ValueError("Parameter n must be in set {0, 1, 2, 3}")
        return np.stack(bbox, axis=1)

xyxy_bbox_rot90(bboxes, factor, rows, cols) classmethod

Rotates a bounding box by 90 degrees CCW (see np.rot90)

Args: bbox: A bounding box tuple (x_min, y_min, x_max, y_max). factor: Number of CCW rotations. Must be in set {0, 1, 2, 3} See np.rot90. rows: Image rows. cols: Image cols.

Returns: tuple: A bounding box tuple (x_min, y_min, x_max, y_max).

Source code in training/transforms/transforms.py
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
@classmethod
def xyxy_bbox_rot90(cls, bboxes, factor: int, rows: int, cols: int):
    """Rotates a bounding box by 90 degrees CCW (see np.rot90)

    Args:
        bbox: A bounding box tuple (x_min, y_min, x_max, y_max).
        factor: Number of CCW rotations. Must be in set {0, 1, 2, 3} See np.rot90.
        rows: Image rows.
        cols: Image cols.

    Returns:
        tuple: A bounding box tuple (x_min, y_min, x_max, y_max).

    """
    x_min, y_min, x_max, y_max = bboxes[:, 0], bboxes[:, 1], bboxes[:, 2], bboxes[:, 3]

    if factor == 0:
        bbox = x_min, y_min, x_max, y_max
    elif factor == 1:
        bbox = y_min, cols - x_max, y_max, cols - x_min
    elif factor == 2:
        bbox = cols - x_max, rows - y_max, cols - x_min, rows - y_min
    elif factor == 3:
        bbox = rows - y_max, x_min, rows - y_min, x_max
    else:
        raise ValueError("Parameter n must be in set {0, 1, 2, 3}")
    return np.stack(bbox, axis=1)

DetectionRescale

Bases: DetectionTransform

Resize image and bounding boxes to given image dimensions without preserving aspect ratio

Attributes: output_shape: (tuple) (rows, cols)

Source code in training/transforms/transforms.py
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
class DetectionRescale(DetectionTransform):
    """
    Resize image and bounding boxes to given image dimensions without preserving aspect ratio

    Attributes:
        output_shape: (tuple) (rows, cols)

    """

    def __init__(self, output_shape: Tuple[int, int]):
        super().__init__()
        self.output_shape = output_shape

    def __call__(self, sample: Dict[str, np.array]):
        img, targets, crowd_targets = sample["image"], sample["target"], sample.get("crowd_target")

        img_resized, scale_factors = self._rescale_image(img)

        sample["image"] = img_resized
        sample["target"] = self._rescale_target(targets, scale_factors)
        if crowd_targets is not None:
            sample["crowd_target"] = self._rescale_target(crowd_targets, scale_factors)
        return sample

    def _rescale_image(self, image):
        sy, sx = self.output_shape[0] / image.shape[0], self.output_shape[1] / image.shape[1]
        resized_img = cv2.resize(
            image,
            dsize=(int(self.output_shape[1]), int(self.output_shape[0])),
            interpolation=cv2.INTER_LINEAR,
        )
        scale_factors = sy, sx
        return resized_img, scale_factors

    def _rescale_target(self, targets: np.array, scale_factors: Tuple[float, float]) -> np.array:
        """SegRescale the target according to a coefficient used to rescale the image.
        This is done to have images and targets at the same scale.

        :param targets:  Target XYXY bboxes to rescale, shape (num_boxes, 5)
        :param r:        SegRescale coefficient that was applied to the image

        :return:         Rescaled targets, shape (num_boxes, 5)
        """
        sy, sx = scale_factors
        targets = targets.astype(np.float32, copy=True) if len(targets) > 0 else np.zeros((0, 5), dtype=np.float32)
        targets[:, 0:4] *= np.array([[sx, sy, sx, sy]], dtype=targets.dtype)
        return targets

DetectionStandardize

Bases: DetectionTransform

Standardize image pixel values with img/max_val

Source code in training/transforms/transforms.py
411
412
413
414
415
416
417
418
419
420
421
422
class DetectionStandardize(DetectionTransform):
    """
    Standardize image pixel values with img/max_val
    """

    def __init__(self, max_value: float = 255.0):
        super().__init__()
        self.max_value = max_value

    def __call__(self, sample: dict) -> dict:
        sample["image"] = sample["image"] / self.max_value
        return sample

DetectionTargetsFormatTransform

Bases: DetectionTransform

Detection targets format transform

Convert targets in input_format to output_format, filter small bboxes and pad targets. Attributes: input_dim: Shape of the images to transform. input_format: Format of the input targets. For instance [xmin, ymin, xmax, ymax, cls_id] refers to XYXY_LABEL. output_format: Format of the output targets. For instance [xmin, ymin, xmax, ymax, cls_id] refers to XYXY_LABEL min_bbox_edge_size: bboxes with edge size lower then this values will be removed. max_targets: Max objects in single image, padding target to this size.

Source code in training/transforms/transforms.py
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
class DetectionTargetsFormatTransform(DetectionTransform):
    """
    Detection targets format transform

    Convert targets in input_format to output_format, filter small bboxes and pad targets.
    Attributes:
        input_dim:          Shape of the images to transform.
        input_format:       Format of the input targets. For instance [xmin, ymin, xmax, ymax, cls_id] refers to XYXY_LABEL.
        output_format:      Format of the output targets. For instance [xmin, ymin, xmax, ymax, cls_id] refers to XYXY_LABEL
        min_bbox_edge_size: bboxes with edge size lower then this values will be removed.
        max_targets:        Max objects in single image, padding target to this size.
    """

    @resolve_param("input_format", ConcatenatedTensorFormatFactory())
    @resolve_param("output_format", ConcatenatedTensorFormatFactory())
    def __init__(
        self,
        input_dim: Optional[tuple] = None,
        input_format: ConcatenatedTensorFormat = XYXY_LABEL,
        output_format: ConcatenatedTensorFormat = LABEL_CXCYWH,
        min_bbox_edge_size: float = 1,
        max_targets: int = 120,
    ):
        super(DetectionTargetsFormatTransform, self).__init__()
        if isinstance(input_format, DetectionTargetsFormat) or isinstance(output_format, DetectionTargetsFormat):
            raise TypeError(
                "DetectionTargetsFormat is not supported for input_format and output_format starting from super_gradients==3.0.7.\n"
                "You can either:\n"
                "\t - use builtin format among super_gradients.training.datasets.data_formats.default_formats.<FORMAT_NAME> (e.g. XYXY_LABEL, CXCY_LABEL, ..)\n"
                "\t - define your custom format using super_gradients.training.datasets.data_formats.formats.ConcatenatedTensorFormat\n"
            )
        self.input_format = input_format
        self.output_format = output_format
        self.max_targets = max_targets
        self.min_bbox_edge_size = min_bbox_edge_size
        self.input_dim = None

        if input_dim is not None:
            self._setup_input_dim_related_params(input_dim)

    def _setup_input_dim_related_params(self, input_dim: tuple):
        """Setup all the parameters that are related to input_dim."""
        self.input_dim = input_dim
        self.min_bbox_edge_size = self.min_bbox_edge_size / max(input_dim) if self.output_format.bboxes_format.format.normalized else self.min_bbox_edge_size
        self.targets_format_converter = ConcatenatedTensorFormatConverter(
            input_format=self.input_format, output_format=self.output_format, image_shape=input_dim
        )

    def __call__(self, sample: dict) -> dict:

        # if self.input_dim not set yet, it will be set with first batch
        if self.input_dim is None:
            self._setup_input_dim_related_params(input_dim=sample["image"].shape[1:])

        sample["target"] = self.apply_on_targets(sample["target"])
        if "crowd_target" in sample.keys():
            sample["crowd_target"] = self.apply_on_targets(sample["crowd_target"])
        return sample

    def apply_on_targets(self, targets: np.ndarray) -> np.ndarray:
        """Convert targets in input_format to output_format, filter small bboxes and pad targets"""
        targets = self.targets_format_converter(targets)
        targets = self.filter_small_bboxes(targets)
        targets = self.pad_targets(targets)
        return targets

    def filter_small_bboxes(self, targets: np.ndarray) -> np.ndarray:
        """Filter bboxes smaller than specified threshold."""

        def _is_big_enough(bboxes: np.ndarray) -> np.ndarray:
            return np.minimum(bboxes[:, 2], bboxes[:, 3]) > self.min_bbox_edge_size

        targets = filter_on_bboxes(fn=_is_big_enough, tensor=targets, tensor_format=self.output_format)
        return targets

    def pad_targets(self, targets: np.ndarray) -> np.ndarray:
        """Pad targets."""
        padded_targets = np.zeros((self.max_targets, targets.shape[-1]))
        padded_targets[range(len(targets))[: self.max_targets]] = targets[: self.max_targets]
        padded_targets = np.ascontiguousarray(padded_targets, dtype=np.float32)
        return padded_targets

apply_on_targets(targets)

Convert targets in input_format to output_format, filter small bboxes and pad targets

Source code in training/transforms/transforms.py
1014
1015
1016
1017
1018
1019
def apply_on_targets(self, targets: np.ndarray) -> np.ndarray:
    """Convert targets in input_format to output_format, filter small bboxes and pad targets"""
    targets = self.targets_format_converter(targets)
    targets = self.filter_small_bboxes(targets)
    targets = self.pad_targets(targets)
    return targets

filter_small_bboxes(targets)

Filter bboxes smaller than specified threshold.

Source code in training/transforms/transforms.py
1021
1022
1023
1024
1025
1026
1027
1028
def filter_small_bboxes(self, targets: np.ndarray) -> np.ndarray:
    """Filter bboxes smaller than specified threshold."""

    def _is_big_enough(bboxes: np.ndarray) -> np.ndarray:
        return np.minimum(bboxes[:, 2], bboxes[:, 3]) > self.min_bbox_edge_size

    targets = filter_on_bboxes(fn=_is_big_enough, tensor=targets, tensor_format=self.output_format)
    return targets

pad_targets(targets)

Pad targets.

Source code in training/transforms/transforms.py
1030
1031
1032
1033
1034
1035
def pad_targets(self, targets: np.ndarray) -> np.ndarray:
    """Pad targets."""
    padded_targets = np.zeros((self.max_targets, targets.shape[-1]))
    padded_targets[range(len(targets))[: self.max_targets]] = targets[: self.max_targets]
    padded_targets = np.ascontiguousarray(padded_targets, dtype=np.float32)
    return padded_targets

DetectionTransform

Detection transform base class.

Complex transforms that require extra data loading can use the the additional_samples_count attribute in a similar fashion to what's been done in COCODetectionDataset:

self._load_additional_inputs_for_transform(sample, transform)

after the above call, sample["additional_samples"] holds a list of additional inputs and targets.

sample = transform(sample)

Attributes: additional_samples_count: (int) additional samples to be loaded. non_empty_targets: (bool) whether the additianl targets can have empty targets or not.

Source code in training/transforms/transforms.py
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
class DetectionTransform:
    """
    Detection transform base class.

    Complex transforms that require extra data loading can use the the additional_samples_count attribute in a
     similar fashion to what's been done in COCODetectionDataset:

    self._load_additional_inputs_for_transform(sample, transform)

    # after the above call, sample["additional_samples"] holds a list of additional inputs and targets.

    sample = transform(sample)



    Attributes:
        additional_samples_count: (int) additional samples to be loaded.
        non_empty_targets: (bool) whether the additianl targets can have empty targets or not.
    """

    def __init__(self, additional_samples_count: int = 0, non_empty_targets: bool = False):
        self.additional_samples_count = additional_samples_count
        self.non_empty_targets = non_empty_targets

    def __call__(self, sample: Union[dict, list]):
        raise NotImplementedError

    def __repr__(self):
        return self.__class__.__name__ + str(self.__dict__).replace("{", "(").replace("}", ")")

SegCropImageAndMask

Bases: SegmentationTransform

Crops image and mask (synchronously). In "center" mode a center crop is performed while, in "random" mode the drop will be positioned around random coordinates.

Source code in training/transforms/transforms.py
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
class SegCropImageAndMask(SegmentationTransform):
    """
    Crops image and mask (synchronously).
    In "center" mode a center crop is performed while, in "random" mode the drop will be positioned around
     random coordinates.
    """

    def __init__(self, crop_size: Union[float, Tuple, List], mode: str):
        """

        :param crop_size: tuple of (width, height) for the final crop size, if is scalar size is a
            square (crop_size, crop_size)
        :param mode: how to choose the center of the crop, 'center' for the center of the input image,
            'random' center the point is chosen randomally
        """

        self.crop_size = crop_size
        self.mode = mode

        self.check_valid_arguments()

    def __call__(self, sample: dict):
        image = sample["image"]
        mask = sample["mask"]

        w, h = image.size
        if self.mode == "random":
            x1 = random.randint(0, w - self.crop_size[0])
            y1 = random.randint(0, h - self.crop_size[1])
        else:
            x1 = int(round((w - self.crop_size[0]) / 2.0))
            y1 = int(round((h - self.crop_size[1]) / 2.0))

        image = image.crop((x1, y1, x1 + self.crop_size[0], y1 + self.crop_size[1]))
        mask = mask.crop((x1, y1, x1 + self.crop_size[0], y1 + self.crop_size[1]))

        sample["image"] = image
        sample["mask"] = mask

        return sample

    def check_valid_arguments(self):
        if self.mode not in ["center", "random"]:
            raise ValueError(f"Unsupported mode: found: {self.mode}, expected: 'center' or 'random'")

        if not isinstance(self.crop_size, collections.abc.Iterable):
            self.crop_size = (self.crop_size, self.crop_size)
        if self.crop_size[0] <= 0 or self.crop_size[1] <= 0:
            raise ValueError(f"Crop size must be positive numbers, found: {self.crop_size}")

__init__(crop_size, mode)

Parameters:

Name Type Description Default
crop_size Union[float, Tuple, List]

tuple of (width, height) for the final crop size, if is scalar size is a square (crop_size, crop_size)

required
mode str

how to choose the center of the crop, 'center' for the center of the input image, 'random' center the point is chosen randomally

required
Source code in training/transforms/transforms.py
210
211
212
213
214
215
216
217
218
219
220
221
222
def __init__(self, crop_size: Union[float, Tuple, List], mode: str):
    """

    :param crop_size: tuple of (width, height) for the final crop size, if is scalar size is a
        square (crop_size, crop_size)
    :param mode: how to choose the center of the crop, 'center' for the center of the input image,
        'random' center the point is chosen randomally
    """

    self.crop_size = crop_size
    self.mode = mode

    self.check_valid_arguments()

SegPadShortToCropSize

Bases: SegmentationTransform

Pads image to 'crop_size'. Should be called only after "SegRescale" or "SegRandomRescale" in augmentations pipeline.

Source code in training/transforms/transforms.py
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
class SegPadShortToCropSize(SegmentationTransform):
    """
    Pads image to 'crop_size'.
    Should be called only after "SegRescale" or "SegRandomRescale" in augmentations pipeline.
    """

    def __init__(self, crop_size: Union[float, Tuple, List], fill_mask: int = 0, fill_image: Union[int, Tuple, List] = 0):
        """
        :param crop_size: tuple of (width, height) for the final crop size, if is scalar size is a
            square (crop_size, crop_size)
        :param fill_mask: value to fill mask labels background.
        :param fill_image: grey value to fill image padded background.
        """
        # CHECK IF CROP SIZE IS A ITERABLE OR SCALAR
        self.crop_size = crop_size
        self.fill_mask = fill_mask
        self.fill_image = tuple(fill_image) if isinstance(fill_image, Sequence) else fill_image

        self.check_valid_arguments()

    def __call__(self, sample: dict):
        image = sample["image"]
        mask = sample["mask"]
        w, h = image.size

        # pad images from center symmetrically
        if w < self.crop_size[0] or h < self.crop_size[1]:
            padh = (self.crop_size[1] - h) / 2 if h < self.crop_size[1] else 0
            pad_top, pad_bottom = math.ceil(padh), math.floor(padh)
            padw = (self.crop_size[0] - w) / 2 if w < self.crop_size[0] else 0
            pad_left, pad_right = math.ceil(padw), math.floor(padw)

            image = ImageOps.expand(image, border=(pad_left, pad_top, pad_right, pad_bottom), fill=self.fill_image)
            mask = ImageOps.expand(mask, border=(pad_left, pad_top, pad_right, pad_bottom), fill=self.fill_mask)

        sample["image"] = image
        sample["mask"] = mask

        return sample

    def check_valid_arguments(self):
        if not isinstance(self.crop_size, collections.abc.Iterable):
            self.crop_size = (self.crop_size, self.crop_size)
        if self.crop_size[0] <= 0 or self.crop_size[1] <= 0:
            raise ValueError(f"Crop size must be positive numbers, found: {self.crop_size}")

        self.fill_mask, self.fill_image = _validate_fill_values_arguments(self.fill_mask, self.fill_image)

__init__(crop_size, fill_mask=0, fill_image=0)

Parameters:

Name Type Description Default
crop_size Union[float, Tuple, List]

tuple of (width, height) for the final crop size, if is scalar size is a square (crop_size, crop_size)

required
fill_mask int

value to fill mask labels background.

0
fill_image Union[int, Tuple, List]

grey value to fill image padded background.

0
Source code in training/transforms/transforms.py
282
283
284
285
286
287
288
289
290
291
292
293
294
def __init__(self, crop_size: Union[float, Tuple, List], fill_mask: int = 0, fill_image: Union[int, Tuple, List] = 0):
    """
    :param crop_size: tuple of (width, height) for the final crop size, if is scalar size is a
        square (crop_size, crop_size)
    :param fill_mask: value to fill mask labels background.
    :param fill_image: grey value to fill image padded background.
    """
    # CHECK IF CROP SIZE IS A ITERABLE OR SCALAR
    self.crop_size = crop_size
    self.fill_mask = fill_mask
    self.fill_image = tuple(fill_image) if isinstance(fill_image, Sequence) else fill_image

    self.check_valid_arguments()

SegRandomFlip

Bases: SegmentationTransform

Randomly flips the image and mask (synchronously) with probability 'prob'.

Source code in training/transforms/transforms.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class SegRandomFlip(SegmentationTransform):
    """
    Randomly flips the image and mask (synchronously) with probability 'prob'.
    """

    def __init__(self, prob: float = 0.5):
        assert 0.0 <= prob <= 1.0, f"Probability value must be between 0 and 1, found {prob}"
        self.prob = prob

    def __call__(self, sample: dict):
        image = sample["image"]
        mask = sample["mask"]
        if random.random() < self.prob:
            image = image.transpose(Image.FLIP_LEFT_RIGHT)
            mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
            sample["image"] = image
            sample["mask"] = mask

        return sample

SegRandomGaussianBlur

Bases: SegmentationTransform

Adds random Gaussian Blur to image with probability 'prob'.

Source code in training/transforms/transforms.py
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
class SegRandomGaussianBlur(SegmentationTransform):
    """
    Adds random Gaussian Blur to image with probability 'prob'.
    """

    def __init__(self, prob: float = 0.5):
        assert 0.0 <= prob <= 1.0, "Probability value must be between 0 and 1"
        self.prob = prob

    def __call__(self, sample: dict):
        image = sample["image"]
        mask = sample["mask"]

        if random.random() < self.prob:
            image = image.filter(ImageFilter.GaussianBlur(radius=random.random()))

        sample["image"] = image
        sample["mask"] = mask

        return sample

SegRandomRescale

Random rescale the image and mask (synchronously) while preserving aspect ratio. Scale factor is randomly picked between scales [min, max] Args: scales: scale range tuple (min, max), if scales is a float range will be defined as (1, scales) if scales > 1, otherwise (scales, 1). must be a positive number.

Source code in training/transforms/transforms.py
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
class SegRandomRescale:
    """
    Random rescale the image and mask (synchronously) while preserving aspect ratio.
    Scale factor is randomly picked between scales [min, max]
    Args:
        scales: scale range tuple (min, max), if scales is a float range will be defined as (1, scales) if scales > 1,
            otherwise (scales, 1). must be a positive number.
    """

    def __init__(self, scales: Union[float, Tuple, List] = (0.5, 2.0)):
        self.scales = scales

        self.check_valid_arguments()

    def __call__(self, sample: dict):
        image = sample["image"]
        mask = sample["mask"]
        w, h = image.size

        scale = random.uniform(self.scales[0], self.scales[1])
        out_size = int(scale * w), int(scale * h)

        image = image.resize(out_size, image_resample)
        mask = mask.resize(out_size, mask_resample)

        sample["image"] = image
        sample["mask"] = mask

        return sample

    def check_valid_arguments(self):
        """
        Check the scale values are valid. if order is wrong, flip the order and return the right scale values.
        """
        if not isinstance(self.scales, collections.abc.Iterable):
            if self.scales <= 1:
                self.scales = (self.scales, 1)
            else:
                self.scales = (1, self.scales)

        if self.scales[0] < 0 or self.scales[1] < 0:
            raise ValueError(f"SegRandomRescale scale values must be positive numbers, found: {self.scales}")
        if self.scales[0] > self.scales[1]:
            self.scales = (self.scales[1], self.scales[0])
        return self.scales

check_valid_arguments()

Check the scale values are valid. if order is wrong, flip the order and return the right scale values.

Source code in training/transforms/transforms.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def check_valid_arguments(self):
    """
    Check the scale values are valid. if order is wrong, flip the order and return the right scale values.
    """
    if not isinstance(self.scales, collections.abc.Iterable):
        if self.scales <= 1:
            self.scales = (self.scales, 1)
        else:
            self.scales = (1, self.scales)

    if self.scales[0] < 0 or self.scales[1] < 0:
        raise ValueError(f"SegRandomRescale scale values must be positive numbers, found: {self.scales}")
    if self.scales[0] > self.scales[1]:
        self.scales = (self.scales[1], self.scales[0])
    return self.scales

SegRandomRotate

Bases: SegmentationTransform

Randomly rotates image and mask (synchronously) between 'min_deg' and 'max_deg'.

Source code in training/transforms/transforms.py
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
class SegRandomRotate(SegmentationTransform):
    """
    Randomly rotates image and mask (synchronously) between 'min_deg' and 'max_deg'.
    """

    def __init__(self, min_deg: float = -10, max_deg: float = 10, fill_mask: int = 0, fill_image: Union[int, Tuple, List] = 0):
        self.min_deg = min_deg
        self.max_deg = max_deg
        self.fill_mask = fill_mask
        # grey color in RGB mode
        self.fill_image = (fill_image, fill_image, fill_image)

        self.check_valid_arguments()

    def __call__(self, sample: dict):
        image = sample["image"]
        mask = sample["mask"]

        deg = random.uniform(self.min_deg, self.max_deg)
        image = image.rotate(deg, resample=image_resample, fillcolor=self.fill_image)
        mask = mask.rotate(deg, resample=mask_resample, fillcolor=self.fill_mask)

        sample["image"] = image
        sample["mask"] = mask

        return sample

    def check_valid_arguments(self):
        self.fill_mask, self.fill_image = _validate_fill_values_arguments(self.fill_mask, self.fill_image)

SegRescale

Bases: SegmentationTransform

Rescales the image and mask (synchronously) while preserving aspect ratio. The rescaling can be done according to scale_factor, short_size or long_size. If more than one argument is given, the rescaling mode is determined by this order: scale_factor, then short_size, then long_size.

Args: scale_factor: rescaling is done by multiplying input size by scale_factor: out_size = (scale_factor * w, scale_factor * h) short_size: rescaling is done by determining the scale factor by the ratio short_size / min(h, w). long_size: rescaling is done by determining the scale factor by the ratio long_size / max(h, w).

Source code in training/transforms/transforms.py
 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
class SegRescale(SegmentationTransform):
    """
    Rescales the image and mask (synchronously) while preserving aspect ratio.
    The rescaling can be done according to scale_factor, short_size or long_size.
    If more than one argument is given, the rescaling mode is determined by this order: scale_factor, then short_size,
    then long_size.

    Args:
        scale_factor: rescaling is done by multiplying input size by scale_factor:
            out_size = (scale_factor * w, scale_factor * h)
        short_size: rescaling is done by determining the scale factor by the ratio short_size / min(h, w).
        long_size: rescaling is done by determining the scale factor by the ratio long_size / max(h, w).
    """

    def __init__(self, scale_factor: Optional[float] = None, short_size: Optional[int] = None, long_size: Optional[int] = None):
        self.scale_factor = scale_factor
        self.short_size = short_size
        self.long_size = long_size

        self.check_valid_arguments()

    def __call__(self, sample: dict):
        image = sample["image"]
        mask = sample["mask"]
        w, h = image.size
        if self.scale_factor is not None:
            scale = self.scale_factor
        elif self.short_size is not None:
            short_size = min(w, h)
            scale = self.short_size / short_size
        else:
            long_size = max(w, h)
            scale = self.long_size / long_size

        out_size = int(scale * w), int(scale * h)

        image = image.resize(out_size, image_resample)
        mask = mask.resize(out_size, mask_resample)

        sample["image"] = image
        sample["mask"] = mask

        return sample

    def check_valid_arguments(self):
        if self.scale_factor is None and self.short_size is None and self.long_size is None:
            raise ValueError("Must assign one rescale argument: scale_factor, short_size or long_size")

        if self.scale_factor is not None and self.scale_factor <= 0:
            raise ValueError(f"Scale factor must be a positive number, found: {self.scale_factor}")
        if self.short_size is not None and self.short_size <= 0:
            raise ValueError(f"Short size must be a positive number, found: {self.short_size}")
        if self.long_size is not None and self.long_size <= 0:
            raise ValueError(f"Long size must be a positive number, found: {self.long_size}")

Standardize

Bases: torch.nn.Module

Standardize image pixel values.

Returns:

Type Description

max_val: float, value to as described above (default=255)

Source code in training/transforms/transforms.py
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
class Standardize(torch.nn.Module):
    """
    Standardize image pixel values.
    :return img/max_val

    attributes:
        max_val: float, value to as described above (default=255)
    """

    def __init__(self, max_val=255.0):
        super(Standardize, self).__init__()
        self.max_val = max_val

    def forward(self, img):
        return img / self.max_val

get_affine_matrix(input_size, target_size, degrees=10, translate=0.1, scales=0.1, shear=10)

Returns a random affine transform matrix.

Parameters:

Name Type Description Default
input_size

(tuple) input shape.

required
target_size

(tuple) desired output shape.

required
degrees

(Union[tuple, float]) degrees for random rotation, when float the random values are drawn uniformly from (-degrees, degrees)

10
translate

(Union[tuple, float]) translate size (in pixels) for random translation, when float the random values are drawn uniformly from (-translate, translate)

0.1
scales

(Union[tuple, float]) values for random rescale, when float the random values are drawn uniformly from (1-scales, 1+scales)

0.1
shear

(Union[tuple, float]) degrees for random shear, when float the random values are drawn uniformly from (-shear, shear)

10

Returns:

Type Description

affine_transform_matrix, drawn_scale

Source code in training/transforms/transforms.py
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
def get_affine_matrix(
    input_size,
    target_size,
    degrees=10,
    translate=0.1,
    scales=0.1,
    shear=10,
):
    """
    Returns a random affine transform matrix.

    :param input_size: (tuple) input shape.

    :param target_size: (tuple) desired output shape.

    :param degrees:  (Union[tuple, float]) degrees for random rotation, when float the random values are drawn uniformly
     from (-degrees, degrees)

    :param translate:  (Union[tuple, float]) translate size (in pixels) for random translation, when float the random values
     are drawn uniformly from (-translate, translate)

    :param scales: (Union[tuple, float]) values for random rescale, when float the random values are drawn uniformly
     from (1-scales, 1+scales)

    :param shear: (Union[tuple, float]) degrees for random shear, when float the random values are drawn uniformly
     from (-shear, shear)

    :return: affine_transform_matrix, drawn_scale
    """

    # Center in pixels
    center_m = np.eye(3)
    center = (input_size[0] // 2, input_size[1] // 2)
    center_m[0, 2] = -center[1]
    center_m[1, 2] = -center[0]

    # Rotation and scale
    rotation_m = np.eye(3)
    rotation_m[:2] = cv2.getRotationMatrix2D(angle=get_aug_params(degrees), center=(0, 0), scale=get_aug_params(scales, center=1.0))

    # Shear in degrees
    shear_m = np.eye(3)
    shear_m[0, 1] = math.tan(get_aug_params(shear) * math.pi / 180)
    shear_m[1, 0] = math.tan(get_aug_params(shear) * math.pi / 180)

    # Translation in pixels
    translation_m = np.eye(3)
    translation_m[0, 2] = get_aug_params(translate, center=0.5) * target_size[1]
    translation_m[1, 2] = get_aug_params(translate, center=0.5) * target_size[0]

    return (translation_m @ shear_m @ rotation_m @ center_m)[:2]

get_aug_params(value, center=0)

Generates a random value for augmentations as described below

Parameters:

Name Type Description Default
value Union[tuple, float]

Union[tuple, float] defines the range of values for generation. Wen tuple- drawn uniformly between (value[0], value[1]), and (center - value, center + value) when float

required
center float

float, defines center to subtract when value is float.

0

Returns:

Type Description

generated value

Source code in training/transforms/transforms.py
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
def get_aug_params(value: Union[tuple, float], center: float = 0):
    """
    Generates a random value for augmentations as described below

    :param value: Union[tuple, float] defines the range of values for generation. Wen tuple-
     drawn uniformly between (value[0], value[1]), and (center - value, center + value) when float
    :param center: float, defines center to subtract when value is float.
    :return: generated value
    """
    if isinstance(value, Number):
        return random.uniform(center - float(value), center + float(value))
    elif len(value) == 2:
        return random.uniform(value[0], value[1])
    else:
        raise ValueError(
            "Affine params should be either a sequence containing two values\
                          or single float values. Got {}".format(
                value
            )
        )

random_affine(img, targets=(), targets_seg=None, target_size=(640, 640), degrees=10, translate=0.1, scales=0.1, shear=10, filter_box_candidates=False, wh_thr=2, ar_thr=20, area_thr=0.1, border_value=114)

Performs random affine transform to img, targets

Parameters:

Name Type Description Default
img np.ndarray

Input image of shape [h, w, c]

required
targets np.ndarray

Input target

()
targets_seg np.ndarray

Targets derived from segmentation masks

None
target_size tuple

Desired output shape

(640, 640)
degrees Union[float, tuple]

Degrees for random rotation, when float the random values are drawn uniformly from (-degrees, degrees).

10
translate Union[float, tuple]

Translate size (in pixels) for random translation, when float the random values are drawn uniformly from (-translate, translate)

0.1
scales Union[float, tuple]

Values for random rescale, when float the random values are drawn uniformly from (0.1-scales, 0.1+scales)

0.1
shear Union[float, tuple]

Degrees for random shear, when float the random values are drawn uniformly from (shear, shear)

10
filter_box_candidates bool

whether to filter out transformed bboxes by edge size, area ratio, and aspect ratio.

False
wh_thr

(float) edge size threshold when filter_box_candidates = True. Bounding oxes with edges smaller then this values will be filtered out. (default=2)

2
ar_thr

(float) aspect ratio threshold filter_box_candidates = True. Bounding boxes with aspect ratio larger then this values will be filtered out. (default=20)

20
area_thr

(float) threshold for area ratio between original image and the transformed one, when when filter_box_candidates = True. Bounding boxes with such ratio smaller then this value will be filtered out. (default=0.1)

0.1
border_value

value for filling borders after applying transforms (default=114).

114

Returns:

Type Description

Image and Target with applied random affine

Source code in training/transforms/transforms.py
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
def random_affine(
    img: np.ndarray,
    targets: np.ndarray = (),
    targets_seg: np.ndarray = None,
    target_size: tuple = (640, 640),
    degrees: Union[float, tuple] = 10,
    translate: Union[float, tuple] = 0.1,
    scales: Union[float, tuple] = 0.1,
    shear: Union[float, tuple] = 10,
    filter_box_candidates: bool = False,
    wh_thr=2,
    ar_thr=20,
    area_thr=0.1,
    border_value=114,
):
    """
    Performs random affine transform to img, targets
    :param img:         Input image of shape [h, w, c]
    :param targets:     Input target
    :param targets_seg: Targets derived from segmentation masks
    :param target_size: Desired output shape
    :param degrees:     Degrees for random rotation, when float the random values are drawn uniformly
                            from (-degrees, degrees).
    :param translate:   Translate size (in pixels) for random translation, when float the random values
                            are drawn uniformly from (-translate, translate)
    :param scales:      Values for random rescale, when float the random values are drawn uniformly
                            from (0.1-scales, 0.1+scales)
    :param shear:       Degrees for random shear, when float the random values are drawn uniformly
                                from (shear, shear)

    :param filter_box_candidates:    whether to filter out transformed bboxes by edge size, area ratio, and aspect ratio.
    :param wh_thr: (float) edge size threshold when filter_box_candidates = True. Bounding oxes with edges smaller
      then this values will be filtered out. (default=2)

    :param ar_thr: (float) aspect ratio threshold filter_box_candidates = True. Bounding boxes with aspect ratio larger
      then this values will be filtered out. (default=20)

    :param area_thr:(float) threshold for area ratio between original image and the transformed one, when when filter_box_candidates = True.
      Bounding boxes with such ratio smaller then this value will be filtered out. (default=0.1)

    :param border_value: value for filling borders after applying transforms (default=114).

    :return:            Image and Target with applied random affine
    """

    targets_seg = np.zeros((targets.shape[0], 0)) if targets_seg is None else targets_seg
    M = get_affine_matrix(img.shape[:2], target_size, degrees, translate, scales, shear)

    img = cv2.warpAffine(img, M, dsize=target_size, borderValue=(border_value, border_value, border_value))

    # Transform label coordinates
    if len(targets) > 0:
        targets_orig = targets.copy()
        targets = apply_affine_to_bboxes(targets, targets_seg, target_size, M)
        if filter_box_candidates:
            box_candidates_ids = _filter_box_candidates(targets_orig[:, :4], targets[:, :4], wh_thr=wh_thr, ar_thr=ar_thr, area_thr=area_thr)
            targets = targets[box_candidates_ids]
    return img, targets

rescale_and_pad_to_size(img, input_size, swap=(2, 0, 1), pad_val=114)

Rescales image according to minimum ratio between the target height /image height, target width / image width, and pads the image to the target size.

Parameters:

Name Type Description Default
img

Image to be rescaled

required
input_size

Target size

required
swap

Axis's to be rearranged.

(2, 0, 1)

Returns:

Type Description

rescaled image, ratio

Source code in training/transforms/transforms.py
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
def rescale_and_pad_to_size(img, input_size, swap=(2, 0, 1), pad_val=114):
    """
    Rescales image according to minimum ratio between the target height /image height, target width / image width,
    and pads the image to the target size.

    :param img: Image to be rescaled
    :param input_size: Target size
    :param swap: Axis's to be rearranged.
    :return: rescaled image, ratio
    """
    if len(img.shape) == 3:
        padded_img = np.ones((input_size[0], input_size[1], img.shape[-1]), dtype=np.uint8) * pad_val
    else:
        padded_img = np.ones(input_size, dtype=np.uint8) * pad_val

    r = min(input_size[0] / img.shape[0], input_size[1] / img.shape[1])
    resized_img = cv2.resize(
        img,
        (int(img.shape[1] * r), int(img.shape[0] * r)),
        interpolation=cv2.INTER_LINEAR,
    ).astype(np.uint8)
    padded_img[: int(img.shape[0] * r), : int(img.shape[1] * r)] = resized_img

    padded_img = padded_img.transpose(swap)
    padded_img = np.ascontiguousarray(padded_img, dtype=np.float32)
    return padded_img, r