base.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 abc import ABC, abstractmethod
  15. from ...utils.subclass_register import AutoRegisterABCMetaClass
  16. import yaml
  17. import codecs
  18. from pathlib import Path
  19. from typing import Any, Dict, Optional
  20. from ..utils.pp_option import PaddlePredictorOption
  21. from ..models import BasePredictor
  22. class BasePipeline(ABC, metaclass=AutoRegisterABCMetaClass):
  23. """Base class for all pipelines.
  24. This class serves as a foundation for creating various pipelines.
  25. It includes common attributes and methods that are shared among all
  26. pipeline implementations.
  27. """
  28. __is_base = True
  29. def __init__(
  30. self,
  31. device: str = None,
  32. pp_option: PaddlePredictorOption = None,
  33. use_hpip: bool = False,
  34. hpi_params: Optional[Dict[str, Any]] = None,
  35. ) -> None:
  36. """
  37. Initializes the class with specified parameters.
  38. Args:
  39. device (str, optional): The device to use for prediction. Defaults to None.
  40. pp_option (PaddlePredictorOption, optional): The options for PaddlePredictor. Defaults to None.
  41. use_hpip (bool, optional): Whether to use high-performance inference (hpip) for prediction. Defaults to False.
  42. hpi_params (Dict[str, Any], optional): Additional parameters for hpip. Defaults to None.
  43. """
  44. super().__init__()
  45. self.device = device
  46. self.pp_option = pp_option
  47. self.use_hpip = use_hpip
  48. self.hpi_params = hpi_params
  49. @abstractmethod
  50. def predict(self, input, **kwargs):
  51. """
  52. Declaration of an abstract method. Subclasses are expected to
  53. provide a concrete implementation of predict.
  54. Args:
  55. input: The input data to predict.
  56. **kwargs: Additional keyword arguments.
  57. """
  58. raise NotImplementedError("The method `predict` has not been implemented yet.")
  59. def create_model(self, config: Dict) -> BasePredictor:
  60. """
  61. Create a model instance based on the given configuration.
  62. Args:
  63. config (Dict): A dictionary containing configuration settings.
  64. Returns:
  65. BasePredictor: An instance of the model.
  66. """
  67. model_dir = config["model_dir"]
  68. if model_dir == None:
  69. model_dir = config["model_name"]
  70. from ...model import create_model
  71. model = create_model(
  72. model=model_dir,
  73. device=self.device,
  74. pp_option=self.pp_option,
  75. use_hpip=self.use_hpip,
  76. hpi_params=self.hpi_params,
  77. )
  78. # [TODO] Support initializing with additional parameters
  79. if "batch_size" in config:
  80. batch_size = config["batch_size"]
  81. model.set_predictor(batch_size=batch_size)
  82. return model
  83. def create_pipeline(self, config: Dict):
  84. """
  85. Creates a pipeline based on the provided configuration.
  86. Args:
  87. config (Dict): A dictionary containing the pipeline configuration.
  88. Returns:
  89. BasePipeline: An instance of the created pipeline.
  90. """
  91. from . import create_pipeline
  92. pipeline_name = config["pipeline_name"]
  93. pipeline = create_pipeline(
  94. pipeline_name,
  95. config=config,
  96. device=self.device,
  97. pp_option=self.pp_option,
  98. use_hpip=self.use_hpip,
  99. hpi_params=self.hpi_params,
  100. )
  101. return pipeline
  102. def __call__(self, input, **kwargs):
  103. """
  104. Calls the predict method with the given input and keyword arguments.
  105. Args:
  106. input: The input data to be predicted.
  107. **kwargs: Additional keyword arguments to be passed to the predict method.
  108. Returns:
  109. The prediction result from the predict method.
  110. """
  111. return self.predict(input, **kwargs)