Safemotion Lib
Loading...
Searching...
No Matches
Classes | Functions | Variables
fastreid.data.transforms.autoaugment Namespace Reference

Classes

class  AugmentOp
 
class  AugMixAugment
 
class  AutoAugment
 
class  RandAugment
 

Functions

 _interpolation (kwargs)
 
 _check_args_tf (kwargs)
 
 shear_x (img, factor, **kwargs)
 
 shear_y (img, factor, **kwargs)
 
 translate_x_rel (img, pct, **kwargs)
 
 translate_y_rel (img, pct, **kwargs)
 
 translate_x_abs (img, pixels, **kwargs)
 
 translate_y_abs (img, pixels, **kwargs)
 
 rotate (img, degrees, **kwargs)
 
 auto_contrast (img, **__)
 
 invert (img, **__)
 
 equalize (img, **__)
 
 solarize (img, thresh, **__)
 
 solarize_add (img, add, thresh=128, **__)
 
 posterize (img, bits_to_keep, **__)
 
 contrast (img, factor, **__)
 
 color (img, factor, **__)
 
 brightness (img, factor, **__)
 
 sharpness (img, factor, **__)
 
 _randomly_negate (v)
 
 _rotate_level_to_arg (level, _hparams)
 
 _enhance_level_to_arg (level, _hparams)
 
 _enhance_increasing_level_to_arg (level, _hparams)
 
 _shear_level_to_arg (level, _hparams)
 
 _translate_abs_level_to_arg (level, hparams)
 
 _translate_rel_level_to_arg (level, hparams)
 
 _posterize_level_to_arg (level, _hparams)
 
 _posterize_increasing_level_to_arg (level, hparams)
 
 _posterize_original_level_to_arg (level, _hparams)
 
 _solarize_level_to_arg (level, _hparams)
 
 _solarize_increasing_level_to_arg (level, _hparams)
 
 _solarize_add_level_to_arg (level, _hparams)
 
 auto_augment_policy_v0 (hparams)
 
 auto_augment_policy_v0r (hparams)
 
 auto_augment_policy_original (hparams)
 
 auto_augment_policy_originalr (hparams)
 
 auto_augment_policy (name="original")
 
 auto_augment_transform (config_str, hparams)
 
 _select_rand_weights (weight_idx=0, transforms=None)
 
 rand_augment_ops (magnitude=10, hparams=None, transforms=None)
 
 rand_augment_transform (config_str, hparams)
 
 augmix_ops (magnitude=10, hparams=None, transforms=None)
 
 augment_and_mix_transform (config_str, hparams)
 

Variables

 _PIL_VER = tuple([int(x) for x in PIL.__version__.split('.')[:2]])
 
tuple _FILL = (128, 128, 128)
 
int _MAX_LEVEL = 10.
 
 _HPARAMS_DEFAULT
 
tuple _RANDOM_INTERPOLATION = (Image.BILINEAR, Image.BICUBIC)
 
dict LEVEL_TO_ARG
 
dict NAME_TO_OP
 
list _RAND_TRANSFORMS
 
dict _RAND_CHOICE_WEIGHTS_0
 
list _AUGMIX_TRANSFORMS
 

Detailed Description

@author:  liaoxingyu
@contact: sherlockliao01@gmail.com

Function Documentation

◆ _check_args_tf()

fastreid.data.transforms.autoaugment._check_args_tf ( kwargs)
protected

Definition at line 53 of file autoaugment.py.

53def _check_args_tf(kwargs):
54 if 'fillcolor' in kwargs and _PIL_VER < (5, 0):
55 kwargs.pop('fillcolor')
56 kwargs['resample'] = _interpolation(kwargs)
57
58

◆ _enhance_increasing_level_to_arg()

fastreid.data.transforms.autoaugment._enhance_increasing_level_to_arg ( level,
_hparams )
protected

Definition at line 193 of file autoaugment.py.

193def _enhance_increasing_level_to_arg(level, _hparams):
194 # the 'no change' level is 1.0, moving away from that towards 0. or 2.0 increases the enhancement blend
195 # range [0.1, 1.9]
196 level = (level / _MAX_LEVEL) * .9
197 level = 1.0 + _randomly_negate(level)
198 return level,
199
200

◆ _enhance_level_to_arg()

fastreid.data.transforms.autoaugment._enhance_level_to_arg ( level,
_hparams )
protected

Definition at line 188 of file autoaugment.py.

188def _enhance_level_to_arg(level, _hparams):
189 # range [0.1, 1.9]
190 return (level / _MAX_LEVEL) * 1.8 + 0.1,
191
192

◆ _interpolation()

fastreid.data.transforms.autoaugment._interpolation ( kwargs)
protected

Definition at line 45 of file autoaugment.py.

45def _interpolation(kwargs):
46 interpolation = kwargs.pop('resample', Image.BILINEAR)
47 if isinstance(interpolation, (list, tuple)):
48 return random.choice(interpolation)
49 else:
50 return interpolation
51
52

◆ _posterize_increasing_level_to_arg()

fastreid.data.transforms.autoaugment._posterize_increasing_level_to_arg ( level,
hparams )
protected

Definition at line 230 of file autoaugment.py.

230def _posterize_increasing_level_to_arg(level, hparams):
231 # As per Tensorflow models research and UDA impl
232 # range [4, 0], 'keep 4 down to 0 MSB of original image',
233 # intensity/severity of augmentation increases with level
234 return 4 - _posterize_level_to_arg(level, hparams)[0],
235
236

