Skip to content

randaugment

anomaly_match.datasets.augmentation.randaugment

RandAugment data augmentation implementation.

RandAugment(n, m)

Random augmentation pipeline implementation.

Randomly applies a series of image transformations.

Parameters:

Name Type Description Default
n int

Number of augmentation operations to apply

required
m int

Magnitude parameter [0, 30] (deprecated)

required

Attributes:

Name Type Description
n

Number of augmentation operations to apply

m

Magnitude parameter (deprecated)

augment_list

List of available augmentation operations

Source code in anomaly_match/datasets/augmentation/randaugment.py
def __init__(self, n: int, m: int) -> None:
    self.n = n
    self.m = m
    self.augment_list = augment_list()

__call__(img)

Apply random augmentations to the input image.

Parameters:

Name Type Description Default
img Image

PIL Image to be augmented

required

Returns:

Type Description
Image

Augmented PIL Image

Source code in anomaly_match/datasets/augmentation/randaugment.py
def __call__(self, img: Image.Image) -> Image.Image:
    """Apply random augmentations to the input image.

    Args:
        img: PIL Image to be augmented

    Returns:
        Augmented PIL Image
    """
    ops = random.choices(self.augment_list, k=self.n)
    for op, min_val, max_val in ops:
        val = min_val + float(max_val - min_val) * random.random()
        img = op(img, val)
    cutout_val = random.random() * 0.5
    img = Cutout(img, cutout_val)  # for fixmatch
    return img

AutoContrast(img, _)

Apply automatic contrast to the image.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required

Returns:

Type Description
Image

PIL Image with automatic contrast applied

Source code in anomaly_match/datasets/augmentation/randaugment.py
def AutoContrast(img: Image.Image, _: float) -> Image.Image:
    """Apply automatic contrast to the image.

    Args:
        img: PIL Image to be processed

    Returns:
        PIL Image with automatic contrast applied
    """
    return PIL.ImageOps.autocontrast(img)

Brightness(img, v)

Adjust the brightness of the image.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Brightness factor (must be >= 0.0)

required

Returns:

Type Description
Image

PIL Image with adjusted brightness

Source code in anomaly_match/datasets/augmentation/randaugment.py
def Brightness(img: Image.Image, v: float) -> Image.Image:
    """Adjust the brightness of the image.

    Args:
        img: PIL Image to be processed
        v: Brightness factor (must be >= 0.0)

    Returns:
        PIL Image with adjusted brightness
    """
    assert v >= 0.0
    return PIL.ImageEnhance.Brightness(img).enhance(v)

Color(img, v)

Adjust the color saturation of the image.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Color saturation factor (must be >= 0.0)

required

Returns:

Type Description
Image

PIL Image with adjusted color saturation

Source code in anomaly_match/datasets/augmentation/randaugment.py
def Color(img: Image.Image, v: float) -> Image.Image:
    """Adjust the color saturation of the image.

    Args:
        img: PIL Image to be processed
        v: Color saturation factor (must be >= 0.0)

    Returns:
        PIL Image with adjusted color saturation
    """
    assert v >= 0.0
    return PIL.ImageEnhance.Color(img).enhance(v)

Contrast(img, v)

Adjust the contrast of the image.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Contrast factor (must be >= 0.0)

required

Returns:

Type Description
Image

PIL Image with adjusted contrast

Source code in anomaly_match/datasets/augmentation/randaugment.py
def Contrast(img: Image.Image, v: float) -> Image.Image:
    """Adjust the contrast of the image.

    Args:
        img: PIL Image to be processed
        v: Contrast factor (must be >= 0.0)

    Returns:
        PIL Image with adjusted contrast
    """
    assert v >= 0.0
    return PIL.ImageEnhance.Contrast(img).enhance(v)

Equalize(img, _)

Equalize the histogram of the image.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required

Returns:

Type Description
Image

PIL Image with equalized histogram

Source code in anomaly_match/datasets/augmentation/randaugment.py
def Equalize(img: Image.Image, _: float) -> Image.Image:
    """Equalize the histogram of the image.

    Args:
        img: PIL Image to be processed

    Returns:
        PIL Image with equalized histogram
    """
    return PIL.ImageOps.equalize(img)

Identity(img, v)

Return the image unchanged.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Unused parameter

required

Returns:

Type Description
Image

The original PIL Image

