当前位置:网站首页>Interpreting the registry class of mmcv

Interpreting the registry class of mmcv

2022-06-10 22:41:00 Wu lele~


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 :
 Insert picture description here

  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 .

原网站

版权声明
本文为[Wu lele~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101653417684.html