instance_segmentation.py 1.9 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 InstanceSegResults
  20. from ..utils.process_hook import batchable_method
  21. class InstanceSegPredictor(DetPredictor):
  22. entities = MODELS
  23. def _build_components(self):
  24. ops = {}
  25. ops["ReadImage"] = ReadImage(
  26. batch_size=self.kwargs.get("batch_size", 1), format="RGB"
  27. )
  28. for cfg in self.config["Preprocess"]:
  29. tf_key = cfg["type"]
  30. func = self._FUNC_MAP.get(tf_key)
  31. cfg.pop("type")
  32. args = cfg
  33. op = func(self, **args) if args else func(self)
  34. ops[tf_key] = op
  35. predictor = ImageInstanceSegPredictor(
  36. model_dir=self.model_dir,
  37. model_prefix=self.MODEL_FILE_PREFIX,
  38. option=self.pp_option,
  39. )
  40. ops["predictor"] = predictor
  41. ops["postprocess"] = InstanceSegPostProcess(
  42. threshold=self.config["draw_threshold"], labels=self.config["label_list"]
  43. )
  44. return ops
  45. @batchable_method
  46. def _pack_res(self, data):
  47. keys = ["img_path", "boxes", "masks", "labels"]
  48. return {"result": InstanceSegResults({key: data[key] for key in keys})}