utils.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. tfs_cfg = self.inner_cfg["PostProcess"]
  65. tfs = []
  66. if tfs_cfg is None:
  67. return tfs
  68. for tf_key in tfs_cfg:
  69. if tf_key == "NormalizeFeatures":
  70. tf = T.NormalizeFeatures()
  71. else:
  72. raise RuntimeError(f"Unsupported type: {tf_key}")
  73. tfs.append(tf)
  74. return tfs