utils.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 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. for name in arg_names:
  32. if name in cfg:
  33. if action == "ignore":
  34. logging.warning(f"Ignoring incompatible argument: {name}")
  35. elif action == "raise":
  36. raise RuntimeError(f"Incompatible argument detected: {name}")
  37. else:
  38. raise ValueError(f"Unknown action: {action}")
  39. tfs_cfg = self.inner_cfg["Deploy"]["transforms"]
  40. tfs = []
  41. for cfg in tfs_cfg:
  42. if cfg["type"] == "Normalize":
  43. tf = image_common.Normalize(
  44. mean=cfg.get("mean", 0.5), std=cfg.get("std", 0.5)
  45. )
  46. elif cfg["type"] == "Resize":
  47. tf = image_common.Resize(
  48. target_size=cfg.get("target_size", (512, 512)),
  49. keep_ratio=cfg.get("keep_ratio", False),
  50. size_divisor=cfg.get("size_divisor", None),
  51. interp=cfg.get("interp", "LINEAR"),
  52. )
  53. elif cfg["type"] == "ResizeByLong":
  54. tf = image_common.ResizeByLong(
  55. target_long_edge=cfg["long_size"],
  56. size_divisor=None,
  57. interp="LINEAR",
  58. )
  59. elif cfg["type"] == "ResizeByShort":
  60. _process_incompct_args(cfg, ["max_size"], action="raise")
  61. tf = image_common.ResizeByShort(
  62. target_short_edge=cfg["short_size"],
  63. size_divisor=None,
  64. interp="LINEAR",
  65. )
  66. elif cfg["type"] == "Padding":
  67. _process_incompct_args(cfg, ["label_padding_value"], action="ignore")
  68. tf = image_common.Pad(
  69. target_size=cfg["target_size"],
  70. val=cfg.get("im_padding_value", 127.5),
  71. )
  72. else:
  73. raise RuntimeError(f"Unsupported type: {cfg['type']}")
  74. tfs.append(tf)
  75. return tfs