-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_mcub.py
More file actions
504 lines (427 loc) · 15.9 KB
/
Copy patheval_mcub.py
File metadata and controls
504 lines (427 loc) · 15.9 KB
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/env python3
"""
MCUB Composition Ablation Evaluation Script.
Tests modality composition for pre-FFN vs KV augmentation architectures:
1. Audio only: Load both adapters, provide only audio input
2. Point cloud only: Load both adapters, provide only point cloud input
3. Both modalities: Load both adapters, provide audio + point cloud
Usage:
# Evaluate pre-FFN composition
python eval_mcub.py \
--config composition_preffn \
--audio-adapter outputs/audio_preffn/best_model.pt \
--pc-adapter outputs/pc_preffn/best_model.pt \
--data-path /path/to/mcub \
--output-dir outputs/eval_mcub_preffn
# Evaluate KV augmentation composition
python eval_mcub.py \
--config composition_kvaug \
--audio-adapter outputs/audio_kvaug/best_model.pt \
--pc-adapter outputs/pc_kvaug/best_model.pt \
--data-path /path/to/mcub \
--output-dir outputs/eval_mcub_kvaug
# Use synthetic data for testing
python eval_mcub.py --synthetic --config composition_preffn
"""
from __future__ import annotations
import argparse
import json
import os
import sys
import time
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader
from tqdm import tqdm
# Add project root to path
sys.path.insert(0, str(Path(__file__).parent))
from configs.composition_configs import get_composition_config, COMPOSITION_CONFIGS
from safe.models.safe_multimodal import SAFEMultiModalModel
from safe.data.mcub_dataset import (
MCUBDataset,
SyntheticMCUBDataset,
mcub_collate_fn,
create_mcub_dataloader,
)
def parse_args():
parser = argparse.ArgumentParser(description="MCUB Composition Ablation Evaluation")
# Model configuration
parser.add_argument(
"--config",
type=str,
required=True,
choices=list(COMPOSITION_CONFIGS.keys()),
help="Composition config name",
)
parser.add_argument(
"--audio-adapter",
type=str,
default=None,
help="Path to trained audio adapter checkpoint",
)
parser.add_argument(
"--pc-adapter",
type=str,
default=None,
help="Path to trained point cloud adapter checkpoint",
)
# Data configuration
parser.add_argument(
"--data-path",
type=str,
default="data/mcub",
help="Path to MCUB dataset",
)
parser.add_argument(
"--synthetic",
action="store_true",
help="Use synthetic data for testing pipeline",
)
parser.add_argument(
"--num-synthetic-samples",
type=int,
default=100,
help="Number of synthetic samples to generate",
)
# Evaluation settings
parser.add_argument(
"--batch-size",
type=int,
default=1,
help="Batch size for evaluation",
)
parser.add_argument(
"--num-workers",
type=int,
default=4,
help="Number of data loading workers",
)
parser.add_argument(
"--conditions",
nargs="+",
default=["audio_only", "pc_only", "both"],
choices=["audio_only", "pc_only", "both"],
help="Evaluation conditions to run",
)
# Output
parser.add_argument(
"--output-dir",
type=str,
default="outputs/eval_mcub",
help="Directory to save evaluation results",
)
parser.add_argument(
"--device",
type=str,
default="cuda" if torch.cuda.is_available() else "cpu",
help="Device to use",
)
return parser.parse_args()
def load_model(
config: Dict[str, Any],
audio_adapter_path: Optional[str] = None,
pc_adapter_path: Optional[str] = None,
device: str = "cuda",
) -> SAFEMultiModalModel:
"""
Load SAFEMultiModalModel with optional pre-trained adapters.
Args:
config: Model configuration dict
audio_adapter_path: Path to trained audio adapter
pc_adapter_path: Path to trained point cloud adapter
device: Device to load model on
Returns:
Loaded model with adapters
"""
print(f"\n{'='*60}")
print(f"Loading model: {config['name']}")
print(f"{'='*60}")
# Create model
model = SAFEMultiModalModel(
llm_model_name=config.get("llm_model_name", "llava-hf/llava-1.5-13b-hf"),
vision_model_name=config.get("vision_model_name", "openai/clip-vit-large-patch14"),
audio_encoder_type=config.get("audio_encoder_type", "clap"),
audio_encoder_config=config.get("audio_encoder_config"),
audio_embed_dim=config.get("audio_embed_dim", 512),
num_audio_tokens=config.get("num_audio_tokens", 8),
pointcloud_encoder_type=config.get("pointcloud_encoder_type", "pointbert"),
pointcloud_encoder_config=config.get("pointcloud_encoder_config"),
pointcloud_embed_dim=config.get("pointcloud_embed_dim", 768),
num_pointcloud_tokens=config.get("num_pointcloud_tokens", 8),
projector_type=config.get("projector_type", "standard"),
projector_config=config.get("projector_config"),
fusion_type=config.get("fusion_type", "multilayer"),
fusion_layer_indices=config.get("fusion_layer_indices"),
lora_rank=config.get("lora_rank", 16),
fusion_config=config.get("fusion_config"),
freeze_base_vl=True,
freeze_audio_encoder=True,
freeze_pointcloud_encoder=True,
llm_hidden_size=config.get("llm_hidden_size", 5120),
)
# Load audio adapter if provided
if audio_adapter_path and Path(audio_adapter_path).exists():
print(f"\nLoading audio adapter from: {audio_adapter_path}")
model.load_adapters(audio_adapter_path, modality="audio")
# Load point cloud adapter if provided
if pc_adapter_path and Path(pc_adapter_path).exists():
print(f"\nLoading point cloud adapter from: {pc_adapter_path}")
model.load_adapters(pc_adapter_path, modality="pointcloud")
model = model.to(device)
model.eval()
# Count parameters
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"\nModel loaded: {total_params/1e6:.1f}M params ({trainable_params/1e6:.1f}M trainable)")
return model
def evaluate_condition(
model: SAFEMultiModalModel,
dataloader: DataLoader,
condition: str,
device: str = "cuda",
) -> Dict[str, Any]:
"""
Evaluate model on MCUB under a specific condition.
Args:
model: The multi-modal model
dataloader: MCUB dataloader
condition: One of "audio_only", "pc_only", "both"
device: Device to use
Returns:
Dict with accuracy and per-sample results
"""
print(f"\n{'='*60}")
print(f"Evaluating condition: {condition}")
print(f"{'='*60}")
# Configure modality masking based on condition
if condition == "audio_only":
model.enable_modality("audio", True)
model.enable_modality("pointcloud", False)
elif condition == "pc_only":
model.enable_modality("audio", False)
model.enable_modality("pointcloud", True)
elif condition == "both":
model.enable_modality("audio", True)
model.enable_modality("pointcloud", True)
else:
raise ValueError(f"Unknown condition: {condition}")
# Get tokenizer from model
tokenizer = model.base_vl.processor.tokenizer
# Evaluation loop
results = []
correct = 0
total = 0
with torch.no_grad():
for batch in tqdm(dataloader, desc=f"Eval {condition}"):
# Move to device
audio = batch["audio"]
pointcloud = batch["pointcloud"]
if audio is not None:
audio = audio.to(device)
if pointcloud is not None:
pointcloud = pointcloud.to(device)
# Tokenize questions
questions = batch["questions"]
encoded = tokenizer(
questions,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512,
)
input_ids = encoded["input_ids"].to(device)
attention_mask = encoded["attention_mask"].to(device)
# Forward pass with text input
outputs = model(
input_ids=input_ids,
attention_mask=attention_mask,
audio=audio if condition != "pc_only" else None,
pointcloud=pointcloud if condition != "audio_only" else None,
return_hidden_states=True,
)
# Get predictions
logits = outputs.get("logits")
last_hidden = outputs.get("last_hidden_state")
# For each sample in batch
for i, sample_id in enumerate(batch["sample_ids"]):
answer = batch["answers"][i]
choices = batch["choices"][i]
# Get model's predicted next token (for multiple choice, look for A/B/C/D)
if logits is not None:
# Get the logits for the last position
last_logits = logits[i, -1, :] # (vocab_size,)
# Get token IDs for A, B, C, D
choice_tokens = tokenizer.encode("A B C D", add_special_tokens=False)
# Filter to just A, B, C, D tokens
choice_ids = [tokenizer.encode(c, add_special_tokens=False)[0] for c in ["A", "B", "C", "D"]]
# Get logits for these tokens
choice_logits = last_logits[choice_ids]
predicted_idx = choice_logits.argmax().item()
predicted_answer = ["A", "B", "C", "D"][predicted_idx]
# Check if correct
is_correct = (predicted_answer == answer) if answer else False
if is_correct:
correct += 1
else:
predicted_answer = None
is_correct = False
result = {
"sample_id": sample_id,
"condition": condition,
"answer": answer,
"predicted": predicted_answer,
"correct": is_correct,
"choices": choices,
"has_audio": audio is not None,
"has_pointcloud": pointcloud is not None,
}
results.append(result)
total += 1
# Compute metrics
accuracy = correct / total if total > 0 else 0.0
metrics = {
"condition": condition,
"total_samples": total,
"correct": correct,
"accuracy": accuracy,
"results": results,
}
print(f" {condition}: {correct}/{total} = {accuracy:.2%}")
return metrics
def run_evaluation(
model: SAFEMultiModalModel,
dataloader: DataLoader,
conditions: List[str],
device: str = "cuda",
) -> Dict[str, Any]:
"""
Run full composition ablation evaluation.
Args:
model: The multi-modal model
dataloader: MCUB dataloader
conditions: List of conditions to evaluate
device: Device to use
Returns:
Dict with results for all conditions
"""
all_results = {}
for condition in conditions:
metrics = evaluate_condition(
model=model,
dataloader=dataloader,
condition=condition,
device=device,
)
all_results[condition] = metrics
return all_results
def print_results_summary(results: Dict[str, Any], config_name: str):
"""Print a summary of evaluation results."""
print(f"\n{'='*60}")
print(f"EVALUATION SUMMARY: {config_name}")
print(f"{'='*60}")
for condition, metrics in results.items():
print(f"\n{condition}:")
print(f" Total samples: {metrics['total_samples']}")
print(f" Correct: {metrics['correct']}")
print(f" Accuracy: {metrics['accuracy']:.2%}")
# Print composition analysis
print(f"\n{'='*60}")
print("COMPOSITION ANALYSIS")
print(f"{'='*60}")
if "audio_only" in results and "pc_only" in results and "both" in results:
print("\nKey questions answered by this ablation:")
print("1. Modality interference: Do inactive adapters degrade performance?")
print(" - Compare 'audio_only' vs single-modality audio baseline")
print(" - Compare 'pc_only' vs single-modality PC baseline")
print("2. Composition benefit: Does having both modalities improve accuracy?")
print(" - Compare 'both' vs 'audio_only' and 'pc_only'")
print("3. Architecture difference: Is this architecture good for composition?")
print(" - Compare these results vs the other architecture (pre-FFN vs KV-aug)")
def main():
args = parse_args()
# Setup output directory
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
# Load config
config = get_composition_config(args.config)
print(f"\nConfig: {config['name']}")
print(f"Description: {config['description']}")
# Create dataloader
if args.synthetic:
print("\nUsing synthetic data for testing...")
dataset = SyntheticMCUBDataset(
num_samples=args.num_synthetic_samples,
num_classes=10,
audio_sample_rate=config.get("audio_encoder_config", {}).get("sample_rate", 48000),
num_points=config.get("pointcloud_encoder_config", {}).get("num_points", 1024),
)
dataloader = DataLoader(
dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers,
collate_fn=mcub_collate_fn,
)
else:
print(f"\nLoading MCUB from: {args.data_path}")
dataloader = create_mcub_dataloader(
data_path=args.data_path,
modalities=["audio", "pointcloud"],
batch_size=args.batch_size,
num_workers=args.num_workers,
)
# Load model
model = load_model(
config=config,
audio_adapter_path=args.audio_adapter,
pc_adapter_path=args.pc_adapter,
device=args.device,
)
# Run evaluation
print(f"\nRunning evaluation with conditions: {args.conditions}")
start_time = time.time()
results = run_evaluation(
model=model,
dataloader=dataloader,
conditions=args.conditions,
device=args.device,
)
elapsed = time.time() - start_time
print(f"\nEvaluation completed in {elapsed:.1f}s")
# Save results
results_path = output_dir / f"results_{config['name']}.json"
with open(results_path, "w") as f:
# Convert results to JSON-serializable format
json_results = {
"config": config["name"],
"conditions": args.conditions,
"audio_adapter": args.audio_adapter,
"pc_adapter": args.pc_adapter,
"synthetic": args.synthetic,
"elapsed_seconds": elapsed,
"results": {
cond: {
"total_samples": metrics["total_samples"],
"correct": metrics["correct"],
"accuracy": metrics["accuracy"],
}
for cond, metrics in results.items()
},
}
json.dump(json_results, f, indent=2)
print(f"\nResults saved to: {results_path}")
# Print summary
print_results_summary(results, config["name"])
# Save per-sample results for detailed analysis
detailed_path = output_dir / f"detailed_{config['name']}.json"
detailed_results = []
for cond, metrics in results.items():
for r in metrics.get("results", []):
detailed_results.append(r)
with open(detailed_path, "w") as f:
json.dump(detailed_results, f, indent=2)
print(f"Detailed results saved to: {detailed_path}")
if __name__ == "__main__":
main()