utils.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 ...base.predictor.transforms import image_common
  17. from . import transforms as T
  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. if "RecPreProcess" in list(self.inner_cfg.keys()):
  31. tfs_cfg = self.inner_cfg["RecPreProcess"]["transform_ops"]
  32. else:
  33. tfs_cfg = self.inner_cfg["PreProcess"]["transform_ops"]
  34. tfs = []
  35. for cfg in tfs_cfg:
  36. tf_key = list(cfg.keys())[0]
  37. if tf_key == "NormalizeImage":
  38. tf = image_common.Normalize(
  39. mean=cfg["NormalizeImage"].get("mean", [0.485, 0.456, 0.406]),
  40. std=cfg["NormalizeImage"].get("std", [0.229, 0.224, 0.225]),
  41. )
  42. elif tf_key == "ResizeImage":
  43. if "resize_short" in list(cfg[tf_key].keys()):
  44. tf = image_common.ResizeByShort(
  45. target_short_edge=cfg["ResizeImage"].get("resize_short", 224),
  46. size_divisor=None,
  47. interp="LINEAR",
  48. )
  49. else:
  50. tf = image_common.Resize(
  51. target_size=cfg["ResizeImage"].get("size", (224, 224))
  52. )
  53. elif tf_key == "CropImage":
  54. tf = image_common.Crop(crop_size=cfg["CropImage"].get("size", 224))
  55. elif tf_key == "ToCHWImage":
  56. tf = image_common.ToCHWImage()
  57. else:
  58. raise RuntimeError(f"Unsupported type: {tf_key}")
  59. tfs.append(tf)
  60. return tfs
  61. @property
  62. def post_transforms(self):
  63. """read postprocess transforms from config file"""
  64. IGNORE_OPS = ["main_indicator", "SavePreLabel"]
  65. tfs_cfg = self.inner_cfg["PostProcess"]
  66. tfs = []
  67. for tf_key in tfs_cfg:
  68. if tf_key == "Topk":
  69. tf = T.Topk(
  70. topk=tfs_cfg["Topk"]["topk"],
  71. class_ids=tfs_cfg["Topk"].get("label_list", None),
  72. )
  73. elif tf_key == "MultiLabelThreshOutput":
  74. tf = T.MultiLabelThreshOutput(
  75. threshold=0.5,
  76. class_ids=tfs_cfg["MultiLabelThreshOutput"].get("label_list", None),
  77. )
  78. elif tf_key in IGNORE_OPS:
  79. continue
  80. else:
  81. raise RuntimeError(f"Unsupported type: {tf_key}")
  82. tfs.append(tf)
  83. return tfs
  84. @property
  85. def labels(self):
  86. """the labels in inner config"""
  87. postprocess_name = self.inner_cfg["PostProcess"].keys()
  88. if "Topk" in postprocess_name:
  89. return self.inner_cfg["PostProcess"]["Topk"].get("label_list", None)
  90. elif "MultiLabelThreshOutput" in postprocess_name:
  91. return self.inner_cfg["PostProcess"]["MultiLabelThreshOutput"].get(
  92. "label_list", None
  93. )
  94. else:
  95. return None