base.py 2.1 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. import yaml
  15. import codecs
  16. from pathlib import Path
  17. from abc import abstractmethod
  18. from ...utils.subclass_register import AutoRegisterABCMetaClass
  19. from ..components.base import BaseComponent, ComponentsEngine
  20. from ..utils.process_hook import generatorable_method
  21. class BasePredictor(BaseComponent, metaclass=AutoRegisterABCMetaClass):
  22. __is_base = True
  23. INPUT_KEYS = "x"
  24. OUTPUT_KEYS = None
  25. KEEP_INPUT = False
  26. MODEL_FILE_PREFIX = "inference"
  27. def __init__(self, model_dir, config=None, device="gpu", **kwargs):
  28. super().__init__()
  29. self.model_dir = Path(model_dir)
  30. self.config = config if config else self.load_config(self.model_dir)
  31. self.device = device
  32. self.kwargs = kwargs
  33. self.components = self._build_components()
  34. self.engine = ComponentsEngine(self.components)
  35. # alias predict() to the __call__()
  36. self.predict = self.__call__
  37. @classmethod
  38. def load_config(cls, model_dir):
  39. config_path = model_dir / f"{cls.MODEL_FILE_PREFIX}.yml"
  40. with codecs.open(config_path, "r", "utf-8") as file:
  41. dic = yaml.load(file, Loader=yaml.FullLoader)
  42. return dic
  43. def apply(self, x):
  44. """predict"""
  45. yield from self._generate_res(self.engine(x))
  46. @generatorable_method
  47. def _generate_res(self, data):
  48. return self._pack_res(data)
  49. @abstractmethod
  50. def _build_components(self):
  51. raise NotImplementedError
  52. @abstractmethod
  53. def _pack_res(self, data):
  54. raise NotImplementedError