-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_during_train.py
309 lines (232 loc) · 12.1 KB
/
test_during_train.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
import torch
import numpy as np
import os
import torch.nn.functional as F
from utils.read_write_data import write_txt
def calculate_cos(image_embedding, text_embedding):
image_embedding_norm = image_embedding / (image_embedding.norm(dim=1, keepdim=True) + 1e-8)
text_embedding_norm = text_embedding / (text_embedding.norm(dim=1, keepdim=True) + 1e-8)
similarity = torch.mm(image_embedding_norm, text_embedding_norm.t())
return similarity
def calculate_cos_part(numpart,image_embedding, text_embedding):
image_embedding = torch.cat([image_embedding[i] for i in range(numpart)], dim=1)
text_embedding = torch.cat([text_embedding[i] for i in range(numpart)], dim=1)
image_embedding_norm = image_embedding / (image_embedding.norm(dim=1, keepdim=True)+ 1e-8)
text_embedding_norm = text_embedding / (text_embedding.norm(dim=1, keepdim=True)+ 1e-8)
similarity = torch.mm(image_embedding_norm, text_embedding_norm.t())
return similarity
def calculate_similarity(image_feature_local, text_feature_local):
with torch.no_grad():
similarity_local_part_i = calculate_cos(image_feature_local, text_feature_local)
similarity = similarity_local_part_i
return similarity.cpu()
def calculate_similarity_part(numpart,image_feature_local, text_feature_local):
with torch.no_grad():
similarity_local_part_i = calculate_cos_part(numpart,image_feature_local, text_feature_local)
similarity = similarity_local_part_i
return similarity.cpu()
def calculate_ap(similarity, label_query, label_gallery):
"""
calculate the similarity, and rank the distance, according to the distance, calculate the ap, cmc
:param label_query: the id of query [1]
:param label_gallery:the id of gallery [N]
:return: ap, cmc
"""
index = np.argsort(similarity)[::-1] # the index of the similarity from huge to small
good_index = np.argwhere(label_gallery == label_query) # the index of the same label in gallery
cmc = np.zeros(index.shape)
mask = np.in1d(index, good_index) # get the flag the if index[i] is in the good_index
precision_result = np.argwhere(mask == True) # get the situation of the good_index in the index
precision_result = precision_result.reshape(precision_result.shape[0])
if precision_result.shape[0] != 0:
cmc[int(precision_result[0]):] = 1 # get the cmc
d_recall = 1.0 / len(precision_result)
ap = 0
for i in range(len(precision_result)): # ap is to calculate the PR area
precision = (i + 1) * 1.0 / (precision_result[i] + 1)
if precision_result[i] != 0:
old_precision = i * 1.0 / precision_result[i]
else:
old_precision = 1.0
ap += d_recall * (old_precision + precision) / 2
return ap, cmc
else:
return None, None
def evaluate(similarity, label_query, label_gallery):
similarity = similarity.numpy()
label_query = label_query.numpy()
label_gallery = label_gallery.numpy()
cmc = np.zeros(label_gallery.shape)
ap = 0
for i in range(len(label_query)):
ap_i, cmc_i = calculate_ap(similarity[i, :], label_query[i], label_gallery)
cmc += cmc_i
ap += ap_i
"""
cmc_i is the vector [0,0,...1,1,..1], the first 1 is the first right prediction n,
rank-n and the rank-k after it all add one right prediction, therefore all of them's index mark 1
Through the add all the vector and then divive the n_query, we can get the rank-k accuracy cmc
cmc[k-1] is the rank-k accuracy
"""
cmc = cmc / len(label_query)
map = ap / len(label_query) # map = sum(ap) / n_query
# print('Rank@1:%f Rank@5:%f Rank@10:%f mAP:%f' % (cmc[0], cmc[4], cmc[9], map))
return cmc, map
def evaluate_without_matching_image(similarity, label_query, label_gallery, txt_img_index):
similarity = similarity.numpy()
label_query = label_query.numpy()
label_gallery = label_gallery.numpy()
cmc = np.zeros(label_gallery.shape[0] - 1)
ap = 0
count = 0
for i in range(len(label_query)):
similarity_i = similarity[i, :]
similarity_i = np.delete(similarity_i, txt_img_index[i])
label_gallery_i = np.delete(label_gallery, txt_img_index[i])
ap_i, cmc_i = calculate_ap(similarity_i, label_query[i], label_gallery_i)
if ap_i is not None:
cmc += cmc_i
ap += ap_i
else:
count += 1
"""
cmc_i is the vector [0,0,...1,1,..1], the first 1 is the first right prediction n,
rank-n and the rank-k after it all add one right prediction, therefore all of them's index mark 1
Through the add all the vector and then divive the n_query, we can get the rank-k accuracy cmc
cmc[k-1] is the rank-k accuracy
"""
cmc = cmc / (len(label_query) - count)
map = ap / (len(label_query) - count) # map = sum(ap) / n_query
# print('Rank@1:%f Rank@5:%f Rank@10:%f mAP:%f' % (cmc[0], cmc[4], cmc[9], map))
return cmc, map
def load_checkpoint(model_root, model_name):
filename = os.path.join(model_root, 'model', model_name)
state = torch.load(filename, map_location='cpu')
return state
def write_result(similarity, img_labels, txt_labels, txt_img_index, name, txt_root, best_txt_root, epoch, best, iteration):
write_txt(name, txt_root)
print(name)
t2i_cmc_wm, t2i_map_wm = evaluate_without_matching_image(similarity.t(), txt_labels, img_labels, txt_img_index)
t2i_cmc, t2i_map = evaluate(similarity.t(), txt_labels, img_labels)
str = "t2i: @R1: {:.4}, @R5: {:.4}, @R10: {:.4}, map: {:.4}".format(t2i_cmc[0], t2i_cmc[4], t2i_cmc[9], t2i_map)
write_txt(str, txt_root)
print(str)
str = "t2i_wm: @R1: {:.4}, @R5: {:.4}, @R10: {:.4}, map: {:.4}".format(t2i_cmc_wm[0], t2i_cmc_wm[4],
t2i_cmc_wm[9], t2i_map_wm)
write_txt(str, txt_root)
print(str)
if t2i_cmc[0] > best:
str = "Testing Epoch: {} Iteration:{}".format(epoch, iteration)
write_txt(str, best_txt_root)
write_txt(name, best_txt_root)
str = "t2i: @R1: {:.4}, @R5: {:.4}, @R10: {:.4}, map: {:.4}".format(t2i_cmc[0], t2i_cmc[4], t2i_cmc[9], t2i_map)
write_txt(str, best_txt_root)
str = "t2i_wm: @R1: {:.4}, @R5: {:.4}, @R10: {:.4}, map: {:.4}".format(t2i_cmc_wm[0], t2i_cmc_wm[4],
t2i_cmc_wm[9], t2i_map_wm)
write_txt(str, best_txt_root)
return t2i_cmc[0]
else:
return best
def test(opt, epoch, iteration, network, img_dataloader, txt_dataloader, best):
txt_root = os.path.join(opt.save_path, 'log', 'test_separate.log')
best_txt_root = os.path.join(opt.save_path, 'log', 'best_test.log')
str = "Testing Epoch: {} Iteration:{}".format(epoch, iteration)
write_txt(str, txt_root)
print(str)
image_feature = torch.FloatTensor().to(opt.device)
img_labels = torch.LongTensor().to(opt.device)
for times, [image, label] in enumerate(img_dataloader):
image = image.to(opt.device)
label = label.to(opt.device)
with torch.no_grad():
image_feature_i = network.img_embedding(image)
image_feature = torch.cat([image_feature, image_feature_i], 0)
img_labels = torch.cat([img_labels, label.view(-1)], 0)
text_feature = torch.FloatTensor().to(opt.device)
txt_labels = torch.LongTensor().to(opt.device)
txt_img_index = []
for times, [label, caption_code, caption_length, caption_matching_img_index] in enumerate(txt_dataloader):
label = label.to(opt.device)
caption_code = caption_code.to(opt.device).long()
caption_length = caption_length.to(opt.device)
txt_img_index.append(caption_matching_img_index)
with torch.no_grad():
text_feature_i = network.txt_embedding(caption_code, caption_length)
text_feature = torch.cat([text_feature, text_feature_i], 0)
txt_labels = torch.cat([txt_labels, label.view(-1)], 0)
txt_img_index = torch.cat(txt_img_index, 0)
similarity = calculate_similarity(image_feature, text_feature)
img_labels = img_labels.cpu()
txt_labels = txt_labels.cpu()
best = write_result(similarity, img_labels, txt_labels, txt_img_index, 'similarity_all:',
txt_root, best_txt_root, epoch, best, iteration)
return best
def test_part(opt, epoch, iteration, network, img_dataloader, txt_dataloader, best):
txt_root = os.path.join(opt.save_path, 'log', 'test_separate.log')
best_txt_root = os.path.join(opt.save_path, 'log', 'best_test.log')
str = "Testing Epoch: {} Iteration:{}".format(epoch, iteration)
write_txt(str, txt_root)
print(str)
image_feature = torch.FloatTensor().to(opt.device)
img_labels = torch.LongTensor().to(opt.device)
for times, [image, label] in enumerate(img_dataloader):
image = image.to(opt.device)
label = label.to(opt.device)
with torch.no_grad():
_,image_feature_i = network.img_embedding(image)
image_feature = torch.cat([image_feature, image_feature_i], 1)
img_labels = torch.cat([img_labels, label.view(-1)], 0)
text_feature = torch.FloatTensor().to(opt.device)
txt_labels = torch.LongTensor().to(opt.device)
txt_img_index = []
for times, [label, caption_code, caption_length, caption_mask, caption_matching_img_index] in enumerate(txt_dataloader):
label = label.to(opt.device)
caption_code = caption_code.to(opt.device).long()
# caption_length = caption_length.to(opt.device)
caption_mask = caption_mask.to(opt.device)
txt_img_index.append(caption_matching_img_index)
with torch.no_grad():
_ , text_feature_i = network.txt_embedding(caption_code, caption_mask)
text_feature = torch.cat([text_feature, text_feature_i], 1)
txt_labels = torch.cat([txt_labels, label.view(-1)], 0)
txt_img_index = torch.cat(txt_img_index, 0)
similarity = calculate_similarity_part(opt.num_query,image_feature, text_feature)
img_labels = img_labels.cpu()
txt_labels = txt_labels.cpu()
best = write_result(similarity, img_labels, txt_labels, txt_img_index, 'similarity_all:',
txt_root, best_txt_root, epoch, best, iteration)
return best
def test_part_MPN(opt, epoch, iteration, network, img_dataloader, txt_dataloader, best):
txt_root = os.path.join(opt.save_path, 'log', 'test_separate.log')
best_txt_root = os.path.join(opt.save_path, 'log', 'best_test.log')
str = "Testing Epoch: {} Iteration:{}".format(epoch, iteration)
write_txt(str, txt_root)
print(str)
image_feature = torch.FloatTensor().to(opt.device)
img_labels = torch.LongTensor().to(opt.device)
for times, [image, label] in enumerate(img_dataloader):
image = image.to(opt.device)
label = label.to(opt.device)
with torch.no_grad():
image_feature_i , _= network.img_embedding(image)
image_feature = torch.cat([image_feature, image_feature_i], 1)
img_labels = torch.cat([img_labels, label.view(-1)], 0)
text_feature = torch.FloatTensor().to(opt.device)
txt_labels = torch.LongTensor().to(opt.device)
txt_img_index = []
for times, [label, caption_code, caption_length, caption_matching_img_index] in enumerate(txt_dataloader):
label = label.to(opt.device)
caption_code = caption_code.to(opt.device).long()
# caption_length = caption_length.to(opt.device)
txt_img_index.append(caption_matching_img_index)
with torch.no_grad():
text_feature_i = network.txt_embedding(caption_code, caption_length)
text_feature = torch.cat([text_feature, text_feature_i], 1)
txt_labels = torch.cat([txt_labels, label.view(-1)], 0)
txt_img_index = torch.cat(txt_img_index, 0)
similarity = calculate_similarity_part(opt.num_query,image_feature, text_feature)
img_labels = img_labels.cpu()
txt_labels = txt_labels.cpu()
best = write_result(similarity, img_labels, txt_labels, txt_img_index, 'similarity_all:',
txt_root, best_txt_root, epoch, best, iteration)
return best