-
Notifications
You must be signed in to change notification settings - Fork 1
/
json2torch.py
384 lines (343 loc) · 13.9 KB
/
json2torch.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import torch
import json
from torch_layers import *
from melspec import MelSpecLayerSimple
def to_torch_layer(conf: dict, name: str) -> tuple[nn.Module, set[str], list[str]]:
r"""Given a configuration dictionary, returns corresponding Pytorch layer.
Args:
conf: Layer configuration
name: Name of the layer
Returns:
Pytorch nn.Module
Set of necessary imports
List of python lines necessary to generate the model
"""
# Recover layer type
type_name = conf["type"]
params = conf["params"]
def cleanify_string(lines: str) -> str:
r"""Turns a string with end-of-lines and whitespaces at the beginning of each line
into a single line without the whitespaces.
Args:
lines: String to cleanify
Returns:
Cleanified version of lines.
"""
return ' '.join([ p.lstrip() for p in lines.split("\n")])
def int_or_keyword(input: str) -> str:
r"""Takes a string that is either a keyword or an int,
and returns that string surrounded with quotes if it is a string.
Args:
input: the string
Returns:
the string, or the string with quotes around.
"""
if input.isdigit():
return input
return f"\"{input}\""
if type_name == "ZeroPad2d":
left, right, top, bot = (
params["left"],
params["right"],
params["top"],
params["bot"],
)
return (
nn.ZeroPad2d(padding=(left, right, top, bot)),
set(),
[f"{name} = nn.ZeroPad2d(padding=({left}, {right}, {top}, {bot}))"]
)
# Layers with learnable weights
if type_name in ["Conv2d", "Dense"]:
imports = set()
if type_name == "Conv2d":
if params["stride"][0] > 1 and params["padding"] == "same":
pt_layer, pt_descr = Conv2dSame(
in_channels=params["in_channels"],
out_channels=params["out_channels"],
kernel_size=params["kernel_size"],
stride=params["stride"],
padding=params["padding"],
bias=params["use_bias"],
groups=params["groups"],
expected_ih=params["ih"],
expected_iw=params["iw"],
), f"""Conv2dSame(
in_channels={params["in_channels"]},
out_channels={params["out_channels"]},
kernel_size={params["kernel_size"]},
stride={params["stride"]},
padding={int_or_keyword(params["padding"])},
bias={params["use_bias"]},
groups={params["groups"]},
expected_ih={params["ih"]},
expected_iw={params["iw"]},
)"""
imports.add("from torch_layers import Conv2dSame")
else:
pt_layer, pt_descr = nn.Conv2d(
in_channels=params["in_channels"],
out_channels=params["out_channels"],
kernel_size=params["kernel_size"],
stride=params["stride"],
padding=params["padding"],
bias=params["use_bias"],
groups=params["groups"],
), f"""nn.Conv2d(
in_channels={params["in_channels"]},
out_channels={params["out_channels"]},
kernel_size={params["kernel_size"]},
stride={params["stride"]},
padding={int_or_keyword(params["padding"])},
bias={params["use_bias"]},
groups={params["groups"]},
)"""
else: # Dense
pt_layer, pt_descr = nn.Linear(
in_features=params["in_features"],
out_features=params["out_features"],
bias=params["use_bias"],
), f"""nn.Linear(
in_features={params["in_features"]},
out_features={params["out_features"]},
bias={params["use_bias"]},
)"""
pt_descr = f"pt_layer = {cleanify_string(pt_descr)}"
# Copy weights
pt_layer.weight = nn.Parameter(torch.Tensor(params["weights"]))
if pt_layer.bias is not None:
pt_layer.bias.data = torch.Tensor(params["bias"])
# Handle activation function
aname = params["activation"]
if aname == "linear":
layer = pt_layer
actual_descr = "pt_layer"
elif aname == "relu":
layer = nn.Sequential(pt_layer, nn.ReLU())
actual_descr = "nn.Sequential(pt_layer, nn.ReLU())"
elif aname == "sigmoid":
layer = nn.Sequential(pt_layer, nn.Sigmoid())
actual_descr = "nn.Sequential(pt_layer, nn.Sigmoid())"
elif aname == "swish":
layer = nn.Sequential(pt_layer, nn.SiLU())
actual_descr = "nn.Sequential(pt_layer, nn.SiLU())"
else:
assert False, f"Activation {aname} not implemented (yet?)"
return layer, imports, [pt_descr, f"{name} = {actual_descr}"]
if type_name == "Dropout":
return nn.Dropout(p=params["p"]), set(), [f"""{name} = nn.Dropout(p={params["p"]})"""]
if type_name == "BatchNorm2d":
pt_layer = nn.BatchNorm2d(
num_features=params["num_features"],
eps=params["eps"],
momentum=params["momentum"],
)
descr = f"""nn.BatchNorm2d(
num_features={params["num_features"]},
eps={params["eps"]},
momentum={params["momentum"]},
)
"""
# Copy weights
pt_layer.gamma = nn.Parameter(torch.Tensor(params["gamma"]))
pt_layer.weight = nn.Parameter(torch.Tensor(params["gamma"]))
pt_layer.beta = nn.Parameter(torch.Tensor(params["beta"]))
pt_layer.bias = nn.Parameter(torch.Tensor(params["beta"]))
pt_layer._buffers["running_mean"] = torch.Tensor(params["mean"])
pt_layer._buffers["running_var"] = torch.Tensor(params["var"])
return pt_layer, set(), [
f"{name} = {cleanify_string(descr)}",
f"""{name}.gamma = nn.Parameter(torch.Tensor({params["gamma"]}))""",
f"""{name}.beta = nn.Parameter(torch.Tensor({params["beta"]}))""",
]
if type_name == "Activation":
aname = params["name"]
if aname == "relu":
return nn.ReLU(), set(), [f"{name} = nn.ReLU()"]
if aname == "sigmoid":
return nn.Sigmoid(), set(), [f"{name} = nn.Sigmoid()"]
if aname == "linear":
return nn.Identity(), set(), [f"{name} = nn.Identity()"]
if aname == "swish":
return nn.SiLU(), set(), [f"{name} = nn.SiLU()"]
assert False, f"Activation {aname} not implemented (yet?)"
if type_name == "MaxPool2d":
descr = f"""nn.MaxPool2d(
kernel_size={params["kernel_size"]},
stride={params["stride"]},
padding={params["padding"]},
)"""
return nn.MaxPool2d(
kernel_size=params["kernel_size"],
stride=params["stride"],
padding=params["padding"],
), set(), [f"{name} = {cleanify_string(descr)}"]
if type_name == "AveragePool2d":
descr = f"""nn.AvgPool2d(
kernel_size={params["kernel_size"]},
stride={params["stride"]},
padding={params["padding"]},
)"""
return nn.AvgPool2d(
kernel_size=params["kernel_size"],
stride=params["stride"],
padding=params["padding"],
), set(), [f"{name} = {cleanify_string(descr)}"]
if type_name == "GlobalAveragePool2d":
descr = f"""GlobalAveragePool2d(
kernel_size={params["kernel_size"]},
)"""
return (
GlobalAveragePool2d(kernel_size=params["kernel_size"],),
set(["from torch_layers import GlobalAveragePool2d"]),
[f"""{name} = {cleanify_string(descr)}"""]
)
if type_name == "Add":
return Add(), set(["from torch_layers import Add"]), [f"{name} = Add()"]
if type_name == "Multiply":
return Multiply(), set(["from torch_layers import Multiply"]), [f"{name} = Multiply()"]
if type_name == "Concatenate":
return (
Concatenate(axis=params["axis"]),
set(["from torch_layers import Concatenate"]),
[f"""{name} = Concatenate(axis={params["axis"]})"""]
)
if type_name == "Reshape":
return (
Reshape(shape=params["shape"]),
set(["from torch_layers import Reshape"]),
[f"""{name} = Reshape(shape={params["shape"]})"""]
)
if type_name == "Softmax2d":
return (
ChannelWiseSoftmax(),
set(["from torch_layers import Softmax2d"]),
[f"{name} = ChannelWiseSoftmax()"]
)
if type_name == "MelSpecLayerSimple":
descr = f"""MelSpecLayerSimple(
sample_rate={params["sample_rate"]},
spec_shape={params["spec_shape"]},
frame_step={params["frame_step"]},
frame_length={params["frame_length"]},
fmin={params["fmin"]},
fmax={params["fmax"]},
)"""
return (
MelSpecLayerSimple(
sample_rate=params["sample_rate"],
spec_shape=params["spec_shape"],
frame_step=params["frame_step"],
frame_length=params["frame_length"],
fmin=params["fmin"],
fmax=params["fmax"],
),
set(["from melspec import MelSpecLayerSimple"]),
[f"""{name} = {cleanify_string(descr)}"""]
)
assert False, f"Layer {type_name} not implemented (yet?)"
class ModelFromJson(nn.Module):
def __init__(
self,
fpath: str,
) -> None:
"""Init model from JSON configuration
Args:
fpath (str): Path to JSON file
"""
super(ModelFromJson, self).__init__()
# Read configuration file
with open(fpath, "r") as fin:
config = json.load(fin)
# Input shapes
self.input_shapes = config["input_shapes"]
# Execution order
self.exec_order = config["exec"]
self.exec_conf = {}
self.imports = set(["import torch", "import torch.nn as nn"])
self.layer_descr = []
# Layer params
layers_conf = config["layers"]
# Set each layer as attribute
for name in layers_conf:
# Build layer from configuration
layer, imports, layer_descrs = to_torch_layer(layers_conf[name]["params"], f"self.{name}")
self.imports.update(imports)
self.layer_descr.extend(layer_descrs)
setattr(self, name, layer)
# Store execution config
self.exec_conf[name] = {
"src": layers_conf[name]["exec"]["src"],
"save": layers_conf[name]["exec"]["save"],
}
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Given the model execution order, process a tensor
Args:
x (torch.Tensor): Input tensor
"""
stack = [x]
X = stack[0]
for name in self.exec_order:
index = self.exec_conf[name]["src"]
if len(index) != 1 or index[0] != -1:
# Fetch inbound tensors (otherwise, use previous one)
X = [stack[i] if i != -1 else X for i in index]
if len(X) == 1:
X = X[0]
X = getattr(self, name)(X)
if self.exec_conf[name]["save"]:
stack.append(X)
return X
def export(self, state_dict_filename, model_filename, classname="ModelFromJson"):
torch.save(self.state_dict(), state_dict_filename)
with open(model_filename, "w") as out:
class Printer: # Used to write python code
def __init__(self, out) -> None:
self.out = out
self.indent = 0
def inc(self): # increases indentation
self.indent += 1
def dec(self): # decreases indentation
self.indent -= 1
def __call__(self, line: str|list[str]): # prints the line(s)
if isinstance(line, str):
self.out.write(f"""{" "*self.indent}{line}\n""")
else:
for l in line:
self(l)
pr = Printer(out)
pr(self.imports)
pr("")
pr(f"class {classname}(nn.Module):")
pr.inc()
# __init__
pr("def __init__(self):")
pr.inc()
pr(f"super({classname}, self).__init__()")
pr(f"self.input_shapes = {self.input_shapes}")
for line in self.layer_descr:
pr(line)
pr.dec()
pr("")
# forward
pr("def forward(self, Xs: torch.Tensor|list[torch.Tensor]) -> torch.Tensor:")
pr.inc()
current_index = 0
if len(self.input_shapes) == 1:
pr(f"X_{current_index} = Xs")
current_index += 1
else:
for _ in self.input_shapes:
pr(f"X_{current_index} = Xs[{current_index}]")
current_index += 1
for name in self.exec_order:
indices = self.exec_conf[name]["src"]
indices = [ "X" if i == -1 else f"X_{i}" for i in indices ]
params = indices[0] if len(indices) == 1 else f"""[{",".join(indices)}]"""
pr(f"""X = self.{name}({params})""")
if self.exec_conf[name]["save"]:
pr(f"""X_{current_index} = X""")
current_index += 1
pr(f"return X")
pr.dec()
pr.dec()