-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
662 lines (481 loc) · 22.6 KB
/
models.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# Model
from torch_geometric.nn import NNConv, GATConv, global_mean_pool
from torch_geometric.nn.norm import BatchNorm as GraphBatchNorm
from torch_geometric.graphgym.init import init_weights
import torch
import torch.nn as nn
import torch.nn.functional as F
class EdgeMLP(nn.Module):
def __init__(self, num_edge_features, input_node_features, output_node_features):
super(EdgeMLP, self).__init__()
# Define a sequential architecture
self.mlp = nn.Sequential(
nn.Linear(num_edge_features, 128),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, input_node_features * output_node_features)
)
# Weight initialization
self.apply(init_weights)
def forward(self, edge_attr):
x = self.mlp(edge_attr)
return x
class gnn_embedder(nn.Module):
def __init__(self, num_node_features, num_edge_features, hidden_channels):
super(gnn_embedder, self).__init__()
# Initialize the MLP for NNConv
self.edge_mlp = EdgeMLP(num_edge_features, num_node_features, hidden_channels[0])
# Encoder
self.conv1 = NNConv(num_node_features, hidden_channels[0], self.edge_mlp)
self.conv2 = GATConv(hidden_channels[0], hidden_channels[1], heads=1, concat=False)
# Projector
self.fc1 = nn.Linear(hidden_channels[1], hidden_channels[2])
self.fc2 = nn.Linear(hidden_channels[2], hidden_channels[3])
self.fc3 = nn.Linear(hidden_channels[3], hidden_channels[4])
# Weight initialization
self.apply(init_weights)
def forward(self, x, edge_index, edge_attr, batch):
# NNConv layer
x = F.relu(self.conv1(x, edge_index, edge_attr))
# GATConv layer
x = F.relu(self.conv2(x, edge_index))
# Global average pooling
x = global_mean_pool(x, batch) #<-- batch vector to keep track of graphs
# Fully connected layers
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
return x
class gnn_embedder2(nn.Module):
def __init__(self, num_node_features, num_edge_features, hidden_channels, batch_norm=True, dropout=True, p=0.1):
super(gnn_embedder2, self).__init__()
"""
The embedding architecture used in RP, TS, and VICRegT models. Comprised of an encoder module and projector module.
"""
# Initialize the MLP for NNConv
self.edge_mlp = EdgeMLP(num_edge_features, num_node_features, hidden_channels[0])
# Encoder
self.conv1 = NNConv(num_node_features, hidden_channels[0], self.edge_mlp)
self.conv2 = GATConv(hidden_channels[0], hidden_channels[1], heads=1, concat=False)
self.conv3 = GATConv(hidden_channels[1], hidden_channels[2], heads=1, concat=False)
# Dropout
self.dropout = dropout
if self.dropout:
self.net_dropout = nn.Dropout(p=p)
else:
self.net_dropout = nn.Identity()
# Projector
self.fc1 = nn.Linear(hidden_channels[2], hidden_channels[3])
self.fc2 = nn.Linear(hidden_channels[3], hidden_channels[4])
self.fc3 = nn.Linear(hidden_channels[4], hidden_channels[5])
if batch_norm:
# Batch Normalization for graph layers
self.bn_graph1 = GraphBatchNorm(hidden_channels[0])
self.bn_graph2 = GraphBatchNorm(hidden_channels[1])
self.bn_graph3 = GraphBatchNorm(hidden_channels[2])
# Batch Normalization for fully connected layers
self.bn1 = nn.BatchNorm1d(hidden_channels[3])
self.bn2 = nn.BatchNorm1d(hidden_channels[4])
else:
self.bn_graph1 = self.bn_graph2 = self.bn_graph3 = self.bn1 = self.bn2 = nn.Identity()
# Weight initialization
self.apply(init_weights)
def forward(self, x, edge_index, edge_attr, batch):
# NNConv layer
x = F.relu(self.bn_graph1(self.conv1(x, edge_index, edge_attr)))
# GATConv layers
x = F.relu(self.bn_graph2(self.conv2(x, edge_index)))
x = F.relu(self.bn_graph3(self.conv3(x, edge_index)))
# Global average pooling
x = global_mean_pool(x, batch) #<-- batch vector to keep track of graphs
# Fully connected layers
x = F.relu(self.bn1(self.fc1(x)))
x = self.net_dropout(x)
x = F.relu(self.bn2(self.fc2(x)))
x = self.net_dropout(x)
x = self.fc3(x)
return x
class relative_positioning(nn.Module):
def __init__(self, config):
super(relative_positioning, self).__init__()
num_node_features = config["num_node_features"]
num_edge_features = config["num_edge_features"]
hidden_channels = config["hidden_channels"]
# GNN embedder
self.embedder = gnn_embedder(num_node_features, num_edge_features, hidden_channels)
# Fully connected layers
self.fc = nn.Linear(hidden_channels[4], 1)
# Weight initialization
self.apply(init_weights)
def forward(self, batch, head="linear"):
# Graph embeddings
z1 = self.embedder(batch.x1, batch.edge_index1, batch.edge_attr1, batch.x1_batch)
z2 = self.embedder(batch.x2, batch.edge_index2, batch.edge_attr2, batch.x2_batch)
# Contrast the embeddings
z = torch.abs(z1 - z2)
# Linear or Logistic regression
z = self.fc(z)
if head == "sigmoid":
z = torch.sigmoid(z)
elif head == "linear":
pass
return z.squeeze(1)
class temporal_shuffling(nn.Module):
def __init__(self, config):
super(temporal_shuffling, self).__init__()
num_node_features = config["num_node_features"]
num_edge_features = config["num_edge_features"]
hidden_channels = config["hidden_channels"]
# GNN embedder
self.embedder = gnn_embedder(num_node_features, num_edge_features, hidden_channels)
# Fully connected layer
self.fc = nn.Linear(2 * hidden_channels[4], 1)
# Weight initialization
self.apply(init_weights)
def forward(self, batch, head="linear"):
# embedding for each graph
z1 = self.embedder(batch.x1, batch.edge_index1, batch.edge_attr1, batch.x1_batch)
z2 = self.embedder(batch.x2, batch.edge_index2, batch.edge_attr2, batch.x2_batch)
z3 = self.embedder(batch.x3, batch.edge_index3, batch.edge_attr3, batch.x3_batch)
# Contrast the embeddings
diff1 = torch.abs(z1 - z2)
diff2 = torch.abs(z2 - z3)
z = torch.cat((diff1, diff2), dim=1)
# Logistic regression
z = self.fc(z)
if head == "linear":
pass
elif head == "sigmoid":
z = torch.sigmoid(z)
return z.squeeze(1)
class Encoder1(nn.Module):
def __init__(self, config):
super(Encoder1, self).__init__()
hidden_channels = config["hidden_channels"]
# Initialize the MLP for NNConv
self.edge_mlp = EdgeMLP(config["num_edge_features"], config["num_node_features"], hidden_channels[0])
# Encoder
self.conv1 = NNConv(config["num_node_features"], hidden_channels[0], self.edge_mlp)
self.conv2 = GATConv(hidden_channels[0], hidden_channels[1], heads=1, concat=False)
self.conv3 = GATConv(hidden_channels[1], hidden_channels[2], heads=1, concat=False)
# Batch Normalization for graph layers
if config["batch_norm"]:
self.bn_graph1 = GraphBatchNorm(hidden_channels[0])
self.bn_graph2 = GraphBatchNorm(hidden_channels[1])
self.bn_graph3 = GraphBatchNorm(hidden_channels[2])
else:
self.bn_graph1 = self.bn_graph2 = self.bn_graph3 = nn.Identity()
def forward(self, batch):
# NNConv layer
x = F.relu(self.bn_graph1(self.conv1(batch.x, batch.edge_index, batch.edge_attr)))
# GATConv layers
x = F.relu(self.bn_graph2(self.conv2(x, batch.edge_index)))
x = F.relu(self.bn_graph3(self.conv3(x, batch.edge_index)))
# Global average pooling
x = global_mean_pool(x, batch.batch)
return x
class Classifier1(nn.Module):
def __init__(self, config):
super(Classifier1, self).__init__()
# If binary classification, output dimension is 1, if multiclass classification, output dimension is 3.
self.classify = config["classify"]
self.head = config["head"]
self.fc1 = nn.Linear(config["hidden_channels"][2], 1)
self.fc2 = nn.Linear(config["hidden_channels"][2], 3)
def forward(self, x):
# Classification mode
if self.classify=="binary":
x = self.fc1(x)
x = x.squeeze(1)
elif self.classify=="multiclass":
x = self.fc2(x)
# Prediction head
if self.head=="linear":
return x
elif self.head=="sigmoid":
return torch.sigmoid(x)
elif self.head=="softmax":
return torch.softmax(x, dim=1)
class supervised(nn.Module):
def __init__(self, config):
super(supervised, self).__init__()
self.encoder = Encoder1(config)
self.classifier = Classifier1(config)
def forward(self, batch):
x = self.encoder(batch)
x = self.classifier(x)
return x
class supervised_model(nn.Module):
def __init__(self, config):
super(supervised_model, self).__init__()
num_node_features = config["num_node_features"]
num_edge_features = config["num_edge_features"]
hidden_channels = config["hidden_channels"]
out_channels = config["out_channels"]
dropout = config["dropout"]
# Initialize the MLP for NNConv
self.edge_mlp = EdgeMLP(num_edge_features, num_node_features, hidden_channels)
# NNConv layer
self.conv1 = NNConv(num_node_features, hidden_channels, self.edge_mlp)
# GATConv layer
self.conv2 = GATConv(hidden_channels, hidden_channels, heads=1, concat=False)
# First fully connected layer
self.fc1 = nn.Linear(hidden_channels, out_channels)
# Dropout
self.dropout = nn.Dropout(p=dropout)
# Last fully connected layer
self.fc2 = nn.Linear(out_channels, 1)
self.fc3 = nn.Linear(out_channels, 3)
# Weight initialization
self.apply(init_weights)
def forward(self, batch, classify="binary", head="linear", dropout=True):
# ECC
x = F.relu(self.conv1(batch.x, batch.edge_index, batch.edge_attr))
# GAT
x = F.relu(self.conv2(x, batch.edge_index))
# Global average pooling
x = global_mean_pool(x, batch.batch)
# Fully connected layers
x = F.relu(self.fc1(x))
if dropout:
x = self.dropout(x)
if classify == "binary":
x = self.fc2(x)
x = x.squeeze(1)
if classify == "multiclass":
x = self.fc3(x)
if head == "linear":
return x
if head == "sigmoid":
return torch.sigmoid(x)
if head == "softmax":
return torch.softmax(x, dim=1)
class VICRegT1(nn.Module):
def __init__(self, config):
super(VICRegT1, self).__init__()
# GNN embedders
self.embedder = gnn_embedder2(config.num_node_features, config.num_edge_features, config.hidden_channels,
config.batch_norm, config.dropout, config.p)
# Weight initialization
self.apply(init_weights)
def forward(self, batch):
# Graph embeddings
z1 = self.embedder(batch.x1, batch.edge_index1, batch.edge_attr1, batch.x1_batch)
z2 = self.embedder(batch.x2, batch.edge_index2, batch.edge_attr2, batch.x2_batch)
return (z1, z2)
# Downstream models
def set_requires_grad(model, requires_grad=True):
"""
Set the requires_grad attribute for all parameters in the encoder (and classifier if needed) of the model.
Args:
model (downstream3): An instance of the downstream3 model.
requires_grad (bool): Whether the layers' parameters should require gradients (unfrozen) or not (frozen).
"""
# Set requires_grad for all parameters in the encoder
for param in model.encoder.parameters():
param.requires_grad = requires_grad
class downstream3(nn.Module):
class Encoder(nn.Module):
def __init__(self, pretrained_layers):
super(downstream3.Encoder, self).__init__()
# Graph layers
self.conv1 = pretrained_layers["conv1"]
self.conv2 = pretrained_layers["conv2"]
self.conv3 = pretrained_layers["conv3"]
# Batch normalization layers
self.bn_graph1 = pretrained_layers["bn_graph1"]
self.bn_graph2 = pretrained_layers["bn_graph2"]
self.bn_graph3 = pretrained_layers["bn_graph3"]
# Assign the pretrained EdgeMLP to the ECC layer
self.conv1.edge_mlp = pretrained_layers["edge_mlp"]
def forward(self, batch):
# ECC layer
x = F.relu(self.bn_graph1(self.conv1(batch.x, batch.edge_index, batch.edge_attr)))
# GATConv layers
x = F.relu(self.bn_graph2(self.conv2(x, batch.edge_index)))
x = F.relu(self.bn_graph3(self.conv3(x, batch.edge_index)))
# Global average pooling
x = global_mean_pool(x, batch.batch)
return x
class Classifier(nn.Module):
def __init__(self, node_dim, classify, head):
super(downstream3.Classifier, self).__init__()
# If binary classification, output dimension is 1, if multiclass classification, output dimension is 3.
self.classify = classify
self.head = head
self.fc1 = nn.Linear(node_dim, 1)
self.fc2 = nn.Linear(node_dim, 3)
def forward(self, x):
# Classification mode
if self.classify=="binary":
x = self.fc1(x)
x = x.squeeze(1)
elif self.classify=="multiclass":
x = self.fc2(x)
# Prediction head
if self.head=="linear":
return x
elif self.head=="sigmoid":
return torch.sigmoid(x)
elif self.head=="softmax":
return torch.softmax(x, dim=1)
def __init__(self, config, pretrained_layers={}, requires_grad=False):
"""
Downstream model for seizure detection (binary or multiclass). Trains a GNN encoder (frozen or unfrozen) and a simple nonlinear classifier ontop
(logistic regression or multinomial logistic regression) with frozen encoder or unfrozen encoder.
Args:
classify (str): Whether to perform binary or multiclass classification. Options: "binary" or "multiclass".
head (str): Whether to use a linear or nonlinear prediction head. Options: "linear", "sigmoid", or "softmax".
pretrained_layers (dict): Dictionary containing the pretrained layers.
requires_grad (bool): Whether to require gradients for pretrained layers. If True, the layers are unfrozen, if False the layers are frozen.
config (dict): Dictionary for configuration of the model. Not used in this model.
"""
super(downstream3, self).__init__()
# Initialize encoder and simple nonlinear classifier
self.encoder = downstream3.Encoder(pretrained_layers)
node_dim = self.encoder.conv3.out_channels
self.classifier = downstream3.Classifier(node_dim, config["classify"], config["head"])
# Freeze or unfreeze the encoder
set_requires_grad(self, requires_grad=requires_grad)
def forward(self, batch):
x = self.encoder(batch)
x = self.classifier(x)
return x
class CPC(nn.Module):
def __init__(self):
super(CPC, self).__init__()
# Deprecated
class downstream1(nn.Module):
def __init__(self, config, pretrained_layers, frozen=False):
super(downstream1, self).__init__()
"""
Downstream model for seizure detection (binary or multiclass). Retrains with a GNN embedder and adds additional layers (total: 2x ECC, 2x GAT).
Args:
config (dict): Dictionary containing the configuration of the model, containing hidden_channels which is a list of values of length 3, and
the dropout probability.
pretrained_layers (tuple): Tuple containing the pretrained layers.
frozen (bool): If True, the pretrained layers are frozen. If False, the pretrained layers are unfrozen.
"""
hidden_channels = config["hidden_channels"]
dropout = config["dropout"]
# Pretrained layers
EdgeMLP_pretrained, NNConv_pretrained, GATConv_pretrained = pretrained_layers
self.conv1 = NNConv_pretrained
self.conv2 = GATConv_pretrained
# Assign the pretrained MLP to the NNConv1
NNConv_pretrained.edge_mlp = EdgeMLP_pretrained
# Output feature dimensions of pretrained layers
num_node_features = GATConv_pretrained.out_channels
num_edge_features = EdgeMLP_pretrained.state_dict()['mlp.0.weight'].size()[1]
# Conditionally freeze or unfreeze pretrained layers
for param in EdgeMLP_pretrained.parameters():
param.requires_grad = not frozen
for param in NNConv_pretrained.parameters():
param.requires_grad = not frozen
for param in GATConv_pretrained.parameters():
param.requires_grad = not frozen
# Initialize the MLP for NNConv
self.edge_mlp2 = EdgeMLP(num_edge_features, num_node_features, hidden_channels[0])
# NNConv layer
self.conv3 = NNConv(num_node_features, hidden_channels[0], self.edge_mlp2)
# GATConv layer
self.conv4 = GATConv(hidden_channels[0], hidden_channels[1], heads=1, concat=False)
# First fully connected layer
self.fc1 = nn.Linear(hidden_channels[1], hidden_channels[2])
# Dropout
self.dropout = nn.Dropout(p=dropout)
# Last fully connected layer
self.fc2 = nn.Linear(hidden_channels[2], 1)
self.fc3 = nn.Linear(hidden_channels[2], 3)
# Weight initialization
self.apply(init_weights)
def forward(self, batch, classify="binary", head="linear", dropout=True):
# ECC 1
x = F.relu(self.conv1(batch.x, batch.edge_index, batch.edge_attr))
# GAT 1
x = F.relu(self.conv2(x, batch.edge_index))
# ECC 2
x = F.relu(self.conv3(x, batch.edge_index, batch.edge_attr))
# GAT 2
x = F.relu(self.conv4(x, batch.edge_index))
# Global average pooling
x = global_mean_pool(x, batch.batch)
# Fully connected layer
x = F.relu(self.fc1(x))
if dropout:
x = self.dropout(x)
# Classification mode
if classify == "binary":
x = self.fc2(x)
x = x.squeeze(1)
if classify == "multiclass":
x = self.fc3(x)
# Prediction head
if head == "linear":
return x
if head == "sigmoid":
return torch.sigmoid(x)
if head == "softmax":
return torch.softmax(x, dim=1)
class downstream2(nn.Module):
def __init__(self, config, pretrained_layers, frozen=False):
"""
Downstream model for seizure detection (binary or multiclass). Retrains the entire GNN embedder and does not add any additional layers.
Args:
config (dict): Dictionary containing the configuration of the model, containing hidden_channels which is a single value, and the dropout probability.
pretrained_layers (tuple): Tuple containing the pretrained layers.
frozen (bool): If True, the pretrained layers are frozen. If False, the pretrained layers are unfrozen.
"""
super(downstream2, self).__init__()
hidden_channels = config["hidden_channels"]
dropout = config["dropout"]
# Pretrained layers
EdgeMLP_pretrained, NNConv_pretrained, GATConv_pretrained = pretrained_layers
self.conv1 = NNConv_pretrained
self.conv2 = GATConv_pretrained
# Assign the pretrained MLP to the NNConv1
NNConv_pretrained.edge_mlp = EdgeMLP_pretrained
# Output feature dimensions of pretrained layers
num_node_features = GATConv_pretrained.out_channels
num_edge_features = EdgeMLP_pretrained.state_dict()['mlp.0.weight'].size()[1]
# Conditionally freeze or unfreeze pretrained layers
for param in EdgeMLP_pretrained.parameters():
param.requires_grad = not frozen
for param in NNConv_pretrained.parameters():
param.requires_grad = not frozen
for param in GATConv_pretrained.parameters():
param.requires_grad = not frozen
# First fully connected layer
self.fc1 = nn.Linear(num_node_features, hidden_channels)
# Dropout
self.dropout = nn.Dropout(p=dropout)
# Last fully connected layer
self.fc2 = nn.Linear(hidden_channels, 1)
self.fc3 = nn.Linear(hidden_channels, 3)
# Weight initialization
self.apply(init_weights)
def forward(self, batch, classify="binary", head="linear", dropout=True):
# ECC
x = F.relu(self.conv1(batch.x, batch.edge_index, batch.edge_attr))
# GAT
x = F.relu(self.conv2(x, batch.edge_index))
# Global average pooling
x = global_mean_pool(x, batch.batch)
# Fully connected layers
x = F.relu(self.fc1(x))
if dropout:
x = self.dropout(x)
# Classification mode
if classify=="binary":
x = self.fc2(x)
x = x.squeeze(1)
if classify=="multiclass":
x = self.fc3(x)
# Prediction head
if head=="linear":
return x
if head=="sigmoid":
return torch.sigmoid(x)
if head=="softmax":
return torch.softmax(x, dim=1)