utils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. from ....utils import logging
  17. from ...base.predictor.transforms import image_common
  18. class InnerConfig(object):
  19. """Inner Config"""
  20. def __init__(self, config_path):
  21. self.inner_cfg = self.load(config_path)
  22. def load(self, config_path):
  23. """load infer config"""
  24. with codecs.open(config_path, "r", "utf-8") as file:
  25. dic = yaml.load(file, Loader=yaml.FullLoader)
  26. return dic
  27. @property
  28. def pre_transforms(self):
  29. """read preprocess transforms from config file"""
  30. def _process_incompct_args(cfg, arg_names, action):
  31. """process incompct arguments"""
  32. for name in arg_names:
  33. if name in cfg:
  34. if action == "ignore":
  35. logging.warning(f"Ignoring incompatible argument: {name}")
  36. elif action == "raise":
  37. raise RuntimeError(f"Incompatible argument detected: {name}")
  38. else:
  39. raise ValueError(f"Unknown action: {action}")
  40. tfs_cfg = self.inner_cfg["Deploy"]["transforms"]
  41. tfs = []
  42. for cfg in tfs_cfg:
  43. if cfg["type"] == "Normalize":
  44. tf = image_common.Normalize(
  45. mean=cfg.get("mean", 0.5), std=cfg.get("std", 0.5)
  46. )
  47. elif cfg["type"] == "Resize":
  48. tf = image_common.Resize(
  49. target_size=cfg.get("target_size", (512, 512)),
  50. keep_ratio=cfg.get("keep_ratio", False),
  51. size_divisor=cfg.get("size_divisor", None),
  52. interp=cfg.get("interp", "LINEAR"),
  53. )
  54. elif cfg["type"] == "ResizeByLong":
  55. tf = image_common.ResizeByLong(
  56. target_long_edge=cfg["long_size"],
  57. size_divisor=None,
  58. interp="LINEAR",
  59. )
  60. elif cfg["type"] == "ResizeByShort":
  61. _process_incompct_args(cfg, ["max_size"], action="raise")
  62. tf = image_common.ResizeByShort(
  63. target_short_edge=cfg["short_size"],
  64. size_divisor=None,
  65. interp="LINEAR",
  66. )
  67. elif cfg["type"] == "Padding":
  68. _process_incompct_args(cfg, ["label_padding_value"], action="ignore")
  69. tf = image_common.Pad(
  70. target_size=cfg["target_size"],
  71. val=cfg.get("im_padding_value", 127.5),
  72. )
  73. else:
  74. raise RuntimeError(f"Unsupported type: {cfg['type']}")
  75. tfs.append(tf)
  76. return tfs
  77. def __getattr__(self, key):
  78. return self.inner_cfg[key]