presets.py 2.57 KB
Newer Older
1
import torch
2
from torchvision.transforms import autoaugment, transforms
3
from torchvision.transforms.functional import InterpolationMode
4
5
6


class ClassificationPresetTrain:
7
8
    def __init__(
        self,
9
        *,
10
11
12
        crop_size,
        mean=(0.485, 0.456, 0.406),
        std=(0.229, 0.224, 0.225),
13
        interpolation=InterpolationMode.BILINEAR,
14
15
        hflip_prob=0.5,
        auto_augment_policy=None,
Ponku's avatar
Ponku committed
16
17
        ra_magnitude=9,
        augmix_severity=3,
18
        random_erase_prob=0.0,
Ponku's avatar
Ponku committed
19
        center_crop=False,
20
    ):
Ponku's avatar
Ponku committed
21
22
23
24
25
        trans = (
            [transforms.RandomResizedCrop(crop_size, interpolation=interpolation)]
            if center_crop
            else [transforms.CenterCrop(crop_size)]
        )
26
27
28
        if hflip_prob > 0:
            trans.append(transforms.RandomHorizontalFlip(hflip_prob))
        if auto_augment_policy is not None:
29
            if auto_augment_policy == "ra":
Ponku's avatar
Ponku committed
30
                trans.append(autoaugment.RandAugment(interpolation=interpolation, magnitude=ra_magnitude))
31
            elif auto_augment_policy == "ta_wide":
32
                trans.append(autoaugment.TrivialAugmentWide(interpolation=interpolation))
33
            elif auto_augment_policy == "augmix":
Ponku's avatar
Ponku committed
34
                trans.append(autoaugment.AugMix(interpolation=interpolation, severity=augmix_severity))
35
36
            else:
                aa_policy = autoaugment.AutoAugmentPolicy(auto_augment_policy)
37
                trans.append(autoaugment.AutoAugment(policy=aa_policy, interpolation=interpolation))
38
39
40
41
42
43
44
        trans.extend(
            [
                transforms.PILToTensor(),
                transforms.ConvertImageDtype(torch.float),
                transforms.Normalize(mean=mean, std=std),
            ]
        )
45
46
47
48
49
50
51
52
53
54
        if random_erase_prob > 0:
            trans.append(transforms.RandomErasing(p=random_erase_prob))

        self.transforms = transforms.Compose(trans)

    def __call__(self, img):
        return self.transforms(img)


class ClassificationPresetEval:
55
56
    def __init__(
        self,
57
        *,
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        crop_size,
        resize_size=256,
        mean=(0.485, 0.456, 0.406),
        std=(0.229, 0.224, 0.225),
        interpolation=InterpolationMode.BILINEAR,
    ):

        self.transforms = transforms.Compose(
            [
                transforms.Resize(resize_size, interpolation=interpolation),
                transforms.CenterCrop(crop_size),
                transforms.PILToTensor(),
                transforms.ConvertImageDtype(torch.float),
                transforms.Normalize(mean=mean, std=std),
            ]
        )
74
75
76

    def __call__(self, img):
        return self.transforms(img)