Skip to content

augmentations

cut_mix

1
2
3
4
5
6
7
cut_mix(
    batch1: tf.Tensor,
    batch2: tf.Tensor,
    ignore_background: bool = False,
    invert_mask: bool = False,
    mask: Optional[tf.Tensor] = None,
) -> Tuple[tf.Tensor, tf.Tensor]

Performs the cutmix operation of two image batches.

A random image patch from batch2 is taken and inserted into batch1.

Parameters:

Name Type Description Default
batch1 tf.Tensor

Batch of grid-shaped data of shape (batch, height, width, channel).

required
batch2 tf.Tensor

Batch of grid-shaped data of shape (batch, height, width, channel).

required
ignore_background bool

If true, pixels belonging to the backgroud are ignored. Only applicable for images where the background is represented as 0. Defaults to False.

False
invert_mask bool

If true, the mask is inverted. 1->0 and 0->1. Defaults to False.

False
mask Optional[tf.Tensor]

Binary mask that requires same shape as batch1 and batch2. If None mask is generated randomly. Defaults to None.

None

Returns:

Name Type Description
ground_truth_mask tf.Tensor

Actual mask that has been applied

new_batch tf.Tensor

Batch with applied cutmix opperation

Source code in DeepSaki/augmentations/grid_cutting.py
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def cut_mix(
    batch1: tf.Tensor,
    batch2: tf.Tensor,
    ignore_background: bool = False,
    invert_mask: bool = False,
    mask: Optional[tf.Tensor] = None,
) -> Tuple[tf.Tensor, tf.Tensor]:
    """Performs the cutmix operation of two image batches.

    A random image patch from `batch2` is taken and inserted into `batch1.`

    Args:
        batch1 (tf.Tensor): Batch of grid-shaped data of shape (`batch`, `height`, `width`, `channel`).
        batch2 (tf.Tensor): Batch of grid-shaped data of shape (`batch`, `height`, `width`, `channel`).
        ignore_background (bool, optional): If true, pixels belonging to the backgroud are ignored. Only applicable for
            images where the background is represented as 0. Defaults to False.
        invert_mask (bool, optional): If true, the mask is inverted. 1->0 and 0->1. Defaults to False.
        mask (Optional[tf.Tensor], optional): Binary mask that requires same shape as `batch1` and `batch2`. If `None`
            mask is generated randomly. Defaults to None.

    Returns:
        ground_truth_mask: Actual mask that has been applied
        new_batch: Batch with applied cutmix opperation
    """
    batch1 = tf.cast(batch1, tf.float32)
    batch2 = tf.cast(batch2, tf.float32)

    if mask is None:  # generate mask
        mask = _get_mask(shape=batch1.shape)

    if ignore_background:  # check where in image are no background pixels (value = 1)
        batch1_mask = tf.cast(tf.where(batch1 > 0, 1, 0), tf.int32)
        batch2_mask = tf.cast(tf.where(batch2 > 0, 1, 0), tf.int32)
        mutal_person_mask = tf.cast(tf.clip_by_value((batch1_mask + batch2_mask), 0, 1), tf.float32)
        ground_truth_mask = 1 - (1 - mask) * mutal_person_mask

    else:
        ground_truth_mask = mask

    if invert_mask:
        ground_truth_mask = _invert_mask(ground_truth_mask)

    new_batch = batch1 * ground_truth_mask + batch2 * _invert_mask(ground_truth_mask)

    return ground_truth_mask, new_batch

cut_out

1
2
3
4
5
cut_out(
    batch: tf.Tensor,
    invert_mask: bool = False,
    mask: Optional[tf.Tensor] = None,
) -> Tuple[tf.Tensor, tf.Tensor]

Performs the cutout operation of a batch of images.

Parameters:

Name Type Description Default
batch tf.Tensor

Batch of grid-shaped data of shape (batch, height, width, channel).

required
invert_mask bool

If true, the mask is inverted. 1->0 and 0->1. Defaults to False.

False
mask Optional[tf.Tensor]

Binary mask that requires same shape as batch1 and batch2. If None mask is generated randomly. Defaults to None.

None

Returns:

Name Type Description
mask tf.Tensor

Actual mask that has been applied

new_batch tf.Tensor

Batch with applied cutout opperation

Source code in DeepSaki/augmentations/grid_cutting.py
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
def cut_out(
    batch: tf.Tensor, invert_mask: bool = False, mask: Optional[tf.Tensor] = None
) -> Tuple[tf.Tensor, tf.Tensor]:
    """Performs the cutout operation of a batch of images.

    Args:
        batch (tf.Tensor): Batch of grid-shaped data of shape (`batch`, `height`, `width`, `channel`).
        invert_mask (bool, optional):  If true, the mask is inverted. 1->0 and 0->1. Defaults to False.
        mask (Optional[tf.Tensor], optional): Binary mask that requires same shape as `batch1` and `batch2`. If `None`
            mask is generated randomly. Defaults to None.

    Returns:
        mask: Actual mask that has been applied
        new_batch: Batch with applied cutout opperation
    """
    batch = tf.cast(batch, tf.float32)

    if mask is None:  # generate mask
        mask = _get_mask(shape=batch.shape)

    if invert_mask:
        mask = _invert_mask(mask)

    new_batch = batch * mask
    return mask, new_batch