utils.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. """
  21. def __init__(self, config_path):
  22. self.inner_cfg = self.load(config_path)
  23. def load(self, config_path):
  24. """ load infer config """
  25. with codecs.open(config_path, 'r', 'utf-8') as file:
  26. dic = yaml.load(file, Loader=yaml.FullLoader)
  27. return dic
  28. @property
  29. def pre_transforms(self):
  30. """ read preprocess transforms from config file """
  31. if "RecPreProcess" in list(self.inner_cfg.keys()):
  32. tfs_cfg = self.inner_cfg['RecPreProcess']['transform_ops']
  33. else:
  34. tfs_cfg = self.inner_cfg['PreProcess']['transform_ops']
  35. tfs = []
  36. for cfg in tfs_cfg:
  37. tf_key = list(cfg.keys())[0]
  38. if tf_key == 'NormalizeImage':
  39. tf = image_common.Normalize(
  40. mean=cfg['NormalizeImage'].get("mean",
  41. [0.485, 0.456, 0.406]),
  42. std=cfg['NormalizeImage'].get("std", [0.229, 0.224, 0.225]))
  43. elif tf_key == 'ResizeImage':
  44. if "resize_short" in list(cfg[tf_key].keys()):
  45. tf = image_common.ResizeByShort(
  46. target_short_edge=cfg['ResizeImage'].get("resize_short",
  47. 224),
  48. size_divisor=None,
  49. interp='LINEAR')
  50. else:
  51. tf = image_common.Resize(
  52. target_size=cfg['ResizeImage'].get('size', (224, 224)))
  53. elif tf_key == "CropImage":
  54. tf = image_common.Crop(crop_size=cfg["CropImage"].get('size',
  55. 224))
  56. elif tf_key == "ToCHWImage":
  57. tf = image_common.ToCHWImage()
  58. else:
  59. raise RuntimeError(f"Unsupported type: {tf_key}")
  60. tfs.append(tf)
  61. return tfs
  62. @property
  63. def post_transforms(self):
  64. """ read postprocess transforms from config file """
  65. IGNORE_OPS = ['main_indicator', 'SavePreLabel']
  66. tfs_cfg = self.inner_cfg['PostProcess']
  67. tfs = []
  68. for tf_key in tfs_cfg:
  69. if tf_key == 'Topk':
  70. tf = T.Topk(
  71. topk=tfs_cfg['Topk']['topk'],
  72. class_ids=tfs_cfg['Topk'].get('label_list', None))
  73. elif tf_key in IGNORE_OPS:
  74. continue
  75. else:
  76. raise RuntimeError(f"Unsupported type: {tf_key}")
  77. tfs.append(tf)
  78. return tfs
  79. @property
  80. def labels(self):
  81. """ the labels in inner config """
  82. return self.inner_cfg['PostProcess']['Topk'].get('label_list', None)