predictor.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 copy
  15. import os
  16. from typing import Any, Dict, List, Tuple, Union
  17. import pandas as pd
  18. from ....modules.ts_forecast.model_list import MODELS
  19. from ...common.batch_sampler import TSBatchSampler
  20. from ...common.reader import ReadTS
  21. from ..base import BasePredictor
  22. from ..common import (
  23. BuildTSDataset,
  24. TimeFeature,
  25. TSCutOff,
  26. TSNormalize,
  27. TStoArray,
  28. TStoBatch,
  29. )
  30. from .processors import ArraytoTS, TSDeNormalize
  31. from .result import TSFcResult
  32. class TSFcPredictor(BasePredictor):
  33. """TSFcPredictor that inherits from BasePredictor."""
  34. entities = MODELS
  35. def __init__(self, *args: List, **kwargs: Dict) -> None:
  36. """Initializes TSFcPredictor.
  37. Args:
  38. *args: Arbitrary positional arguments passed to the superclass.
  39. **kwargs: Arbitrary keyword arguments passed to the superclass.
  40. """
  41. super().__init__(*args, **kwargs)
  42. self.preprocessors, self.infer, self.postprocessors = self._build()
  43. def _build_batch_sampler(self) -> TSBatchSampler:
  44. """Builds and returns an ImageBatchSampler instance.
  45. Returns:
  46. ImageBatchSampler: An instance of ImageBatchSampler.
  47. """
  48. return TSBatchSampler()
  49. def _get_result_class(self) -> type:
  50. """Returns the result class, TopkResult.
  51. Returns:
  52. type: The TopkResult class.
  53. """
  54. return TSFcResult
  55. def _build(self) -> Tuple:
  56. """Build the preprocessors, inference engine, and postprocessors based on the configuration.
  57. Returns:
  58. tuple: A tuple containing the preprocessors, inference engine, and postprocessors.
  59. """
  60. preprocessors = {
  61. "ReadTS": ReadTS(),
  62. "TSCutOff": TSCutOff(self.config["size"]),
  63. }
  64. if self.config.get("scale", None):
  65. scaler_file_path = os.path.join(self.model_dir, "scaler.pkl")
  66. if not os.path.exists(scaler_file_path):
  67. raise Exception(f"Cannot find scaler file: {scaler_file_path}")
  68. preprocessors["TSNormalize"] = TSNormalize(
  69. scaler_file_path, self.config["info_params"]
  70. )
  71. preprocessors["BuildTSDataset"] = BuildTSDataset(self.config["info_params"])
  72. if self.config.get("time_feat", None):
  73. preprocessors["TimeFeature"] = TimeFeature(
  74. self.config["info_params"],
  75. self.config["size"],
  76. self.config["holiday"],
  77. )
  78. preprocessors["TStoArray"] = TStoArray(self.config["input_data"])
  79. preprocessors["TStoBatch"] = TStoBatch()
  80. infer = self.create_static_infer()
  81. postprocessors = {}
  82. postprocessors["ArraytoTS"] = ArraytoTS(self.config["info_params"])
  83. if self.config.get("scale", None):
  84. scaler_file_path = os.path.join(self.model_dir, "scaler.pkl")
  85. if not os.path.exists(scaler_file_path):
  86. raise Exception(f"Cannot find scaler file: {scaler_file_path}")
  87. postprocessors["TSDeNormalize"] = TSDeNormalize(
  88. scaler_file_path, self.config["info_params"]
  89. )
  90. return preprocessors, infer, postprocessors
  91. def process(self, batch_data: List[Union[str, pd.DataFrame]]) -> Dict[str, Any]:
  92. """
  93. Process a batch of data through the preprocessing, inference, and postprocessing.
  94. Args:
  95. batch_data (List[Union[str, pd.DataFrame], ...]): A batch of input data (e.g., image file paths).
  96. Returns:
  97. dict: A dictionary containing the input path, raw image, class IDs, scores, and label names for every instance of the batch. Keys include 'input_path', 'input_img', 'class_ids', 'scores', and 'label_names'.
  98. """
  99. batch_raw_ts = self.preprocessors["ReadTS"](ts_list=batch_data.instances)
  100. batch_raw_ts_ori = copy.deepcopy(batch_raw_ts)
  101. batch_cutoff_ts = self.preprocessors["TSCutOff"](ts_list=batch_raw_ts)
  102. if "TSNormalize" in self.preprocessors:
  103. batch_ts = self.preprocessors["TSNormalize"](ts_list=batch_cutoff_ts)
  104. batch_input_ts = self.preprocessors["BuildTSDataset"](ts_list=batch_ts)
  105. else:
  106. batch_input_ts = self.preprocessors["BuildTSDataset"](
  107. ts_list=batch_cutoff_ts
  108. )
  109. if "TimeFeature" in self.preprocessors:
  110. batch_ts = self.preprocessors["TimeFeature"](ts_list=batch_input_ts)
  111. batch_ts = self.preprocessors["TStoArray"](ts_list=batch_ts)
  112. else:
  113. batch_ts = self.preprocessors["TStoArray"](ts_list=batch_input_ts)
  114. x = self.preprocessors["TStoBatch"](ts_list=batch_ts)
  115. batch_preds = self.infer(x=x)
  116. batch_ts_preds = self.postprocessors["ArraytoTS"](
  117. ori_ts_list=batch_input_ts, pred_list=batch_preds
  118. )
  119. if "TSDeNormalize" in self.postprocessors:
  120. batch_ts_preds = self.postprocessors["TSDeNormalize"](
  121. preds_list=batch_ts_preds
  122. )
  123. return {
  124. "input_path": batch_data.input_paths,
  125. "input_ts": batch_raw_ts,
  126. "cutoff_ts": batch_raw_ts_ori,
  127. "forecast": batch_ts_preds,
  128. }