-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_set_of_VSE.py
executable file
·353 lines (290 loc) · 12.5 KB
/
experiment_set_of_VSE.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
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python
import argparse
import os
import sys
import random
from PIL import Image
from tqdm import tqdm
import torch
import clip
import numpy as np
from copy import deepcopy
import matplotlib
from matplotlib import pyplot as plt
# import open_clip
import clip
from predict_clip_ret_score import load_clip, id2vocab, tokenizer, clip_pred, Recall_at_K
parser = argparse.ArgumentParser()
parser.add_argument("--model_name", type=str, default="ViT-B/32",choices=["ViT-B/32", "RN50"])
parser.add_argument("--device", type=str, default="cuda:0")
parser.add_argument("--batch_size", type=int, default=1)
parser.add_argument("--n_cpu", type=int, default=4)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--save_dir", type=str, default="results_setVSE")
parser.add_argument("--dataset_type", type=str, default="test", choices=["train", "valid", "test"])
parser.add_argument("--img_type", type=str, default="global", choices=["global", "partial", "hybrid"])
parser.add_argument("--text_type", type=str, default="global", choices=["global", "partial", "hybrid"])
parser.add_argument("--IPOT", action="store_true", help="Use IPOT for VSE")
args = parser.parse_args()
# Load CLIP model
device = args.device
model, img_preprocess = load_clip(args.model_name, device=device)
# model, _, img_preprocess = open_clip.create_model_and_transforms('ViT-B-32', pretrained='laion2b_s34b_b79k')
# tokenizer = open_clip.get_tokenizer('ViT-B-32')
# Load dataset
from densely_captioned_images.dataset.impl import get_summarized_dataset_with_settings, DenseCaptionedDataset
def load_dataset(dataset_type):
ds: DenseCaptionedDataset = get_summarized_dataset_with_settings(
split=dataset_type,
load_base_image=True,
load_subcaptions=True,
negative_source="swaps",
negative_strategy="rand",
count=1e30)
return ds
ds = load_dataset(args.dataset_type)
if args.dataset_type == "train":
random.seed(args.seed)
random.shuffle(ds)
# Load image database
@torch.no_grad()
def load_image_db(image_db_dir, ds, model, img_preprocess, device, img_type):
if os.path.exists(image_db_dir):
print("Loading image db from", image_db_dir)
satate_dict = torch.load(image_db_dir)
image_db = satate_dict["image_db"]
img_group_ids = satate_dict["img_group_ids"]
else:
# create image db
image_db = []
img_group_ids = []
with torch.no_grad():
for i, sample in enumerate(ds):
global_set = sample[0] # 画像と説明文のセット
partial_sets = sample[1:] # 部分画像と説明文のセット(複数)
if img_type == "global" or img_type == "hybrid" or len(partial_sets) == 0:
g_img = global_set["image"]
img = img_preprocess(Image.fromarray(g_img))
img = img.unsqueeze(0).to(device)
img_feat = model.encode_image(img)
image_db.append(img_feat.to("cpu"))
img_group_ids.append(i)
if (img_type == "partial" or img_type == "hybrid") and len(partial_sets) > 0:
for partial_set in partial_sets:
p_img = partial_set["image"]
img = img_preprocess(Image.fromarray(p_img))
img = img.unsqueeze(0).to(device)
img_feat = model.encode_image(img)
image_db.append(img_feat.to("cpu"))
img_group_ids.append(i)
image_db = torch.cat(image_db, dim=0)
img_group_ids = torch.tensor(img_group_ids)
state_dict = {
"image_db": image_db,
"img_group_ids": img_group_ids
}
torch.save(state_dict, image_db_dir)
print("image_db.shape", image_db.shape)
return image_db, img_group_ids
image_db_dir = args.dataset_type + "_img_db_"+ args.img_type
image_db, img_group_ids = load_image_db(image_db_dir, ds, model, img_preprocess, device, args.img_type)
@torch.no_grad()
def load_text_db(text_db_dir, ds, model, device, text_type):
if os.path.exists(text_db_dir):
print("Loading text db from", text_db_dir)
state_dict = torch.load(text_db_dir)
text_db = state_dict["text_db"]
text_group_ids = state_dict["text_group_ids"]
else:
# create text db
text_db = []
text_group_ids = []
with torch.no_grad():
for i, sample in enumerate(ds):
global_set = sample[0] # 画像と説明文のセット
partial_sets = sample[1:] # 部分画像と説明文のセット(複数)
if text_type == "global" or text_type == "hybrid" or len(partial_sets) == 0:
g_caption = global_set["caption"]
texts = clip.tokenize(g_caption, truncate=True).to(device)
text_feat = model.encode_text(texts)
text_db.append(text_feat.to("cpu"))
text_group_ids.append(i)
if (text_type == "partial" or text_type == "hybrid") and len(partial_sets) > 0:
for partial_set in partial_sets:
p_caption = partial_set["caption"]
texts = clip.tokenize(p_caption, truncate=True).to(device)
text_feat = model.encode_text(texts)
text_db.append(text_feat.to("cpu"))
text_group_ids.append(i)
text_db = torch.cat(text_db, dim=0)
text_group_ids = torch.tensor(text_group_ids)
state_dict = {
"text_db": text_db,
"text_group_ids": text_group_ids
}
torch.save(state_dict, text_db_dir)
print("text_db.shape", text_db.shape)
return text_db, text_group_ids
text_db_dir = args.dataset_type + "_text_db_" + args.text_type
text_db, text_group_ids = load_text_db(text_db_dir, ds, model, device, args.text_type)
# restrict the size of the database
N_limit = 1000
# idsの中身の最大値はN_limitまでになるようにする
with torch.no_grad():
mask = img_group_ids < N_limit
img_group_ids = img_group_ids[mask]
image_db = image_db[mask]
t_mask = text_group_ids < N_limit
text_group_ids = text_group_ids[t_mask]
text_db = text_db[t_mask]
image_db = image_db.float()
text_db = text_db.float()
@torch.no_grad()
def IPOT(query, db, n_iter=50, beta=0.5, save_dir=None, query_group_ids=None, db_group_ids=None):
if save_dir is not None:
matplotlib.rc('xtick', labelsize=10)
matplotlib.rc('ytick', labelsize=10)
if query_group_ids is not None and db_group_ids is not None:
src = [str(t) for t in query_group_ids]
tgt = [str(t) for t in db_group_ids]
embs1 = query.float()
embs2 = db.float()
n = len(embs1)
m = len(embs2)
m_one = torch.ones(m,1)
n_one = torch.ones(n,1)
sigma = m_one/m
T = torch.matmul(n_one, m_one.T)
norm1 = torch.norm(embs1, dim=1)
norm2 = torch.norm(embs2, dim=1)
cossim = 1 - torch.matmul(embs1, embs2.T) / torch.matmul(norm1.unsqueeze(1), norm2.unsqueeze(0))
#heatmap ref: https://matplotlib.org/stable/gallery/images_contours_and_fields/image_annotated_heatmap.html
A = torch.exp(-cossim/beta)
count = 0
for k in range(n_iter):
Q = A * T
delta = 1/n / torch.matmul(Q, sigma)
sigma = 1/m / torch.matmul(Q.T, delta)
T = torch.matmul(torch.diag(delta.squeeze()), Q)
T = torch.matmul(T, torch.diag(sigma.squeeze()))
T_t = T.T
if (k+1) % 10 == 0 or k==0:
if save_dir is not None:
plt.clf()
fig, ax = plt.subplots(figsize=(50,50))
if img_group_ids is not None and query_group_ids is not None:
# We want to show all ticks...
ax.set_xticks(np.arange(len(src)))
ax.set_yticks(np.arange(len(tgt)))
# ... and label them with the respective list entries
ax.set_xticklabels(src)
ax.set_yticklabels(tgt)
# plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
# rotation_mode="anchor")
# for i in range(len(src)):
# for j in range(len(tgt)):
# text = ax.text(i, j, "%1.2f" % T[i, j],
# ha="center", va="center", color="k")
# fig.tight_layout()
ax.set_title("iter = {}".format(k+1), fontsize=10)
im = ax.imshow(T.detach().numpy())
savepath = save_dir + "/OT_alignment_"+str(count).zfill(2)+".png"
print("figure saved to: ", savepath)
plt.savefig(savepath)
plt.close()
count += 1
# print(T)
return T # (N, M)
@torch.no_grad()
def pooling_sim(sim, query_group_ids, db_group_ids, pooling="average"):
array = sim.detach().numpy()
query_group_ids = query_group_ids.detach().numpy()
db_group_ids = db_group_ids.detach().numpy()
row_sums = np.array([array[query_group_ids == label].sum(axis=0) for label in np.unique(query_group_ids)])
column_sums = np.array([row_sums[:, db_group_ids == label].sum(axis=1) for label in np.unique(db_group_ids)])
new_sim = torch.tensor(column_sums.T)
return new_sim
# Recall_at_K
@torch.no_grad()
def recall_at_k(sim):
"""
VSE:
sim = sim # (N: image, B: query text)
IPOT:
sim = T.T # (N: image, B: query text)
"""
Ks = [1,5,10,100]
n_count = {K:0 for K in Ks}
n_total = 0
max_K = max(Ks)
ordered_index = torch.argsort(sim, dim=0, descending=True) # (N,B)
query_ids = torch.arange(sim.shape[1])
db_ids = torch.arange(sim.shape[0])
# Extract the index of query_id that matches db_id
ordered_index = ordered_index.cpu()
query_ids = query_ids
ordered_db_ids = db_ids[ordered_index].cpu()
condition = (ordered_db_ids == query_ids)
assert torch.any(condition, dim=0).all() == True, "No query_id matches db_id"
query_id_ranks = torch.argmax(condition*1.0, dim=0) # (B)
# Calculate matching rate in image_id and query_id on each Top-K
for k in n_count.keys():
n_count[k] += torch.sum(query_id_ranks < k).item()
n_total += query_ids.shape[0]
# image_ids that are ranked higher than query_id
higher_ranked_image_ids = {}
for query_id, query_id_rank in zip(query_ids, query_id_ranks):
higher_ranked_image_ids[query_id.item()] = ordered_db_ids[:query_id_rank].numpy().tolist()
recall = {K: n_count[K]/n_total for K in n_count.keys()}
print(recall)
# Standard VSE
emb_text = text_db / torch.norm(text_db, dim=1).unsqueeze(1)
emb_img = image_db / torch.norm(image_db, dim=1).unsqueeze(1)
Text2Img_sim_partial = torch.matmul(emb_text, emb_img.T)
Img2Text_sim_partial = torch.matmul(emb_img, emb_text.T)
Text2Img_sim = pooling_sim(Text2Img_sim_partial, text_group_ids, img_group_ids)
Img2Text_sim = pooling_sim(Img2Text_sim_partial, img_group_ids, text_group_ids)
print("Standard VSE + pooling shape")
print(Text2Img_sim.shape)
print(Img2Text_sim.shape)
recall_at_k(Text2Img_sim)
recall_at_k(Img2Text_sim)
# Standard VSE + IPOT
if args.IPOT:
save_dir_T2I = args.save_dir + "/IPOT_T2I"
if not os.path.exists(save_dir_T2I):
os.makedirs(save_dir_T2I)
save_dir_I2T = args.save_dir + "/IPOT_I2T"
if not os.path.exists(save_dir_I2T):
os.makedirs(save_dir_I2T)
Text2Img_OT_matrix = IPOT(
query=text_db,
db=image_db,
save_dir=save_dir_T2I,
query_group_ids=None,
db_group_ids=None)
Img2Text_OT_matrix = IPOT(
query=image_db,
db=text_db,
save_dir=save_dir_I2T,
query_group_ids=None,
db_group_ids=None)
state_dict = {
"query_group_ids": text_group_ids,
"db_group_ids": img_group_ids,
"T": Text2Img_OT_matrix
}
torch.save(state_dict, save_dir_T2I + "/IPOT_alignment.pt")
state_dict = {
"query_group_ids": img_group_ids,
"db_group_ids": text_group_ids,
"T": Img2Text_OT_matrix
}
torch.save(state_dict, save_dir_I2T + "/IPOT_alignment.pt")
Text2Img_OT_sim = pooling_sim(Text2Img_OT_matrix, text_group_ids, img_group_ids)
Img2Text_OT_sim = pooling_sim(Img2Text_OT_matrix, img_group_ids, text_group_ids)
print("Standard VSE + IPOT + pooling shape")
print(Text2Img_OT_sim.shape)
print(Img2Text_OT_sim.shape)
recall_at_k(Text2Img_OT_sim)
recall_at_k(Img2Text_OT_sim)