◆ _posterize_level_to_arg()

fastreid.data.transforms.autoaugment._posterize_level_to_arg ( level,
_hparams )
protected

Definition at line 223 of file autoaugment.py.

223def _posterize_level_to_arg(level, _hparams):
224 # As per Tensorflow TPU EfficientNet impl
225 # range [0, 4], 'keep 0 up to 4 MSB of original image'
226 # intensity/severity of augmentation decreases with level
227 return int((level / _MAX_LEVEL) * 4),
228
229

◆ _posterize_original_level_to_arg()

fastreid.data.transforms.autoaugment._posterize_original_level_to_arg ( level,
_hparams )
protected

Definition at line 237 of file autoaugment.py.

237def _posterize_original_level_to_arg(level, _hparams):
238 # As per original AutoAugment paper description
239 # range [4, 8], 'keep 4 up to 8 MSB of image'
240 # intensity/severity of augmentation decreases with level
241 return int((level / _MAX_LEVEL) * 4) + 4,
242
243

◆ _randomly_negate()

fastreid.data.transforms.autoaugment._randomly_negate ( v)
protected
With 50% prob, negate the value

Definition at line 176 of file autoaugment.py.

176def _randomly_negate(v):
177 """With 50% prob, negate the value"""
178 return -v if random.random() > 0.5 else v
179
180

◆ _rotate_level_to_arg()

fastreid.data.transforms.autoaugment._rotate_level_to_arg ( level,
_hparams )
protected

Definition at line 181 of file autoaugment.py.

181def _rotate_level_to_arg(level, _hparams):
182 # range [-30, 30]
183 level = (level / _MAX_LEVEL) * 30.
184 level = _randomly_negate(level)
185 return level,
186
187

◆ _select_rand_weights()

fastreid.data.transforms.autoaugment._select_rand_weights ( weight_idx = 0,
transforms = None )
protected

Definition at line 600 of file autoaugment.py.

600def _select_rand_weights(weight_idx=0, transforms=None):
601 transforms = transforms or _RAND_TRANSFORMS
602 assert weight_idx == 0 # only one set of weights currently
603 rand_weights = _RAND_CHOICE_WEIGHTS_0
604 probs = [rand_weights[k] for k in transforms]
605 probs /= np.sum(probs)
606 return probs
607
608

◆ _shear_level_to_arg()

fastreid.data.transforms.autoaugment._shear_level_to_arg ( level,
_hparams )
protected

Definition at line 201 of file autoaugment.py.

201def _shear_level_to_arg(level, _hparams):
202 # range [-0.3, 0.3]
203 level = (level / _MAX_LEVEL) * 0.3
204 level = _randomly_negate(level)
205 return level,
206
207

◆ _solarize_add_level_to_arg()

fastreid.data.transforms.autoaugment._solarize_add_level_to_arg ( level,
_hparams )
protected

Definition at line 256 of file autoaugment.py.

256def _solarize_add_level_to_arg(level, _hparams):
257 # range [0, 110]
258 return int((level / _MAX_LEVEL) * 110),
259
260

◆ _solarize_increasing_level_to_arg()

fastreid.data.transforms.autoaugment._solarize_increasing_level_to_arg ( level,
_hparams )
protected

Definition at line 250 of file autoaugment.py.

250def _solarize_increasing_level_to_arg(level, _hparams):
251 # range [0, 256]
252 # intensity/severity of augmentation increases with level
253 return 256 - _solarize_level_to_arg(level, _hparams)[0],
254
255

◆ _solarize_level_to_arg()

fastreid.data.transforms.autoaugment._solarize_level_to_arg ( level,
_hparams )
protected

Definition at line 244 of file autoaugment.py.

244def _solarize_level_to_arg(level, _hparams):
245 # range [0, 256]
246 # intensity/severity of augmentation decreases with level
247 return int((level / _MAX_LEVEL) * 256),
248
249

◆ _translate_abs_level_to_arg()

fastreid.data.transforms.autoaugment._translate_abs_level_to_arg ( level,
hparams )
protected

Definition at line 208 of file autoaugment.py.

208def _translate_abs_level_to_arg(level, hparams):
209 translate_const = hparams['translate_const']
210 level = (level / _MAX_LEVEL) * float(translate_const)
211 level = _randomly_negate(level)
212 return level,
213
214

◆ _translate_rel_level_to_arg()

fastreid.data.transforms.autoaugment._translate_rel_level_to_arg ( level,
hparams )
protected

Definition at line 215 of file autoaugment.py.

215def _translate_rel_level_to_arg(level, hparams):
216 # default range [-0.45, 0.45]
217 translate_pct = hparams.get('translate_pct', 0.45)
218 level = (level / _MAX_LEVEL) * translate_pct
219 level = _randomly_negate(level)
220 return level,
221
222

◆ augment_and_mix_transform()

fastreid.data.transforms.autoaugment.augment_and_mix_transform ( config_str,
hparams )
 Create AugMix PyTorch transform
:param config_str: String defining configuration of random augmentation. Consists of multiple sections separated by
dashes ('-'). The first section defines the specific variant of rand augment (currently only 'rand'). The remaining
sections, not order sepecific determine
    'm' - integer magnitude (severity) of augmentation mix (default: 3)
    'w' - integer width of augmentation chain (default: 3)
    'd' - integer depth of augmentation chain (-1 is random [1, 3], default: -1)
    'b' - integer (bool), blend each branch of chain into end result without a final blend, less CPU (default: 0)
    'mstd' -  float std deviation of magnitude noise applied (default: 0)