Source code in anomaly_match/datasets/augmentation/randaugment.py
def Identity(img: Image.Image, v: float) -> Image.Image:
    """Return the image unchanged.

    Args:
        img: PIL Image to be processed
        v: Unused parameter

    Returns:
        The original PIL Image
    """
    return img

Posterize(img, v)

Reduce the number of bits for each color channel.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Number of bits to keep for each channel (range [4, 8])

required

Returns:

Type Description
Image

PIL Image with reduced color channels

Source code in anomaly_match/datasets/augmentation/randaugment.py
def Posterize(img: Image.Image, v: float) -> Image.Image:
    """Reduce the number of bits for each color channel.

    Args:
        img: PIL Image to be processed
        v: Number of bits to keep for each channel (range [4, 8])

    Returns:
        PIL Image with reduced color channels
    """
    v = int(v)
    v = max(1, v)
    return PIL.ImageOps.posterize(img, v)

Rotate(img, v)

Rotate the image by v degrees.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Rotation angle in degrees (range [-30, 30])

required

Returns:

Type Description
Image

Rotated PIL Image

Source code in anomaly_match/datasets/augmentation/randaugment.py
def Rotate(img: Image.Image, v: float) -> Image.Image:
    """Rotate the image by v degrees.

    Args:
        img: PIL Image to be processed
        v: Rotation angle in degrees (range [-30, 30])

    Returns:
        Rotated PIL Image
    """
    return img.rotate(v)

Sharpness(img, v)

Adjust the sharpness of the image.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Sharpness factor (must be >= 0.0)

required

Returns:

Type Description
Image

PIL Image with adjusted sharpness

Source code in anomaly_match/datasets/augmentation/randaugment.py
def Sharpness(img: Image.Image, v: float) -> Image.Image:
    """Adjust the sharpness of the image.

    Args:
        img: PIL Image to be processed
        v: Sharpness factor (must be >= 0.0)

    Returns:
        PIL Image with adjusted sharpness
    """
    assert v >= 0.0
    return PIL.ImageEnhance.Sharpness(img).enhance(v)

ShearX(img, v)

Apply horizontal shear to the image.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Shear factor (range [-0.3, 0.3])

required

Returns:

Type Description
Image

PIL Image with horizontal shear applied

Source code in anomaly_match/datasets/augmentation/randaugment.py
def ShearX(img: Image.Image, v: float) -> Image.Image:
    """Apply horizontal shear to the image.

    Args:
        img: PIL Image to be processed
        v: Shear factor (range [-0.3, 0.3])

    Returns:
        PIL Image with horizontal shear applied
    """
    return img.transform(img.size, PIL.Image.AFFINE, (1, v, 0, 0, 1, 0))

ShearY(img, v)

Apply vertical shear to the image.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Shear factor (range [-0.3, 0.3])

required

Returns:

Type Description
Image

PIL Image with vertical shear applied

Source code in anomaly_match/datasets/augmentation/randaugment.py
def ShearY(img: Image.Image, v: float) -> Image.Image:
    """Apply vertical shear to the image.

    Args:
        img: PIL Image to be processed
        v: Shear factor (range [-0.3, 0.3])

    Returns:
        PIL Image with vertical shear applied
    """
    return img.transform(img.size, PIL.Image.AFFINE, (1, 0, 0, v, 1, 0))

TranslateX(img, v)

Translate the image horizontally by a percentage of its width.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Translation factor as a percentage of image width (range [-0.3, 0.3])

required

Returns:

Type Description
Image

PIL Image with horizontal translation applied

Source code in anomaly_match/datasets/augmentation/randaugment.py
def TranslateX(img: Image.Image, v: float) -> Image.Image:
    """Translate the image horizontally by a percentage of its width.

    Args:
        img: PIL Image to be processed
        v: Translation factor as a percentage of image width (range [-0.3, 0.3])

    Returns:
        PIL Image with horizontal translation applied
    """
    v = v * img.size[0]
    return img.transform(img.size, PIL.Image.AFFINE, (1, 0, v, 0, 1, 0))

TranslateY(img, v)

Translate the image vertically by a percentage of its height.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Translation factor as a percentage of image height (range [-0.3, 0.3])

required

Returns:

Type Description
Image

PIL Image with vertical translation applied

Source code in anomaly_match/datasets/augmentation/randaugment.py
def TranslateY(img: Image.Image, v: float) -> Image.Image:
    """Translate the image vertically by a percentage of its height.

    Args:
        img: PIL Image to be processed
        v: Translation factor as a percentage of image height (range [-0.3, 0.3])

    Returns:
        PIL Image with vertical translation applied
    """
    v = v * img.size[1]
    return img.transform(img.size, PIL.Image.AFFINE, (1, 0, 0, 0, 1, v))

Solarize(img, v)

Invert all pixel values above a threshold.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Threshold for solarization (range [0, 256])

required

Returns:

Type Description
Image

Solarized PIL Image

Source code in anomaly_match/datasets/augmentation/randaugment.py
def Solarize(img: Image.Image, v: float) -> Image.Image:
    """Invert all pixel values above a threshold.

    Args:
        img: PIL Image to be processed
        v: Threshold for solarization (range [0, 256])

    Returns:
        Solarized PIL Image
    """
    assert 0 <= v <= 256
    return PIL.ImageOps.solarize(img, v)

Cutout(img, v)

Apply cutout augmentation to the image.

Creates a square mask at a random location in the image.

Parameters:

Name Type Description Default
img Image | ndarray

PIL Image or numpy array to be processed

required
v float

Size of the cutout as a percentage of image size (range [0.0, 0.5])

required

Returns:

Type Description
Image | ndarray

Image with cutout applied

Source code in anomaly_match/datasets/augmentation/randaugment.py
def Cutout(img: Image.Image | np.ndarray, v: float) -> Image.Image | np.ndarray:
    """Apply cutout augmentation to the image.

    Creates a square mask at a random location in the image.

    Args:
        img: PIL Image or numpy array to be processed
        v: Size of the cutout as a percentage of image size (range [0.0, 0.5])

    Returns:
        Image with cutout applied
    """
    assert 0.0 <= v <= 0.5
    if v <= 0.0:
        return img

    if isinstance(img, np.ndarray):
        v = int(v * img.shape[0])
        return A.Cutout(1, v, v, always_apply=True, fill_value=(128))(image=img)["image"]
    else:
        v = v * img.size[0]
        return CutoutAbs(img, v)

CutoutAbs(img, v)

Apply cutout augmentation with absolute size to the image.

Creates a square mask at a random location in the image with specified absolute size.

Parameters:

Name Type Description Default
img Image

PIL Image to be processed

required
v float

Absolute size of the cutout in pixels

required

Returns:

Type Description
Image

PIL Image with cutout applied

Source code in anomaly_match/datasets/augmentation/randaugment.py
def CutoutAbs(img: Image.Image, v: float) -> Image.Image:
    """Apply cutout augmentation with absolute size to the image.

    Creates a square mask at a random location in the image with specified absolute size.

    Args:
        img: PIL Image to be processed
        v: Absolute size of the cutout in pixels

    Returns:
        PIL Image with cutout applied
    """
    if v < 0:
        return img
    w, h = img.size
    x0 = np.random.uniform(w)
    y0 = np.random.uniform(h)

    x0 = int(max(0, x0 - v / 2.0))
    y0 = int(max(0, y0 - v / 2.0))
    x1 = min(w, x0 + v)
    y1 = min(h, y0 + v)

    xy = (x0, y0, x1, y1)
    color = (125, 123, 114)
    img = img.copy()
    PIL.ImageDraw.Draw(img).rectangle(xy, color)
    return img

augment_list()

Return a list of available augmentation operations with their value ranges.

Returns:

Type Description
list[tuple]

List of tuples (operation, min_value, max_value) for each augmentation

Source code in anomaly_match/datasets/augmentation/randaugment.py
def augment_list() -> list[tuple]:
    """Return a list of available augmentation operations with their value ranges.

    Returns:
        List of tuples (operation, min_value, max_value) for each augmentation
    """
    augments = [
        (AutoContrast, 0, 1),
        (Brightness, 0.05, 0.95),
        (Color, 0.05, 0.95),
        (Contrast, 0.05, 0.95),
        (Equalize, 0, 1),
        (Identity, 0, 1),
        (Posterize, 4, 8),
        (Rotate, -30, 30),
        (Sharpness, 0.05, 0.95),
        (ShearX, -0.3, 0.3),
        (ShearY, -0.3, 0.3),
        (Solarize, 0, 256),
        (TranslateX, -0.3, 0.3),
        (TranslateY, -0.3, 0.3),
    ]
    return augments