predictor.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.semantic_segmentation.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, ToBatch, ToCHWImage
  22. from .processors import Resize, SegPostProcess
  23. from .result import SegResult
  24. class SegPredictor(BasePredictor):
  25. """SegPredictor that inherits from BasePredictor."""
  26. entities = MODELS
  27. _FUNC_MAP = {}
  28. register = FuncRegister(_FUNC_MAP)
  29. def __init__(
  30. self,
  31. target_size: Union[int, Tuple[int], None] = None,
  32. *args: List,
  33. **kwargs: Dict,
  34. ) -> None:
  35. """Initializes SegPredictor.
  36. Args:
  37. target_size: Image size used for inference.
  38. *args: Arbitrary positional arguments passed to the superclass.
  39. **kwargs: Arbitrary keyword arguments passed to the superclass.
  40. """
  41. super().__init__(*args, **kwargs)
  42. self.target_size = target_size
  43. self.preprocessors, self.infer, self.postprocessers = 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, SegResult.
  52. Returns:
  53. type: The SegResult class.
  54. """
  55. return SegResult
  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. if "Resize" not in preprocessors:
  71. _, op = self._FUNC_MAP["Resize"](self, target_size=-1)
  72. preprocessors["Resize"] = op
  73. if self.target_size is not None:
  74. _, op = self._FUNC_MAP["Resize"](self, target_size=self.target_size)
  75. preprocessors["Resize"] = op
  76. infer = self.create_static_infer()
  77. postprocessers = SegPostProcess()
  78. return preprocessors, infer, postprocessers
  79. def process(
  80. self,
  81. batch_data: List[Union[str, np.ndarray]],
  82. target_size: Union[int, Tuple[int], None] = None,
  83. ) -> Dict[str, Any]:
  84. """
  85. Process a batch of data through the preprocessing, inference, and postprocessing.
  86. Args:
  87. batch_data (List[Union[str, np.ndarray], ...]): A batch of input data (e.g., image file paths).
  88. target_size: Image size used for inference.
  89. Returns:
  90. 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'.
  91. """
  92. batch_raw_imgs = self.preprocessors["Read"](imgs=batch_data.instances)
  93. batch_imgs = self.preprocessors["Resize"](
  94. imgs=batch_raw_imgs, target_size=target_size
  95. )
  96. batch_imgs = self.preprocessors["Normalize"](imgs=batch_imgs)
  97. batch_imgs = self.preprocessors["ToCHW"](imgs=batch_imgs)
  98. x = self.preprocessors["ToBatch"](imgs=batch_imgs)
  99. batch_preds = self.infer(x=x)
  100. if len(batch_data) > 1:
  101. batch_preds = np.split(batch_preds[0], len(batch_data), axis=0)
  102. # postprocess
  103. batch_preds = self.postprocessers(batch_preds, batch_raw_imgs)
  104. return {
  105. "input_path": batch_data.input_paths,
  106. "page_index": batch_data.page_indexes,
  107. "input_img": batch_raw_imgs,
  108. "pred": batch_preds,
  109. }
  110. @register("Normalize")
  111. def build_normalize(
  112. self,
  113. mean=0.5,
  114. std=0.5,
  115. ):
  116. op = Normalize(mean=mean, std=std)
  117. return "Normalize", op
  118. @register("Resize")
  119. def build_resize(
  120. self,
  121. target_size=-1,
  122. keep_ratio=True,
  123. size_divisor=32,
  124. interp="LINEAR",
  125. ):
  126. op = Resize(
  127. target_size=target_size,
  128. keep_ratio=keep_ratio,
  129. size_divisor=size_divisor,
  130. interp=interp,
  131. )
  132. return "Resize", op