utils.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # !/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. ################################################################################
  4. #
  5. # Copyright (c) 2024 Baidu.com, Inc. All Rights Reserved
  6. #
  7. ################################################################################
  8. """
  9. Author: PaddlePaddle Authors
  10. """
  11. import codecs
  12. import yaml
  13. from ....utils import logging
  14. from ...base.predictor.transforms import image_common
  15. from .transforms import SaveDetResults, PadStride, DetResize
  16. class InnerConfig(object):
  17. """Inner Config"""
  18. def __init__(self, config_path):
  19. self.inner_cfg = self.load(config_path)
  20. def load(self, config_path):
  21. """ load infer config """
  22. with codecs.open(config_path, 'r', 'utf-8') as file:
  23. dic = yaml.load(file, Loader=yaml.FullLoader)
  24. return dic
  25. @property
  26. def pre_transforms(self):
  27. """ read preprocess transforms from config file """
  28. tfs_cfg = self.inner_cfg["Preprocess"]
  29. tfs = []
  30. for cfg in tfs_cfg:
  31. if cfg['type'] == 'NormalizeImage':
  32. mean = cfg.get('mean', 0.5)
  33. std = cfg.get('std', 0.5)
  34. scale = 1. / 255. if cfg.get('is_scale', True) else 1
  35. norm_type = cfg.get('norm_type', "mean_std")
  36. if norm_type != "mean_std":
  37. mean = 0
  38. std = 1
  39. tf = image_common.Normalize(mean=mean, std=std, scale=scale)
  40. elif cfg['type'] == 'Resize':
  41. interp = cfg.get('interp', 'LINEAR')
  42. if isinstance(interp, int):
  43. interp = {
  44. 0: 'NEAREST',
  45. 1: 'LINEAR',
  46. 2: 'CUBIC',
  47. 3: 'AREA',
  48. 4: 'LANCZOS4'
  49. }[interp]
  50. tf = DetResize(
  51. target_hw=cfg['target_size'],
  52. keep_ratio=cfg.get('keep_ratio', True),
  53. interp=interp)
  54. elif cfg['type'] == 'Permute':
  55. tf = image_common.ToCHWImage()
  56. elif cfg['type'] == 'PadStride':
  57. stride = cfg.get('stride', 32)
  58. tf = PadStride(stride=stride)
  59. else:
  60. raise RuntimeError(f"Unsupported type: {cfg['type']}")
  61. tfs.append(tf)
  62. return tfs
  63. @property
  64. def labels(self):
  65. """ the labels in inner config """
  66. return self.inner_cfg["label_list"]