当前位置:网站首页>Interpreting the registry class of mmcv
Interpreting the registry class of mmcv
2022-06-10 22:41:00 【Wu lele~】
List of articles
Preface
This paper mainly introduces mmcv Of Registry class . Readers are advised to configure mmcv Environmental Science :mmcv Source code installation . I believe most readers are interested in Registry Class is a little confused , Mainly involves python Knowledge of decorators in . therefore , This article tries to cover everything , Will briefly introduce the usage of some decorators .
1、Registry effect
Registry Class can be simply understood as a dictionary , for instance , stay mmdetection in , For example, we created a file named dataset Register object for , Then the Registrar dataset Contained in the (CocoDataset class ,VOCDataset class ,Lvis class ); Empathy ,detector The Registrar object contains (FasterRcnn class ,SSD class ,YOLO Class etc. ). therefore ,Registry Object can be understood as a dictionary , It stores the same series of classes .
2、 Source code analysis
Registry Although it is a dictionary , But it has to be realized Additions and deletions The function of . increase That is, add a new class to the dictionary ; check That is, whether this class exists in the query dictionary . So in Registry How to implement these functions in the class ?
2.1. Initialization part
class Registry:
"""A registry to map strings to classes. Args: name (str): Registry name. """
def __init__(self, name):
self._name = name
self._module_dict = dict()
def __len__(self):
return len(self._module_dict)
def __contains__(self, key):
return self.get(key) is not None
def __repr__(self):
format_str = self.__class__.__name__ + \
f'(name={
self._name}, ' \
f'items={
self._module_dict})'
return format_str
This part is relatively simple , It's just that a name And internally defines a self._module_dict Dictionaries .
2.2. check
lookup self._module_dict There is a certain class The implementation is also relatively simple :
def get(self, key):
return self._module_dict.get(key, None)
The main use get Method , If you have any key Returns the corresponding value; If there is no key Then return to None.
2.3. increase
The way to increase mmdetection There are two ways , The difference is the method _register_module() Is specified module Parameters :
This function is mainly used to self._module_dict Add class . Be careful , Adding to the dictionary are classes . The following code contains the two methods in the above figure . Here I intercepted the core code :
def _register_module(self, module_class, module_name=None, force=False):
if module_name is None:
module_name = module_class.__name__
if isinstance(module_name, str):
module_name = [module_name]
self._module_dict[name] = module_class
def register_module(self, name=None, force=False, module=None):
# If specified module, execute if sentence , Complete after execution module Class add
if module is not None:
self._register_module(
module_class=module, module_name=name, force=force)
return module
# If not specified module, execute _register function .
def _register(cls):
self._register_module(
module_class=cls, module_name=name, force=force)
return cls
return _register
I will introduce these two methods in two sections .
2.3.1 Appoint module Parameters
Now we want to go to the dictionary self._module_dict Add a new class to the dictionary . The easiest way to think of it is as follows :
if __name__ == '__main__':
backbones = Registry('backbone')
class MobileNet:
pass
backbones.register_module(module=MobileNet)
print(backbones)
That is, specify parameters directly module=MobileNet. Through internal self._module_dict[name]=module_class To complete the registration .
2.3.2 Don't specify module Parameters
The method provided in the previous section can , But using mmdetection When developing new models , If you create one class at a time , Then register by the above method , It's really inconvenient . It is bound to affect mmdetection Expansibility . And decorators can easily expand new functions for classes , Decorator I will write a separate article when I have a chance ,
Here, simply remember the usage of the decorator :funB = funA(funB), The decorated function funB, Through the decorator funA The decoration of , Something else may have happened in the middle , Final funA Of return funB.
First, let's look at the usage : For example, I want to register ResNet.
if __name__ == '__main__':
backbones = Registry('backbone')
@backbones.register_module()
class ResNet:
pass
print(backbones)
Here, the following functions are actually passed inside :
def _register(cls):
self._register_module(
module_class=cls, module_name=name, force=force)
return cls
In the process ,funB amount to cls. and _register Function is equivalent to funA. Middle to self._module_dict The class is registered in the dictionary cls. then return cls. namely funB.
summary
This paper mainly introduces Registry Class . If you have any questions, welcome +vx:wulele2541612007, Pull you into the group to discuss communication .
边栏推荐
- What are MySQL clustered indexes and nonclustered indexes?
- Sealem Finance打造Web3去中心化金融平台基础设施
- [tcapulusdb knowledge base] tcapulusdb viewing online operation
- 笔记(五)- JVM
- How can small and medium-sized conferences be upgraded digitally?
- [MySQL] Table constraints
- LeetCode - 5. Longest Palindromic Substring
- Storage engine analysis
- 手机号码更新不出来,怎么处理
- Tcapulusdb Jun · industry news collection (III)
猜你喜欢
![[tcapulusdb knowledge base] tcapulusdb viewing process status introduction](/img/8d/fc454b23489edcd8a7528a0acd3a02.png)
[tcapulusdb knowledge base] tcapulusdb viewing process status introduction

修改SpriteMask 的 frontSortingLayer 变量

【TcaplusDB知识库】TcaplusDB引擎参数调整介绍

Storage engine analysis

Whale conference sharing: what should we do if the conference is difficult?

Tcapulusdb Jun · industry news collection (V)

【TcaplusDB知识库】TcaplusDB推送配置介绍

Web3生态去中心化金融平台——Sealem Finance

MySQL master-slave replication solves read-write separation

Management solution for whale conference smart scenic spot
随机推荐
dc_labs--lab1的学习与总结
Add, delete, query and modify [MySQL] table data (DML)
【TcaplusDB知识库】TcaplusDB引擎参数调整介绍
创新探索层层加码,容智流程挖掘领域模式趋向成熟
数字孪生:第三人称鼠标操作
Innovation and exploration are added layer by layer, and the field model of intelligent process mining tends to be mature
鲸会务会议分享:大会难办怎么办?
IDEA出现“XXX has broken path”报错解决方法
《暗黑破坏神不朽》数据库资料站地址 暗黑不朽资料库网址
How can small and medium-sized conferences be upgraded digitally?
【TcaplusDB知识库】TcaplusDB查看线上运行情况介绍
[tcapulusdb knowledge base] Introduction to tcapulusdb push configuration
Web3生态去中心化金融平台——Sealem Finance
[MySQL] Table constraints
Diablo immortality database station address Diablo immortality database website
Latex error: file ‘xxx.sty‘ not found
[py] the failure of interface signature verification may be due to ensure_ ASCII problems
进阶高级程序员必知必会. 要不然,蠢材吗
Opencv_ 100 questions_ Chapter IV (16-20)
Different ways to create four indexes in MySQL