predictor.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. model_dir = self._download_from_url(model_dir)
  30. self.model_dir = self.uncompress_tar_file(model_dir)
  31. self.device = kernel_option.get_device()
  32. self.output = output
  33. config_path = self.get_config_path()
  34. self.pdx_config, self.pdx_model = build_model(
  35. model_name, config_path=config_path
  36. )
  37. def uncompress_tar_file(self, model_dir):
  38. """unpackage the tar file containing training outputs and update weight path"""
  39. if tarfile.is_tarfile(model_dir):
  40. dest_path = Path(model_dir).parent
  41. with tarfile.open(model_dir, "r") as tar:
  42. tar.extractall(path=dest_path)
  43. return dest_path / "best_accuracy.pdparams/best_model/model.pdparams"
  44. return model_dir
  45. def get_config_path(self) -> Union[str, None]:
  46. """
  47. get config path
  48. Returns:
  49. config_path (str): The path to the config
  50. """
  51. if Path(self.model_dir).exists():
  52. config_path = Path(self.model_dir).parent.parent / "config.yaml"
  53. if config_path.exists():
  54. return config_path
  55. else:
  56. logging.warning(
  57. f"The config file(`{config_path}`) related to model weight file(`{self.model_dir}`) \
  58. is not exist, use default instead."
  59. )
  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. # self.update_config()
  73. input["input_path"] = self._download_from_url(input["input_path"])
  74. result = self.pdx_model.predict(**input, **self.get_predict_kwargs())
  75. assert (
  76. result.returncode == 0
  77. ), f"Encountered an unexpected error({result.returncode}) in predicting!"
  78. return result
  79. def get_predict_kwargs(self) -> dict:
  80. """get key-value arguments of model predict function
  81. Returns:
  82. dict: the arguments of predict function.
  83. """
  84. return {
  85. "weight_path": self.model_dir,
  86. "device": self.device,
  87. "save_dir": self.output,
  88. }
  89. def _get_post_transforms_from_config(self):
  90. pass
  91. def _get_pre_transforms_from_config(self):
  92. pass
  93. def _run(self):
  94. pass
  95. def get_input_keys(self):
  96. """get input keys"""
  97. return ["input_path"]
  98. def get_output_keys(self):
  99. """get output keys"""
  100. pass