__init__.py 5.7 KB
Newer Older
F
Felix 已提交
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
F
Felix 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#
# 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.

from ppcls.data.preprocess.ops.autoaugment import ImageNetPolicy as RawImageNetPolicy
from ppcls.data.preprocess.ops.randaugment import RandAugment as RawRandAugment
17
from ppcls.data.preprocess.ops.randaugment import RandomApply
18
from ppcls.data.preprocess.ops.randaugment import RandAugmentV2 as RawRandAugmentV2
Y
Yang Nie 已提交
19
from ppcls.data.preprocess.ops.randaugment import RandAugmentV3 as RawRandAugmentV3
G
gaotingquan 已提交
20
from ppcls.data.preprocess.ops.timm_autoaugment import RawTimmAutoAugment
F
Felix 已提交
21 22 23 24 25 26 27 28 29
from ppcls.data.preprocess.ops.cutout import Cutout

from ppcls.data.preprocess.ops.hide_and_seek import HideAndSeek
from ppcls.data.preprocess.ops.random_erasing import RandomErasing
from ppcls.data.preprocess.ops.grid import GridMask

from ppcls.data.preprocess.ops.operators import DecodeImage
from ppcls.data.preprocess.ops.operators import ResizeImage
from ppcls.data.preprocess.ops.operators import CropImage
30
from ppcls.data.preprocess.ops.operators import CropImageAtRatio
H
add xbm  
HydrogenSulfate 已提交
31
from ppcls.data.preprocess.ops.operators import CenterCrop, Resize
F
Felix 已提交
32
from ppcls.data.preprocess.ops.operators import RandCropImage
H
HydrogenSulfate 已提交
33
from ppcls.data.preprocess.ops.operators import RandCropImageV2
F
Felix 已提交
34 35 36 37
from ppcls.data.preprocess.ops.operators import RandFlipImage
from ppcls.data.preprocess.ops.operators import NormalizeImage
from ppcls.data.preprocess.ops.operators import ToCHWImage
from ppcls.data.preprocess.ops.operators import AugMix
W
weishengyu 已提交
38
from ppcls.data.preprocess.ops.operators import Pad
H
HydrogenSulfate 已提交
39 40
from ppcls.data.preprocess.ops.operators import ToTensor
from ppcls.data.preprocess.ops.operators import Normalize
D
dongshuilong 已提交
41
from ppcls.data.preprocess.ops.operators import RandomHorizontalFlip
H
add xbm  
HydrogenSulfate 已提交
42
from ppcls.data.preprocess.ops.operators import RandomResizedCrop
43 44 45
from ppcls.data.preprocess.ops.operators import CropWithPadding
from ppcls.data.preprocess.ops.operators import RandomInterpolationAugment
from ppcls.data.preprocess.ops.operators import ColorJitter
Z
zh-hike 已提交
46
from ppcls.data.preprocess.ops.operators import RandomGrayscale
Z
zhiboniu 已提交
47
from ppcls.data.preprocess.ops.operators import RandomCropImage
H
HydrogenSulfate 已提交
48
from ppcls.data.preprocess.ops.operators import RandomRotation
Z
zhiboniu 已提交
49
from ppcls.data.preprocess.ops.operators import Padv2
50
from ppcls.data.preprocess.ops.operators import RandomRot90
G
gaotingquan 已提交
51
from ppcls.data.preprocess.ops.operators import PCALighting
G
gaotingquan 已提交
52
from .ops.operators import format_data
D
dongshuilong 已提交
53
from paddle.vision.transforms import Pad as Pad_paddle_vision
F
Felix 已提交
54

55
from ppcls.data.preprocess.batch_ops.batch_operators import MixupOperator, CutmixOperator, OpSampler, FmixOperator
悟、's avatar
悟、 已提交
56
from ppcls.data.preprocess.batch_ops.batch_operators import MixupCutmixHybrid
F
Felix 已提交
57 58 59

import numpy as np
from PIL import Image
C
cuicheng01 已提交
60
import random
F
Felix 已提交
61

Y
Yang Nie 已提交
62

F
Felix 已提交
63 64 65 66 67 68 69 70 71
def transform(data, ops=[]):
    """ transform """
    for op in ops:
        data = op(data)
    return data


class AutoAugment(RawImageNetPolicy):
    """ ImageNetPolicy wrapper to auto fit different img types """
72

F
Felix 已提交
73
    def __init__(self, *args, **kwargs):
G
gaotingquan 已提交
74
        super().__init__(*args, **kwargs)
F
Felix 已提交
75 76 77 78 79 80

    def __call__(self, img):
        if not isinstance(img, Image.Image):
            img = np.ascontiguousarray(img)
            img = Image.fromarray(img)

G
gaotingquan 已提交
81
        img = super().__call__(img)
F
Felix 已提交
82 83 84 85 86 87 88 89 90

        if isinstance(img, Image.Image):
            img = np.asarray(img)

        return img


class RandAugment(RawRandAugment):
    """ RandAugment wrapper to auto fit different img types """
91

F
Felix 已提交
92
    def __init__(self, *args, **kwargs):
G
gaotingquan 已提交
93
        super().__init__(*args, **kwargs)
F
Felix 已提交
94 95 96 97 98 99

    def __call__(self, img):
        if not isinstance(img, Image.Image):
            img = np.ascontiguousarray(img)
            img = Image.fromarray(img)

G
gaotingquan 已提交
100 101 102 103 104 105 106 107
        img = super().__call__(img)

        if isinstance(img, Image.Image):
            img = np.asarray(img)

        return img


108 109 110 111 112 113
class RandAugmentV2(RawRandAugmentV2):
    """ RandAugmentV2 wrapper to auto fit different img types """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

114 115 116 117 118 119 120 121 122 123 124 125
    def __call__(self, img):
        if not isinstance(img, Image.Image):
            img = np.ascontiguousarray(img)
            img = Image.fromarray(img)

        img = super().__call__(img)

        if isinstance(img, Image.Image):
            img = np.asarray(img)

        return img

126

Y
Yang Nie 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
class RandAugmentV3(RawRandAugmentV3):
    """ RandAugmentV3 wrapper to auto fit different img types """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def __call__(self, img):
        if not isinstance(img, Image.Image):
            img = np.ascontiguousarray(img)
            img = Image.fromarray(img)

        img = super().__call__(img)

        if isinstance(img, Image.Image):
            img = np.asarray(img)

        return img


G
gaotingquan 已提交
146 147 148
class TimmAutoAugment(RawTimmAutoAugment):
    """ TimmAutoAugment wrapper to auto fit different img tyeps. """

C
cuicheng01 已提交
149
    def __init__(self, prob=1.0, *args, **kwargs):
G
gaotingquan 已提交
150
        super().__init__(*args, **kwargs)
C
cuicheng01 已提交
151
        self.prob = prob
G
gaotingquan 已提交
152

G
gaotingquan 已提交
153 154
    @format_data
    def __call__(self, img):
G
gaotingquan 已提交
155 156 157
        if not isinstance(img, Image.Image):
            img = np.ascontiguousarray(img)
            img = Image.fromarray(img)
C
cuicheng01 已提交
158 159
        if random.random() < self.prob:
            img = super().__call__(img)
F
Felix 已提交
160 161
        if isinstance(img, Image.Image):
            img = np.asarray(img)
G
gaotingquan 已提交
162

Y
Yang Nie 已提交
163
        return img