Ex 'augmix-m5-w4-d2' results in AugMix with severity 5, chain width 4, chain depth 2
:param hparams: Other hparams (kwargs) for the Augmentation transforms
:return: A PyTorch compatible Transform

Definition at line 769 of file autoaugment.py.

769def augment_and_mix_transform(config_str, hparams):
770 """ Create AugMix PyTorch transform
771 :param config_str: String defining configuration of random augmentation. Consists of multiple sections separated by
772 dashes ('-'). The first section defines the specific variant of rand augment (currently only 'rand'). The remaining
773 sections, not order sepecific determine
774 'm' - integer magnitude (severity) of augmentation mix (default: 3)
775 'w' - integer width of augmentation chain (default: 3)
776 'd' - integer depth of augmentation chain (-1 is random [1, 3], default: -1)
777 'b' - integer (bool), blend each branch of chain into end result without a final blend, less CPU (default: 0)
778 'mstd' - float std deviation of magnitude noise applied (default: 0)
779 Ex 'augmix-m5-w4-d2' results in AugMix with severity 5, chain width 4, chain depth 2
780 :param hparams: Other hparams (kwargs) for the Augmentation transforms
781 :return: A PyTorch compatible Transform
782 """
783 magnitude = 3
784 width = 3
785 depth = -1
786 alpha = 1.
787 blended = False
788 config = config_str.split('-')
789 assert config[0] == 'augmix'
790 config = config[1:]
791 for c in config:
792 cs = re.split(r'(\d.*)', c)
793 if len(cs) < 2:
794 continue
795 key, val = cs[:2]
796 if key == 'mstd':
797 # noise param injected via hparams for now
798 hparams.setdefault('magnitude_std', float(val))
799 elif key == 'm':
800 magnitude = int(val)
801 elif key == 'w':
802 width = int(val)
803 elif key == 'd':
804 depth = int(val)
805 elif key == 'a':
806 alpha = float(val)
807 elif key == 'b':
808 blended = bool(val)
809 else:
810 assert False, 'Unknown AugMix config section'
811 ops = augmix_ops(magnitude=magnitude, hparams=hparams)
812 return AugMixAugment(ops, alpha=alpha, width=width, depth=depth, blended=blended)

◆ augmix_ops()

fastreid.data.transforms.autoaugment.augmix_ops ( magnitude = 10,
hparams = None,
transforms = None )

Definition at line 695 of file autoaugment.py.

695def augmix_ops(magnitude=10, hparams=None, transforms=None):
696 hparams = hparams or _HPARAMS_DEFAULT
697 transforms = transforms or _AUGMIX_TRANSFORMS
698 return [AugmentOp(
699 name, prob=1.0, magnitude=magnitude, hparams=hparams) for name in transforms]
700
701

◆ auto_augment_policy()

fastreid.data.transforms.autoaugment.auto_augment_policy ( name = "original")

Definition at line 481 of file autoaugment.py.

481def auto_augment_policy(name="original"):
482 hparams = _HPARAMS_DEFAULT
483 if name == 'original':
484 return auto_augment_policy_original(hparams)
485 elif name == 'originalr':
486 return auto_augment_policy_originalr(hparams)
487 elif name == 'v0':
488 return auto_augment_policy_v0(hparams)
489 elif name == 'v0r':
490 return auto_augment_policy_v0r(hparams)
491 else:
492 assert False, 'Unknown AA policy (%s)' % name
493
494

◆ auto_augment_policy_original()

fastreid.data.transforms.autoaugment.auto_augment_policy_original ( hparams)

Definition at line 415 of file autoaugment.py.

415def auto_augment_policy_original(hparams):
416 # ImageNet policy from https://arxiv.org/abs/1805.09501
417 policy = [
418 [('PosterizeOriginal', 0.4, 8), ('Rotate', 0.6, 9)],
419 [('Solarize', 0.6, 5), ('AutoContrast', 0.6, 5)],
420 [('Equalize', 0.8, 8), ('Equalize', 0.6, 3)],
421 [('PosterizeOriginal', 0.6, 7), ('PosterizeOriginal', 0.6, 6)],
422 [('Equalize', 0.4, 7), ('Solarize', 0.2, 4)],
423 [('Equalize', 0.4, 4), ('Rotate', 0.8, 8)],
424 [('Solarize', 0.6, 3), ('Equalize', 0.6, 7)],
425 [('PosterizeOriginal', 0.8, 5), ('Equalize', 1.0, 2)],
426 [('Rotate', 0.2, 3), ('Solarize', 0.6, 8)],
427 [('Equalize', 0.6, 8), ('PosterizeOriginal', 0.4, 6)],
428 [('Rotate', 0.8, 8), ('Color', 0.4, 0)],
429 [('Rotate', 0.4, 9), ('Equalize', 0.6, 2)],
430 [('Equalize', 0.0, 7), ('Equalize', 0.8, 8)],
431 [('Invert', 0.6, 4), ('Equalize', 1.0, 8)],
432 [('Color', 0.6, 4), ('Contrast', 1.0, 8)],
433 [('Rotate', 0.8, 8), ('Color', 1.0, 2)],
434 [('Color', 0.8, 8), ('Solarize', 0.8, 7)],
435 [('Sharpness', 0.4, 7), ('Invert', 0.6, 8)],
436 [('ShearX', 0.6, 5), ('Equalize', 1.0, 9)],
437 [('Color', 0.4, 0), ('Equalize', 0.6, 3)],
438 [('Equalize', 0.4, 7), ('Solarize', 0.2, 4)],
439 [('Solarize', 0.6, 5), ('AutoContrast', 0.6, 5)],
440 [('Invert', 0.6, 4), ('Equalize', 1.0, 8)],
441 [('Color', 0.6, 4), ('Contrast', 1.0, 8)],
442 [('Equalize', 0.8, 8), ('Equalize', 0.6, 3)],
443 ]
444 pc = [[AugmentOp(*a, hparams=hparams) for a in sp] for sp in policy]
445 return pc
446
447

