base.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 typing import Any, Dict, Optional
  16. from ..predictors import create_predictor
  17. from ...utils.subclass_register import AutoRegisterABCMetaClass
  18. from ..predictors import create_predictor
  19. def create_pipeline(
  20. pipeline_name: str,
  21. model_list: list,
  22. model_dir_list: list,
  23. output: str,
  24. device: str,
  25. use_hpip: bool,
  26. hpi_params: Optional[Dict[str, Any]] = None,
  27. ) -> "BasePipeline":
  28. """build model evaluater
  29. Args:
  30. pipeline_name (str): the pipeline name, that is name of pipeline class
  31. Returns:
  32. BasePipeline: the pipeline, which is subclass of BasePipeline.
  33. """
  34. predictor_kwargs = {"use_hpip": use_hpip}
  35. if hpi_params is not None:
  36. predictor_kwargs["hpi_params"] = hpi_params
  37. pipeline = BasePipeline.get(pipeline_name)(
  38. output=output, device=device, predictor_kwargs=predictor_kwargs
  39. )
  40. pipeline.update_model(model_list, model_dir_list)
  41. pipeline.load_model()
  42. return pipeline
  43. class BasePipeline(ABC, metaclass=AutoRegisterABCMetaClass):
  44. """Base Pipeline"""
  45. __is_base = True
  46. def __init__(self, predictor_kwargs: Optional[Dict[str, Any]]) -> None:
  47. super().__init__()
  48. if predictor_kwargs is None:
  49. predictor_kwargs = {}
  50. self._predictor_kwargs = predictor_kwargs
  51. # alias the __call__() to predict()
  52. def __call__(self, *args, **kwargs):
  53. yield from self.predict(*args, **kwargs)
  54. def _create_predictor(self, *args, **kwargs):
  55. return create_predictor(*args, **kwargs, **self._predictor_kwargs)