RandAugment¶
- class mmcls.datasets.transforms.RandAugment(policies, num_policies, magnitude_level, magnitude_std=0.0, total_level=10, hparams={'pad_val': 128})[source]¶
Random augmentation.
This data augmentation is proposed in RandAugment: Practical automated data augmentation with a reduced search space.
- Parameters
The policies of random augmentation. If string, use preset policies collection like “timm_increasing”. If list, each item is one specific augmentation policy dict. The policy dict shall should have these keys:
type
(str), The type of augmentation.magnitude_range
(Sequence[number], optional): For those augmentation have magnitude, you need to specify the magnitude level mapping range. For example, assumetotal_level
is 10,magnitude_level=3
specify magnitude is 3 ifmagnitude_range=(0, 10)
while specify magnitude is 7 ifmagnitude_range=(10, 0)
.other keyword arguments of the augmentation.
num_policies (int) – Number of policies to select from policies each time.
magnitude_level (int | float) – Magnitude level for all the augmentation selected.
magnitude_std (Number | str) –
Deviation of magnitude noise applied.
If positive number, the magnitude obeys normal distribution \(\mathcal{N}(magnitude_level, magnitude_std)\).
If 0 or negative number, magnitude remains unchanged.
If str “inf”, the magnitude obeys uniform distribution \(Uniform(min, magnitude)\).
total_level (int | float) – Total level for the magnitude. Defaults to 10.
hparams (dict) – Configs of hyperparameters. Hyperparameters will be used in policies that require these arguments if these arguments are not set in policy dicts. Defaults to
dict(pad_val=128)
.
Examples
To use “timm-increasing” policies collection, select two policies every time, and magnitude_level of every policy is 6 (total is 10 by default)
>>> import numpy as np >>> from mmcls.datasets import RandAugment >>> transform = RandAugment( ... policies='timm_increasing', ... num_policies=2, ... magnitude_level=6, ... ) >>> data = {'img': np.random.randint(0, 256, (224, 224, 3))} >>> results = transform(data) >>> print(results['img'].shape) (224, 224, 3)
If you want the
magnitude_level
randomly changes every time, you can usemagnitude_std
to specify the random distribution. For example, a normal distribution \(\mathcal{N}(6, 0.5)\).>>> transform = RandAugment( ... policies='timm_increasing', ... num_policies=2, ... magnitude_level=6, ... magnitude_std=0.5, ... )
You can also use your own policies:
>>> policies = [ ... dict(type='AutoContrast'), ... dict(type='Rotate', magnitude_range=(0, 30)), ... dict(type='ColorTransform', magnitude_range=(0, 0.9)), ... ] >>> transform = RandAugment( ... policies=policies, ... num_policies=2, ... magnitude_level=6 ... )
Note
magnitude_std
will introduce some randomness to policy, modified by https://github.com/rwightman/pytorch-image-models.When magnitude_std=0, we calculate the magnitude as follows:
\[\text{magnitude} = \frac{\text{magnitude_level}} {\text{totallevel}} \times (\text{val2} - \text{val1}) + \text{val1}\]