Transforms to apply data augmentation in Computer Vision
img = PILImage(PILImage.create(TEST_IMAGE).resize((600,400)))

class RandTransform[source]

RandTransform(p=1.0, nm=None, before_call=None, **kwargs) :: DisplayedTransform

A transform that before_call its state at each __call__

As for all Transform you can pass encodes and decodes at init or subclass and implement them. You can do the same for the before_call method that is called at each __call__. Note that to have a consistent state for inputs and targets, a RandTransform must be applied at the tuple level.

By default the before_call behavior is to execute the transform with probability p (if subclassing and wanting to tweak that behavior, the attribute, if it exists, is looked for to decide if the transform is executed or not).


RandTransform.before_call(b, split_idx)

Set based on self.p

def _add1(x): return x+1
dumb_tfm = RandTransform(enc=_add1, p=0.5)
start,d1,d2 = 2,False,False
for _ in range(40):
    t = dumb_tfm(start, split_idx=0)
    if test_eq(t, start+1); d1=True
    else:           test_eq(t, start)  ; d2=True
assert d1 and d2
_add1 -- {'p': 0.5}:
encodes: (object,object) -> _add1decodes: 

Item transforms

class FlipItem[source]

FlipItem(p=0.5) :: RandTransform

Randomly flip with probability p

Calls @patch'd flip_lr behaviors for Image, TensorImage, TensorPoint, and TensorBBox

tflip = FlipItem(p=1.)
test_eq(tflip(bbox,split_idx=0), tensor([[1.,0., 0.,1]]) -1)

class DihedralItem[source]

DihedralItem(p=1.0, nm=None, before_call=None, **kwargs) :: RandTransform

Randomly flip with probability p

Calls @patch'd dihedral behaviors for PILImage, TensorImage, TensorPoint, and TensorBBox

By default each of the 8 dihedral transformations (including noop) have the same probability of being picked when the transform is applied. You can customize this behavior by passing your own draw function. To force a specific flip, you can also pass an integer between 0 and 7.

_,axs = subplots(2, 4)
for ax in axs.flatten():
    show_image(DihedralItem(p=1.)(img, split_idx=0), ctx=ax)

Resize with crop, pad or squish

class PadMode[source]

PadMode(*args, **kwargs)

All possible padding mode as attributes to get tab-completion and typo-proofing

class CropPad[source]

CropPad(size, pad_mode='zeros', **kwargs) :: DisplayedTransform

Center crop or pad an image to size

Calls @patch'd crop_pad behaviors for Image, TensorImage, TensorPoint, and TensorBBox

_,axs = plt.subplots(1,3,figsize=(12,4))
for ax,sz in zip(axs.flatten(), [300, 500, 700]):
    show_image(img.crop_pad(sz), ctx=ax, title=f'Size {sz}');
_,axs = plt.subplots(1,3,figsize=(12,4))
for ax,mode in zip(axs.flatten(), [PadMode.Zeros, PadMode.Border, PadMode.Reflection]):
    show_image(img.crop_pad((600,700), pad_mode=mode), ctx=ax, title=mode);

class RandomCrop[source]

RandomCrop(size, **kwargs) :: RandTransform

Randomly crop an image to size

class OldRandomCrop[source]

OldRandomCrop(size, pad_mode='zeros', enc=None, dec=None, split_idx=None, order=None) :: CropPad

Randomly crop an image to size

_,axs = plt.subplots(1,3,figsize=(12,4))
f = RandomCrop(200)
for ax in axs: show_image(f(img), ctx=ax);

On the validation set, we take a center crop.

_,axs = plt.subplots(1,3,figsize=(12,4))
for ax in axs: show_image(f(img, split_idx=1), ctx=ax);