Skip to content

Commit 520c643

Browse files
rsomani95sgugger
authored andcommittedMar 22, 2019
Adding A New Kind of Data Augmentation for Images (fastai#1860)
* Added RGB-randomiser * Updated documentation to include the 'rgb_randomise' data augmentation * Removed accidental formatting changes. * Randomise -> Randomize
1 parent 203aa2c commit 520c643

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed
 

‎docs_src/vision.transform.ipynb

+48-1
Large diffs are not rendered by default.

‎fastai/vision/transform.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .image import _affine_mult
55

66
__all__ = ['brightness', 'contrast', 'crop', 'crop_pad', 'cutout', 'dihedral', 'dihedral_affine', 'flip_affine', 'flip_lr',
7-
'get_transforms', 'jitter', 'pad', 'perspective_warp', 'rand_pad', 'rand_crop', 'rand_zoom', 'rotate', 'skew', 'squish',
7+
'get_transforms', 'jitter', 'pad', 'perspective_warp', 'rand_pad', 'rand_crop', 'rand_zoom', 'rgb_randomize', 'rotate', 'skew', 'squish',
88
'rand_resize_crop', 'symmetric_warp', 'tilt', 'zoom', 'zoom_crop']
99

1010
_pad_mode_convert = {'reflection':'reflect', 'zeros':'constant', 'border':'replicate'}
@@ -134,6 +134,14 @@ def _cutout(x, n_holes:uniform_int=1, length:uniform_int=40):
134134

135135
cutout = TfmPixel(_cutout, order=20)
136136

137+
def _rgb_randomize(x, channel:int=None, thresh:float=0.3):
138+
"Randomize one of the channels of the input image"
139+
if channel is None: channel = np.random.randint(0, x.shape[0] - 1)
140+
x[channel] = torch.rand(x.shape[1:]) * np.random.uniform(0, thresh)
141+
return x
142+
143+
rgb_randomize = TfmPixel(_rgb_randomize)
144+
137145
def _minus_epsilon(row_pct:float, col_pct:float, eps:float=1e-7):
138146
if row_pct==1.: row_pct -= 1e-7
139147
if col_pct==1.: col_pct -= 1e-7

0 commit comments

Comments
 (0)