当前位置:网站首页>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 .
边栏推荐
- Latex error: file ‘xxx.sty‘ not found
- torch_geometric
- 进阶高级程序员必知必会. 要不然,蠢材吗
- Sealem finance builds Web3 decentralized financial platform infrastructure
- Pytorch 安装超简单
- [MySQL] summary of common data types
- GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.
- Implementation of simply untyped lambda calculus
- Matlab - 演化博弈论实现
- [XPath] use following sibling to obtain the following peer nodes
猜你喜欢

TcaplusDB君 · 行业新闻汇编(一)

【TcaplusDB知识库】TcaplusDB刷新tbus通道介绍
![[MySQL] summary of common data types](/img/96/010c21f0aa7b443c130c5f55e5277a.png)
[MySQL] summary of common data types
![[tcapulusdb knowledge base] tcapulusdb machine initialization and launch introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb machine initialization and launch introduction

【TcaplusDB知识库】TcaplusDB查看进程状态介绍

Visio 转为高质量PDF

鲸会务会议分享:大会难办怎么办?

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

小微企业如何低成本搭建微官网
![[tcapulusdb knowledge base] tcapulusdb viewing process status introduction](/img/8d/fc454b23489edcd8a7528a0acd3a02.png)
[tcapulusdb knowledge base] tcapulusdb viewing process status introduction
随机推荐
GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.
Shell basic concepts
[tcapulusdb knowledge base] tcapulusdb viewing online operation
小微企业如何低成本搭建微官网
[tcapulusdb knowledge base] Introduction to tcapulusdb push configuration
修改SpriteMask 的 frontSortingLayer 变量
很流行的状态管理库 MobX 是怎么回事?
What should I do if the mobile phone number cannot be updated
Modify frontsortinglayer variable of spritemask
[tcapulusdb knowledge base] Introduction to tcapulusdb process startup
记录(二)
鲸会务会议分享:大会难办怎么办?
Opencv_100问_第三章 (11-15)
README
datagrip 报错 “The specified database user/password combination is rejected...”的解决方法
QT custom delegation
Whale conference sharing: what should we do if the conference is difficult?
Tcapulusdb Jun · industry news collection (VI)
String search in C
Solution to "XXX has broken path" error in idea