predictor.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. import tarfile
  16. from typing import Union
  17. from ...utils import logging
  18. from ..base.build_model import build_model
  19. from ..base.predictor import BasePredictor
  20. from ...utils.errors import raise_unsupported_api_error, raise_model_not_found_error
  21. from .model_list import MODELS
  22. from ...utils.download import download
  23. from ...utils.cache import CACHE_DIR
  24. class TSFCPredictor(BasePredictor):
  25. """ TS Forecast Model Predictor """
  26. entities = MODELS
  27. def __init__(self, model_name, model_dir, kernel_option, output):
  28. """initialize
  29. """
  30. model_dir = self._download_from_url(model_dir)
  31. self.model_dir = self.uncompress_tar_file(model_dir)
  32. self.device = kernel_option.get_device()
  33. self.output = output
  34. config_path = self.get_config_path()
  35. self.pdx_config, self.pdx_model = build_model(
  36. model_name, config_path=config_path)
  37. def uncompress_tar_file(self, model_dir):
  38. """unpackage the tar file containing training outputs and update weight path
  39. """
  40. if tarfile.is_tarfile(model_dir):
  41. dest_path = Path(model_dir).parent
  42. with tarfile.open(model_dir, 'r') as tar:
  43. tar.extractall(path=dest_path)
  44. return dest_path / "best_accuracy.pdparams/best_model/model.pdparams"
  45. return model_dir
  46. def get_config_path(self) -> Union[str, None]:
  47. """
  48. get config path
  49. Returns:
  50. config_path (str): The path to the config
  51. """
  52. if Path(self.model_dir).exists():
  53. config_path = Path(self.model_dir).parent.parent / "config.yaml"
  54. if config_path.exists():
  55. return config_path
  56. else:
  57. logging.warning(
  58. f"The config file(`{config_path}`) related to model weight file(`{self.model_dir}`) \
  59. is not exist, use default instead.")
  60. else:
  61. raise_model_not_found_error(self.model_dir)
  62. return None
  63. def _download_from_url(self, in_path):
  64. if in_path.startswith("http"):
  65. file_name = Path(in_path).name
  66. save_path = Path(CACHE_DIR) / "predict_input" / file_name
  67. download(in_path, save_path, overwrite=True)
  68. return save_path.as_posix()
  69. return in_path
  70. def predict(self, input):
  71. """execute model predict
  72. """
  73. # self.update_config()
  74. input['input_path'] = self._download_from_url(input['input_path'])
  75. result = self.pdx_model.predict(**input, **self.get_predict_kwargs())
  76. assert result.returncode == 0, f"Encountered an unexpected error({result.returncode}) in predicting!"
  77. return result
  78. def get_predict_kwargs(self) -> dict:
  79. """get key-value arguments of model predict function
  80. Returns:
  81. dict: the arguments of predict function.
  82. """
  83. return {
  84. "weight_path": self.model_dir,
  85. "device": self.device,
  86. "save_dir": self.output
  87. }
  88. def _get_post_transforms_from_config(self):
  89. pass
  90. def _get_pre_transforms_from_config(self):
  91. pass
  92. def _run(self):
  93. pass
  94. def get_input_keys(self):
  95. """ get input keys """
  96. return ["input_path"]
  97. def get_output_keys(self):
  98. """ get output keys """
  99. pass