predictor.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 pathlib import Path
  15. from typing import Union
  16. from ...utils import logging
  17. from ..base.build_model import build_model
  18. from ..base.predictor import BasePredictor
  19. from ...utils.errors import raise_unsupported_api_error, raise_model_not_found_error
  20. from .support_models import SUPPORT_MODELS
  21. class TSFCPredictor(BasePredictor):
  22. """ TS Forecast Model Predictor """
  23. support_models = SUPPORT_MODELS
  24. def __init__(self, config):
  25. """Initialize the instance.
  26. Args:
  27. config (AttrDict): PaddleX pipeline config, which is loaded from pipeline yaml file.
  28. """
  29. self.global_config = config.Global
  30. self.predict_config = config.Predict
  31. config_path = self.get_config_path()
  32. self.pdx_config, self.pdx_model = build_model(
  33. self.global_config.model, config_path=config_path)
  34. def get_config_path(self) -> Union[str, None]:
  35. """
  36. get config path
  37. Returns:
  38. config_path (str): The path to the config
  39. """
  40. model_dir = self.predict_config.model_dir
  41. if Path(model_dir).exists():
  42. config_path = Path(model_dir).parent.parent / "config.yaml"
  43. if config_path.exists():
  44. return config_path
  45. else:
  46. logging.warning(
  47. f"The config file(`{config_path}`) related to model weight file(`{self.predict_config.model_dir}`) \
  48. is not exist, use default instead.")
  49. else:
  50. raise_model_not_found_error(model_dir)
  51. return None
  52. def predict(self, input=None, batch_size=1):
  53. """execute model predict
  54. Returns:
  55. dict: the prediction results
  56. """
  57. results = self.predict()
  58. def predict(self):
  59. """predict using specified model
  60. """
  61. # self.update_config()
  62. result = self.pdx_model.predict(**self.get_predict_kwargs())
  63. assert result.returncode == 0, f"Encountered an unexpected error({result.returncode}) in predicting!"
  64. def get_predict_kwargs(self) -> dict:
  65. """get key-value arguments of model predict function
  66. Returns:
  67. dict: the arguments of predict function.
  68. """
  69. return {
  70. "weight_path": self.predict_config.model_dir,
  71. "input_path": self.predict_config.input_path,
  72. "device": self.global_config.device,
  73. "save_dir": self.global_config.output
  74. }
  75. def _get_post_transforms_from_config(self):
  76. pass
  77. def _get_pre_transforms_from_config(self):
  78. pass
  79. def _run(self):
  80. pass
  81. def get_input_keys(self):
  82. """ get input keys """
  83. pass
  84. def get_output_keys(self):
  85. """ get output keys """
  86. pass