augments.py 2.73 KB
Newer Older
1
# Copyright (c) OpenMMLab. All rights reserved.
unknown's avatar
unknown committed
2
3
4
5
6
7
8
9
10
11
12
import random

import numpy as np

from .builder import build_augment


class Augments(object):
    """Data augments.

    We implement some data augmentation methods, such as mixup, cutmix.
13

unknown's avatar
unknown committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    Args:
        augments_cfg (list[`mmcv.ConfigDict`] | obj:`mmcv.ConfigDict`):
            Config dict of augments

    Example:
        >>> augments_cfg = [
                dict(type='BatchCutMix', alpha=1., num_classes=10, prob=0.5),
                dict(type='BatchMixup', alpha=1., num_classes=10, prob=0.3)
            ]
        >>> augments = Augments(augments_cfg)
        >>> imgs = torch.randn(16, 3, 32, 32)
        >>> label = torch.randint(0, 10, (16, ))
        >>> imgs, label = augments(imgs, label)

    To decide which augmentation within Augments block is used
    the following rule is applied.
    We pick augmentation based on the probabilities. In the example above,
    we decide if we should use BatchCutMix with probability 0.5,
    BatchMixup 0.3. As Identity is not in augments_cfg, we use Identity with
    probability 1 - 0.5 - 0.3 = 0.2.
    """

    def __init__(self, augments_cfg):
        super(Augments, self).__init__()

        if isinstance(augments_cfg, dict):
            augments_cfg = [augments_cfg]

        assert len(augments_cfg) > 0, \
            'The length of augments_cfg should be positive.'
        self.augments = [build_augment(cfg) for cfg in augments_cfg]
        self.augment_probs = [aug.prob for aug in self.augments]

        has_identity = any([cfg['type'] == 'Identity' for cfg in augments_cfg])
        if has_identity:
            assert sum(self.augment_probs) == 1.0,\
                'The sum of augmentation probabilities should equal to 1,' \
                ' but got {:.2f}'.format(sum(self.augment_probs))
        else:
            assert sum(self.augment_probs) <= 1.0,\
                'The sum of augmentation probabilities should less than or ' \
                'equal to 1, but got {:.2f}'.format(sum(self.augment_probs))
            identity_prob = 1 - sum(self.augment_probs)
            if identity_prob > 0:
                num_classes = self.augments[0].num_classes
                self.augments += [
                    build_augment(
                        dict(
                            type='Identity',
                            num_classes=num_classes,
                            prob=identity_prob))
                ]
                self.augment_probs += [identity_prob]

    def __call__(self, img, gt_label):
        if self.augments:
            random_state = np.random.RandomState(random.randint(0, 2**32 - 1))
            aug = random_state.choice(self.augments, p=self.augment_probs)
            return aug(img, gt_label)
        return img, gt_label