123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- # -*- coding: utf-8 -*-
- # @Time : 2022/2/25 13:53
- # @Author : MaochengHu
- # @Email : wojiaohumaocheng@gmail.com
- # @File : cf.py
- # @Project : server_develop
- import os
- import logging
- import prettytable as pt
- from load_yaml_config import load_yaml
- class SplitLine(object):
- def __init__(self, name="", line_type="="):
- self.name = name
- self.line_type = line_type
- def __enter__(self):
- columns = 90
- print(self.line_type * columns + f" {self.name} config_files" + self.line_type * columns)
- def __exit__(self, exc_type, exc_val, exc_tb):
- columns = 100
- print(self.line_type * columns + f" {self.name} " + self.line_type * columns)
- class Config(object):
- def __init__(self, config_dict: dict):
- config_dict["basic_params"].update({"root_dir": os.path.dirname(os.path.dirname(os.path.abspath(__file__)))})
- for key, val in config_dict.items():
- self.__setattr__(key, val)
- def copy(self, new_config_dict: dict):
- """
- Copies this config_files into a new config_files object, making
- the changes given by new_config_dict.
- """
- ret = Config(vars(self))
- for key, val in new_config_dict.items():
- ret.__setattr__(key, val)
- return ret
- def replace(self, new_config_dict):
- """
- Copies new_config_dict into this config_files object.
- Note: new_config_dict can also be a config_files object.
- """
- if isinstance(new_config_dict, Config):
- new_config_dict = vars(new_config_dict)
- for key, val in new_config_dict.items():
- self.__setattr__(key, val)
- def get(self, k, rv=None):
- dict_v = self.__dict__.get(k, rv)
- return dict_v
- def __call__(self):
- key_params = ["basic_params", "advanced_params"]
- model_name = self.__dict__.get(key_params[0]).get("name", "unknown model")
- with SplitLine(model_name, "=") as title_line:
- for key_param in key_params:
- tb = pt.PrettyTable()
- tb.header_style = "title"
- tb.padding_width = 20
- if key_param in self.__dict__.keys():
- tb.field_names = [f"{key_param}({model_name})", f"value({model_name})"]
- for k, v in self.__dict__[key_param].items():
- tb.add_row((k, v))
- logging.info(tb)
-
- def load_yaml_path(yaml_name):
- yaml_path = os.path.join(os.path.dirname(__file__), yaml_name)
- yaml_config = Config(load_yaml(yaml_path))
- yaml_config()
- return yaml_config
- # yolov5 structure config_files
- yolov5_yaml_config = load_yaml_path("yolov5_config.yaml")
- def set_cfg():
- global yolov5_yaml_config
|