-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwmodule.py
More file actions
executable file
·72 lines (61 loc) · 2.21 KB
/
wmodule.py
File metadata and controls
executable file
·72 lines (61 loc) · 2.21 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
59
60
61
62
63
64
65
66
67
68
69
70
71
#coding=utf-8
class WModule(object):
def __init__(self,cfg,parent=None,is_training=False,*args,**kwargs):
self.maped_attr = {"is_training":is_training}
self.local_attr = {}
self.cfg = cfg
self.parent = parent
self.childs = []
if self.parent is not None:
self.parent.add_child(self)
def add_child(self,obj):
self.childs.append(obj)
def __getattr__(self,item):
if item == "maped_attr" or item == 'local_attr':
return self.__dict__[item]
elif item in self.local_attr:
return self.local_attr[item]
elif item in self.maped_attr:
if self.parent is None:
return self.maped_attr[item]
elif item in self.parent.local_attr:
return self.parent.local_attr[item]
elif item in self.parent.maped_attr:
return self.parent.maped_attr[item]
else:
if item in self.__dict__:
return self.__dict__[item]
else:
raise AttributeError(r"object has no attribute '%s'" % item)
def __setattr__(self, key, value):
if key == "maped_attr":
self.__dict__[key] = value
elif key in self.maped_attr:
if self.parent is None:
self.maped_attr[key] = value
else:
self.parent.maped_attr[key] = value
else:
self.__dict__[key] = value
def __call__(self, *args, **kwargs):
return self.forward(*args,**kwargs)
def disable_training(self):
self.local_attr['is_training'] = False
class WRootModule(WModule):
def __init__(self):
super().__init__(None,None)
class WChildModule(WModule):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
assert self.parent is not None
class WModelList(WModule):
def __init__(self,models,*args,**kwargs):
super().__init__(*args,**kwargs)
assert self.parent is not None
self.models = models
assert len(self.models)>0
def farward(self,*args,**kwargs):
res = self.models[0](*args,**kwargs)
for m in self.models[1:]:
res = m(res)
return res