温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Detectron2注册机制Registry实现的示例分析

发布时间:2021-11-15 15:07:30 来源:亿速云 阅读:170 作者:柒染 栏目:大数据

今天就跟大家聊聊有关Detectron2注册机制Registry实现的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。


Detectron2注册机制 Registry 实现


在Detectron2 中,经常会对一个类或者函数进行注册:

Detectron2注册机制Registry实现的示例分析

关于这种操作,必须要明确两点:


1.目的

1.1 注册机制的使用方法

首先来看一下注册机制是如何使用的:
registry_machine = Registry('registry_machine')
registry_machine.register()def print_hello_world(word):    print('hello {}'.format(word))

registry_machine.register()def print_hi_world(word):    print('hi {}'.format(word))
if __name__ == '__main__':
   cfg1 = 'print_hello_word'    registry_machine.get(cfg1)('world')
   cfg2 = 'print_hi_word'    registry_machine.get(cfg2)('world')

可以看到,如果创建了一个Registry的对象,并在方法/类定义的时候用装饰器装饰它,则可以通过 registry_machine.get(方法名)的 办法来间接的调用被注册的函数

1.2 为什么使用注册类

对于detectron2这种,需要支持许多不同的模型的大型框架,理想情况下所有的模型的参数都希望写在配置文件中,那问题来了,如果我希望根据我的配置文件,决定我是需要用VGG还是用ResNet ,我要怎么写呢?

如果是我,我可能会写出这种可扩展性超级低的暴搓的代码:

if class_name == 'VGG':    model = build_VGG(args)elif class_name == 'ResNet':    model = build_ResNet(args)

但是如果用了注册类,代码就是这样的:

class_name = 'VGG' # 'ResNet'model = model_registry(class_name)(args)
可以看到代码的可扩展性变得非常强了

2 具体实现细节

这部分就直接展示注册类的代码了,有兴趣的朋友可以研究一下其中的细节,个人觉得对装饰器的应用是非常的好了

# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reservedclass Registry(object):
   def __init__(self, name):        """        Args:            name (str): the name of this registry        """        self._name = name
       self._obj_map = {}
   def _do_register(self, name, obj):        assert (            name not in self._obj_map        ), "An object named '{}' was already registered in '{}' registry!".format(name, self._name)        self._obj_map[name] = obj
   def register(self, obj=None):        """        Register the given object under the the name `obj.__name__`.        Can be used as either a decorator or not. See docstring of this class for usage.        """        if obj is None:            # used as a decorator            def deco(func_or_class):                name = func_or_class.__name__                self._do_register(name, func_or_class)                return func_or_class
           return deco
       # used as a function call        name = obj.__name__        self._do_register(name, obj)
   def get(self, name):        ret = self._obj_map.get(name)        if ret is None:            raise KeyError("No object named '{}' found in '{}' registry!".format(name, self._name))        return ret


registry_machine = Registry('registry_machine')
registry_machine.register()def print_hello_world(word):    print('hello {}'.format(word))

registry_machine.register()def print_hi_world(word):    print('hi {}'.format(word))
if __name__ == '__main__':
   cfg1 = 'print_hello_word'    registry_machine.get(cfg1)('world')
   cfg2 = 'print_hi_word'    registry_machine.get(cfg2)('world')

看完上述内容,你们对Detectron2注册机制Registry实现的示例分析有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI