This notebook contains implementation of the randaugment item transform
%reload_ext autoreload
%autoreload 2
import matplotlib.pyplot as plt
from fastai2.data.all import untar_data, URLs
from fastai2.vision.all import get_image_files

cifar = untar_data(URLs.CIFAR)
img = Image.open(get_image_files(cifar)[0])

All transformations as functions f(img, level), where level is in $[0, \texttt{PARAMETER_MAX}]$ as in randaugment paper.

float_parameter[source]

float_parameter(level, maxval)

Helper function to scale val between 0 and maxval.

int_parameter[source]

int_parameter(level, maxval)

Helper function to scale val between 0 and maxval.

We use standard geometric transforms, pixel level color transforms and cutout (cutout was used in UDA implementation, and is also present in randaugment) from PIL and albumenations. Geometric transforms:

flip_lr[source]

flip_lr(img, level)

flip_ud[source]

flip_ud(img, level)

rotate[source]

rotate(img, level)

Rotate for 30 degrees max

scale[source]

scale(img, level)

Scale image with level. Zoom in/out at random

shift[source]

shift(img, level)

Do shift with level strength in random directions

cutout[source]

cutout(img, level)

Cutout level blocks from image

find_coeffs[source]

find_coeffs(pa, pb)

perspective[source]

perspective(img, level)

Color transforms include, inversion, autocontrast, histogram equalization, contrast change, color change, brightness change, sharpness change. For contrast, color, brightness and sharpness we manually select ranges (e.g color could change to black and white, or realy vivid based on the value of level parameter + randomness).

invert[source]

invert(img, level)

Negative image

equalize[source]

equalize(img, level)

Equalize image histogram

posterize[source]

posterize(img, level)

Control bits count used to store colors

contrast[source]

contrast(img, level)

Change contrast with param in [0.5, 1.0, 3.0]

color[source]

color(img, level)

Change color with param in [0, 1.0, 3.0]

brightness[source]

brightness(img, level)

Controll brightness with param in [1/3, 2.0]

sharpness[source]

sharpness(img, level)

Controll sharpness with param in [0, 4]

All implemented transforms:

Visualize transforms

def identity(img, level):
    return img
plt.figure(figsize=(10,6))
cnt = len(all_transforms)
print(f"Showing all {cnt} transformations")

for i, t in enumerate([identity] + ALL_TRANSFORMS, 1):
    plt.subplot((cnt + 1) // 5 + (cnt+1) % 5, 5, i)
    plt.imshow(t(img, 10))
    plt.axis(False)
    plt.title(t.__name__)

plt.tight_layout()
plt.show()
Showing all 14 transformations

Policy, randomly choosing 2 transformations

class RandAugment[source]

RandAugment() :: Transform

Apply randaugment augmentation policy

ra = RandAugment()

plt.figure(figsize=(15,15))
for i in range(1,31):
    plt.subplot(5,6,i)
    plt.imshow(ra(PILImage.create(files[0])))
plt.show()
from nbdev.export import notebook2script
notebook2script()
Converted 00_cifar.ipynb.
Converted 01_randaugment.ipynb.
Converted 02_supervised_baselines.ipynb.
Converted index.ipynb.