cf.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2022/2/25 13:53
  3. # @Author : MaochengHu
  4. # @Email : wojiaohumaocheng@gmail.com
  5. # @File : cf.py
  6. # @Project : server_develop
  7. import os
  8. import logging
  9. import prettytable as pt
  10. from load_yaml_config import load_yaml
  11. class SplitLine(object):
  12. def __init__(self, name="", line_type="="):
  13. self.name = name
  14. self.line_type = line_type
  15. def __enter__(self):
  16. columns = 90
  17. print(self.line_type * columns + f" {self.name} config_files" + self.line_type * columns)
  18. def __exit__(self, exc_type, exc_val, exc_tb):
  19. columns = 100
  20. print(self.line_type * columns + f" {self.name} " + self.line_type * columns)
  21. class Config(object):
  22. def __init__(self, config_dict: dict):
  23. config_dict["basic_params"].update({"root_dir": os.path.dirname(os.path.dirname(os.path.abspath(__file__)))})
  24. for key, val in config_dict.items():
  25. self.__setattr__(key, val)
  26. def copy(self, new_config_dict: dict):
  27. """
  28. Copies this config_files into a new config_files object, making
  29. the changes given by new_config_dict.
  30. """
  31. ret = Config(vars(self))
  32. for key, val in new_config_dict.items():
  33. ret.__setattr__(key, val)
  34. return ret
  35. def replace(self, new_config_dict):
  36. """
  37. Copies new_config_dict into this config_files object.
  38. Note: new_config_dict can also be a config_files object.
  39. """
  40. if isinstance(new_config_dict, Config):
  41. new_config_dict = vars(new_config_dict)
  42. for key, val in new_config_dict.items():
  43. self.__setattr__(key, val)
  44. def get(self, k, rv=None):
  45. dict_v = self.__dict__.get(k, rv)
  46. return dict_v
  47. def __call__(self):
  48. key_params = ["basic_params", "advanced_params"]
  49. model_name = self.__dict__.get(key_params[0]).get("name", "unknown model")
  50. with SplitLine(model_name, "=") as title_line:
  51. for key_param in key_params:
  52. tb = pt.PrettyTable()
  53. tb.header_style = "title"
  54. tb.padding_width = 20
  55. if key_param in self.__dict__.keys():
  56. tb.field_names = [f"{key_param}({model_name})", f"value({model_name})"]
  57. for k, v in self.__dict__[key_param].items():
  58. tb.add_row((k, v))
  59. logging.info(tb)
  60. def load_yaml_path(yaml_name):
  61. yaml_path = os.path.join(os.path.dirname(__file__), yaml_name)
  62. yaml_config = Config(load_yaml(yaml_path))
  63. yaml_config()
  64. return yaml_config
  65. # yolov5 structure config_files
  66. yolov5_yaml_config = load_yaml_path("yolov5_config.yaml")
  67. def set_cfg():
  68. global yolov5_yaml_config