In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about the example analysis of the implementation of Detectron2 registration mechanism Registry, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Registry implementation of Detectron2 Registration Mechanism
In Detectron2, it is common to register a class or function:
With regard to this operation, two points must be made clear:
1. Purpose 1.1 how to use the registration mechanism
Let's first take a look at how the registration mechanism is used: 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')
As you can see, if you create an object of Registry and decorate it with a decorator when the method / class is defined, you can call the registered function indirectly through registry_machine.get (method name).
Why use registry classes for detectron2, which requires a large framework that supports many different models, ideally all model parameters want to be written in the configuration file, then the question is, if I want to decide whether I need to use VGG or ResNet based on my configuration file, how should I write it?
If it were me, I might write this kind of code with super low scalability:
If class_name = 'VGG': model = build_VGG (args) elif class_name =' ResNet': model = build_ResNet (args)
But if you use a registration class, the code looks like this:
Class_name = 'VGG' #' ResNet'model = model_registry (class_name) (args) you can see that the extensibility of the code has become very strong. 2 specific implementation details
This part directly shows the code of the registration class. Interested friends can study the details. Personally, I think the application of the decorator is very good.
# 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') after reading the above, do you have any further understanding of the example analysis of the Registry implementation of the Detectron2 registration mechanism? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 216
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.