__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from paddlex.ppcls.data.preprocess.ops.autoaugment import ImageNetPolicy as RawImageNetPolicy
  15. from paddlex.ppcls.data.preprocess.ops.randaugment import RandAugment as RawRandAugment
  16. from paddlex.ppcls.data.preprocess.ops.timm_autoaugment import RawTimmAutoAugment
  17. from paddlex.ppcls.data.preprocess.ops.cutout import Cutout
  18. from paddlex.ppcls.data.preprocess.ops.hide_and_seek import HideAndSeek
  19. from paddlex.ppcls.data.preprocess.ops.random_erasing import RandomErasing
  20. from paddlex.ppcls.data.preprocess.ops.grid import GridMask
  21. from paddlex.ppcls.data.preprocess.ops.operators import DecodeImage
  22. from paddlex.ppcls.data.preprocess.ops.operators import ResizeImage
  23. from paddlex.ppcls.data.preprocess.ops.operators import CropImage
  24. from paddlex.ppcls.data.preprocess.ops.operators import RandCropImage
  25. from paddlex.ppcls.data.preprocess.ops.operators import RandFlipImage
  26. from paddlex.ppcls.data.preprocess.ops.operators import NormalizeImage
  27. from paddlex.ppcls.data.preprocess.ops.operators import ToCHWImage
  28. from paddlex.ppcls.data.preprocess.ops.operators import AugMix
  29. from paddlex.ppcls.data.preprocess.batch_ops.batch_operators import MixupOperator, CutmixOperator, OpSampler, FmixOperator
  30. import numpy as np
  31. from PIL import Image
  32. def transform(data, ops=[]):
  33. """ transform """
  34. for op in ops:
  35. data = op(data)
  36. return data
  37. class AutoAugment(RawImageNetPolicy):
  38. """ ImageNetPolicy wrapper to auto fit different img types """
  39. def __init__(self, *args, **kwargs):
  40. super().__init__(*args, **kwargs)
  41. def __call__(self, img):
  42. if not isinstance(img, Image.Image):
  43. img = np.ascontiguousarray(img)
  44. img = Image.fromarray(img)
  45. img = super().__call__(img)
  46. if isinstance(img, Image.Image):
  47. img = np.asarray(img)
  48. return img
  49. class RandAugment(RawRandAugment):
  50. """ RandAugment wrapper to auto fit different img types """
  51. def __init__(self, *args, **kwargs):
  52. super().__init__(*args, **kwargs)
  53. def __call__(self, img):
  54. if not isinstance(img, Image.Image):
  55. img = np.ascontiguousarray(img)
  56. img = Image.fromarray(img)
  57. img = super().__call__(img)
  58. if isinstance(img, Image.Image):
  59. img = np.asarray(img)
  60. return img
  61. class TimmAutoAugment(RawTimmAutoAugment):
  62. """ TimmAutoAugment wrapper to auto fit different img tyeps. """
  63. def __init__(self, *args, **kwargs):
  64. super().__init__(*args, **kwargs)
  65. def __call__(self, img):
  66. if not isinstance(img, Image.Image):
  67. img = np.ascontiguousarray(img)
  68. img = Image.fromarray(img)
  69. img = super().__call__(img)
  70. if isinstance(img, Image.Image):
  71. img = np.asarray(img)
  72. return img