File size: 7,989 Bytes
24e41d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
UniSITH Demo: Analyze a DINOv2 model using captioned images as concept pool.

This script demonstrates the full UniSITH pipeline:
1. Load a unimodal ViT model (DINOv2-large)
2. Build a visual concept pool from Recap-COCO-30K
3. Analyze attention heads via SVD + COMP
4. Display human-interpretable concept attributions

Usage:
    python run_unisith.py --model facebook/dinov2-large --max-concepts 1000
    python run_unisith.py --model openai/clip-vit-large-patch14 --architecture clip
"""

import argparse
import torch
import os
import sys
import json
from transformers import AutoModel, AutoProcessor, AutoImageProcessor
from transformers import CLIPModel, CLIPProcessor
from datasets import load_dataset

# Add parent dir to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from unimodal_sith.concept_pool import VisualConceptPool
from unimodal_sith.unisith import UniSITH


# Model configurations
MODEL_CONFIGS = {
    "facebook/dinov2-large": {
        "architecture": "dinov2",
        "n_heads": 16,
        "d_model": 1024,
    },
    "facebook/dinov2-base": {
        "architecture": "dinov2",
        "n_heads": 12,
        "d_model": 768,
    },
    "facebook/dinov2-small": {
        "architecture": "dinov2",
        "n_heads": 6,
        "d_model": 384,
    },
    "openai/clip-vit-large-patch14": {
        "architecture": "clip",
        "n_heads": 16,
        "d_model": 1024,
    },
    "openai/clip-vit-base-patch16": {
        "architecture": "clip",
        "n_heads": 12,
        "d_model": 768,
    },
    "google/vit-large-patch16-224": {
        "architecture": "vit",
        "n_heads": 16,
        "d_model": 1024,
    },
    "google/vit-base-patch16-224": {
        "architecture": "vit",
        "n_heads": 12,
        "d_model": 768,
    },
}


def load_model_and_processor(model_name: str, architecture: str):
    """Load model and processor based on architecture type."""
    print(f"Loading model: {model_name}")
    
    if architecture == "clip":
        model = CLIPModel.from_pretrained(model_name)
        processor = CLIPProcessor.from_pretrained(model_name)
    elif architecture == "dinov2":
        model = AutoModel.from_pretrained(model_name)
        processor = AutoImageProcessor.from_pretrained(model_name)
    elif architecture == "vit":
        model = AutoModel.from_pretrained(model_name)
        processor = AutoImageProcessor.from_pretrained(model_name)
    else:
        raise ValueError(f"Unknown architecture: {architecture}")
    
    model.eval()
    return model, processor


def build_concept_pool(
    model,
    processor,
    architecture: str,
    max_concepts: int = 1000,
    cache_path: str = None,
    device: str = "cpu",
):
    """Build visual concept pool from Recap-COCO-30K."""
    print(f"Building concept pool with {max_concepts} concepts...")
    
    # Load dataset
    dataset = load_dataset("UCSC-VLAA/Recap-COCO-30K", split="train")
    
    pool = VisualConceptPool.from_dataset(
        dataset=dataset,
        model=model,
        processor=processor,
        architecture=architecture,
        image_column="image",
        caption_column="caption",  # Short COCO captions for readability
        image_id_column="image_id",
        batch_size=32,
        max_concepts=max_concepts,
        device=device,
        cache_path=cache_path,
    )
    
    return pool


def print_results(results, max_sv=3, max_heads=4):
    """Pretty-print analysis results."""
    print("\n" + "=" * 80)
    print("UniSITH Analysis Results")
    print("=" * 80)
    
    for layer_idx in sorted(results.keys()):
        heads = results[layer_idx]
        print(f"\n{'─' * 80}")
        print(f"LAYER {layer_idx}")
        print(f"{'─' * 80}")
        
        for head in heads[:max_heads]:
            print(f"\n  Head {head.head_idx}:")
            for sv in head.singular_vectors[:max_sv]:
                print(f"    SV {sv.sv_idx} (σ={sv.singular_value:.4f}, "
                      f"fidelity={sv.fidelity:.4f}):")
                for caption, coeff in zip(sv.concepts, sv.coefficients):
                    print(f"      [{coeff:.4f}] {caption}")


def main():
    parser = argparse.ArgumentParser(description="UniSITH: Unimodal SITH Analysis")
    parser.add_argument(
        "--model", type=str, default="facebook/dinov2-base",
        help="Model name/path"
    )
    parser.add_argument(
        "--architecture", type=str, default=None,
        help="Architecture type (auto-detected from model name if not set)"
    )
    parser.add_argument(
        "--max-concepts", type=int, default=1000,
        help="Maximum concepts in the pool"
    )
    parser.add_argument(
        "--layers", type=int, nargs="+", default=None,
        help="Layers to analyze (default: last 4)"
    )
    parser.add_argument(
        "--n-sv", type=int, default=5,
        help="Number of singular vectors per head"
    )
    parser.add_argument(
        "--K", type=int, default=5,
        help="Concepts per singular vector"
    )
    parser.add_argument(
        "--lambda-coh", type=float, default=0.3,
        help="COMP coherence weight"
    )
    parser.add_argument(
        "--method", type=str, default="comp", choices=["comp", "top_k"],
        help="Concept attribution method"
    )
    parser.add_argument(
        "--device", type=str, default="cpu",
        help="Device (cpu/cuda)"
    )
    parser.add_argument(
        "--cache-dir", type=str, default="./cache",
        help="Cache directory for concept embeddings"
    )
    parser.add_argument(
        "--output", type=str, default="./results/unisith_results.json",
        help="Output JSON path"
    )
    
    args = parser.parse_args()
    
    # Auto-detect architecture
    if args.architecture is None:
        if args.model in MODEL_CONFIGS:
            config = MODEL_CONFIGS[args.model]
            args.architecture = config["architecture"]
            n_heads = config["n_heads"]
            d_model = config["d_model"]
        else:
            raise ValueError(
                f"Unknown model {args.model}. Specify --architecture manually or use "
                f"one of: {list(MODEL_CONFIGS.keys())}"
            )
    else:
        if args.model in MODEL_CONFIGS:
            config = MODEL_CONFIGS[args.model]
            n_heads = config["n_heads"]
            d_model = config["d_model"]
        else:
            raise ValueError(
                f"Model {args.model} not in MODEL_CONFIGS. Add it or specify n_heads/d_model."
            )
    
    device = args.device
    if device == "cuda" and not torch.cuda.is_available():
        print("CUDA not available, falling back to CPU")
        device = "cpu"
    
    # Load model
    model, processor = load_model_and_processor(args.model, args.architecture)
    model = model.to(device)
    
    # Build concept pool
    cache_path = os.path.join(
        args.cache_dir,
        f"concept_pool_{args.model.replace('/', '_')}_{args.max_concepts}.pt"
    )
    
    pool = build_concept_pool(
        model=model,
        processor=processor,
        architecture=args.architecture,
        max_concepts=args.max_concepts,
        cache_path=cache_path,
        device=device,
    )
    
    print(f"Concept pool: {pool.num_concepts} concepts, dim={pool.embed_dim}")
    
    # Create UniSITH analyzer
    analyzer = UniSITH(
        model=model,
        architecture=args.architecture,
        n_heads=n_heads,
        d_model=d_model,
        concept_pool=pool,
        device=device,
    )
    
    # Run analysis
    results = analyzer.analyze_model(
        layers=args.layers,
        n_singular_vectors=args.n_sv,
        K=args.K,
        lambda_coh=args.lambda_coh,
        method=args.method,
    )
    
    # Print results
    print_results(results)
    
    # Save results
    UniSITH.save_results(results, args.output)
    
    print(f"\nDone! Results saved to {args.output}")


if __name__ == "__main__":
    main()