pretrain_transform.py 3.19 KB
Newer Older
yuguo960516's avatar
bloom  
yuguo960516 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# coding=utf-8
# Copyright 2021 The OneFlow Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import random

import oneflow as flow
from flowvision import transforms
from flowvision.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
from PIL import ImageFilter, ImageOps

from libai.config import LazyCall


class GaussianBlur(object):
    """Gaussian blur augmentation from SimCLR: https://arxiv.org/abs/2002.05709"""

    def __init__(self, sigma=[0.1, 2.0]):
        self.sigma = sigma

    def __call__(self, x):
        sigma = random.uniform(self.sigma[0], self.sigma[1])
        x = x.filter(ImageFilter.GaussianBlur(radius=sigma))
        return x


class Solarize(object):
    """Solarize augmentation from BYOL: https://arxiv.org/abs/2006.07733"""

    def __call__(self, x):
        return ImageOps.solarize(x)


# follow BYOL's augmentation recipe: https://arxiv.org/abs/2006.07733
augmentation1 = [
    LazyCall(transforms.RandomResizedCrop)(size=224, scale=(0.2, 1.0)),
    LazyCall(transforms.RandomApply)(
        transforms=[
            LazyCall(transforms.ColorJitter)(
                brightness=0.4, contrast=0.4, saturation=0.2, hue=0.1
            )  # not strengthened
        ],
        p=0.8,
    ),
    # TODO: Add RandomGrayscale
    # LazyCall(transforms.RandomGrayscale)(p=0.2),
    LazyCall(transforms.RandomApply)(transforms=[LazyCall(GaussianBlur)(sigma=[0.1, 2.0])], p=1.0),
    LazyCall(transforms.RandomHorizontalFlip)(),
    LazyCall(transforms.ToTensor)(),
    LazyCall(transforms.Normalize)(mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD),
]

augmentation2 = [
    LazyCall(transforms.RandomResizedCrop)(size=224, scale=(0.2, 1.0)),
    LazyCall(transforms.RandomApply)(
        transforms=[
            LazyCall(transforms.ColorJitter)(
                brightness=0.4, contrast=0.4, saturation=0.2, hue=0.1
            )  # not strengthened
        ],
        p=0.8,
    ),
    # TODO: Add RandomGrayscale
    # LazyCall(transforms.RandomGrayscale)(p=0.2),
    LazyCall(transforms.RandomApply)(transforms=[LazyCall(GaussianBlur)(sigma=[0.1, 2.0])], p=1.0),
    LazyCall(transforms.RandomApply)(transforms=[LazyCall(Solarize)()], p=0.2),
    LazyCall(transforms.RandomHorizontalFlip)(),
    LazyCall(transforms.ToTensor)(),
    LazyCall(transforms.Normalize)(mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD),
]


class TwoCropsTransform:
    """Take two random crops of one image"""

    def __init__(self, base_transform1, base_transform2):
        self.base_transform1 = base_transform1
        self.base_transform2 = base_transform2

    def __call__(self, x):
        im1 = self.base_transform1(x)
        im2 = self.base_transform2(x)
        return flow.cat((im1, im2), dim=0)