◆ auto_augment_policy_originalr()

fastreid.data.transforms.autoaugment.auto_augment_policy_originalr ( hparams)

Definition at line 448 of file autoaugment.py.

448def auto_augment_policy_originalr(hparams):
449 # ImageNet policy from https://arxiv.org/abs/1805.09501 with research posterize variation
450 policy = [
451 [('PosterizeIncreasing', 0.4, 8), ('Rotate', 0.6, 9)],
452 [('Solarize', 0.6, 5), ('AutoContrast', 0.6, 5)],
453 [('Equalize', 0.8, 8), ('Equalize', 0.6, 3)],
454 [('PosterizeIncreasing', 0.6, 7), ('PosterizeIncreasing', 0.6, 6)],
455 [('Equalize', 0.4, 7), ('Solarize', 0.2, 4)],
456 [('Equalize', 0.4, 4), ('Rotate', 0.8, 8)],
457 [('Solarize', 0.6, 3), ('Equalize', 0.6, 7)],
458 [('PosterizeIncreasing', 0.8, 5), ('Equalize', 1.0, 2)],
459 [('Rotate', 0.2, 3), ('Solarize', 0.6, 8)],
460 [('Equalize', 0.6, 8), ('PosterizeIncreasing', 0.4, 6)],
461 [('Rotate', 0.8, 8), ('Color', 0.4, 0)],
462 [('Rotate', 0.4, 9), ('Equalize', 0.6, 2)],
463 [('Equalize', 0.0, 7), ('Equalize', 0.8, 8)],
464 [('Invert', 0.6, 4), ('Equalize', 1.0, 8)],
465 [('Color', 0.6, 4), ('Contrast', 1.0, 8)],
466 [('Rotate', 0.8, 8), ('Color', 1.0, 2)],
467 [('Color', 0.8, 8), ('Solarize', 0.8, 7)],
468 [('Sharpness', 0.4, 7), ('Invert', 0.6, 8)],
469 [('ShearX', 0.6, 5), ('Equalize', 1.0, 9)],
470 [('Color', 0.4, 0), ('Equalize', 0.6, 3)],
471 [('Equalize', 0.4, 7), ('Solarize', 0.2, 4)],
472 [('Solarize', 0.6, 5), ('AutoContrast', 0.6, 5)],
473 [('Invert', 0.6, 4), ('Equalize', 1.0, 8)],
474 [('Color', 0.6, 4), ('Contrast', 1.0, 8)],
475 [('Equalize', 0.8, 8), ('Equalize', 0.6, 3)],
476 ]
477 pc = [[AugmentOp(*a, hparams=hparams) for a in sp] for sp in policy]
478 return pc
479
480

◆ auto_augment_policy_v0()

fastreid.data.transforms.autoaugment.auto_augment_policy_v0 ( hparams)

Definition at line 348 of file autoaugment.py.

348def auto_augment_policy_v0(hparams):
349 # ImageNet v0 policy from TPU EfficientNet impl, cannot find a paper reference.
350 policy = [
351 [('Equalize', 0.8, 1), ('ShearY', 0.8, 4)],
352 [('Color', 0.4, 9), ('Equalize', 0.6, 3)],
353 [('Color', 0.4, 1), ('Rotate', 0.6, 8)],
354 [('Solarize', 0.8, 3), ('Equalize', 0.4, 7)],
355 [('Solarize', 0.4, 2), ('Solarize', 0.6, 2)],
356 [('Color', 0.2, 0), ('Equalize', 0.8, 8)],
357 [('Equalize', 0.4, 8), ('SolarizeAdd', 0.8, 3)],
358 [('ShearX', 0.2, 9), ('Rotate', 0.6, 8)],
359 [('Color', 0.6, 1), ('Equalize', 1.0, 2)],
360 [('Invert', 0.4, 9), ('Rotate', 0.6, 0)],
361 [('Equalize', 1.0, 9), ('ShearY', 0.6, 3)],
362 [('Color', 0.4, 7), ('Equalize', 0.6, 0)],
363 [('Posterize', 0.4, 6), ('AutoContrast', 0.4, 7)],
364 [('Solarize', 0.6, 8), ('Color', 0.6, 9)],
365 [('Solarize', 0.2, 4), ('Rotate', 0.8, 9)],
366 [('Rotate', 1.0, 7), ('TranslateYRel', 0.8, 9)],
367 [('ShearX', 0.0, 0), ('Solarize', 0.8, 4)],
368 [('ShearY', 0.8, 0), ('Color', 0.6, 4)],
369 [('Color', 1.0, 0), ('Rotate', 0.6, 2)],
370 [('Equalize', 0.8, 4), ('Equalize', 0.0, 8)],
371 [('Equalize', 1.0, 4), ('AutoContrast', 0.6, 2)],
372 [('ShearY', 0.4, 7), ('SolarizeAdd', 0.6, 7)],
373 [('Posterize', 0.8, 2), ('Solarize', 0.6, 10)], # This results in black image with Tpu posterize
374 [('Solarize', 0.6, 8), ('Equalize', 0.6, 1)],
375 [('Color', 0.8, 6), ('Rotate', 0.4, 5)],
376 ]
377 pc = [[AugmentOp(*a, hparams=hparams) for a in sp] for sp in policy]
378 return pc
379
380

