__init__.py 3.6 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
G
gaotingquan 已提交
17
from ppcls.data.preprocess.ops.timm_autoaugment import RawTimmAutoAugment
F
Felix 已提交
18 19 20 21 22 23 24 25 26 27
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
from ppcls.data.preprocess.ops.operators import RandCropImage
H
HydrogenSulfate 已提交
28
from ppcls.data.preprocess.ops.operators import RandCropImageV2
F
Felix 已提交
29 30 31 32
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 已提交
33
from ppcls.data.preprocess.ops.operators import Pad
H
HydrogenSulfate 已提交
34 35
from ppcls.data.preprocess.ops.operators import ToTensor
from ppcls.data.preprocess.ops.operators import Normalize
Z
zhiboniu 已提交
36
from ppcls.data.preprocess.ops.operators import RandomCropImage
Z
zhiboniu 已提交
37
from ppcls.data.preprocess.ops.operators import Padv2
F
Felix 已提交
38

39
from ppcls.data.preprocess.batch_ops.batch_operators import MixupOperator, CutmixOperator, OpSampler, FmixOperator
F
Felix 已提交
40 41 42

import numpy as np
from PIL import Image
C
cuicheng01 已提交
43
import random
F
Felix 已提交
44 45 46 47 48 49 50 51 52 53 54


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 """
55

F
Felix 已提交
56
    def __init__(self, *args, **kwargs):
G
gaotingquan 已提交
57
        super().__init__(*args, **kwargs)
F
Felix 已提交
58 59 60 61 62 63

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

G
gaotingquan 已提交
64
        img = super().__call__(img)
F
Felix 已提交
65 66 67 68 69 70 71 72 73

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

        return img


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

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

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

G
gaotingquan 已提交
83 84 85 86 87 88 89 90 91 92 93
        img = super().__call__(img)

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

        return img


class TimmAutoAugment(RawTimmAutoAugment):
    """ TimmAutoAugment wrapper to auto fit different img tyeps. """

C
cuicheng01 已提交
94
    def __init__(self, prob=1.0, *args, **kwargs):
G
gaotingquan 已提交
95
        super().__init__(*args, **kwargs)
C
cuicheng01 已提交
96
        self.prob = prob
G
gaotingquan 已提交
97 98 99 100 101

    def __call__(self, img):
        if not isinstance(img, Image.Image):
            img = np.ascontiguousarray(img)
            img = Image.fromarray(img)
C
cuicheng01 已提交
102 103
        if random.random() < self.prob:
            img = super().__call__(img)
F
Felix 已提交
104 105 106 107
        if isinstance(img, Image.Image):
            img = np.asarray(img)

        return img