instance_segmentation.py 2.0 KB

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