◆ auto_augment_policy_v0r()

fastreid.data.transforms.autoaugment.auto_augment_policy_v0r ( hparams)

Definition at line 381 of file autoaugment.py.

381def auto_augment_policy_v0r(hparams):
382 # ImageNet v0 policy from TPU EfficientNet impl, with variation of Posterize used
383 # in Google research implementation (number of bits discarded increases with magnitude)
384 policy = [
385 [('Equalize', 0.8, 1), ('ShearY', 0.8, 4)],
386 [('Color', 0.4, 9), ('Equalize', 0.6, 3)],
387 [('Color', 0.4, 1), ('Rotate', 0.6, 8)],
388 [('Solarize', 0.8, 3), ('Equalize', 0.4, 7)],
389 [('Solarize', 0.4, 2), ('Solarize', 0.6, 2)],
390 [('Color', 0.2, 0), ('Equalize', 0.8, 8)],
391 [('Equalize', 0.4, 8), ('SolarizeAdd', 0.8, 3)],
392 [('ShearX', 0.2, 9), ('Rotate', 0.6, 8)],
393 [('Color', 0.6, 1), ('Equalize', 1.0, 2)],
394 [('Invert', 0.4, 9), ('Rotate', 0.6, 0)],
395 [('Equalize', 1.0, 9), ('ShearY', 0.6, 3)],
396 [('Color', 0.4, 7), ('Equalize', 0.6, 0)],
397 [('PosterizeIncreasing', 0.4, 6), ('AutoContrast', 0.4, 7)],
398 [('Solarize', 0.6, 8), ('Color', 0.6, 9)],
399 [('Solarize', 0.2, 4), ('Rotate', 0.8, 9)],
400 [('Rotate', 1.0, 7), ('TranslateYRel', 0.8, 9)],
401 [('ShearX', 0.0, 0), ('Solarize', 0.8, 4)],
402 [('ShearY', 0.8, 0), ('Color', 0.6, 4)],
403 [('Color', 1.0, 0), ('Rotate', 0.6, 2)],
404 [('Equalize', 0.8, 4), ('Equalize', 0.0, 8)],
405 [('Equalize', 1.0, 4), ('AutoContrast', 0.6, 2)],
406 [('ShearY', 0.4, 7), ('SolarizeAdd', 0.6, 7)],
407 [('PosterizeIncreasing', 0.8, 2), ('Solarize', 0.6, 10)],
408 [('Solarize', 0.6, 8), ('Equalize', 0.6, 1)],
409 [('Color', 0.8, 6), ('Rotate', 0.4, 5)],
410 ]
411 pc = [[AugmentOp(*a, hparams=hparams) for a in sp] for sp in policy]
412 return pc
413
414

◆ auto_augment_transform()

fastreid.data.transforms.autoaugment.auto_augment_transform ( config_str,
hparams )
Create a AutoAugment transform
:param config_str: String defining configuration of auto augmentation. Consists of multiple sections separated by
dashes ('-'). The first section defines the AutoAugment policy (one of 'v0', 'v0r', 'original', 'originalr').
The remaining sections, not order sepecific determine
    'mstd' -  float std deviation of magnitude noise applied
Ex 'original-mstd0.5' results in AutoAugment with original policy, magnitude_std 0.5
:param hparams: Other hparams (kwargs) for the AutoAugmentation scheme
:return: A PyTorch compatible Transform

Definition at line 513 of file autoaugment.py.

513def auto_augment_transform(config_str, hparams):
514 """
515 Create a AutoAugment transform
516 :param config_str: String defining configuration of auto augmentation. Consists of multiple sections separated by
517 dashes ('-'). The first section defines the AutoAugment policy (one of 'v0', 'v0r', 'original', 'originalr').
518 The remaining sections, not order sepecific determine
519 'mstd' - float std deviation of magnitude noise applied
520 Ex 'original-mstd0.5' results in AutoAugment with original policy, magnitude_std 0.5
521 :param hparams: Other hparams (kwargs) for the AutoAugmentation scheme
522 :return: A PyTorch compatible Transform
523 """
524 config = config_str.split('-')
525 policy_name = config[0]
526 config = config[1:]
527 for c in config:
528 cs = re.split(r'(\d.*)', c)
529 if len(cs) < 2:
530 continue
531 key, val = cs[:2]
532 if key == 'mstd':
533 # noise param injected via hparams for now
534 hparams.setdefault('magnitude_std', float(val))
535 else:
536 assert False, 'Unknown AutoAugment config section'
537 aa_policy = auto_augment_policy(policy_name)
538 return AutoAugment(aa_policy)
539
540

◆ auto_contrast()

fastreid.data.transforms.autoaugment.auto_contrast ( img,
** __ )

Definition at line 123 of file autoaugment.py.

123def auto_contrast(img, **__):
124 return ImageOps.autocontrast(img)
125
126

◆ brightness()

fastreid.data.transforms.autoaugment.brightness ( img,
factor,
** __ )

Definition at line 168 of file autoaugment.py.

168def brightness(img, factor, **__):
169 return ImageEnhance.Brightness(img).enhance(factor)
170
171

◆ color()

fastreid.data.transforms.autoaugment.color ( img,
factor,
** __ )

