instance_segmentation.py 2.3 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. 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. model_names = ["RT-DETR", "SOLOv2", "RCNN", "YOLO"]
  37. if any(name in self.model_name for name in model_names):
  38. predictor.set_inputs(
  39. {"img": "img", "scale_factors": "scale_factors", "img_size": "img_size"}
  40. )
  41. postprecss = InstanceSegPostProcess(
  42. threshold=self.config["draw_threshold"],
  43. labels=self.config["label_list"],
  44. )
  45. if "SOLOv2" in self.model_name:
  46. postprecss.set_inputs(
  47. {
  48. "class_id": "class_id",
  49. "masks": "masks",
  50. "img_size": "img_size",
  51. }
  52. )
  53. self._add_component([predictor, postprecss])
  54. def _pack_res(self, single):
  55. keys = ["img_path", "boxes", "masks"]
  56. return InstanceSegResult({key: single[key] for key in keys})