utils.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(
  36. f"Ignoring incompatible argument: {name}")
  37. elif action == 'raise':
  38. raise RuntimeError(
  39. f"Incompatible argument detected: {name}")
  40. else:
  41. raise ValueError(f"Unknown action: {action}")
  42. tfs_cfg = self.inner_cfg['Deploy']['transforms']
  43. tfs = []
  44. for cfg in tfs_cfg:
  45. if cfg['type'] == 'Normalize':
  46. tf = image_common.Normalize(
  47. mean=cfg.get('mean', 0.5), std=cfg.get('std', 0.5))
  48. elif cfg['type'] == 'Resize':
  49. tf = image_common.Resize(
  50. target_size=cfg.get('target_size', (512, 512)),
  51. keep_ratio=cfg.get('keep_ratio', False),
  52. size_divisor=cfg.get('size_divisor', None),
  53. interp=cfg.get('interp', 'LINEAR'))
  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. 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. elif cfg['type'] == 'Padding':
  66. _process_incompct_args(
  67. cfg, ['label_padding_value'], action='ignore')
  68. tf = image_common.Pad(target_size=cfg['target_size'],
  69. val=cfg.get('im_padding_value', 127.5))
  70. else:
  71. raise RuntimeError(f"Unsupported type: {cfg['type']}")
  72. tfs.append(tf)
  73. return tfs
  74. def __getattr__(self, key):
  75. return self.inner_cfg[key]