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

class RandTransform[source]

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

A transform that before_call its state at each __call__

Type Default Details
p float 1.0 Probability of applying Transform
nm str None No Content
before_call callable None Optional batchwise preprocessing function
kwargs No Content

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 self.do, if it exists, is looked for to decide if the transform is executed or not).

RandTransform.before_call[source]

RandTransform.before_call(b, split_idx:int)

This function can be overridden. Set self.do based on self.p

Type Default Details
b No Content
split_idx int Index of the train/valid dataset
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 dumb_tfm.do: test_eq(t, start+1); d1=True
    else:           test_eq(t, start)  ; d2=True
assert d1 and d2
dumb_tfm
_add1 -- {'p': 0.5}:
encodes: (object,object) -> _add1decodes: 

Item transforms

class FlipItem[source]

FlipItem(p:float=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:float=1.0, nm:str=None, before_call:callable=None, **kwargs) :: RandTransform

Randomly flip with probability p

Type Default Details
p float 1.0 Probability of applying Transform
nm str None No Content
before_call callable None Optional batchwise preprocessing function
kwargs No Content

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:(int, tuple), pad_mode:PadMode='zeros', **kwargs) :: DisplayedTransform

Center crop or pad an image to size

Type Default Details
size (int, tuple) Size to crop or pad to, duplicated if one value is specified
pad_mode PadMode zeros A PadMode
Valid Keyword Arguments
enc None Argument passed to Transform.__init__
dec None Argument passed to Transform.__init__
split_idx None Argument passed to Transform.__init__
order None Argument passed to Transform.__init__

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}');
    print(img.crop_pad(sz).shape)
(300, 300)
(500, 500)
(700, 700)
_,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:(int, tuple), **kwargs) :: RandTransform

Randomly crop an image to size

Type Default Details
size (int, tuple) Size to crop to, duplicated if one value is specified
Valid Keyword Arguments
p float 1.0 Probability of applying Transform passed to RandTransform.__init__
nm str None Argument passed to RandTransform.__init__
before_call callable None Optional batchwise preprocessing function passed to RandTransform.__init__

class OldRandomCrop[source]

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

Randomly crop an image to size

Type Default Details
size (int, tuple) Size to crop or pad to, duplicated if one value is specified
pad_mode PadMode zeros A PadMode
Valid Keyword Arguments
enc None Argument passed to Transform.__init__
dec None Argument passed to Transform.__init__
split_idx None Argument passed to Transform.__init__
order None Argument passed to Transform.__init__
_,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);