pipeline.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. from typing import Any, Dict, Optional, Union, Tuple
  15. import numpy as np
  16. from ...utils.pp_option import PaddlePredictorOption
  17. from ..base import BasePipeline
  18. # [TODO] 待更新models_new到models
  19. from ...models_new.open_vocabulary_segmentation.results import SAMSegResult
  20. Number = Union[int, float]
  21. class OpenVocabularySegmentationPipeline(BasePipeline):
  22. """Open Vocabulary Segmentation pipeline"""
  23. entities = "open_vocabulary_segmentation"
  24. def __init__(
  25. self,
  26. config: Dict,
  27. device: str = None,
  28. pp_option: PaddlePredictorOption = None,
  29. use_hpip: bool = False,
  30. ) -> None:
  31. """
  32. Initializes the class with given configurations and options.
  33. Args:
  34. config (Dict): Configuration dictionary containing model and other parameters.
  35. device (str): The device to run the prediction on. Default is None.
  36. pp_option (PaddlePredictorOption): Options for PaddlePaddle predictor. Default is None.
  37. use_hpip (bool): Whether to use high-performance inference (hpip) for prediction. Defaults to False.
  38. """
  39. super().__init__(device=device, pp_option=pp_option, use_hpip=use_hpip)
  40. # create box-prompted SAM-H
  41. box_prompted_model_cfg = config.get("SubModules", {}).get(
  42. "BoxPromptSegmentation",
  43. {"model_config_error": "config error for doc_ori_classify_model!"},
  44. )
  45. self.box_prompted_model = self.create_model(box_prompted_model_cfg)
  46. # create point-prompted SAM-H
  47. point_prompted_model_cfg = config.get("SubModules", {}).get(
  48. "PointPromptSegmentation",
  49. {"model_config_error": "config error for doc_ori_classify_model!"},
  50. )
  51. self.point_prompted_model = self.create_model(point_prompted_model_cfg)
  52. def predict(
  53. self,
  54. input: str | list[str] | np.ndarray | list[np.ndarray],
  55. prompt: list[list[float]] | np.ndarray,
  56. prompt_type: str = "box",
  57. **kwargs
  58. ) -> SAMSegResult:
  59. """Predicts image segmentation results for the given input.
  60. Args:
  61. input (str | list[str] | np.ndarray | list[np.ndarray]): The input image(s) or path(s) to the images.
  62. prompt (list[list[int]] | np.ndarray): The prompt for the input image(s).
  63. prompt_type (str): The type of prompt, either 'box' or 'point'. Default is 'box'.
  64. **kwargs: Additional keyword arguments that can be passed to the function.
  65. Returns:
  66. SAMSegResult: The predicted SAM segmentation results.
  67. """
  68. if prompt_type == "box":
  69. yield from self.box_prompted_model(input, prompts={"box_prompt": prompt})
  70. elif prompt_type == "point":
  71. yield from self.point_prompted_model(
  72. input, prompts={"point_prompt": prompt}
  73. )
  74. else:
  75. raise ValueError(
  76. "Invalid prompt type. Only 'box' and 'point' are supported"
  77. )