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