pipeline.py 3.5 KB

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