-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphVQA.patch
323 lines (266 loc) · 13.5 KB
/
GraphVQA.patch
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
diff --git a/Constants.py b/Constants.py
index 6eb7bd5..15a4d85 100755
--- a/Constants.py
+++ b/Constants.py
@@ -10,7 +10,7 @@ import numpy as np
# directory constants
-ROOT_DIR = pathlib.Path('/home/weixin/neuralPoolTest/')
+ROOT_DIR = pathlib.Path('/gqavis/server')
# specials = list(OrderedDict.fromkeys(
# tok for tok in [self.unk_token, self.pad_token, self.init_token,
diff --git a/baseline_and_test_models/pipeline_model_gcn.py b/baseline_and_test_models/pipeline_model_gcn.py
index cdb092b..472dab4 100644
--- a/baseline_and_test_models/pipeline_model_gcn.py
+++ b/baseline_and_test_models/pipeline_model_gcn.py
@@ -9,9 +9,9 @@ from torch.nn import Sequential as Seq, Linear as Lin, ReLU
from torch_scatter import scatter_mean, scatter_add
import logging
import torch_geometric
-from gqa_dataset_entry import GQATorchDataset
+from GraphVQA.gqa_dataset_entry import GQATorchDataset
-from graph_utils import my_graph_layernorm
+from GraphVQA.graph_utils import my_graph_layernorm
import torch.nn.functional as F
@@ -181,7 +181,7 @@ class MyConditionalGlobalAttention(torch.nn.Module):
gate = torch_geometric.utils.softmax(gate, batch, num_nodes=size)
out = scatter_add(gate * x, batch, dim=0, dim_size=size)
- return out
+ return (out, gate.squeeze().tolist())
def __repr__(self):
return '{}(gate_nn={}, node_nn={}, ques_nn={})'.format(self.__class__.__name__,
@@ -556,7 +556,7 @@ class TransformerQuestionEncoder(torch.nn.Module):
class GroundTruth_SceneGraph_Encoder(torch.nn.Module):
def __init__(self):
super(GroundTruth_SceneGraph_Encoder, self).__init__()
- from gqa_dataset_entry import GQA_gt_sg_feature_lookup
+ from GraphVQA.gqa_dataset_entry import GQA_gt_sg_feature_lookup
sg_TEXT = GQA_gt_sg_feature_lookup.SG_ENCODING_TEXT
sg_vocab = GQA_gt_sg_feature_lookup.SG_ENCODING_TEXT.vocab
@@ -860,7 +860,7 @@ class PipelineModel(torch.nn.Module):
# (batch_size, channels)
##################################
global_language_feature = questions_encoded[0] # should be changed when completing NEM
- graph_final_feature = self.graph_global_attention_pooling(
+ graph_final_feature, graph_gate = self.graph_global_attention_pooling(
x = x_executed, # x=x_encoded,
u = global_language_feature,
batch = gt_scene_graphs.batch,
@@ -881,7 +881,7 @@ class PipelineModel(torch.nn.Module):
- return programs_output, short_answer_logits
+ return programs_output, short_answer_logits, graph_gate, [], []
def load_state_dict(self, state_dict, strict=True):
model_dict = self.state_dict()
diff --git a/baseline_and_test_models/pipeline_model_gine.py b/baseline_and_test_models/pipeline_model_gine.py
index 9451c61..a51dff4 100644
--- a/baseline_and_test_models/pipeline_model_gine.py
+++ b/baseline_and_test_models/pipeline_model_gine.py
@@ -9,9 +9,9 @@ from torch.nn import Sequential as Seq, Linear as Lin, ReLU
from torch_scatter import scatter_mean, scatter_add
import logging
import torch_geometric
-from gqa_dataset_entry import GQATorchDataset
+from GraphVQA.gqa_dataset_entry import GQATorchDataset
-from graph_utils import my_graph_layernorm
+from GraphVQA.graph_utils import my_graph_layernorm
import torch.nn.functional as F
@@ -181,7 +181,7 @@ class MyConditionalGlobalAttention(torch.nn.Module):
gate = torch_geometric.utils.softmax(gate, batch, num_nodes=size)
out = scatter_add(gate * x, batch, dim=0, dim_size=size)
- return out
+ return (out, gate.squeeze().tolist())
def __repr__(self):
return '{}(gate_nn={}, node_nn={}, ques_nn={})'.format(self.__class__.__name__,
@@ -556,7 +556,7 @@ class TransformerQuestionEncoder(torch.nn.Module):
class GroundTruth_SceneGraph_Encoder(torch.nn.Module):
def __init__(self):
super(GroundTruth_SceneGraph_Encoder, self).__init__()
- from gqa_dataset_entry import GQA_gt_sg_feature_lookup
+ from GraphVQA.gqa_dataset_entry import GQA_gt_sg_feature_lookup
sg_TEXT = GQA_gt_sg_feature_lookup.SG_ENCODING_TEXT
sg_vocab = GQA_gt_sg_feature_lookup.SG_ENCODING_TEXT.vocab
@@ -864,7 +864,7 @@ class PipelineModel(torch.nn.Module):
# (batch_size, channels)
##################################
global_language_feature = questions_encoded[0] # should be changed when completing NEM
- graph_final_feature = self.graph_global_attention_pooling(
+ graph_final_feature, graph_gate = self.graph_global_attention_pooling(
x = x_executed, # x=x_encoded,
u = global_language_feature,
batch = gt_scene_graphs.batch,
@@ -885,7 +885,7 @@ class PipelineModel(torch.nn.Module):
- return programs_output, short_answer_logits
+ return programs_output, short_answer_logits, graph_gate, [], []
def load_state_dict(self, state_dict, strict=True):
model_dict = self.state_dict()
diff --git a/baseline_and_test_models/pipeline_model_lcgn.py b/baseline_and_test_models/pipeline_model_lcgn.py
index 29c8a57..1730c60 100644
--- a/baseline_and_test_models/pipeline_model_lcgn.py
+++ b/baseline_and_test_models/pipeline_model_lcgn.py
@@ -9,11 +9,11 @@ from torch.nn import Sequential as Seq, Linear as Lin, ReLU
from torch_scatter import scatter_mean, scatter_add
import logging
import torch_geometric
-from gqa_dataset_entry import GQATorchDataset
+from GraphVQA.gqa_dataset_entry import GQATorchDataset
-from graph_utils import my_graph_layernorm
+from GraphVQA.graph_utils import my_graph_layernorm
-from lcgn import lcgn_seq # use second version of gat
+from GraphVQA.baseline_and_test_models.lcgn import lcgn_seq # use second version of gat
"""
Graph Meta Layer, Example funciton
"""
@@ -178,7 +178,7 @@ class MyConditionalGlobalAttention(torch.nn.Module):
gate = torch_geometric.utils.softmax(gate, batch, num_nodes=size)
out = scatter_add(gate * x, batch, dim=0, dim_size=size)
- return out
+ return (out, gate.squeeze().tolist())
def __repr__(self):
return '{}(gate_nn={}, node_nn={}, ques_nn={})'.format(self.__class__.__name__,
@@ -553,7 +553,7 @@ class TransformerQuestionEncoder(torch.nn.Module):
class GroundTruth_SceneGraph_Encoder(torch.nn.Module):
def __init__(self):
super(GroundTruth_SceneGraph_Encoder, self).__init__()
- from gqa_dataset_entry import GQA_gt_sg_feature_lookup
+ from GraphVQA.gqa_dataset_entry import GQA_gt_sg_feature_lookup
sg_TEXT = GQA_gt_sg_feature_lookup.SG_ENCODING_TEXT
sg_vocab = GQA_gt_sg_feature_lookup.SG_ENCODING_TEXT.vocab
@@ -797,7 +797,7 @@ class PipelineModel(torch.nn.Module):
# (batch_size, channels)
##################################
global_language_feature = questions_encoded[0] # should be changed when completing NEM
- graph_final_feature = self.graph_global_attention_pooling(
+ graph_final_feature, graph_gate = self.graph_global_attention_pooling(
x = x_executed, # x=x_encoded,
u = global_language_feature,
batch = gt_scene_graphs.batch,
@@ -818,7 +818,7 @@ class PipelineModel(torch.nn.Module):
- return programs_output, short_answer_logits
+ return programs_output, short_answer_logits, graph_gate, [], []
def load_state_dict(self, state_dict, strict=True):
model_dict = self.state_dict()
diff --git a/gat_skip.py b/gat_skip.py
index 4c20e3f..8ec44f9 100644
--- a/gat_skip.py
+++ b/gat_skip.py
@@ -249,6 +249,7 @@ class gat_seq(torch.nn.Module):
def forward(self, x, edge_index, edge_attr, instr_vectors, batch):
num_conv_layers = len(self.convs)
+ attention_weights_list = []
h = x
for i in range(num_conv_layers):
@@ -266,9 +267,12 @@ class gat_seq(torch.nn.Module):
# feed into the GAT:
- conv_res = self.convs[i](x=x_cat, edge_index=edge_index, edge_attr=edge_cat)
+ conv_res, attention_weights = self.convs[i](x=x_cat, edge_index=edge_index, edge_attr=edge_cat, return_attention_weights=True)
h = conv_res + h # skip connection
+ edge_idx, attention_values = attention_weights
+ attention_weights_list.append(attention_values)
+
# do BN, ReLU, Droupout in-between all conv layers
if i != num_conv_layers-1:
h = self.bns[i](h)
@@ -276,5 +280,5 @@ class gat_seq(torch.nn.Module):
h = F.dropout(h, p=self.dropout, training=self.training)
- return h # return the last layer's hidden rep.
+ return (h, (edge_idx, attention_weights_list)) # return the last layer's hidden rep.
diff --git a/gqa_dataset_entry.py b/gqa_dataset_entry.py
index d1949af..8fa7115 100644
--- a/gqa_dataset_entry.py
+++ b/gqa_dataset_entry.py
@@ -10,7 +10,7 @@ import pickle
import pathlib
import json
import torch
-import Constants
+import GraphVQA.Constants as Constants
import numpy as np
import torch_geometric
import torchtext
@@ -27,7 +27,7 @@ import torchtext
ROOT_DIR = Constants.ROOT_DIR
-SCENEGRAPHS = ROOT_DIR.joinpath('GraphVQA', 'sceneGraphs') # SCENEGRAPHS = ROOT_DIR / 'Downloads' / 'sceneGraphs'
+SCENEGRAPHS = ROOT_DIR.joinpath('dataset', 'scenegraphs') # SCENEGRAPHS = ROOT_DIR / 'Downloads' / 'sceneGraphs'
EXPLAINABLE_GQA_DIR = ROOT_DIR.joinpath('GraphVQA')
SPLIT_TO_H5_PATH_TABLE = {
@@ -154,7 +154,7 @@ class GQA_gt_sg_feature_lookup:
tmp_text_list += load_str_list(ROOT_DIR / 'GraphVQA/meta_info/attr_gqa.txt')
tmp_text_list += load_str_list(ROOT_DIR / 'GraphVQA/meta_info/rel_gqa.txt')
- import Constants
+ import GraphVQA.Constants
tmp_text_list += Constants.OBJECTS_INV + Constants.RELATIONS_INV + Constants.ATTRIBUTES_INV
tmp_text_list.append("<self>") # add special token for self-connection
tmp_text_list = [ tmp_text_list ]
diff --git a/pipeline_model_gat.py b/pipeline_model_gat.py
index aa431f2..e2cf13a 100644
--- a/pipeline_model_gat.py
+++ b/pipeline_model_gat.py
@@ -9,11 +9,11 @@ from torch.nn import Sequential as Seq, Linear as Lin, ReLU
from torch_scatter import scatter_mean, scatter_add
import logging
import torch_geometric
-from gqa_dataset_entry import GQATorchDataset
+from GraphVQA.gqa_dataset_entry import GQATorchDataset
-from graph_utils import my_graph_layernorm
+from GraphVQA.graph_utils import my_graph_layernorm
-from gat_skip import gat_seq # use second version of gat
+from GraphVQA.gat_skip import gat_seq # use second version of gat
"""
Graph Meta Layer, Example funciton
"""
@@ -178,7 +178,7 @@ class MyConditionalGlobalAttention(torch.nn.Module):
gate = torch_geometric.utils.softmax(gate, batch, num_nodes=size)
out = scatter_add(gate * x, batch, dim=0, dim_size=size)
- return out
+ return (out, gate.squeeze().tolist())
def __repr__(self):
return '{}(gate_nn={}, node_nn={}, ques_nn={})'.format(self.__class__.__name__,
@@ -553,7 +553,7 @@ class TransformerQuestionEncoder(torch.nn.Module):
class GroundTruth_SceneGraph_Encoder(torch.nn.Module):
def __init__(self):
super(GroundTruth_SceneGraph_Encoder, self).__init__()
- from gqa_dataset_entry import GQA_gt_sg_feature_lookup
+ from GraphVQA.gqa_dataset_entry import GQA_gt_sg_feature_lookup
sg_TEXT = GQA_gt_sg_feature_lookup.SG_ENCODING_TEXT
sg_vocab = GQA_gt_sg_feature_lookup.SG_ENCODING_TEXT.vocab
@@ -788,8 +788,8 @@ class PipelineModel(torch.nn.Module):
# x_executed = self.gat_seq(x=x_cat, edge_index=gt_scene_graphs.edge_index, edge_attr=edge_cat)
- x_executed = self.gat_seq(x=x_encoded, edge_index=gt_scene_graphs.edge_index, edge_attr=edge_attr_encoded, instr_vectors=instr_vectors, batch=gt_scene_graphs.batch)
-
+ x_executed, edge_info_packed = self.gat_seq(x=x_encoded, edge_index=gt_scene_graphs.edge_index, edge_attr=edge_attr_encoded, instr_vectors=instr_vectors, batch=gt_scene_graphs.batch)
+ edge_index, attention_weights_list = edge_info_packed
##################################
@@ -797,7 +797,7 @@ class PipelineModel(torch.nn.Module):
# (batch_size, channels)
##################################
global_language_feature = questions_encoded[0] # should be changed when completing NEM
- graph_final_feature = self.graph_global_attention_pooling(
+ graph_final_feature, graph_gate = self.graph_global_attention_pooling(
x = x_executed, # x=x_encoded,
u = global_language_feature,
batch = gt_scene_graphs.batch,
@@ -818,7 +818,7 @@ class PipelineModel(torch.nn.Module):
- return programs_output, short_answer_logits
+ return programs_output, short_answer_logits, graph_gate, edge_index, attention_weights_list
def load_state_dict(self, state_dict, strict=True):
model_dict = self.state_dict()
diff --git a/questions/testdev_balanced_programs.json b/questions/testdev_balanced_programs.json
new file mode 100644
index 0000000..fe51488
--- /dev/null
+++ b/questions/testdev_balanced_programs.json
@@ -0,0 +1 @@
+[]
diff --git a/questions/train_balanced_programs.json b/questions/train_balanced_programs.json
new file mode 100644
index 0000000..fe51488
--- /dev/null
+++ b/questions/train_balanced_programs.json
@@ -0,0 +1 @@
+[]
diff --git a/questions/val_balanced_programs.json b/questions/val_balanced_programs.json
new file mode 100644
index 0000000..fe51488
--- /dev/null
+++ b/questions/val_balanced_programs.json
@@ -0,0 +1 @@
+[]