predictor.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. from typing import Any, Dict, List, Tuple, Union
  15. import numpy as np
  16. from ....modules.anomaly_detection.model_list import MODELS
  17. from ....utils.func_register import FuncRegister
  18. from ...common.batch_sampler import ImageBatchSampler
  19. from ...common.reader import ReadImage
  20. from ..base import BasePredictor
  21. from ..common import Normalize, Resize, ToBatch, ToCHWImage
  22. from .processors import MapToMask
  23. from .result import UadResult
  24. class UadPredictor(BasePredictor):
  25. """UadPredictor that inherits from BasePredictor."""
  26. entities = MODELS
  27. _FUNC_MAP = {}
  28. register = FuncRegister(_FUNC_MAP)
  29. def __init__(self, *args: List, **kwargs: Dict) -> None:
  30. """Initializes UadPredictor.
  31. Args:
  32. *args: Arbitrary positional arguments passed to the superclass.
  33. **kwargs: Arbitrary keyword arguments passed to the superclass.
  34. """
  35. super().__init__(*args, **kwargs)
  36. self.preprocessors, self.infer, self.postprocessors = self._build()
  37. def _build_batch_sampler(self) -> ImageBatchSampler:
  38. """Builds and returns an ImageBatchSampler instance.
  39. Returns:
  40. ImageBatchSampler: An instance of ImageBatchSampler.
  41. """
  42. return ImageBatchSampler()
  43. def _get_result_class(self) -> type:
  44. """Returns the result class, UadResult.
  45. Returns:
  46. type: The UadResult class.
  47. """
  48. return UadResult
  49. def _build(self) -> Tuple:
  50. """Build the preprocessors, inference engine, and postprocessors based on the configuration.
  51. Returns:
  52. tuple: A tuple containing the preprocessors, inference engine, and postprocessors.
  53. """
  54. preprocessors = {"Read": ReadImage(format="RGB")}
  55. preprocessors["ToCHW"] = ToCHWImage()
  56. for cfg in self.config["Deploy"]["transforms"]:
  57. tf_key = cfg.pop("type")
  58. func = self._FUNC_MAP[tf_key]
  59. args = cfg
  60. name, op = func(self, **args) if args else func(self)
  61. preprocessors[name] = op
  62. preprocessors["ToBatch"] = ToBatch()
  63. infer = self.create_static_infer()
  64. postprocessors = {"Map_to_mask": MapToMask()}
  65. return preprocessors, infer, postprocessors
  66. def process(self, batch_data: List[Union[str, np.ndarray]]) -> Dict[str, Any]:
  67. """
  68. Process a batch of data through the preprocessing, inference, and postprocessing.
  69. Args:
  70. batch_data (List[Union[str, np.ndarray], ...]): A batch of input data (e.g., image file paths).
  71. Returns:
  72. dict: A dictionary containing the input path, raw image, and predicted segmentation maps for every instance of the batch. Keys include 'input_path', 'input_img', and 'pred'.
  73. """
  74. batch_raw_imgs = self.preprocessors["Read"](imgs=batch_data.instances)
  75. batch_imgs = self.preprocessors["Resize"](imgs=batch_raw_imgs)
  76. batch_imgs = self.preprocessors["Normalize"](imgs=batch_imgs)
  77. batch_imgs = self.preprocessors["ToCHW"](imgs=batch_imgs)
  78. x = self.preprocessors["ToBatch"](imgs=batch_imgs)
  79. batch_preds = self.infer(x=x)
  80. batch_preds = self.postprocessors["Map_to_mask"](preds=batch_preds)
  81. if len(batch_data) > 1:
  82. batch_preds = np.split(batch_preds[0], len(batch_data), axis=0)
  83. return {
  84. "input_path": batch_data.input_paths,
  85. "page_index": batch_data.page_indexes,
  86. "input_img": batch_raw_imgs,
  87. "pred": batch_preds,
  88. }
  89. @register("Resize")
  90. def build_resize(
  91. self, target_size, keep_ratio=False, size_divisor=None, interp="LINEAR"
  92. ):
  93. assert target_size
  94. op = Resize(
  95. target_size=target_size,
  96. keep_ratio=keep_ratio,
  97. size_divisor=size_divisor,
  98. interp=interp,
  99. )
  100. return "Resize", op
  101. @register("Normalize")
  102. def build_normalize(
  103. self,
  104. mean=0.5,
  105. std=0.5,
  106. ):
  107. op = Normalize(mean=mean, std=std)
  108. return "Normalize", op
  109. @register("Map_to_mask")
  110. def map_to_mask(self, mask_map):
  111. op = MapToMask()
  112. return "Map_to_mask", op