-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostprocess.py
349 lines (306 loc) · 15.5 KB
/
postprocess.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
import json
import gc
import os
import argparse
import numpy as np
import utils
from tqdm import tqdm
from vis import visualize, attention_interpolation
eps = 1e-15
parser = argparse.ArgumentParser()
parser.add_argument('--input_dir', type=str, default="/data/logs/gqa/gqa_spatial_baseline", help="dir path to prediction files.")
parser.add_argument('--out_dir', type=str, default="/data/logs/gqa/gqa_spatial_baseline", help="dir path to save output files")
parser.add_argument('--data_dir', type=str, default="/data/visualreasoning/mac-with-data/mac-network-gqa-capsules", help='path to data needed to process output')
parser.add_argument('--exp_name', type=str, default="gqaExperiment-Spatial")
parser.add_argument('--featureType', type=str, default="spatial", help="'spatial' for spatial features, 'object'")
parser.add_argument('--tier', default = "val", type = str, help = "Tier, e.g. train, val")
parser.add_argument('--scenes', default="{tier}_sceneGraphs.json", type = str, help = "Scene graphs file name format.")
parser.add_argument('--predictions', default="{tier}_predictions.json", type = str, help = "Answers file name format.")
parser.add_argument('--attentions', default="{tier}_attentions.json", type = str, help = "Attentions file name format.")
parser.add_argument('--attnStep', default="last", type = str, choices=['mean', 'last', 'all'], help = "From which reasoing step, want to save attention")
parser.add_argument('--objdata_dir', type=str, default="/data/visualreasoning/mac-with-data/mac-network-gqa-capsules/mac-network-w-capsules/data/")
parser.add_argument('--dataset', type=str, default="gqa", choices=['gqa', 'clevr'])
parser.add_argument('--chunk_size', type=int, default=20000, help='chunk size to process prediction bboxes into chunks--useful to avoid OOM')
parser.add_argument('--grounding', action="store_true", help = "True to compute grounding score (If model uses attention).")
args = parser.parse_args()
if args.dataset == 'gqa':
h,w = 7, 7
elif args.dataset == 'clevr':
h,w = 14, 14
else:
print('dataset not supported.')
def loadFile(name):
# load standard json file
if os.path.isfile(name):
with open(name) as file:
data = json.load(file)
# load file chunks if too big
elif os.path.isdir(name.split(".")[0]):
data = {}
chunks = glob.glob('{dir}/{dir}_*.{ext}'.format(dir = name.split(".")[0], ext = name.split(".")[1]))
for chunk in chunks:
with open(chunk) as file:
data.update(json.load(file))
else:
raise Exception("Can't find {}".format(name))
return data
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
# Load scene graphs
print("Loading scene graphs...")
scenes = loadFile(os.path.join(args.data_dir, 'sceneGraphs',args.scenes.format(tier = args.tier)))
in_path = os.path.join(args.input_dir, args.exp_name)
save_path = os.path.join(args.out_dir, args.exp_name)
os.makedirs(os.path.join(save_path, args.attnStep), exist_ok=True)
pred_file = args.tier+'Predictions-'+args.exp_name+'.json'
print('Loading predictions from {} at path {}...'.format(pred_file, in_path))
preds = utils.load_json(os.path.join(in_path, pred_file))
print('loaded preditions successfully!')
print('processing prediction file for evaluation script...')
def getRegion(obj, img_w, img_h):
x0 = float(obj["x"]) / img_w
y0 = float(obj["y"]) / img_h
x1 = float(obj["x"] + obj["w"]) / img_w
y1 = float(obj["y"] + obj["h"]) / img_h
return x0, y0, x1, y1
def get_ts2detections_gqa(pred):
img_id = pred['imageId']['id']
img_w = scenes[img_id]['width']
img_h = scenes[img_id]['height']
qid = pred['questionId']
question = pred['questionStr']
answer = pred["answer"]
im_path = os.path.join(args.data_dir, 'images', img_id + '.jpg')
attns = pred['attentions']['kb']
max_step = len(attns)
attns = [np.array(attn).reshape((7, 7)) for attn in attns]
attns = np.stack(attns, axis=0)
t2detections = visualize(max_step=max_step, im_path=im_path, attns=attns, question=question, answer=answer, h=img_h,
w=img_w)
return qid, t2detections
def get_grounding_result_gqa(pred_lst):
# global qid2t2detections
qid2t2detections = {}
results = [get_ts2detections_gqa(pred) for pred in tqdm(pred_lst)] #list of tuples (qid, t2detection)
qid2t2detections = {tup[0]:tup[1] for tup in results}
# for pred in tqdm(preds):
# get_ts2detections(pred)
# print(len(qid2t2detections.items()))
# utils.save_json(qid2t2detections, os.path.join(save_path,
# 'grounding_results_corrected_{tier}_0.5_vqa_gt_{chunk}.json'.format(
# tier=args.tier, chunk=chunk)))
# qid2t2detections = []
# results = []
return qid2t2detections
def get_ts2detections_clevr(pred):
img_id = pred['imageId']['id']
img_w = 480
img_h = 320
qid = pred['questionId']
question = pred['questionStr']
answer = pred["answer"]
im_path = os.path.join(args.data_dir, 'images', args.tier, img_id + '.png')
attns = pred['attentions']['kb']
max_step = len(attns)
attns = [np.array(attn).reshape((14, 14)) for attn in attns]
attns = np.stack(attns, axis=0)
t2detections = visualize(max_step=max_step, im_path=im_path, attns=attns, question=question, answer=answer, h=img_h,
w=img_w)
return qid, t2detections
def get_grounding_result_clevr(pred_lst):
# global qid2t2detections
qid2t2detections = {}
results = [get_ts2detections_clevr(pred) for pred in tqdm(pred_lst)] #list of tuples (qid, t2detection)
qid2t2detections = {tup[0]:tup[1] for tup in results}
# for pred in tqdm(preds):
# get_ts2detections(pred)
# print(len(qid2t2detections.items()))
# utils.save_json(qid2t2detections, os.path.join(save_path,
# 'grounding_results_corrected_{tier}_0.5_vqa_gt_{chunk}.json'.format(
# tier=args.tier, chunk=chunk)))
# qid2t2detections = []
# results = []
return qid2t2detections
if args.featureType == 'spatial':
spatial_attn = []
predictions = []
if args.attnStep in ['mean', 'last']:
for pred in tqdm(preds):
if args.grounding:
if args.attnStep == 'last':
attn = np.array(pred['attentions']['kb'][-1]).reshape((h, w)).tolist()
elif args.attnStep == 'mean':
# print(np.shape(np.mean(np.array(pred['attentions']['kb']), axis=0)))
attn = np.mean(np.array(pred['attentions']['kb']), axis=0).reshape((7, 7)).tolist()
spatial_attn.append(
{
'questionId': pred['questionId'],
'attention': attn
}
)
predictions.append(
{
'questionId': pred['questionId'],
'prediction': pred['prediction']
}
)
utils.save_json(spatial_attn, os.path.join(save_path, args.attnStep, args.attentions.format(tier=args.tier)))
utils.save_json(predictions, os.path.join(save_path, args.attnStep, args.predictions.format(tier=args.tier)))
else:
if args.attnStep == 'all':
num_steps = len(preds[0]['attentions']['kb'])
for t in tqdm(range(num_steps)):
spatial_attn = []
predictions = []
for pred in preds:
if args.grounding:
spatial_attn.append(
{
'questionId': pred['questionId'],
'attention': np.array(pred['attentions']['kb'][t]).reshape((h,w)).tolist()
}
)
predictions.append(
{
'questionId': pred['questionId'],
'prediction': pred['prediction']
}
)
os.makedirs(os.path.join(save_path, args.attnStep, t), exist_ok=True)
utils.save_json(spatial_attn, os.path.join(save_path, args.attnStep, t, args.attentions.format(tier=args.tier)))
utils.save_json(predictions, os.path.join(save_path, args.attnStep, t, args.predictions.format(tier=args.tier)))
del spatial_attn, predictions
else:
print('unknown argument: {}'.format(args.attnStep))
exit()
##################### post process for object detections #######################
print('done with processing files and saved!\n ')
if args.grounding:
print('Now get attention maps for object detections...')
if args.dataset == 'gqa':
#read questions file to get img width and height
#divide preds file in chunks
preds_list = list(chunks(preds, args.chunk_size))
qid2t2detections_full = {}
for chunk, pred_lst in enumerate(preds_list):
qid2t2detections = get_grounding_result_gqa(pred_lst)
qid2t2detections_full.update(qid2t2detections)
print('processed grouding results for chunk {}/{}'.format(chunk, len(preds_list)))
print('saving grounding results..')
utils.save_json(qid2t2detections_full, os.path.join(save_path,
'grounding_results_corrected_{tier}_0.5_vqa_gt.json'.format(
tier=args.tier)))
print('saved grouding results successfully to {}!'.format(save_path))
elif args.dataset == 'clevr':
preds_list = list(chunks(preds, args.chunk_size))
qid2t2detections_full = {}
for chunk, pred_lst in enumerate(preds_list):
qid2t2detections = get_grounding_result_clevr(pred_lst)
qid2t2detections_full.update(qid2t2detections)
print('processed grouding results for chunk {}/{}'.format(chunk, len(preds_list)))
print('saving grounding results..')
utils.save_json(qid2t2detections_full, os.path.join(save_path,
'grounding_results_corrected_{tier}_0.5_vqa_gt.json'.format(
tier=args.tier)))
print('saved grouding results successfully to {}!'.format(save_path))
else:
print("ERROR: invalid dataset. Select from allowed choices: 'gqa' or 'clevr'. ")
elif args.featureType == 'object':
print('reading obj info..')
#read obj info file
obj_info_dir = os.path.join(args.objdata_dir, 'gqa_objects_merged_info_with_bboxes.json')
if os.path.isfile(obj_info_dir):
obj_info = utils.load_json(obj_info_dir)
else:
print('file not found, loading obj feat and info file to obtain bboxes...')
import h5py
#read obj feat file and obj info file and merge boxes
h = h5py.File(
os.path.join(args.objdata_dir, 'gqa_objects.h5'),
'r')
bboxes = h['bboxes']
obj_info = utils.load_json(
os.path.join(args.objdata_dir, 'gqa_objects_merged_info.json'))
for key, value in tqdm(obj_info.items()):
obj_info[key]['bboxes'] = bboxes[obj_info[key]['index']].tolist()
utils.save_json(obj_info,
os.path.join(args.objdata_dir, 'gqa_objects_merged_info_with_bboxes.json')
)
print('merged and saved obj info file.')
if args.attnStep in ['mean', 'last']:
spatial_attn = []
predictions = []
for pred in tqdm(preds):
if args.grounding:
if args.attnStep == 'last':
attn = np.array(pred['attentions']['kb'][-1][:pred['objectsNum']]).tolist()
elif args.attnStep == 'mean':
attn = np.mean(np.array(pred['attentions']['kb'][:][:pred['objectsNum']]), axis=-1).tolist()
pred_bboxes = obj_info[pred['imageId']['id']]['bboxes']
width, height = obj_info[pred['imageId']['id']]['width'], obj_info[pred['imageId']['id']]['height']
pboxes_normalized = []
for pbox in pred_bboxes:
obj = {
'x': pbox[0],
'y': pbox[1],
'w': pbox[2],
'h': pbox[3]
}
pboxes_normalized.append(getRegion(obj, width, height))
# pbox = [pb+eps for pb in pbox if pb==0.0]
attn_with_boxes = [[*bb, atn] for bb, atn in zip(pboxes_normalized, attn)]
spatial_attn.append(
{
'questionId': pred['questionId'],
'attention': attn_with_boxes
}
)
predictions.append(
{
'questionId': pred['questionId'],
'prediction': pred['prediction']
}
)
utils.save_json(spatial_attn, os.path.join(save_path, args.attnStep, args.attentions.format(tier=args.tier)))
utils.save_json(predictions, os.path.join(save_path, args.attnStep, args.predictions.format(tier=args.tier)))
else:
if args.attnStep == 'all':
num_steps = len(preds[0]['attentions']['kb'])
for t in range(num_steps):
spatial_attn = []
predictions = []
for pred in preds:
attn = np.array(pred['attentions']['kb'][t][:pred['objectsNum']]).tolist()
pred_bboxes = obj_info[pred['imageId']['id']]['bboxes']
#normalize bounding boxes
width, height = obj_info[pred['imageId']['id']]['width'], obj_info[pred['imageId']['id']]['height']
for pbox in pred_bboxes:
pbox[2] = pbox[0] + pbox[2] # x+w
pbox[3] = pbox[1] + pbox[3] # y+h
pbox[0] /= width # x0
pbox[2] /= width # x1
pbox[1] /= height # y0
pbox[3] /= height # y1
# pbox = [pb + eps for pb in pbox if pb == 0.0]
attn_with_boxes = [[*bb, atn] for bb, atn in zip(pred_bboxes, attn)]
spatial_attn.append(
{
'questionId': pred['questionId'],
'attention': attn_with_boxes
}
)
predictions.append(
{
'questionId': pred['questionId'],
'prediction': pred['prediction']
}
)
os.makedirs(os.path.join(save_path, args.attnStep, t), exist_ok=True)
utils.save_json(spatial_attn, os.path.join(save_path, args.attnStep, t, args.attentions.format(tier=args.tier)))
utils.save_json(predictions, os.path.join(save_path, args.attnStep, t, args.predictions.format(tier=args.tier)))
del spatial_attn, predictions
else:
print('unknown argument: {}'.format(args.attnStep))
exit()