processors.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (c) 2024 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. import numpy as np
  15. from ...utils.benchmark import benchmark
  16. from ..common.vision import F
  17. @benchmark.timeit
  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. def __call__(self, imgs):
  37. """apply"""
  38. return [self.crop(img) for img in imgs]
  39. def crop(self, img):
  40. h, w = img.shape[:2]
  41. cw, ch = self.crop_size
  42. if self.mode == "C":
  43. x1 = max(0, (w - cw) // 2)
  44. y1 = max(0, (h - ch) // 2)
  45. elif self.mode == "TL":
  46. x1, y1 = 0, 0
  47. x2 = min(w, x1 + cw)
  48. y2 = min(h, y1 + ch)
  49. coords = (x1, y1, x2, y2)
  50. if w < cw or h < ch:
  51. raise ValueError(
  52. f"Input image ({w}, {h}) smaller than the target size ({cw}, {ch})."
  53. )
  54. img = F.slice(img, coords=coords)
  55. return img
  56. @benchmark.timeit
  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. def __call__(self, preds, topk=5):
  69. indexes = preds[0].argsort(axis=1)[:, -topk:][:, ::-1].astype("int32")
  70. scores = [
  71. np.around(pred[index], decimals=5) for pred, index in zip(preds[0], indexes)
  72. ]
  73. label_names = [[self.class_id_map[i] for i in index] for index in indexes]
  74. return indexes, scores, label_names