forked from puzzlelib/PuzzleLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlueprint.py
195 lines (125 loc) · 4.79 KB
/
Blueprint.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import json, os, importlib.util
import numpy as np
from PuzzleLib import Config
from PuzzleLib.Backend import gpuarray
from PuzzleLib.Modules.Module import Module
from PuzzleLib.Containers.Node import Node
class BlueprintError(Exception):
pass
class BlueprintFactory:
def __init__(self):
self.containers = {}
self.modules = {}
paths = ["Containers", "Modules"]
ignores = [
{"Node", "Container", "__init__"},
{"Module", "__init__"}
]
factories = [self.containers, self.modules]
for path, ignore, factory in zip(paths, ignores, factories):
factoryPath = os.path.join(os.path.dirname(__file__), path)
for file in os.listdir(factoryPath):
if file.endswith(".py") and os.path.splitext(file)[0] not in ignore:
filepath = os.path.abspath(os.path.join(factoryPath, file))
spec = importlib.util.spec_from_file_location(os.path.basename(filepath)[:-3], filepath)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
name = os.path.splitext(file)[0]
factory[name] = getattr(mod, name)
def build(self, blueprint, log=False):
classname, scheme = blueprint["classname"], blueprint["scheme"]
if classname in self.containers:
graph, elements = blueprint["graph"], blueprint["modules"]
if classname in {"Sequential", "Parallel"}:
mod = self.containers[classname](name=scheme["name"])
for name in graph:
cl = self.build(elements[name], log=log)
mod.append(cl)
elif classname == "Graph":
nodes = {name: Node(self.build(bprint, log=log)) for name, bprint in elements.items()}
for node in nodes.values():
node.addBackwards([(nodes[name], slots) for name, slots in graph[node.name]])
inputs = [nodes[name] for name in blueprint["inputs"]]
outputs = [nodes[name] for name in blueprint["outputs"]]
mod = self.containers[classname](inputs, outputs, name=scheme["name"])
else:
raise NotImplementedError(classname)
elif classname in self.modules:
if "initscheme" in scheme:
scheme["initscheme"] = "none"
mod = self.modules[classname](**scheme)
else:
raise BlueprintError("Cannot build module with class name '%s'" % classname)
if log:
Config.getLogger().info("Loaded %s", mod)
return mod
def load(hdf, name=None, assumeUniqueNames=False, log=False):
with Module.ensureHdf(hdf, "r") as hdf:
blueprint = json.loads(str(np.array(hdf["blueprint"])))
if log:
Config.getLogger().info("Building model from blueprint ...")
mod = BlueprintFactory().build(blueprint, log=log)
if log:
Config.getLogger().info("Loading model data ...")
mod.load(hdf, name=name, assumeUniqueNames=assumeUniqueNames)
return mod
def unittest():
fileTest()
memoryTest()
graphTest()
def buildNet():
from PuzzleLib.Containers import Sequential, Parallel
from PuzzleLib.Modules import Linear, Activation, relu, Replicate, Concat
seq = Sequential()
seq.append(Linear(20, 10, name="linear-1"))
seq.append(Activation(relu, name="relu-1"))
seq.append(Linear(10, 5, name="linear-2"))
seq.append(Activation(relu, name="relu-2"))
seq.append(Replicate(times=2, name="repl"))
seq.append(Parallel().append(Linear(5, 2, name="linear-3-1")).append(Linear(5, 3, name="linear-3-2")))
seq.append(Concat(axis=1, name="concat"))
return seq
def buildGraph():
from PuzzleLib.Containers import Graph
from PuzzleLib.Modules import Linear, Activation, relu, Concat
inp = Linear(20, 10, name="linear-1").node()
h = Activation(relu, name="relu-1").node(inp)
h1 = Linear(10, 5, name="linear-2").node(h)
h2 = Linear(10, 5, name="linear-3").node(h)
output = Concat(axis=1, name="concat").node(h1, h2)
graph = Graph(inputs=inp, outputs=output)
return graph
def fileTest():
seq = buildNet()
data = gpuarray.to_gpu(np.random.randn(32, 20).astype(np.float32))
origOutData = seq(data)
try:
seq.save("./TestData/seq.hdf", withBlueprint=True)
newSeq = load("./TestData/seq.hdf", log=True)
finally:
if os.path.exists("./TestData/seq.hdf"):
os.remove("./TestData/seq.hdf")
newOutData = newSeq(data)
assert np.allclose(origOutData.get(), newOutData.get())
def memoryTest():
seq = buildNet()
data = gpuarray.to_gpu(np.random.randn(32, 20).astype(np.float32))
origOutData = seq(data)
mmap = seq.save(withBlueprint=True)
newSeq = load(mmap, log=True)
newOutData = newSeq(data)
assert np.allclose(origOutData.get(), newOutData.get())
def graphTest():
graph = buildGraph()
data = gpuarray.to_gpu(np.random.randn(32, 20).astype(np.float32))
origOutData = graph(data)
try:
graph.save("./TestData/graph.hdf", withBlueprint=True)
newGraph = load("./TestData/graph.hdf", log=True)
finally:
if os.path.exists("./TestData/graph.hdf"):
os.remove("./TestData/graph.hdf")
newOutData = newGraph(data)
assert np.allclose(origOutData.get(), newOutData.get())
if __name__ == "__main__":
unittest()