predictor.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 os
  15. import numpy as np
  16. from ....utils import logging
  17. from ...base.predictor.transforms import ts_common
  18. from ...base import BasePredictor
  19. from .keys import TSFCKeys as K
  20. from . import transforms as T
  21. from .utils import InnerConfig
  22. from ..model_list import MODELS
  23. class TSADPredictor(BasePredictor):
  24. """SegPredictor"""
  25. entities = MODELS
  26. def __init__(
  27. self,
  28. model_name,
  29. model_dir,
  30. kernel_option,
  31. output,
  32. pre_transforms=None,
  33. post_transforms=None,
  34. ):
  35. super().__init__(
  36. model_name=model_name,
  37. model_dir=model_dir,
  38. kernel_option=kernel_option,
  39. output=output,
  40. pre_transforms=pre_transforms,
  41. post_transforms=post_transforms,
  42. )
  43. def load_other_src(self):
  44. """load the inner config file"""
  45. infer_cfg_file_path = os.path.join(self.model_dir, "inference.yml")
  46. if not os.path.exists(infer_cfg_file_path):
  47. raise FileNotFoundError(f"Cannot find config file: {infer_cfg_file_path}")
  48. return InnerConfig(infer_cfg_file_path, self.model_dir)
  49. @classmethod
  50. def get_input_keys(cls):
  51. """get input keys"""
  52. return [[K.TS], [K.TS_PATH]]
  53. @classmethod
  54. def get_output_keys(cls):
  55. """get output keys"""
  56. return [K.PRED]
  57. def _run(self, batch_input):
  58. """run"""
  59. n = len(batch_input[0][K.TS])
  60. input_ = [
  61. np.stack([lst[i] for lst in [data[K.TS] for data in batch_input]], axis=0)
  62. for i in range(n)
  63. ]
  64. outputs = self._predictor.predict(input_)
  65. batch_output = outputs[0]
  66. # In-place update
  67. for dict_, output in zip(batch_input, batch_output):
  68. dict_[K.PRED] = output
  69. return batch_input
  70. def _get_pre_transforms_from_config(self):
  71. """_get_pre_transforms_from_config"""
  72. logging.info(
  73. f"Transformation operators for data preprocessing will be inferred from config file."
  74. )
  75. pre_transforms = self.other_src.pre_transforms
  76. pre_transforms.insert(0, ts_common.ReadTS())
  77. return pre_transforms
  78. def _get_post_transforms_from_config(self):
  79. """_get_post_transforms_from_config"""
  80. post_transforms = self.other_src.post_transforms
  81. post_transforms.append(T.SaveTSResults(self.output))
  82. return post_transforms