当前位置:网站首页>Mmcv Config class introduction
Mmcv Config class introduction
2022-06-10 22:28:00 【Wu lele~】
Prepare information
This series mainly introduces mmcv And mmdetection Source code interpretation . therefore , It is suggested that the reader should install it locally first mmdetection Environmental Science . Installation tutorial :mmdet2.8 The latest installation tutorial !
List of articles
Preface
This is mmcv Source code interpretation Config class Introduce . The code address is mmcv/utils/config.py In file .
1、FasterRcnn For example
Most online Config Class explanation is a particularly dry code introduction , Lack of a concrete example to understand . therefore , This article takes mmdetection Medium FasterRcnn Network as an example Config class .( Of course ,Config Class also has many functions , Still need to learn to explore , Welcome to exchange :Q2541612007).
Stick it down FasterRcnn Configuration file for mmdetection/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py):
_base_ = [
'../_base_/models/faster_rcnn_r50_fpn.py',
'../_base_/datasets/coco_detection.py',
'../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
]
In the above configuration file ,_base_ The length is 4 Of list: Model , Data sets , Optimizer , How to train . Here under Config Class action :“ take The fields in the configuration file are converted into a dictionary ”. A simple example : With coco_detection.py Some file fields are taken as examples :
dataset_type = 'CocoDataset'
data_root = 'data/coco/'
img_norm_cfg = dict(
mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
Emotionally : Will be Above “=” The one on the left is regarded as a dictionary key;“=” The content on the right is regarded as value. I call it “ Equal sign rule ”. The final course Config Class returns an instantiated cfg object , use _cfg_dict initialization Config class , Become an attribute . actually _cfg_dict Still a dictionary .
2、 Implementation process
As can be seen from the first part :Config Class actually does two things :
(1) Set the configuration file as “ Equal sign rule “ Put the contents of the configuration file into a dictionary _cfg_dict.
(2) Using a dictionary _cfg_dict complete Config Class initialization , Make it a Config An attribute of a class .
therefore , The first part of the work actually belongs to Config Class . therefore , It can be used Python Static methods in @staticmethod complete . The second part is direct init The class can . Next, I will use two parts to introduce . The process is shown in the figure below :

2.1. The configuration file is transferred to the dictionary (_file2dict function )
from faster_rcnn From the configuration file of : In fact, one with _base_ List at the beginning of the field . The elements in the list are four paths . therefore , The function program needs to traverse 4 A path , Then add it to a dictionary . The overall logic of the function is as follows ( use ppt Painted , Forgive me ~):
After the overall logic is clarified , It's much easier to look at the code . I only post some here The core Of ( I can't understand many details , What the boss wrote is so awesome ).
@staticmethod
def _file2dict(filename, use_predefined_variables=True):
filename = osp.abspath(osp.expanduser(filename)) # filename: py The absolute path to the file
# take file Go to a dictionary
cfg_dict = {
name: value
for name, value in mod.__dict__.items()
if not name.startswith('__')
}
if BASE_KEY in cfg_dict: # To determine if there is _base_ Field
base_filename = cfg_dict.pop(BASE_KEY) # base_filename = ['../_base_/models/faster_rcnn_r50_fpn.py','../_base_/datasets/coco_detection.py','','']
cfg_dict_list = list() # Store final results
cfg_text_list = list()
for f in base_filename:
_cfg_dict, _cfg_text = Config._file2dict(osp.join(cfg_dir, f)) # Traversal recursive call file2path
cfg_dict_list.append(_cfg_dict)
cfg_text_list.append(_cfg_text)
# After the traversal is over ,cfg_dict_list=[{},{},{},{}] , cfg_text_list = ['','','',''] What's in it is Contents in the four configuration files
base_cfg_dict = dict()
for c in cfg_dict_list:
if len(base_cfg_dict.keys() & c.keys()) > 0:
raise KeyError('Duplicate key is not allowed among bases')
base_cfg_dict.update(c)
return cfg_dict, cfg_text
Probably no problem . Easier to understand .
2.2. Config Class initialization
2.1 Got it cfg_dict, Then you can use it to complete initialization .
def __init__(self, cfg_dict=None, cfg_text=None, filename=None):
super(Config, self).__setattr__('_cfg_dict', ConfigDict(cfg_dict))# Config Class to set a _cfg_dict attribute , And will cfg_dict Example is ConfigDict class .
super(Config, self).__setattr__('_filename', filename) # Config Class to set a _filename attribute .
super(Config, self).__setattr__('_text', text) # Config Adding a '_text' attribute
def __setattr__(self, name, value):
if isinstance(value, dict):
value = ConfigDict(value)
self._cfg_dict.__setattr__(name, value)
The code rewrites __setattr__ Method . Simple is to cfg_dict Become a ConfigDict class . The essence is a dictionary , It just rewrites __getattr__ Method , On the basis of key Some logical judgments are added to the index (keyerror What? ), Here it is still understood as a common dictionary .
class ConfigDict(Dict):
def __missing__(self, name):
raise KeyError(name)
def __getattr__(self, name):
try:
value = super(ConfigDict, self).__getattr__(name)
except KeyError:
ex = AttributeError(f"'{
self.__class__.__name__}' object has no "
f"attribute '{
name}'")
except Exception as e:
ex = e
else:
return value
raise ex
summary
This article mainly introduces Config The design concept of class puts . actually Config Class has many other methods , such as _merge_a_to_b etc. . But they can't escape those two parts : Change the field to according to the equal sign rule dict, And then use it dict initialization Config class .
If you have any questions, welcome +vx:wulele2541612007, Pull you into the group to discuss communication .
边栏推荐
- 记录(三)
- Record (III)
- JVM运行时数据区
- Only this is the most true reason why leaders promote you. The rest is nonsense!
- Array remove duplicates from an array
- ArrayList的扩容机制
- Sealem finance builds Web3 decentralized financial platform infrastructure
- C use s7 Net connected to Siemens s1200plc, C # directly connected to Siemens PLC
- [1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas
- Add, delete, query and modify [MySQL] table data (DML)
猜你喜欢

Back to table query of MySQL? How to avoid it?

OC swift hybrid

【MySQL】表的约束

Exec function of PHP

【phpstorm】 No data sources are configured to run this SQL and provide advanced c

很流行的状态管理库 MobX 是怎么回事?

Latex error: file ‘xxx. sty‘ not found

GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.

2022-06-09 rk817 PMU battery temperature detection
Super detailed tutorial for installing mysql8 in centos7 (no pit!)
随机推荐
SQL server queries are case sensitive
数组 移动0
To do desktop plug-in, a good helper for office workers
Notes (IV) - multithreading
A number that appears only once in an array
在D天内送达包裹的能力[抽象类二分--抽象判定方式]
JVM runtime data area
torch_geometric
String analysis and use
【MySQL】常见数据类型总结
Different ways to create four indexes in MySQL
Forward slash "/", backslash "\," escape character "\" and file path separator cannot be clearly remembered
Has the samesite cookie problem occurred when using identityserver?
SQL exercise 4: string processing function
Array union set
【C#】overide可重写从更高父级继承来的virtual方法
如何激发文化创新的活力和驱动力
Mysql的回表查询?如何避免?
Abbexa AML1 DNA binding ELISA Kit instructions
Cordova plugin /jpush phonegap Aurora push_ Local push_ Message push