-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.py
More file actions
58 lines (44 loc) · 1.63 KB
/
registry.py
File metadata and controls
58 lines (44 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
This file defines a registry structure so that we can reference certain model or loss
function using strings in the config files.
"""
def _register(module_dict, module_name, module):
"""
Register module to the target dictionary
:param module_dict: module dictionary
:param module_name: the name of the module to be registered
:param module: the module class to be registered
:return: None
"""
assert module_name not in module_dict
module_dict[module_name] = module
class Registry(dict):
"""
This class provides some helper functions for module registration.
It extends the standard dictionary class and provides a register function.
To create a registry:
example_registry = Registry({"default": default_module})
To register functions / class to the registry, use decorator:
@example_registry.register("foo_module", foo)
def foo():
...
Access the module is same as a dictionary:
f = example_registry["foo_module"]
"""
def __init__(self, *args, **kwargs):
super(Registry, self).__init__(*args, **kwargs)
if "registry_name" in kwargs:
self.registry_name = kwargs["registry_name"]
def register(self, module_name, module=None):
print("Registering: " + module_name)
if module is not None:
_register(self, module_name, module)
return
def register_fn(fn):
_register(self, module_name, fn)
return fn
return register_fn
# Declare a few global registry
DATA_SETS = Registry()
LOSSES = Registry()
MODELS = Registry({"registry_name": "Models"})