predictor.py 5.1 KB

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