PaddleClas/ppcls/data/preprocess/__init__.py

163 lines
5.4 KiB
Python
Raw Normal View History

2021-05-31 16:00:32 +08:00
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2021-05-31 12:53:48 +08:00
#
# 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
from ppcls.data.preprocess.ops.timm_autoaugment import RawTimmAutoAugment
2021-05-31 12:53:48 +08:00
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
2022-10-17 15:45:45 +08:00
from ppcls.data.preprocess.ops.operators import CenterCrop, Resize
2021-05-31 12:53:48 +08:00
from ppcls.data.preprocess.ops.operators import RandCropImage
2022-05-05 20:28:59 +08:00
from ppcls.data.preprocess.ops.operators import RandCropImageV2
2021-05-31 12:53:48 +08:00
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
2022-03-31 15:06:06 +08:00
from ppcls.data.preprocess.ops.operators import Pad
2022-05-05 19:55:08 +08:00
from ppcls.data.preprocess.ops.operators import ToTensor
from ppcls.data.preprocess.ops.operators import Normalize
2022-05-16 11:50:35 +08:00
from ppcls.data.preprocess.ops.operators import RandomHorizontalFlip
2022-10-17 15:45:45 +08:00
from ppcls.data.preprocess.ops.operators import RandomResizedCrop
from ppcls.data.preprocess.ops.operators import CropWithPadding
from ppcls.data.preprocess.ops.operators import RandomInterpolationAugment
from ppcls.data.preprocess.ops.operators import ColorJitter
2022-12-12 17:50:17 +08:00
from ppcls.data.preprocess.ops.operators import RandomGrayscale
2022-05-12 10:52:34 +08:00
from ppcls.data.preprocess.ops.operators import RandomCropImage
2022-08-23 15:54:58 +08:00
from ppcls.data.preprocess.ops.operators import RandomRotation
2022-05-12 20:49:21 +08:00
from ppcls.data.preprocess.ops.operators import Padv2
from ppcls.data.preprocess.ops.operators import RandomRot90
2022-09-28 11:51:34 +08:00
from .ops.operators import format_data
2022-10-25 11:28:43 +08:00
from paddle.vision.transforms import Pad as Pad_paddle_vision
2021-05-31 12:53:48 +08:00
from ppcls.data.preprocess.batch_ops.batch_operators import MixupOperator, CutmixOperator, OpSampler, FmixOperator
2022-09-08 16:11:25 +08:00
from ppcls.data.preprocess.batch_ops.batch_operators import MixupCutmixHybrid
2021-05-31 12:53:48 +08:00
2022-12-12 17:50:17 +08:00
from .ops.randaugmentmc import RandAugmentMC, RandomApply
2021-05-31 12:53:48 +08:00
import numpy as np
from PIL import Image
2022-05-14 17:31:52 +08:00
import random
2022-12-12 17:50:17 +08:00
from paddle.vision.transforms import transforms as T
from paddle.vision.transforms.transforms import RandomCrop, ToTensor, Normalize
2021-05-31 12:53:48 +08:00
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 """
2021-05-31 12:53:48 +08:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2021-05-31 12:53:48 +08:00
def __call__(self, img):
if not isinstance(img, Image.Image):
img = np.ascontiguousarray(img)
img = Image.fromarray(img)
img = super().__call__(img)
2021-05-31 12:53:48 +08:00
if isinstance(img, Image.Image):
img = np.asarray(img)
return img
class RandAugment(RawRandAugment):
""" RandAugment wrapper to auto fit different img types """
2021-05-31 12:53:48 +08:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2021-05-31 12:53:48 +08:00
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
class TimmAutoAugment(RawTimmAutoAugment):
""" TimmAutoAugment wrapper to auto fit different img tyeps. """
2022-05-14 17:31:52 +08:00
def __init__(self, prob=1.0, *args, **kwargs):
super().__init__(*args, **kwargs)
2022-05-14 17:31:52 +08:00
self.prob = prob
2022-09-28 11:51:34 +08:00
@format_data
def __call__(self, img):
if not isinstance(img, Image.Image):
img = np.ascontiguousarray(img)
img = Image.fromarray(img)
2022-05-14 17:31:52 +08:00
if random.random() < self.prob:
img = super().__call__(img)
2021-05-31 12:53:48 +08:00
if isinstance(img, Image.Image):
img = np.asarray(img)
2022-09-28 11:51:34 +08:00
return img
2022-12-12 17:50:17 +08:00
class BaseTransform:
def __init__(self, cfg) -> None:
"""
Args:
cfg: list [dict, dict, dict]
"""
ts = []
for op in cfg:
name = list(op.keys())[0]
if op[name] is None:
ts.append(eval(name)())
else:
ts.append(eval(name)(**(op[name])))
self.t = T.Compose(ts)
def __call__(self, img):
return self.t(img)
class ListTransform:
def __init__(self, ops) -> None:
"""
Args:
ops: list[list[dict, dict], ...]
"""
self.ts = []
for op in ops:
self.ts.append(BaseTransform(op))
def __call__(self, img):
results = []
for op in self.ts:
results.append(op(img))
return results