Definition at line 164 of file autoaugment.py.

164def color(img, factor, **__):
165 return ImageEnhance.Color(img).enhance(factor)
166
167

◆ contrast()

fastreid.data.transforms.autoaugment.contrast ( img,
factor,
** __ )

Definition at line 160 of file autoaugment.py.

160def contrast(img, factor, **__):
161 return ImageEnhance.Contrast(img).enhance(factor)
162
163

◆ equalize()

fastreid.data.transforms.autoaugment.equalize ( img,
** __ )

Definition at line 131 of file autoaugment.py.

131def equalize(img, **__):
132 return ImageOps.equalize(img)
133
134

◆ invert()

fastreid.data.transforms.autoaugment.invert ( img,
** __ )

Definition at line 127 of file autoaugment.py.

127def invert(img, **__):
128 return ImageOps.invert(img)
129
130

◆ posterize()

fastreid.data.transforms.autoaugment.posterize ( img,
bits_to_keep,
** __ )

Definition at line 154 of file autoaugment.py.

154def posterize(img, bits_to_keep, **__):
155 if bits_to_keep >= 8:
156 return img
157 return ImageOps.posterize(img, bits_to_keep)
158
159

◆ rand_augment_ops()

fastreid.data.transforms.autoaugment.rand_augment_ops ( magnitude = 10,
hparams = None,
transforms = None )

Definition at line 609 of file autoaugment.py.

609def rand_augment_ops(magnitude=10, hparams=None, transforms=None):
610 hparams = hparams or _HPARAMS_DEFAULT
611 transforms = transforms or _RAND_TRANSFORMS
612 return [AugmentOp(
613 name, prob=0.5, magnitude=magnitude, hparams=hparams) for name in transforms]
614
615

◆ rand_augment_transform()

fastreid.data.transforms.autoaugment.rand_augment_transform ( config_str,
hparams )
Create a RandAugment transform
:param config_str: String defining configuration of random augmentation. Consists of multiple sections separated by
dashes ('-'). The first section defines the specific variant of rand augment (currently only 'rand'). The remaining
sections, not order sepecific determine
    'm' - integer magnitude of rand augment
    'n' - integer num layers (number of transform ops selected per image)
    'w' - integer probabiliy weight index (index of a set of weights to influence choice of op)
    'mstd' -  float std deviation of magnitude noise applied
    'inc' - integer (bool), use augmentations that increase in severity with magnitude (default: 0)
Ex 'rand-m9-n3-mstd0.5' results in RandAugment with magnitude 9, num_layers 3, magnitude_std 0.5
'rand-mstd1-w0' results in magnitude_std 1.0, weights 0, default magnitude of 10 and num_layers 2
:param hparams: Other hparams (kwargs) for the RandAugmentation scheme
:return: A PyTorch compatible Transform

Definition at line 631 of file autoaugment.py.

631def rand_augment_transform(config_str, hparams):
632 """
633 Create a RandAugment transform
634 :param config_str: String defining configuration of random augmentation. Consists of multiple sections separated by
635 dashes ('-'). The first section defines the specific variant of rand augment (currently only 'rand'). The remaining
636 sections, not order sepecific determine
637 'm' - integer magnitude of rand augment
638 'n' - integer num layers (number of transform ops selected per image)
639 'w' - integer probabiliy weight index (index of a set of weights to influence choice of op)
640 'mstd' - float std deviation of magnitude noise applied
641 'inc' - integer (bool), use augmentations that increase in severity with magnitude (default: 0)
642 Ex 'rand-m9-n3-mstd0.5' results in RandAugment with magnitude 9, num_layers 3, magnitude_std 0.5
643 'rand-mstd1-w0' results in magnitude_std 1.0, weights 0, default magnitude of 10 and num_layers 2
644 :param hparams: Other hparams (kwargs) for the RandAugmentation scheme
645 :return: A PyTorch compatible Transform
646 """
647 magnitude = _MAX_LEVEL # default to _MAX_LEVEL for magnitude (currently 10)
648 num_layers = 2 # default to 2 ops per image
649 weight_idx = None # default to no probability weights for op choice
650 transforms = _RAND_TRANSFORMS
651 config = config_str.split('-')
652 assert config[0] == 'rand'
653 config = config[1:]
654 for c in config:
655 cs = re.split(r'(\d.*)', c)
656 if len(cs) < 2:
657 continue
658 key, val = cs[:2]
659 if key == 'mstd':
660 # noise param injected via hparams for now
661 hparams.setdefault('magnitude_std', float(val))
662 elif key == 'inc':
663 if bool(val):
664 transforms = _RAND_INCREASING_TRANSFORMS
665 elif key == 'm':
666 magnitude = int(val)
667 elif key == 'n':
668 num_layers = int(val)
669 elif key == 'w':
670 weight_idx = int(val)
671 else:
672 assert False, 'Unknown RandAugment config section'
673 ra_ops = rand_augment_ops(magnitude=magnitude, hparams=hparams, transforms=transforms)
674 choice_weights = None if weight_idx is None else _select_rand_weights(weight_idx)
675 return RandAugment(ra_ops, num_layers, choice_weights=choice_weights)
676
677

◆ rotate()

fastreid.data.transforms.autoaugment.rotate ( img,
degrees,
** kwargs )

Definition at line 91 of file autoaugment.py.

