base.py 2.1 KB

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