instance_segmentation.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 .object_detection import DetPredictor
  16. from ...utils.func_register import FuncRegister
  17. from ...modules.instance_segmentation.model_list import MODELS
  18. from ..components import *
  19. from ..results import InstanceSegResult
  20. from ..utils.process_hook import batchable_method
  21. class InstanceSegPredictor(DetPredictor):
  22. entities = MODELS
  23. def _build_components(self):
  24. self._add_component(ReadImage(format="RGB"))
  25. for cfg in self.config["Preprocess"]:
  26. tf_key = cfg["type"]
  27. func = self._FUNC_MAP.get(tf_key)
  28. cfg.pop("type")
  29. args = cfg
  30. op = func(self, **args) if args else func(self)
  31. self._add_component(op)
  32. predictor = ImageDetPredictor(
  33. model_dir=self.model_dir,
  34. model_prefix=self.MODEL_FILE_PREFIX,
  35. option=self.pp_option,
  36. )
  37. if "RT-DETR" in self.model_name:
  38. predictor.set_inputs(
  39. {"img": "img", "scale_factors": "scale_factors", "img_size": "img_size"}
  40. )
  41. self._add_component(
  42. [
  43. ("Predictor", predictor),
  44. InstanceSegPostProcess(
  45. threshold=self.config["draw_threshold"],
  46. labels=self.config["label_list"],
  47. ),
  48. ]
  49. )
  50. def _pack_res(self, single):
  51. keys = ["img_path", "boxes", "masks"]
  52. return InstanceSegResult({key: single[key] for key in keys})