91def rotate(img, degrees, **kwargs):
92 _check_args_tf(kwargs)
93 if _PIL_VER >= (5, 2):
94 return img.rotate(degrees, **kwargs)
95 elif _PIL_VER >= (5, 0):
96 w, h = img.size
97 post_trans = (0, 0)
98 rotn_center = (w / 2.0, h / 2.0)
99 angle = -math.radians(degrees)
100 matrix = [
101 round(math.cos(angle), 15),
102 round(math.sin(angle), 15),
103 0.0,
104 round(-math.sin(angle), 15),
105 round(math.cos(angle), 15),
106 0.0,
107 ]
108
109 def transform(x, y, matrix):
110 (a, b, c, d, e, f) = matrix
111 return a * x + b * y + c, d * x + e * y + f
112
113 matrix[2], matrix[5] = transform(
114 -rotn_center[0] - post_trans[0], -rotn_center[1] - post_trans[1], matrix
115 )
116 matrix[2] += rotn_center[0]
117 matrix[5] += rotn_center[1]
118 return img.transform(img.size, Image.AFFINE, matrix, **kwargs)
119 else:
120 return img.rotate(degrees, resample=kwargs['resample'])
121
122

◆ sharpness()

fastreid.data.transforms.autoaugment.sharpness ( img,
factor,
** __ )

Definition at line 172 of file autoaugment.py.

172def sharpness(img, factor, **__):
173 return ImageEnhance.Sharpness(img).enhance(factor)
174
175

◆ shear_x()

fastreid.data.transforms.autoaugment.shear_x ( img,
factor,
** kwargs )

Definition at line 59 of file autoaugment.py.

59def shear_x(img, factor, **kwargs):
60 _check_args_tf(kwargs)
61 return img.transform(img.size, Image.AFFINE, (1, factor, 0, 0, 1, 0), **kwargs)
62
63

◆ shear_y()

fastreid.data.transforms.autoaugment.shear_y ( img,
factor,
** kwargs )

Definition at line 64 of file autoaugment.py.

64def shear_y(img, factor, **kwargs):
65 _check_args_tf(kwargs)
66 return img.transform(img.size, Image.AFFINE, (1, 0, 0, factor, 1, 0), **kwargs)
67
68

◆ solarize()

fastreid.data.transforms.autoaugment.solarize ( img,
thresh,
** __ )

Definition at line 135 of file autoaugment.py.

135def solarize(img, thresh, **__):
136 return ImageOps.solarize(img, thresh)
137
138

◆ solarize_add()

fastreid.data.transforms.autoaugment.solarize_add ( img,
add,
thresh = 128,
** __ )

Definition at line 139 of file autoaugment.py.

139def solarize_add(img, add, thresh=128, **__):
140 lut = []
141 for i in range(256):
142 if i < thresh:
143 lut.append(min(255, i + add))
144 else:
145 lut.append(i)
146 if img.mode in ("L", "RGB"):
147 if img.mode == "RGB" and len(lut) == 256:
148 lut = lut + lut + lut
149 return img.point(lut)
150 else:
151 return img
152
153

◆ translate_x_abs()

fastreid.data.transforms.autoaugment.translate_x_abs ( img,
pixels,
** kwargs )

Definition at line 81 of file autoaugment.py.

81def translate_x_abs(img, pixels, **kwargs):
82 _check_args_tf(kwargs)
83 return img.transform(img.size, Image.AFFINE, (1, 0, pixels, 0, 1, 0), **kwargs)
84
85

◆ translate_x_rel()

fastreid.data.transforms.autoaugment.translate_x_rel ( img,
pct,
** kwargs )

Definition at line 69 of file autoaugment.py.

69def translate_x_rel(img, pct, **kwargs):
70 pixels = pct * img.size[0]
71 _check_args_tf(kwargs)
72 return img.transform(img.size, Image.AFFINE, (1, 0, pixels, 0, 1, 0), **kwargs)
73
74

◆ translate_y_abs()

fastreid.data.transforms.autoaugment.translate_y_abs ( img,
pixels,
** kwargs )

Definition at line 86 of file autoaugment.py.

86def translate_y_abs(img, pixels, **kwargs):
87 _check_args_tf(kwargs)
88 return img.transform(img.size, Image.AFFINE, (1, 0, 0, 0, 1, pixels), **kwargs)
89
90

◆ translate_y_rel()

fastreid.data.transforms.autoaugment.translate_y_rel ( img,
pct,
** kwargs )

Definition at line 75 of file autoaugment.py.

75def translate_y_rel(img, pct, **kwargs):
76 pixels = pct * img.size[1]
77 _check_args_tf(kwargs)
78 return img.transform(img.size, Image.AFFINE, (1, 0, 0, 0, 1, pixels), **kwargs)
79
80

Variable Documentation

◆ _AUGMIX_TRANSFORMS

list fastreid.data.transforms.autoaugment._AUGMIX_TRANSFORMS
protected
Initial value:
1= [
2 'AutoContrast',
3 'ColorIncreasing', # not in paper
4 'ContrastIncreasing', # not in paper
5 'BrightnessIncreasing', # not in paper
6 'SharpnessIncreasing', # not in paper
7 'Equalize',
8 'Rotate',
9 'PosterizeIncreasing',
10 'SolarizeIncreasing',
11 'ShearX',
12 'ShearY',
13 'TranslateXRel',
14 'TranslateYRel',
15]

Definition at line 678 of file autoaugment.py.

◆ _FILL

tuple fastreid.data.transforms.autoaugment._FILL = (128, 128, 128)
protected

Definition at line 31 of file autoaugment.py.

◆ _HPARAMS_DEFAULT

