ssl_img_aug.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  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. import math
  15. import cv2
  16. import numpy as np
  17. import random
  18. from PIL import Image
  19. from .rec_img_aug import resize_norm_img
  20. class SSLRotateResize(object):
  21. def __init__(self,
  22. image_shape,
  23. padding=False,
  24. select_all=True,
  25. mode="train",
  26. **kwargs):
  27. self.image_shape = image_shape
  28. self.padding = padding
  29. self.select_all = select_all
  30. self.mode = mode
  31. def __call__(self, data):
  32. img = data["image"]
  33. data["image_r90"] = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
  34. data["image_r180"] = cv2.rotate(data["image_r90"],
  35. cv2.ROTATE_90_CLOCKWISE)
  36. data["image_r270"] = cv2.rotate(data["image_r180"],
  37. cv2.ROTATE_90_CLOCKWISE)
  38. images = []
  39. for key in ["image", "image_r90", "image_r180", "image_r270"]:
  40. images.append(
  41. resize_norm_img(
  42. data.pop(key),
  43. image_shape=self.image_shape,
  44. padding=self.padding)[0])
  45. data["image"] = np.stack(images, axis=0)
  46. data["label"] = np.array(list(range(4)))
  47. if not self.select_all:
  48. data["image"] = data["image"][0::2] # just choose 0 and 180
  49. data["label"] = data["label"][0:2] # label needs to be continuous
  50. if self.mode == "test":
  51. data["image"] = data["image"][0]
  52. data["label"] = data["label"][0]
  53. return data