Additional Batch Augmentations

Adding more native batch augmentations to fastai

source

GrayScaleMode

 GrayScaleMode (value, names=None, module=None, qualname=None, type=None,
                start=1)

GrayScaleModes for GrayScale


source

GrayScale

 GrayScale (p:float=0.1, mode:GrayScaleMode=<GrayScaleMode.Random: 3>)

Convert RGB image into grayscale using luma_bt.601, luma_bt.709, averaging, or randomly selected

Type Default Details
p float 0.1 Per-item probability
mode GrayScaleMode GrayScaleMode.Random GrayScaleMode to apply to images. Random applies all three element-wise with equal probability
with less_random():
    _,axs = plt.subplots(1,3,figsize=(20,4))
    for ax, mode in zip(axs, [GrayScaleMode.Luma601, GrayScaleMode.Luma709, GrayScaleMode.Average]): 
        f = partial(GrayScale(p=1, mode=mode), split_idx=0)
        f(_batch(img.clone())).squeeze().show(ctx=ax, title=f'mode={mode}')


source

ChannelDrop

 ChannelDrop (p:float=0.1, replace:float|None=None)

Drop entire channel by replacing it with random solid value [0,1)

Type Default Details
p float 0.1 Per-item probability
replace float | None None Set constant replacement value. Defaults to element-wise random value [0,1)
with less_random():
    _,axs = plt.subplots(1,3,figsize=(20,4))
    f = ChannelDrop(p=1)
    for ax in axs: f(_batch(img.clone()), split_idx=0).squeeze().show(ctx=ax)


source

RandomNoise

 RandomNoise (p:float=0.25, stdev:float|tuple=(0.1, 0.25),
              random:bool=True)

Add random guassian noise based on stdev

Type Default Details
p float 0.25 Per-item probability
stdev float | tuple (0.1, 0.25) Maximum or range of the standard deviation of added noise
random bool True Randomize standard deviation of added noise between [stdev[0], stdev[1])

Larger images can use higher stdev as seen with this 600x400 pixel example:

with less_random():
    _,axs = plt.subplots(1,4,figsize=(20,4))
    for ax, stdev in zip(axs, [0.1, 0.25, 0.5, 0.75]): 
        f = partial(RandomNoise(p=1, stdev=stdev, random=False), split_idx=0)
        norm_apply_denorm(_batch(img.clone()), f, nrm).squeeze().show(ctx=ax, title=f'stdev={stdev}')

But smaller images should use lower stdev as seen with this 150x100 pixel example:

with less_random():
    _,axs = plt.subplots(1,4,figsize=(20,4))
    r = Resize((100, 150))
    for ax, stdev in zip(axs, [0.1, 0.2, 0.3, 0.5]): 
        f = partial(RandomNoise(p=1, stdev=stdev, random=False), split_idx=0)
        norm_apply_denorm(_batch(r(img.clone(), )), f, nrm).squeeze().show(ctx=ax, title=f'stdev={stdev}')


source

RandomErasingBatch

 RandomErasingBatch (p:float=0.25, sl:float=0.0, sh:float=0.3,
                     min_aspect:float=0.3, max_count:int=1,
                     element:bool=False)

Randomly selects a rectangle region in an image and randomizes its pixels.

Type Default Details
p float 0.25 Per-item probability
sl float 0.0 Minimum proportion of erased area
sh float 0.3 Maximum proportion of erased area
min_aspect float 0.3 Minimum aspect ratio of erased area
max_count int 1 Maximum number of erasing blocks per image, area per box is scaled by count
element bool False Loop over through batch and apply element-wise unique erasing
with less_random():
    _,axs = plt.subplots(1,4,figsize=(18,4))
    for ax, area in zip(axs, [0.05, 0.1, 0.2, 0.3]):
        f = partial(RandomErasingBatch(p=1, sl=area, sh=area), split_idx=0)
        norm_apply_denorm(_batch(img.clone()), f, nrm).squeeze().show(ctx=ax, title=f'area={area}')