predictor.py 4.9 KB

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