-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutilities.py
326 lines (311 loc) · 10.1 KB
/
utilities.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
import matplotlib.pyplot as plt
from CNN.Network import Network1, Network2, Network3, Network4, Network5, Network6
def plot_loss(loss, title, filename):
"""
Plots the loss for each epoch.
:param loss: A list with the loss per epoch.
:param title: The plot title.
:param filename: The filename.
"""
plt.title(title)
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.plot(loss)
plt.savefig(f"figures/{filename}.png")
def plot_losses(losses, labels, title, filename):
"""
Plots multiple losses for each epoch.
:param losses: A 2D list with the losses per epoch.
:param labels: The label for each case.
:param title: The plot title.
:param filename: The filename.
"""
plt.title(title)
plt.xlabel("Epoch")
plt.ylabel("Loss")
for i in range(len(losses)):
plt.plot(losses[i], label=labels[i])
plt.legend()
plt.savefig(f"figures/{filename}.png")
def instantiate_network(architecture):
"""
Instantiates the network that corresponds to the given
architecture.
:param architecture: The network architecture.
:return: The network.
"""
if architecture == 1:
return Network1()
elif architecture == 2:
return Network2()
elif architecture == 3:
return Network3()
elif architecture == 4:
return Network4()
elif architecture == 5:
return Network5()
else:
return Network6()
def register_hooks(model, architecture, hook):
"""
Registers the given hook to the given model.
:param model: The model.
:param architecture: Model's architecture.
:param hook: The hook to be registered.
:return: The model instance and the layers list.
"""
layers = []
if architecture == 1:
model.conv1.register_forward_hook(hook("conv1"))
model.conv2.register_forward_hook(hook("conv2"))
model.t_conv1.register_forward_hook(hook("t_conv1"))
model.t_conv2.register_forward_hook(hook("t_conv2"))
layers = [
{
"name": "conv1",
"channels": model.conv1.out_channels
},
{
"name": "conv2",
"channels": model.conv2.out_channels
},
{
"name": "t_conv1",
"channels": model.t_conv1.out_channels
},
{
"name": "t_conv2",
"channels": model.t_conv2.out_channels
}
]
elif architecture == 2:
model.conv1.register_forward_hook(hook("conv1"))
model.conv2.register_forward_hook(hook("conv2"))
model.conv3.register_forward_hook(hook("conv3"))
model.t_conv1.register_forward_hook(hook("t_conv1"))
model.t_conv2.register_forward_hook(hook("t_conv2"))
model.t_conv3.register_forward_hook(hook("t_conv3"))
layers = [
{
"name": "conv1",
"channels": model.conv1.out_channels
},
{
"name": "conv2",
"channels": model.conv2.out_channels
},
{
"name": "conv3",
"channels": model.conv3.out_channels
},
{
"name": "t_conv1",
"channels": model.t_conv1.out_channels
},
{
"name": "t_conv2",
"channels": model.t_conv2.out_channels
},
{
"name": "t_conv3",
"channels": model.t_conv3.out_channels
}
]
elif architecture == 3:
model.conv1.register_forward_hook(hook("conv1"))
model.conv2.register_forward_hook(hook("conv2"))
model.conv3.register_forward_hook(hook("conv3"))
model.t_conv1.register_forward_hook(hook("t_conv1"))
model.t_conv2.register_forward_hook(hook("t_conv2"))
model.t_conv3.register_forward_hook(hook("t_conv3"))
model.output.register_forward_hook(hook("output"))
layers = [
{
"name": "conv1",
"channels": model.conv1.out_channels
},
{
"name": "conv2",
"channels": model.conv2.out_channels
},
{
"name": "conv3",
"channels": model.conv3.out_channels
},
{
"name": "t_conv1",
"channels": model.t_conv1.out_channels
},
{
"name": "t_conv2",
"channels": model.t_conv2.out_channels
},
{
"name": "t_conv3",
"channels": model.t_conv3.out_channels
},
{
"name": "output",
"channels": model.output.out_channels
}
]
elif architecture == 4:
model.conv1.register_forward_hook(hook("conv1"))
model.conv2.register_forward_hook(hook("conv2"))
model.conv3.register_forward_hook(hook("conv3"))
model.conv4.register_forward_hook(hook("conv4"))
model.t_conv1.register_forward_hook(hook("t_conv1"))
model.t_conv2.register_forward_hook(hook("t_conv2"))
model.t_conv3.register_forward_hook(hook("t_conv3"))
model.t_conv4.register_forward_hook(hook("t_conv4"))
model.output.register_forward_hook(hook("output"))
layers = [
{
"name": "conv1",
"channels": model.conv1.out_channels
},
{
"name": "conv2",
"channels": model.conv2.out_channels
},
{
"name": "conv3",
"channels": model.conv3.out_channels
},
{
"name": "conv4",
"channels": model.conv4.out_channels
},
{
"name": "t_conv1",
"channels": model.t_conv1.out_channels
},
{
"name": "t_conv2",
"channels": model.t_conv2.out_channels
},
{
"name": "t_conv3",
"channels": model.t_conv3.out_channels
},
{
"name": "t_conv4",
"channels": model.t_conv4.out_channels
},
{
"name": "output",
"channels": model.output.out_channels
}
]
elif architecture == 5:
model.conv1.register_forward_hook(hook("conv1"))
model.conv2.register_forward_hook(hook("conv2"))
model.conv3.register_forward_hook(hook("conv3"))
model.conv4.register_forward_hook(hook("conv4"))
model.conv5.register_forward_hook(hook("conv5"))
model.t_conv1.register_forward_hook(hook("t_conv1"))
model.t_conv2.register_forward_hook(hook("t_conv2"))
model.t_conv3.register_forward_hook(hook("t_conv3"))
model.t_conv4.register_forward_hook(hook("t_conv4"))
model.output.register_forward_hook(hook("output"))
layers = [
{
"name": "conv1",
"channels": model.conv1.out_channels
},
{
"name": "conv2",
"channels": model.conv2.out_channels
},
{
"name": "conv3",
"channels": model.conv3.out_channels
},
{
"name": "conv4",
"channels": model.conv4.out_channels
},
{
"name": "conv5",
"channels": model.conv5.out_channels
},
{
"name": "t_conv1",
"channels": model.t_conv1.out_channels
},
{
"name": "t_conv2",
"channels": model.t_conv2.out_channels
},
{
"name": "t_conv3",
"channels": model.t_conv3.out_channels
},
{
"name": "t_conv4",
"channels": model.t_conv4.out_channels
},
{
"name": "output",
"channels": model.output.out_channels
}
]
elif architecture == 6:
model.conv1.register_forward_hook(hook("conv1"))
model.conv2.register_forward_hook(hook("conv2"))
model.conv3.register_forward_hook(hook("conv3"))
model.conv4.register_forward_hook(hook("conv4"))
model.conv5.register_forward_hook(hook("conv5"))
model.conv6.register_forward_hook(hook("conv6"))
model.t_conv1.register_forward_hook(hook("t_conv1"))
model.t_conv2.register_forward_hook(hook("t_conv2"))
model.t_conv3.register_forward_hook(hook("t_conv3"))
model.t_conv4.register_forward_hook(hook("t_conv4"))
model.output.register_forward_hook(hook("output"))
layers = [
{
"name": "conv1",
"channels": model.conv1.out_channels
},
{
"name": "conv2",
"channels": model.conv2.out_channels
},
{
"name": "conv3",
"channels": model.conv3.out_channels
},
{
"name": "conv4",
"channels": model.conv4.out_channels
},
{
"name": "conv5",
"channels": model.conv5.out_channels
},
{
"name": "conv6",
"channels": model.conv6.out_channels
},
{
"name": "t_conv1",
"channels": model.t_conv1.out_channels
},
{
"name": "t_conv2",
"channels": model.t_conv2.out_channels
},
{
"name": "t_conv3",
"channels": model.t_conv3.out_channels
},
{
"name": "t_conv4",
"channels": model.t_conv4.out_channels
},
{
"name": "output",
"channels": model.output.out_channels
}
]
return model, layers