processors.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # copyright (c) 2024 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 numpy as np
  15. from ....utils import logging
  16. from ..common.vision import F
  17. from ...utils.benchmark import benchmark
  18. class Crop:
  19. """Crop region from the image."""
  20. def __init__(self, crop_size, mode="C"):
  21. """
  22. Initialize the instance.
  23. Args:
  24. crop_size (list|tuple|int): Width and height of the region to crop.
  25. mode (str, optional): 'C' for cropping the center part and 'TL' for
  26. cropping the top left part. Default: 'C'.
  27. """
  28. super().__init__()
  29. if isinstance(crop_size, int):
  30. crop_size = [crop_size, crop_size]
  31. F.check_image_size(crop_size)
  32. self.crop_size = crop_size
  33. if mode not in ("C", "TL"):
  34. raise ValueError("Unsupported interpolation method")
  35. self.mode = mode
  36. @benchmark.timeit
  37. def __call__(self, imgs):
  38. """apply"""
  39. return [self.crop(img) for img in imgs]
  40. def crop(self, img):
  41. h, w = img.shape[:2]
  42. cw, ch = self.crop_size
  43. if self.mode == "C":
  44. x1 = max(0, (w - cw) // 2)
  45. y1 = max(0, (h - ch) // 2)
  46. elif self.mode == "TL":
  47. x1, y1 = 0, 0
  48. x2 = min(w, x1 + cw)
  49. y2 = min(h, y1 + ch)
  50. coords = (x1, y1, x2, y2)
  51. if w < cw or h < ch:
  52. raise ValueError(
  53. f"Input image ({w}, {h}) smaller than the target size ({cw}, {ch})."
  54. )
  55. img = F.slice(img, coords=coords)
  56. return img
  57. class Topk:
  58. """Topk Transform"""
  59. def __init__(self, class_ids=None):
  60. super().__init__()
  61. self.class_id_map = self._parse_class_id_map(class_ids)
  62. def _parse_class_id_map(self, class_ids):
  63. """parse class id to label map file"""
  64. if class_ids is None:
  65. return None
  66. class_id_map = {id: str(lb) for id, lb in enumerate(class_ids)}
  67. return class_id_map
  68. @benchmark.timeit
  69. def __call__(self, preds, topk=5):
  70. indexes = preds[0].argsort(axis=1)[:, -topk:][:, ::-1].astype("int32")
  71. scores = [
  72. np.around(pred[index], decimals=5) for pred, index in zip(preds[0], indexes)
  73. ]
  74. label_names = [[self.class_id_map[i] for i in index] for index in indexes]
  75. return indexes, scores, label_names