fastreid.data.transforms.autoaugment._HPARAMS_DEFAULT
protected
Initial value:
1= dict(
2 translate_const=57,
3 img_mean=_FILL,
4)

Definition at line 37 of file autoaugment.py.

◆ _MAX_LEVEL

int fastreid.data.transforms.autoaugment._MAX_LEVEL = 10.
protected

Definition at line 35 of file autoaugment.py.

◆ _PIL_VER

fastreid.data.transforms.autoaugment._PIL_VER = tuple([int(x) for x in PIL.__version__.split('.')[:2]])
protected

Definition at line 29 of file autoaugment.py.

◆ _RAND_CHOICE_WEIGHTS_0

dict fastreid.data.transforms.autoaugment._RAND_CHOICE_WEIGHTS_0
protected
Initial value:
1= {
2 'Rotate': 0.3,
3 'ShearX': 0.2,
4 'ShearY': 0.2,
5 'TranslateXRel': 0.1,
6 'TranslateYRel': 0.1,
7 'Color': .025,
8 'Sharpness': 0.025,
9 'AutoContrast': 0.025,
10 'Solarize': .005,
11 'SolarizeAdd': .005,
12 'Contrast': .005,
13 'Brightness': .005,
14 'Equalize': .005,
15 'Posterize': 0,
16 'Invert': 0,
17}

Definition at line 581 of file autoaugment.py.

◆ _RAND_TRANSFORMS

list fastreid.data.transforms.autoaugment._RAND_TRANSFORMS
protected
Initial value:
1= [
2 'AutoContrast',
3 'Equalize',
4 'Invert',
5 'Rotate',
6 'Posterize',
7 'Solarize',
8 'SolarizeAdd',
9 'Color',
10 'Contrast',
11 'Brightness',
12 'Sharpness',
13 'ShearX',
14 'ShearY',
15 'TranslateXRel',
16 'TranslateYRel',
17 # 'Cutout' # NOTE I've implement this as random erasing separately]_RAND_INCREASING_TRANSFORMS = [ 'AutoContrast', 'Equalize', 'Invert', 'Rotate', 'PosterizeIncreasing', 'SolarizeIncreasing', 'SolarizeAdd', 'ColorIncreasing', 'ContrastIncreasing', 'BrightnessIncreasing', 'SharpnessIncreasing', 'ShearX', 'ShearY', 'TranslateXRel', 'TranslateYRel', # 'Cutout' # NOTE I've implement this as random erasing separately
18]

Definition at line 541 of file autoaugment.py.

◆ _RANDOM_INTERPOLATION

tuple fastreid.data.transforms.autoaugment._RANDOM_INTERPOLATION = (Image.BILINEAR, Image.BICUBIC)
protected

Definition at line 42 of file autoaugment.py.

◆ LEVEL_TO_ARG

dict fastreid.data.transforms.autoaugment.LEVEL_TO_ARG
Initial value:
1= {
2 'AutoContrast': None,
3 'Equalize': None,
4 'Invert': None,
5 'Rotate': _rotate_level_to_arg,
6 # There are several variations of the posterize level scaling in various Tensorflow/Google repositories/papers
7 'Posterize': _posterize_level_to_arg,
8 'PosterizeIncreasing': _posterize_increasing_level_to_arg,
9 'PosterizeOriginal': _posterize_original_level_to_arg,
10 'Solarize': _solarize_level_to_arg,
11 'SolarizeIncreasing': _solarize_increasing_level_to_arg,
12 'SolarizeAdd': _solarize_add_level_to_arg,
13 'Color': _enhance_level_to_arg,
14 'ColorIncreasing': _enhance_increasing_level_to_arg,
15 'Contrast': _enhance_level_to_arg,
16 'ContrastIncreasing': _enhance_increasing_level_to_arg,
17 'Brightness': _enhance_level_to_arg,
18 'BrightnessIncreasing': _enhance_increasing_level_to_arg,
19 'Sharpness': _enhance_level_to_arg,
20 'SharpnessIncreasing': _enhance_increasing_level_to_arg,
21 'ShearX': _shear_level_to_arg,
22 'ShearY': _shear_level_to_arg,
23 'TranslateX': _translate_abs_level_to_arg,
24 'TranslateY': _translate_abs_level_to_arg,
25 'TranslateXRel': _translate_rel_level_to_arg,
26 'TranslateYRel': _translate_rel_level_to_arg,
27}

Definition at line 261 of file autoaugment.py.

◆ NAME_TO_OP

dict fastreid.data.transforms.autoaugment.NAME_TO_OP
Initial value:
1= {
2 'AutoContrast': auto_contrast,
3 'Equalize': equalize,
4 'Invert': invert,
5 'Rotate': rotate,
6 'Posterize': posterize,
7 'PosterizeIncreasing': posterize,
8 'PosterizeOriginal': posterize,
9 'Solarize': solarize,
10 'SolarizeIncreasing': solarize,
11 'SolarizeAdd': solarize_add,
12 'Color': color,
13 'ColorIncreasing': color,
14 'Contrast': contrast,
15 'ContrastIncreasing': contrast,
16 'Brightness': brightness,
17 'BrightnessIncreasing': brightness,
18 'Sharpness': sharpness,
19 'SharpnessIncreasing': sharpness,
20 'ShearX': shear_x,
21 'ShearY': shear_y,
22 'TranslateX': translate_x_abs,
23 'TranslateY': translate_y_abs,
24 'TranslateXRel': translate_x_rel,
25 'TranslateYRel': translate_y_rel,
26}

Definition at line 289 of file autoaugment.py.