MCPcopy Create free account
hub / github.com/HobbitLong/PyContrast / AutoAugmentOp

Class AutoAugmentOp

pycontrast/datasets/RandAugment.py:281–309  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

279
280
281class AutoAugmentOp:
282
283 def __init__(self, name, prob=0.5, magnitude=10, hparams=None):
284 hparams = hparams or _HPARAMS_DEFAULT
285 self.aug_fn = NAME_TO_OP[name]
286 self.level_fn = LEVEL_TO_ARG[name]
287 self.prob = prob
288 self.magnitude = magnitude
289 self.hparams = hparams.copy()
290 self.kwargs = dict(
291 fillcolor=hparams['img_mean'] if 'img_mean' in hparams else _FILL,
292 resample=hparams['interpolation'] if 'interpolation' in hparams else _RANDOM_INTERPOLATION,
293 )
294
295 # If magnitude_std is > 0, we introduce some randomness
296 # in the usually fixed policy and sample magnitude from a normal distribution
297 # with mean `magnitude` and std-dev of `magnitude_std`.
298 # NOTE This is my own hack, being tested, not in papers or reference impls.
299 self.magnitude_std = self.hparams.get('magnitude_std', 0)
300
301 def __call__(self, img):
302 if random.random() > self.prob:
303 return img
304 magnitude = self.magnitude
305 if self.magnitude_std and self.magnitude_std > 0:
306 magnitude = random.gauss(magnitude, self.magnitude_std)
307 magnitude = min(_MAX_LEVEL, max(0, magnitude)) # clip to valid range
308 level_args = self.level_fn(magnitude, self.hparams) if self.level_fn is not None else tuple()
309 return self.aug_fn(img, *level_args, **self.kwargs)
310
311
312_RAND_TRANSFORMS = [

Callers 2

rand_augment_opsFunction · 0.85
rand_augment_ops_cmcFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected