instance_segmentation.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 self.model_name in [
  38. "Mask-RT-DETR-S",
  39. "Mask-RT-DETR-M",
  40. "Mask-RT-DETR-L",
  41. "Mask-RT-DETR-H",
  42. "Mask-RT-DETR-X",
  43. ]:
  44. predictor.set_inputs(
  45. {"img": "img", "scale_factors": "scale_factors", "img_size": "img_size"}
  46. )
  47. self._add_component(
  48. [
  49. ("Predictor", predictor),
  50. InstanceSegPostProcess(
  51. threshold=self.config["draw_threshold"],
  52. labels=self.config["label_list"],
  53. ),
  54. ]
  55. )
  56. def _pack_res(self, single):
  57. keys = ["img_path", "boxes", "masks"]
  58. return InstanceSegResult({key: single[key] for key in keys})