Spaces:
Build error
Build error
commit
Browse files- yolov6/utils/config.py +101 -0
yolov6/utils/config.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
# The code is based on
|
4 |
+
# https://github.com/open-mmlab/mmcv/blob/master/mmcv/utils/config.py
|
5 |
+
# Copyright (c) OpenMMLab.
|
6 |
+
|
7 |
+
import os.path as osp
|
8 |
+
import shutil
|
9 |
+
import sys
|
10 |
+
import tempfile
|
11 |
+
from importlib import import_module
|
12 |
+
from addict import Dict
|
13 |
+
|
14 |
+
|
15 |
+
class ConfigDict(Dict):
|
16 |
+
|
17 |
+
def __missing__(self, name):
|
18 |
+
raise KeyError(name)
|
19 |
+
|
20 |
+
def __getattr__(self, name):
|
21 |
+
try:
|
22 |
+
value = super(ConfigDict, self).__getattr__(name)
|
23 |
+
except KeyError:
|
24 |
+
ex = AttributeError("'{}' object has no attribute '{}'".format(
|
25 |
+
self.__class__.__name__, name))
|
26 |
+
except Exception as e:
|
27 |
+
ex = e
|
28 |
+
else:
|
29 |
+
return value
|
30 |
+
raise ex
|
31 |
+
|
32 |
+
|
33 |
+
class Config(object):
|
34 |
+
|
35 |
+
@staticmethod
|
36 |
+
def _file2dict(filename):
|
37 |
+
filename = str(filename)
|
38 |
+
if filename.endswith('.py'):
|
39 |
+
with tempfile.TemporaryDirectory() as temp_config_dir:
|
40 |
+
shutil.copyfile(filename,
|
41 |
+
osp.join(temp_config_dir, '_tempconfig.py'))
|
42 |
+
sys.path.insert(0, temp_config_dir)
|
43 |
+
mod = import_module('_tempconfig')
|
44 |
+
sys.path.pop(0)
|
45 |
+
cfg_dict = {
|
46 |
+
name: value
|
47 |
+
for name, value in mod.__dict__.items()
|
48 |
+
if not name.startswith('__')
|
49 |
+
}
|
50 |
+
# delete imported module
|
51 |
+
del sys.modules['_tempconfig']
|
52 |
+
else:
|
53 |
+
raise IOError('Only .py type are supported now!')
|
54 |
+
cfg_text = filename + '\n'
|
55 |
+
with open(filename, 'r') as f:
|
56 |
+
cfg_text += f.read()
|
57 |
+
|
58 |
+
return cfg_dict, cfg_text
|
59 |
+
|
60 |
+
@staticmethod
|
61 |
+
def fromfile(filename):
|
62 |
+
cfg_dict, cfg_text = Config._file2dict(filename)
|
63 |
+
return Config(cfg_dict, cfg_text=cfg_text, filename=filename)
|
64 |
+
|
65 |
+
def __init__(self, cfg_dict=None, cfg_text=None, filename=None):
|
66 |
+
if cfg_dict is None:
|
67 |
+
cfg_dict = dict()
|
68 |
+
elif not isinstance(cfg_dict, dict):
|
69 |
+
raise TypeError('cfg_dict must be a dict, but got {}'.format(
|
70 |
+
type(cfg_dict)))
|
71 |
+
|
72 |
+
super(Config, self).__setattr__('_cfg_dict', ConfigDict(cfg_dict))
|
73 |
+
super(Config, self).__setattr__('_filename', filename)
|
74 |
+
if cfg_text:
|
75 |
+
text = cfg_text
|
76 |
+
elif filename:
|
77 |
+
with open(filename, 'r') as f:
|
78 |
+
text = f.read()
|
79 |
+
else:
|
80 |
+
text = ''
|
81 |
+
super(Config, self).__setattr__('_text', text)
|
82 |
+
|
83 |
+
@property
|
84 |
+
def filename(self):
|
85 |
+
return self._filename
|
86 |
+
|
87 |
+
@property
|
88 |
+
def text(self):
|
89 |
+
return self._text
|
90 |
+
|
91 |
+
def __repr__(self):
|
92 |
+
return 'Config (path: {}): {}'.format(self.filename,
|
93 |
+
self._cfg_dict.__repr__())
|
94 |
+
|
95 |
+
def __getattr__(self, name):
|
96 |
+
return getattr(self._cfg_dict, name)
|
97 |
+
|
98 |
+
def __setattr__(self, name, value):
|
99 |
+
if isinstance(value, dict):
|
100 |
+
value = ConfigDict(value)
|
101 |
+
self._cfg_dict.__setattr__(name, value)
|