utils.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. """
  21. def __init__(self, config_path):
  22. self.inner_cfg = self.load(config_path)
  23. def load(self, config_path):
  24. """load config
  25. """
  26. with codecs.open(config_path, 'r', 'utf-8') as file:
  27. dic = yaml.load(file, Loader=yaml.FullLoader)
  28. return dic
  29. @property
  30. def pre_transforms(self):
  31. """ read preprocess transforms from config file """
  32. def _process_incompct_args(cfg, arg_names, action):
  33. for name in arg_names:
  34. if name in cfg:
  35. if action == 'ignore':
  36. logging.warning(
  37. f"Ignoring incompatible argument: {name}")
  38. elif action == 'raise':
  39. raise RuntimeError(
  40. f"Incompatible argument detected: {name}")
  41. else:
  42. raise ValueError(f"Unknown action: {action}")
  43. tfs_cfg = self.inner_cfg['Deploy']['transforms']
  44. tfs = []
  45. for cfg in tfs_cfg:
  46. if cfg['type'] == 'Normalize':
  47. tf = image_common.Normalize(
  48. mean=cfg.get('mean', 0.5), std=cfg.get('std', 0.5))
  49. elif cfg['type'] == 'Resize':
  50. tf = image_common.Resize(
  51. target_size=cfg.get('target_size', (512, 512)),
  52. keep_ratio=cfg.get('keep_ratio', False),
  53. size_divisor=cfg.get('size_divisor', None),
  54. interp=cfg.get('interp', 'LINEAR'))
  55. elif cfg['type'] == 'ResizeByLong':
  56. tf = image_common.ResizeByLong(
  57. target_long_edge=cfg['long_size'],
  58. size_divisor=None,
  59. interp='LINEAR')
  60. elif cfg['type'] == 'ResizeByShort':
  61. _process_incompct_args(cfg, ['max_size'], action='raise')
  62. tf = image_common.ResizeByShort(
  63. target_short_edge=cfg['short_size'],
  64. size_divisor=None,
  65. interp='LINEAR')
  66. elif cfg['type'] == 'Padding':
  67. _process_incompct_args(
  68. cfg, ['label_padding_value'], action='ignore')
  69. tf = image_common.Pad(target_size=cfg['target_size'],
  70. val=cfg.get('im_padding_value', 127.5))
  71. else:
  72. raise RuntimeError(f"Unsupported type: {cfg['type']}")
  73. tfs.append(tf)
  74. return tfs