-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEval.py
485 lines (371 loc) · 19.2 KB
/
Eval.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import torch
import os
import cv2
import numpy as np
from PIL import Image
from tqdm import tqdm
from typing import Optional
from torchvision import transforms as vt
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DataLoader
from util import get_prompt_template
from viz_utils import draw_overall, draw_overlaid
import VGGSS.eval_utils as vggss_eval
import VGGSS.extend_eval_utils as exvggss_eval
import Flickr.eval_utils as flickr_eval
import Flickr.extend_eval_utils as exflickr_eval
import AVSBench.eval_utils as avsbench_eval
from typing import List, Optional, Tuple, Dict
@torch.no_grad()
def eval_vggss_agg(
model: torch.nn.Module,
test_dataloader: DataLoader,
result_dir: str,
epoch: Optional[int] = None,
tensorboard_path: Optional[str] = None
) -> Dict[str, float]:
'''
Evaluate provided model on VGG-SS (VGG Sound Source) test dataset.
Args:
model (torch.nn.Module): Sound localization model to evaluate.
test_dataloader (DataLoader): DataLoader for the test dataset.
result_dir (str): Directory to save the evaluation results.
epoch (int, optional): The current epoch number (default: None).
tensorboard_path (str, optional): Path to store TensorBoard logs. If None, TensorBoard logs won't be written.
Returns:
result_dict (Dict): Best AUC value (threshold optimized)
Notes:
The evaluation includes threshold optimization for VGG-SS.
'''
if tensorboard_path is not None and epoch is not None:
os.makedirs(tensorboard_path, exist_ok=True)
writer = SummaryWriter(tensorboard_path)
test_split = test_dataloader.dataset.split
# Get placeholder text
prompt_template, text_pos_at_prompt, prompt_length = get_prompt_template()
# Thresholds for evaluation
thrs = [0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.70, 0.75, 0.8, 0.85, 0.9, 0.95]
evaluators = [vggss_eval.Evaluator() for i in range(len(thrs))]
for step, data in enumerate(tqdm(test_dataloader, desc=f"Evaluate VGG-SS({test_split}) dataset...")):
images, audios, bboxes = data['images'], data['audios'], data['bboxes']
labels, name = data['labels'], data['ids']
# Inference
placeholder_tokens = model.get_placeholder_token(prompt_template.replace('{}', ''))
placeholder_tokens = placeholder_tokens.repeat((test_dataloader.batch_size, 1))
audio_driven_embedding = model.encode_audio(audios.to(model.device), placeholder_tokens, text_pos_at_prompt,
prompt_length)
# Localization result
out_dict = model(images.to(model.device), audio_driven_embedding, 224)
# Evaluation for all thresholds
for i, thr in enumerate(thrs):
evaluators[i].evaluate_batch(out_dict['heatmap'], bboxes, thr)
# Visual results
for j in range(test_dataloader.batch_size):
seg = out_dict['heatmap'][j:j+1]
seg_image = ((1 - seg.squeeze().detach().cpu().numpy()) * 255).astype(np.uint8)
os.makedirs(f'{result_dir}/heatmap', exist_ok=True)
cv2.imwrite(f'{result_dir}/heatmap/{name[j]}.jpg', seg_image)
# Overall figure
for j in range(test_dataloader.batch_size):
original_image = Image.open(os.path.join(test_dataloader.dataset.image_path, name[j] + '.jpg')).resize(
(224, 224))
gt_image = vt.ToPILImage()(bboxes[j]).resize((224, 224)).point(lambda p: 255 - p)
heatmap_image = Image.open(f'{result_dir}/heatmap/{name[j]}.jpg').resize((224, 224))
seg_image = Image.open(f'{result_dir}/heatmap/{name[j]}.jpg').resize((224, 224)).point(
lambda p: 0 if p / 255 < 0.5 else 255)
draw_overall(result_dir, original_image, gt_image, heatmap_image, seg_image, labels[j], name[j])
draw_overlaid(result_dir, original_image, heatmap_image, name[j])
# Save result
rst_path = os.path.join(f'{result_dir}/', 'test_rst.txt')
msg = ''
# Final result
best_AUC = 0.0
for i, thr in enumerate(thrs):
audio_loc_key, audio_loc_dict = evaluators[i].finalize()
msg += f'{model.__class__.__name__} ({test_split} with thr = {thr})\n'
msg += 'AP50(cIoU)={}, AUC={}\n'.format(audio_loc_dict['cIoU'], audio_loc_dict['AUC'])
if tensorboard_path is not None and epoch is not None:
writer.add_scalars(f'test/{test_split}({thr})', audio_loc_dict, epoch)
best_AUC = audio_loc_dict['AUC'] if best_AUC < audio_loc_dict['AUC'] else best_AUC
print(msg)
with open(rst_path, 'w') as fp_rst:
fp_rst.write(msg)
if tensorboard_path is not None and epoch is not None:
writer.close()
result_dict = {'epoch': epoch, 'best_AUC': best_AUC}
return result_dict
@torch.no_grad()
def eval_avsbench_agg(
model: torch.nn.Module,
test_dataloader: DataLoader,
result_dir: str,
epoch: Optional[int] = None,
tensorboard_path: Optional[str] = None
) -> None:
'''
Evaluate provided model on AVSBench (S4, MS3) test dataset.
Args:
model (torch.nn.Module): Sound localization model to evaluate.
test_dataloader (DataLoader): DataLoader for the test dataset.
result_dir (str): Directory to save the evaluation results.
epoch (int, optional): The current epoch number (default: None).
tensorboard_path (str, optional): Path to store TensorBoard logs. If None, TensorBoard logs won't be written.
Returns:
None
Notes:
The evaluation includes threshold optimization for AVSBench.
'''
if tensorboard_path is not None and epoch is not None:
os.makedirs(tensorboard_path, exist_ok=True)
writer = SummaryWriter(tensorboard_path)
test_split = test_dataloader.dataset.setting
# Get placeholder text
prompt_template, text_pos_at_prompt, prompt_length = get_prompt_template()
# Thresholds for evaluation
thrs = [0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.70, 0.75, 0.8, 0.85, 0.9, 0.95]
evaluators = [avsbench_eval.Evaluator() for i in range(len(thrs))]
for step, data in enumerate(tqdm(test_dataloader, desc=f"Evaluate AVSBench dataset({test_split})...")):
images, audios, gts, labels, name = data['images'], data['audios'], data['gts'], data['labels'], data['ids']
# Inference
placeholder_tokens = model.get_placeholder_token(prompt_template.replace('{}', ''))
placeholder_tokens = placeholder_tokens.repeat((test_dataloader.batch_size, 1))
audio_driven_embedding = model.encode_audio(audios.to(model.device), placeholder_tokens, text_pos_at_prompt,
prompt_length)
# Localization result
out_dict = model(images.to(model.device), audio_driven_embedding, 224)
# Evaluation for all thresholds
for i, thr in enumerate(thrs):
evaluators[i].evaluate_batch(out_dict['heatmap'], gts.to(model.device), thr)
# Visual results
for j in range(test_dataloader.batch_size):
seg = out_dict['heatmap'][j:j+1]
seg_image = ((1 - seg.squeeze().detach().cpu().numpy()) * 255).astype(np.uint8)
os.makedirs(f'{result_dir}/heatmap', exist_ok=True)
cv2.imwrite(f'{result_dir}/heatmap/{name[j]}.jpg', seg_image)
# Overall figure
for j in range(test_dataloader.batch_size):
original_image = Image.open(os.path.join(test_dataloader.dataset.image_path, name[j] + '.png')).resize(
(224, 224))
gt_image = Image.open(os.path.join(test_dataloader.dataset.gt_path, name[j] + '.png')).resize(
(224, 224)).point(
lambda p: 255 - p)
heatmap_image = Image.open(f'{result_dir}/heatmap/{name[j]}.jpg').resize((224, 224))
seg_image = Image.open(f'{result_dir}/heatmap/{name[j]}.jpg').resize((224, 224)).point(
lambda p: 0 if p / 255 < 0.5 else 255)
draw_overall(result_dir, original_image, gt_image, heatmap_image, seg_image, labels[j], name[j])
draw_overlaid(result_dir, original_image, heatmap_image, name[j])
# Save result
rst_path = os.path.join(f'{result_dir}', 'test_rst.txt')
msg = ''
# Final result
for i, thr in enumerate(thrs):
audio_loc_key, audio_loc_dict = evaluators[i].finalize()
msg += f'{model.__class__.__name__} ({test_split} with thr = {thr})\n'
msg += 'mIoU={}, F={}\n'.format(audio_loc_dict['mIoU'], audio_loc_dict['Fmeasure'])
if tensorboard_path is not None and epoch is not None:
writer.add_scalars(f'test/avs({test_split})({thr})', audio_loc_dict, epoch)
print(msg)
with open(rst_path, 'w') as fp_rst:
fp_rst.write(msg)
if tensorboard_path is not None and epoch is not None:
writer.close()
@torch.no_grad()
def eval_flickr_agg(
model: torch.nn.Module,
test_dataloader: DataLoader,
result_dir: str,
epoch: Optional[int] = None,
tensorboard_path: Optional[str] = None
) -> None:
'''
Evaluate provided model on AVSBench (S4, MS3) test dataset.
Args:
model (torch.nn.Module): Sound localization model to evaluate.
test_dataloader (DataLoader): DataLoader for the test dataset.
result_dir (str): Directory to save the evaluation results.
epoch (int, optional): The current epoch number (default: None).
tensorboard_path (str, optional): Path to store TensorBoard logs. If None, TensorBoard logs won't be written.
Returns:
None
Notes:
The evaluation includes threshold optimization for AVSBench.
'''
if tensorboard_path is not None and epoch is not None:
os.makedirs(tensorboard_path, exist_ok=True)
writer = SummaryWriter(tensorboard_path)
test_split = test_dataloader.dataset.split
# Get placeholder text
prompt_template, text_pos_at_prompt, prompt_length = get_prompt_template()
# Thresholds for evaluation
thrs = [0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.70, 0.75, 0.8, 0.85, 0.9, 0.95]
evaluators = [flickr_eval.Evaluator() for i in range(len(thrs))]
for step, data in enumerate(tqdm(test_dataloader, desc="Evaluate Flickr dataset...")):
images, audios, bboxes = data['images'], data['audios'], data['bboxes']
labels, name = data['labels'], data['ids']
# Inference
placeholder_tokens = model.get_placeholder_token(prompt_template.replace('{}', ''))
placeholder_tokens = placeholder_tokens.repeat((test_dataloader.batch_size, 1))
audio_driven_embedding = model.encode_audio(audios.to(model.device), placeholder_tokens, text_pos_at_prompt,
prompt_length)
# Localization result
out_dict = model(images.to(model.device), audio_driven_embedding, 224)
# Evaluation for all thresholds
for i, thr in enumerate(thrs):
evaluators[i].evaluate_batch(out_dict['heatmap'], bboxes, thr)
# Visual results
for j in range(test_dataloader.batch_size):
seg = (out_dict['heatmap'][j:j+1])
seg_image = ((1 - seg.squeeze().detach().cpu().numpy()) * 255).astype(np.uint8)
os.makedirs(f'{result_dir}/heatmap', exist_ok=True)
cv2.imwrite(f'{result_dir}/heatmap/{name[j]}.jpg', seg_image)
# Overall figure
for j in range(test_dataloader.batch_size):
original_image = Image.open(os.path.join(test_dataloader.dataset.image_path, name[j] + '.jpg')).resize(
(224, 224))
gt_image = vt.ToPILImage()(bboxes[j]).resize((224, 224)).point(lambda p: 255 - p)
heatmap_image = Image.open(f'{result_dir}/heatmap/{name[j]}.jpg').resize((224, 224))
seg_image = Image.open(f'{result_dir}/heatmap/{name[j]}.jpg').resize((224, 224)).point(
lambda p: 0 if p / 255 < 0.5 else 255)
draw_overall(result_dir, original_image, gt_image, heatmap_image, seg_image, labels[j], name[j])
draw_overlaid(result_dir, original_image, heatmap_image, name[j])
# Save result
rst_path = os.path.join(f'{result_dir}/', 'test_rst.txt')
msg = ''
# Final result (aggressive)
for i, thr in enumerate(thrs):
audio_loc_key, audio_loc_dict = evaluators[i].finalize()
msg += f'{model.__class__.__name__} ({test_split} with thr = {thr})\n'
msg += 'AP50(cIoU)={}, AUC={}\n'.format(audio_loc_dict['cIoU'], audio_loc_dict['AUC'])
if tensorboard_path is not None and epoch is not None:
writer.add_scalars(f'test/flickr({thr})', audio_loc_dict, epoch)
print(msg)
with open(rst_path, 'w') as fp_rst:
fp_rst.write(msg)
if tensorboard_path is not None and epoch is not None:
writer.close()
@torch.no_grad()
def eval_exvggss_agg(
model: torch.nn.Module,
test_dataloader: DataLoader,
result_dir: str,
epoch: Optional[int] = None,
tensorboard_path: Optional[str] = None
) -> None:
'''
Evaluate provided model on AVSBench (S4, MS3) test dataset.
Args:
model (torch.nn.Module): Sound localization model to evaluate.
test_dataloader (DataLoader): DataLoader for the test dataset.
result_dir (str): Directory to save the evaluation results.
epoch (int, optional): The current epoch number (default: None).
tensorboard_path (str, optional): Path to store TensorBoard logs. If None, TensorBoard logs won't be written.
Returns:
None
Notes:
The evaluation includes threshold optimization for AVSBench.
'''
if tensorboard_path is not None and epoch is not None:
os.makedirs(tensorboard_path, exist_ok=True)
writer = SummaryWriter(tensorboard_path)
test_split = test_dataloader.dataset.split
# Get placeholder text
prompt_template, text_pos_at_prompt, prompt_length = get_prompt_template()
# Thresholds for evaluation
thrs = [0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.70, 0.75, 0.8, 0.85, 0.9, 0.95]
evaluators = [exvggss_eval.Evaluator() for i in range(len(thrs))]
for step, data in enumerate(tqdm(test_dataloader, desc="Evaluate Extend VGG-SS dataset...")):
images, audios, bboxes, = data['images'], data['audios'], data['bboxes']
labels, name = data['labels'], data['ids']
# Inference
placeholder_tokens = model.get_placeholder_token(prompt_template.replace('{}', ''))
placeholder_tokens = placeholder_tokens.repeat((test_dataloader.batch_size, 1))
audio_driven_embedding = model.encode_audio(audios.to(model.device), placeholder_tokens, text_pos_at_prompt,
prompt_length)
# Localization result
out_dict = model(images.to(model.device), audio_driven_embedding, 224)
# Calculate confidence value for extended dataset
v_f = model.encode_masked_vision(images.to(model.device), audio_driven_embedding)[0]
ind = torch.arange(test_dataloader.batch_size).to(images.device)
confs = torch.cosine_similarity(v_f[ind, ind, :], audio_driven_embedding)
# Evaluation for all thresholds
for i, thr in enumerate(thrs):
evaluators[i].evaluate_batch(out_dict['heatmap'], bboxes, labels, confs, name, thr)
# Save result
os.makedirs(result_dir, exist_ok=True)
rst_path = os.path.join(f'{result_dir}/', 'test_rst.txt')
msg = ''
# Final result
for i, thr in enumerate(thrs):
audio_loc_key, audio_loc_dict = evaluators[i].finalize()
msg += f'{model.__class__.__name__} ({test_split} with thr = {thr})\n'
msg += 'AP={}, Max-F1={}\n'.format(audio_loc_dict['AP'], audio_loc_dict['Max-F1'])
if tensorboard_path is not None and epoch is not None:
writer.add_scalars(f'test/exvggss({thr})', audio_loc_dict, epoch)
print(msg)
with open(rst_path, 'w') as fp_rst:
fp_rst.write(msg)
if tensorboard_path is not None and epoch is not None:
writer.close()
@torch.no_grad()
def eval_exflickr_agg(
model: torch.nn.Module,
test_dataloader: DataLoader,
result_dir: str,
epoch: Optional[int] = None,
tensorboard_path: Optional[str] = None
) -> None:
'''
Evaluate provided model on AVSBench (S4, MS3) test dataset.
Args:
model (torch.nn.Module): Sound localization model to evaluate.
test_dataloader (DataLoader): DataLoader for the test dataset.
result_dir (str): Directory to save the evaluation results.
epoch (int, optional): The current epoch number (default: None).
tensorboard_path (str, optional): Path to store TensorBoard logs. If None, TensorBoard logs won't be written.
Returns:
None
Notes:
The evaluation includes threshold optimization for AVSBench.
'''
if tensorboard_path is not None and epoch is not None:
os.makedirs(tensorboard_path, exist_ok=True)
writer = SummaryWriter(tensorboard_path)
test_split = test_dataloader.dataset.split
# Get placeholder text
prompt_template, text_pos_at_prompt, prompt_length = get_prompt_template()
# Thresholds for evaluation
thrs = [0.05, 0.1, 0.15, 0.2, 0.25, 0.30, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.70, 0.75, 0.8, 0.85, 0.9, 0.95]
evaluators = [exflickr_eval.Evaluator() for i in range(len(thrs))]
for step, data in enumerate(tqdm(test_dataloader, desc="Evaluate Extend Flickr dataset...")):
images, audios, bboxes, = data['images'], data['audios'], data['bboxes']
labels, name = data['labels'], data['ids']
# Inference
placeholder_tokens = model.get_placeholder_token(prompt_template.replace('{}', ''))
placeholder_tokens = placeholder_tokens.repeat((test_dataloader.batch_size, 1))
audio_driven_embedding = model.encode_audio(audios.to(model.device), placeholder_tokens, text_pos_at_prompt,
prompt_length)
# Localization result
out_dict = model(images.to(model.device), audio_driven_embedding, 224)
# Calculate confidence value for extended dataset
v_f = model.encode_masked_vision(images.to(model.device), audio_driven_embedding)[0]
ind = torch.arange(test_dataloader.batch_size).to(images.device)
confs = torch.cosine_similarity(v_f[ind, ind, :], audio_driven_embedding)
# Evaluation for all thresholds
for i, thr in enumerate(thrs):
evaluators[i].evaluate_batch(out_dict['heatmap'], bboxes, labels, confs, name, thr)
# Save result
os.makedirs(result_dir, exist_ok=True)
rst_path = os.path.join(f'{result_dir}/', 'test_rst.txt')
msg = ''
# Final result
for i, thr in enumerate(thrs):
audio_loc_key, audio_loc_dict = evaluators[i].finalize()
msg += f'{model.__class__.__name__} ({test_split} with thr = {thr})\n'
msg += 'AP={}, Max-F1={}\n'.format(audio_loc_dict['AP'], audio_loc_dict['Max-F1'])
if tensorboard_path is not None and epoch is not None:
writer.add_scalars(f'test/exflickr({thr})', audio_loc_dict, epoch)
print(msg)
with open(rst_path, 'w') as fp_rst:
fp_rst.write(msg)
if tensorboard_path is not None and epoch is not None:
writer.close()