当前位置:网站首页>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 .
边栏推荐
- 【MySQL】表结构的增删查改操作(DDL)
- AI blessing real-time interaction | analysis of zegoavatar facial expression following technology
- Array move 0
- 系统重装以及查询系统性能
- Differences between disk serial number, disk ID and volume serial number
- 数组 求上升区间的高度和
- Before we learn about high-performance computing, let's take a look at its history
- [1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas
- Abbexa AML1 DNA binding ELISA Kit instructions
- Ceph分布式存储集群Pool资源池的概念以及使用
猜你喜欢

Principle of gravure overprint and factors affecting overprint

【MySQL】常见数据类型总结

Modify frontsortinglayer variable of spritemask

【小程序】Vant滑动单元格添加点击其他位置自动关闭的功能
Super detailed tutorial for installing mysql8 in centos7 (no pit!)

What are MySQL clustered indexes and nonclustered indexes?

如何激发文化创新的活力和驱动力

Sealem Finance打造Web3去中心化金融平台基础设施

AI blessing real-time interaction | analysis of zegoavatar facial expression following technology

Abbexa low sample size chicken lysozyme C (Lyz) ELISA Kit
随机推荐
SQL exercise 4: string processing function
Ceph分布式存储集群Pool资源池的概念以及使用
leetcode:333. 最大 BST 子树
SQL Server row to column (pivot), column to row (unpivot)
【Xpath】使用following-sibling获取后面的同级节点
Array plus one
【MySQL】表数据的增删查改(DML)
IDEA出现“XXX has broken path”报错解决方法
Whether there are duplicate elements in the array
Latex error: file ‘xxx.sty‘ not found
KDD2022 | 基于对抗性知识蒸馏的深度图神经网络压缩
SQL第四练:字符串处理函数
Mysql中创建4种索引的不同方式
SQL Server查询区分大小写
Visio 转为高质量PDF
Are you still writing the TS type code
Ability to deliver packages within D days [abstract class dichotomy -- Abstract judgment method]
【问题】解决Websocket字符串长度限制问题单包过大
C language - quick sorting in sorting
Abbexa 1,3-dipalmitonin CLIA kit solution