utils.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 codecs
  15. import yaml
  16. import os
  17. from ....utils import logging
  18. from ...base.predictor.transforms import ts_common
  19. class InnerConfig(object):
  20. """Inner Config"""
  21. def __init__(self, config_path, model_dir=None):
  22. self.inner_cfg = self.load(config_path)
  23. self.model_dir = model_dir
  24. def load(self, config_path):
  25. """load config"""
  26. with codecs.open(config_path, "r", "utf-8") as file:
  27. dic = yaml.load(file, Loader=yaml.FullLoader)
  28. return dic
  29. @property
  30. def pre_transforms(self):
  31. """read preprocess transforms from config file"""
  32. tfs = []
  33. if self.inner_cfg.get("info_params", False):
  34. if self.inner_cfg.get("scale", False):
  35. scaler_file_path = os.path.join(self.model_dir, "scaler.pkl")
  36. if not os.path.exists(scaler_file_path):
  37. raise FileNotFoundError(
  38. f"Cannot find scaler file: {scaler_file_path}"
  39. )
  40. tf = ts_common.TSNormalize(
  41. scaler_file_path, self.inner_cfg["info_params"]
  42. )
  43. tfs.append(tf)
  44. tf = ts_common.BuildTSDataset(self.inner_cfg["info_params"])
  45. tfs.append(tf)
  46. tf = ts_common.BuildPadMask(self.inner_cfg["input_data"])
  47. tfs.append(tf)
  48. tf = ts_common.TStoArray(self.inner_cfg["input_data"])
  49. tfs.append(tf)
  50. else:
  51. raise ValueError("info_params is not found in config file")
  52. return tfs