-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcn.py
executable file
·136 lines (123 loc) · 4.89 KB
/
gcn.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
# <gcn.py>
#
# Implementation calls of GCN classifier through torch-geometric.
#
# @Authors and Contributors:
# Lucas Pascotti Valem <lucas.valem@unesp.br>
# João Gabriel Camacho Presotto <joaopresotto@gmail.com>
# Nikolas Gomes de Sá <NIKOLAS567@hotmail.com>
# Daniel Carlos Guimarães Pedronette <daniel.pedronette@unesp.br>
#
# ------------------------------------------------------------------------------
#
# This file is part of Weakly Supervised Experiments Framework (WSEF).
# Official Repository: https://github.com/UDLF/WSEF
#
# WSEF is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# WSEF is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with WSEF. If not, see <http://www.gnu.org/licenses/>.
#
# ------------------------------------------------------------------------------
import torch
from torch_geometric.data import Data
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
class Net(torch.nn.Module):
def __init__(self, pNFeatures, pNNeurons, numberOfClasses):
super(Net, self).__init__()
self.conv1 = GCNConv(pNFeatures, pNNeurons) #dataset.num_node_features
self.conv2 = GCNConv(pNNeurons, numberOfClasses) #dataset.num_classes
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = F.relu(x)
x = F.dropout(x, training=self.training)
x = self.conv2(x, edge_index)
return F.log_softmax(x, dim=1)
class GCNClassifier():
def __init__(self, gcn_type, rks, pN, number_neighbors=40):
# Parameters
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.pK = number_neighbors
self.pN = pN
self.rks = rks
self.pLR = 0.001
self.pNNeurons = 32
self.pNEpochs = 50
self.gcn_type = gcn_type
def fit(self, test_index, train_index, features, labels):
# masks
print('Creating masks ...')
self.train_mask = []
self.val_mask = []
self.test_mask = []
self.train_size = len(train_index)
self.test_size = len(test_index)
self.train_mask = [False for i in range(self.pN)]
self.val_mask = [False for i in range(self.pN)]
self.test_mask = [False for i in range(self.pN)]
for index in train_index:
self.train_mask[index] = True
for index in test_index:
self.test_mask[index] = True
self.train_mask = torch.tensor(self.train_mask)
self.val_mask = torch.tensor(self.val_mask)
self.test_mask = torch.tensor(self.test_mask)
# labels
print('Set labels ...')
y = labels
self.numberOfClasses = max(y)+1
self.y = torch.tensor(y).to(self.device)
# features
self.x = torch.tensor(features).to(self.device)
self.pNFeatures = len(features[0])
# build graph
self.create_graph()
def create_graph(self):
print('Making edge list ...')
self.top_k = self.pK
# compute traditional knn graph
edge_index = []
for img1 in range(len(self.rks)):
for pos in range(self.top_k):
img2 = self.rks[img1][pos]
edge_index.append([img1, img2])
edge_index = torch.tensor(edge_index)
# convert to torch format
self.edge_index = edge_index.t().contiguous().to(self.device)
def predict(self):
# data object
print('Loading data object...')
data = Data(x=self.x.float(),
edge_index=self.edge_index,
y=self.y,
test_mask=self.test_mask,
train_mask=self.train_mask,
val_mask=self.val_mask)
# TRAIN MODEL #
model = Net(self.pNFeatures, self.pNNeurons, self.numberOfClasses).to(self.device)
optimizer = torch.optim.Adam(model.parameters(), lr=self.pLR, weight_decay=5e-4)
print('Training...')
model.train()
for epoch in range(self.pNEpochs):
print("Training epoch: ", epoch)
optimizer.zero_grad()
out = model(data)
data.y = torch.tensor(data.y, dtype=torch.long)
loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
loss.backward()
optimizer.step()
# MODEL EVAL #
model.eval()
_, pred = model(data).max(dim=1)
pred = torch.masked_select(pred, data.test_mask)
return pred.tolist()