forked from AIVIETNAMResearch/AI-City-2023-Track2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_submit.py
192 lines (158 loc) · 6.39 KB
/
get_submit.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
import json
import os
import pickle
import numpy as np
import torch
from numpy import linalg as LA
import torch
import torch.nn.functional as F
import os.path as osp
import IPython
from main import prepare_start
from test import inference_vis_and_lang
# with open("data2022/test-queries_nlpaug.json") as f:
with open("data/AIC23_Track2_NL_Retrieval/data/test-queries.json") as f:
queries = json.load(f)
with open("data/AIC23_Track2_NL_Retrieval/data/test-tracks.json") as f:
tracks = json.load(f)
query_ids = list(queries.keys())
tacks_ids = list(tracks.keys())
print(len(tacks_ids), len(query_ids))
def get_lang_v(texts):
location = 0
direction = 0
num_left = 0
num_right = 0
for text in texts:
if 'turn' in text:
if 'left' in text:
num_left += 1
if 'right' in text:
num_right += 1
if 'intersection' in text:
location = 1
if num_left > num_right:
direction = 1
if num_left < num_right:
direction = 2
loc_map = [[1, 0], [0, 1]]
dir_map = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
return loc_map[location], dir_map[direction]
def get_mean_feats(img_feat, tacks_ids):
mean_gallery = []
for k in tacks_ids:
tmp = []
for fid in img_feat[k]:
tmp.append(img_feat[k][fid])
tmp = np.vstack(tmp)
tmp = np.mean(tmp,0)
mean_gallery.append(tmp)
mean_gallery = np.vstack(mean_gallery)
return mean_gallery
def feature_mean_score_test(args, config_list, merge_weights, enforced=False, spatial=False):
img_feats = []
nlp_feats = []
for i, cur_config in enumerate(config_list):
feat_pth_path = inference_vis_and_lang(cur_config, args, enforced)
vis_feats, lang_ems = args.ossSaver.load_pth(feat_pth_path)
vis_feats = [v for k, v in vis_feats.items()]
img_feats.append(vis_feats)
nlp_feats.append(lang_ems)
if spatial:
# # Location similarity (long-distance spatial relationship)
loc_dict = {'c005': 1, 'c014': 0, 'c029': 0, 'c003': 1, 'c017': 0, 'c035': 0, 'c013': 0, 'c027': 0, 'c038': 0,
'c016': 0, 'c002': 1, 'c021': 0, 'c019': 0, 'c030': 0, 'c020': 0, 'c026': 0, 'c010': 0, 'c004': 1,
'c033': 0, 'c012': 1, 'c001': 1, 'c034': 0, 'c022': 0, 'c036': 0, 'c040': 1, 'c025': 0, 'c032': 0,
'c037': 1}
loc_logits = dict()
for track_id, track in tracks.items():
cam = track['frames'][0].split('/')[-3]
if loc_dict[cam] == 1:
loc_logits[track_id] = [0, 1]
else:
loc_logits[track_id] = [1, 0]
img_loc_logits = np.array([v for k, v in loc_logits.items()])
lang_loc_v = dict()
lang_dir_v = dict()
for q_id, record in queries.items():
texts = queries[q_id]["nl"]
loc_v, dir_v = get_lang_v(texts)
lang_loc_v[q_id] = loc_v
lang_dir_v[q_id] = dir_v
# # Multi-vehicle similarity (short-distance spatial relationship)
# load feature of vehicle in text
with open('spatial_feat/query_lang_embeds.pkl', "rb") as fb:
query_lang_embeds = pickle.load(fb)
# load feature of bbox in tracks
with open('spatial_feat/track_car_embeds.pkl', "rb") as fb:
track_car_embeds = pickle.load(fb)
results = dict()
sim_matrix = np.zeros((184,184))
for id, query in enumerate(query_ids):
score = 0.
for i in range(len(nlp_feats)):
q = nlp_feats[i][query]
cur_sim = np.mean(np.matmul(q, np.array(img_feats[i]).T), 0)
if spatial:
car_lang_embeds = query_lang_embeds[query]
if len(car_lang_embeds) >= 2:
relation_sim = []
other_lang_embed = car_lang_embeds[1]
for _, car_vis_embeds in track_car_embeds.items():
max_sim_val = 0
for car_vis_embed in car_vis_embeds:
other_lang_embed = other_lang_embed / np.linalg.norm(other_lang_embed)
car_vis_embed = car_vis_embed / np.linalg.norm(car_vis_embed)
max_sim_val = max(max_sim_val, np.matmul(other_lang_embed, car_vis_embed.T))
relation_sim.append(max_sim_val)
relation_sim = np.array(relation_sim)
relation_sim = relation_sim / np.max(relation_sim) * np.max(cur_sim) * 0.233333
cur_sim = cur_sim + relation_sim
loc_sim = np.matmul(np.array([lang_loc_v[query]]), img_loc_logits.T)
loc_sim = loc_sim.T
loc_sim = np.squeeze(loc_sim)
cur_sim = cur_sim + loc_sim
score += merge_weights[i] * cur_sim
sim_matrix[id] = score
index = np.argsort(score)[::-1]
results[query] = []
for i in index:
results[query].append(tacks_ids[i])
os.makedirs(args.cfg.DATA.TEST_OUTPUT, exist_ok=True)
save_path = osp.join(args.cfg.DATA.TEST_OUTPUT, args.save_name)
with open(save_path, "w") as fs:
json.dump(results, fs, indent=4)
print(f"====> save {save_path} done")
with open("sim_mat.npy", "wb") as f:
np.save(f, sim_matrix)
def main():
args, cfg = prepare_start()
config_dict = {
'view_triplet_hard': 1.5,
'single_baseline_aug1_plus': 1.5,
##'dual_baseline_aug1': 1,
'dual_baseline_aug3': 1,
'circle_loss': 1,
##'single_baseline_aug2': 1,
##'single_baseline_aug1_plus_concat_frms': 1,
##'dual_baseline_multi_back': 1,
##'single_baseline_aug1_vit': 0.8,
##'dual_baseline_aug3_multiframes': 0.2
##'single_baseline_aug1_clipfeats': 1
'single_baseline_aug1_plus_pseudo': 1.5,
#'view_triplet_hard_pseudo': 1,
#'dual_baseline_aug1_pseudo': 1
'single_baseline_aug1_plus_multi_queries': 1
}
config_file_list = list(config_dict.keys())
merge_weights = list(config_dict.values())
feat = -1
args.save_name = f'multi_queries_ens.json'
args.cfg.MODEL.MAIN_FEAT_IDX = feat
# spatial = False
spatial = True
# enforced = True
enforced = False
feature_mean_score_test(args, config_file_list, merge_weights, enforced=enforced, spatial=spatial)
if __name__ == '__main__':
main()