forked from m-srk/SGAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_spice_sg.py
277 lines (236 loc) · 7.54 KB
/
process_spice_sg.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
import json
import numpy as np
import os
from models.ass_fun import *
root_path = '/home/yangxu/project/self-critical.pytorch/'
#root_path = '/home/yangsheng/project/self-critical.pytorch/'
train_sg_path = root_path + 'data/spice_sg_train.json'
val_sg_path = root_path + 'data/spice_sg_val.json'
spice_sg_folder = root_path + 'data/coco_spice_sg2/'
spice_sg_dict_raw_path = root_path + 'data/spice_sg_dict_raw2.npz'
spice_sg_dict_path = root_path + 'data/spice_sg_dict2.npz'
dict_info = json.load(open(root_path+'data/cocobu2.json'))
ix_to_word = dict_info['ix_to_word']
ix_max = 0
N_save = 10
dict_spice = {}
dict_num = {}
for ix in ix_to_word.keys():
dict_spice[ix_to_word[ix]] = int(ix)
dict_num[ix_to_word[ix]] = N_save+1
ix_max = max(int(ix), ix_max)
dict_index = ix_max+1
train_sg = json.load(open(train_sg_path))
val_sg = json.load(open(val_sg_path))
train_sg.update(val_sg)
all_sg = train_sg
img_ids = all_sg.keys()
if os.path.isfile(spice_sg_dict_raw_path) == 0:
print('create a raw dict:')
t = 0
for img_id in img_ids:
t += 1
if t % 1000 == 0:
print("processing {0} data".format(t))
sg_temp = all_sg[img_id]
rela = sg_temp['rela']
sbj = sg_temp['subject']
obj = sg_temp['object']
attr = sg_temp['attribute']
N_rela = len(rela)
N_attr = len(attr)
for i in range(N_rela):
rela_temp = rela[i].strip()
sbj_temp = sbj[i].strip()
obj_temp = obj[i].strip()
rela_temp = change_word(rela_temp)
sbj_temp = change_word(sbj_temp)
obj_temp = change_word(obj_temp)
all_sg[img_id]['rela'][i] = rela_temp
all_sg[img_id]['subject'][i] = sbj_temp
all_sg[img_id]['object'][i] = obj_temp
if rela_temp not in dict_spice.keys():
dict_spice[rela_temp] = dict_index
dict_num[rela_temp] = 1
dict_index += 1
else:
dict_num[rela_temp] += 1
if sbj_temp not in dict_spice.keys():
dict_spice[sbj_temp] = dict_index
dict_num[sbj_temp] = 1
dict_index += 1
else:
dict_num[sbj_temp] += 1
if obj_temp not in dict_spice.keys():
dict_spice[obj_temp] = dict_index
dict_num[obj_temp] = 1
dict_index += 1
else:
dict_num[obj_temp] += 1
for i in range(N_attr):
node_temp = attr[i][5:].strip()
node_temp = change_word(node_temp)
all_sg[img_id]['attribute'][i] = all_sg[img_id]['attribute'][i][:5] + node_temp
if node_temp not in dict_spice.keys():
dict_spice[node_temp] = dict_index
dict_num[node_temp] = 1
dict_index += 1
else:
dict_num[node_temp] += 1
print('ix_max is {0}, dict_index is {1}'.format(ix_max, dict_index))
spice_ix_to_word = {}
for word in dict_spice.keys():
spice_ix_to_word[dict_spice[word]] = word
spice_word_to_ix = dict_spice
spice_dict= {}
spice_dict['spice_ix_to_word'] = spice_ix_to_word
spice_dict['spice_word_to_ix'] = spice_word_to_ix
spice_dict['dict_num'] = dict_num
spice_dict_raw = spice_dict
np.savez(spice_sg_dict_raw_path, spice_dict = spice_dict)
n = 0
word_keys = dict_num.keys()
for word in word_keys:
if dict_num[word] >= N_save:
n += 1
print('number of words larger than N_save'.format(n))
else:
num_ssg = 0
print('load raw dict:')
spice_dict_raw = np.load(spice_sg_dict_raw_path)['spice_dict'][()]
dict_num_raw = spice_dict_raw['dict_num']
n = 0
word_keys = dict_num_raw.keys()
for word in word_keys:
if dict_num_raw[word] >= N_save:
n += 1
if dict_num_raw[word] > N_save+1:
num_ssg += 1
print('number of words larger than N_save is {0}'.format(n))
print('number of words larger than N_save is {0}'.format(num_ssg))
if os.path.isfile(spice_sg_dict_path) == 0:
print('create spice sg dict')
spice_raw_itw = spice_dict_raw['spice_ix_to_word']
spice_raw_wti = spice_dict_raw['spice_word_to_ix']
dict_num_raw = spice_dict_raw['dict_num']
itw_new = {}
wti_new = {}
word_keys = dict_num_raw.keys()
for word in word_keys:
if dict_num_raw[word] >= N_save:
wti_new[word] = spice_raw_wti[word]
itw_new[wti_new[word]] = word
if dict_num_raw[word] > N_save:
num_ssg = num_ssg + 1
wti = {}
itw = {}
N_dict = len(itw_new)
ids = itw_new.keys()
ids_sort = np.sort(ids)
for i in range(1,N_dict+1):
itw[i] = itw_new[ids_sort[i-1]]
wti[itw[i]] = i
spice_dict = {}
spice_dict['ix_to_word'] = itw
spice_dict['word_to_ix'] = wti
np.savez(spice_sg_dict_path, spice_dict = spice_dict)
print('num_ssg: {0}'.format(num_ssg))
else:
print('load dict:')
spice_dict = np.load(spice_sg_dict_path)['spice_dict'][()]
wti = spice_dict['word_to_ix']
itw = spice_dict['ix_to_word']
t = 0
for img_id in img_ids:
t += 1
if t % 1000 == 0:
print("processing {0} data".format(t))
sg_temp = all_sg[img_id]
rela = sg_temp['rela']
sbj = sg_temp['subject']
obj = sg_temp['object']
attr = sg_temp['attribute']
ids = 0
N_rela = len(rela)
N_attr = len(attr)
rela_matrix = np.zeros([N_rela, 3])
unique_obj = []
for i in range(N_rela):
sbj_temp = sbj[i].strip()
obj_temp = obj[i].strip()
rela_temp = rela[i].strip()
sbj_temp = change_word(sbj_temp)
obj_temp = change_word(obj_temp)
rela_temp = change_word(rela_temp)
if sbj_temp in wti.keys():
unique_obj.append(wti[sbj_temp])
if obj_temp in wti.keys():
unique_obj.append(wti[obj_temp])
if sbj_temp in wti.keys():
if obj_temp in wti.keys():
if rela_temp in wti.keys():
rela_matrix[ids,0] = wti[sbj_temp]
rela_matrix[ids,1] = wti[obj_temp]
rela_matrix[ids,2] = wti[rela_temp]
ids += 1
N_rela = ids
rela_matrix = rela_matrix[:N_rela]
for i in range(N_attr):
node_temp = attr[i][5:].strip()
node_temp = change_word(node_temp)
index = attr[i][:5]
if index == 'node:':
if node_temp in wti.keys():
unique_obj.append(wti[node_temp])
unique_obj = np.unique(unique_obj)
obj_info = []
N_obj = len(unique_obj)
for i in range(N_obj):
attr_info = []
attr_info.append(unique_obj[i])
for j in range(N_attr):
if attr[j][:5]== 'node:':
node_temp = attr[j][5:].strip()
node_temp = change_word(node_temp)
if node_temp in wti.keys():
if wti[node_temp] == unique_obj[i]:
for k in range(j+1, N_attr):
if attr[k][:5] == 'node:':
break
attr_temp = attr[k][5:].strip()
attr_temp = change_word(attr_temp)
if attr_temp in wti.keys():
attr_info.append(wti[attr_temp])
obj_info.append(attr_info)
for i in range(N_rela):
sbj_index = np.where(unique_obj==rela_matrix[i,0])[0][0]
obj_index = np.where(unique_obj==rela_matrix[i,1])[0][0]
rela_matrix[i,0] = sbj_index
rela_matrix[i,1] = obj_index
ssg = {}
ssg['obj_info'] = obj_info
ssg['rela_info'] = rela_matrix
save_path_temp = spice_sg_folder + img_id +'.npy'
if t<=10:
N_rela = len(rela_matrix)
for i in range(N_rela):
sbj_temp = itw[int(obj_info[int(rela_matrix[i,0])][0])]
obj_temp = itw[int(obj_info[int(rela_matrix[i,1])][0])]
rela_temp = itw[int(rela_matrix[i,2])]
print('{0}-{1}-{2}'.format(sbj_temp,rela_temp,obj_temp))
N_obj = len(obj_info)
for i in range(N_obj):
obj_temp = obj_info[i]
N_attr = len(obj_temp)
for j in range(N_attr):
if j!= 0:
print('--{0}'.format(itw[int(obj_temp[j])]))
else:
print(itw[int(obj_temp[j])])
N_rela = len(sg_temp['rela'])
for i in range(N_rela):
print('{0}-{1}-{2}'.format(sg_temp['subject'][i], sg_temp['rela'][i], sg_temp['object'][i]))
N_attr = len(sg_temp['attribute'])
for i in range(N_attr):
print(sg_temp['attribute'][i])
np.save(save_path_temp, ssg)