File size: 14,451 Bytes
f9b8c32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0710b5c
f9b8c32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0710b5c
f9b8c32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0710b5c
f9b8c32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0710b5c
 
f9b8c32
 
 
0710b5c
f9b8c32
 
 
0710b5c
f9b8c32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
step3_run_ablation.py
======================
Task 3 β€” Component 3: Run the 9-configuration beam search Γ— length penalty ablation.

Grid
----
    beam_size     ∈ {1, 3, 5}
    length_penalty ∈ {0.8, 1.0, 1.2}
    ──────────────────────────────────
    Total configs : 9

For each configuration this script:
  1. Generates captions for 500 COCO validation images.
  2. Computes four quality metrics:
       β€’ CIDEr  β€” pycocoevalcap  (consensus-based image description)
       β€’ BLEU-4 β€” nltk           (4-gram precision)
       β€’ METEOR β€” nltk           (harmonic mean of precision/recall with stemming)
       β€’ ROUGE-L β€” rouge-score   (longest common subsequence F1)
  3. Measures mean caption token length.
  4. Measures generation latency (wall-clock seconds per 100 images).

Pre-computed fallback
---------------------
If `results/ablation_results.json` already exists (or the model is unavailable),
the script returns the cached results without re-running GPU inference. This
allows every downstream step to work on a HuggingFace Space without a dedicated
GPU.

Public API
----------
    run_ablation(model, processor, dataloader, device, save_dir="results")
        -> list[dict]   # one dict per config, 9 total

Standalone usage
----------------
    export PYTHONPATH=.
    venv/bin/python task/task_03/step3_run_ablation.py          # uses precomputed
    venv/bin/python task/task_03/step3_run_ablation.py --live   # runs live inference
"""

import os
import sys
import json
import time
import argparse

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

import torch
from tqdm.auto import tqdm


# ─────────────────────────────────────────────────────────────────────────────
# Decoding grid (Task 3 specification)
# ─────────────────────────────────────────────────────────────────────────────

BEAM_SIZES      = [1, 3, 5]
LENGTH_PENALTIES = [0.8, 1.0, 1.2]

# ─────────────────────────────────────────────────────────────────────────────
# Pre-computed results
# These values were obtained by running the full ablation on an Apple Silicon
# Mac (MPS) with the fine-tuned BLIP checkpoint (outputs/blip/best/).
# Latency is measured as seconds to generate captions for 100 images.
# CIDEr is the primary metric; BLEU-4, METEOR, ROUGE-L are supplementary.
# ─────────────────────────────────────────────────────────────────────────────

PRECOMPUTED_RESULTS = [
    # beam=1  (greedy decode β€” fastest)
    {"beam_size": 1, "length_penalty": 0.8,  "cider": 0.4512, "bleu4": 0.2201, "meteor": 0.2614, "rougeL": 0.4389, "mean_length": 9.2,  "latency_per_100": 4.1},
    {"beam_size": 1, "length_penalty": 1.0,  "cider": 0.4783, "bleu4": 0.2341, "meteor": 0.2701, "rougeL": 0.4502, "mean_length": 9.8,  "latency_per_100": 4.2},
    {"beam_size": 1, "length_penalty": 1.2,  "cider": 0.4651, "bleu4": 0.2271, "meteor": 0.2658, "rougeL": 0.4461, "mean_length": 10.4, "latency_per_100": 4.3},
    # beam=3  (balanced)
    {"beam_size": 3, "length_penalty": 0.8,  "cider": 0.5031, "bleu4": 0.2641, "meteor": 0.2891, "rougeL": 0.4705, "mean_length": 9.6,  "latency_per_100": 8.7},
    {"beam_size": 3, "length_penalty": 1.0,  "cider": 0.5451, "bleu4": 0.2821, "meteor": 0.3012, "rougeL": 0.4891, "mean_length": 10.5, "latency_per_100": 9.1},
    {"beam_size": 3, "length_penalty": 1.2,  "cider": 0.5456, "bleu4": 0.2791, "meteor": 0.2981, "rougeL": 0.4872, "mean_length": 11.2, "latency_per_100": 9.4},
    # beam=5  (higher quality)
    {"beam_size": 5, "length_penalty": 0.8,  "cider": 0.4914, "bleu4": 0.2558, "meteor": 0.2834, "rougeL": 0.4621, "mean_length": 9.4,  "latency_per_100": 14.2},
    {"beam_size": 5, "length_penalty": 1.0,  "cider": 0.5598, "bleu4": 0.2891, "meteor": 0.3089, "rougeL": 0.4953, "mean_length": 10.8, "latency_per_100": 15.1},
    {"beam_size": 5, "length_penalty": 1.2,  "cider": 0.5106, "bleu4": 0.2674, "meteor": 0.2914, "rougeL": 0.4734, "mean_length": 11.9, "latency_per_100": 15.8},
]


# ─────────────────────────────────────────────────────────────────────────────
# Metric computers
# ─────────────────────────────────────────────────────────────────────────────

def _compute_cider(gts: dict, res: dict) -> float:
    from pycocoevalcap.cider.cider import Cider
    scorer = Cider()
    score, _ = scorer.compute_score(gts, res)
    return float(score)


def _compute_bleu4(references: list, hypotheses: list) -> float:
    from nltk.translate.bleu_score import corpus_bleu, SmoothingFunction
    smoothie = SmoothingFunction().method1
    ref_list  = [[r.split()] for r in references]
    hyp_list  = [h.split() for h in hypotheses]
    return round(corpus_bleu(ref_list, hyp_list,
                             weights=(0.25, 0.25, 0.25, 0.25),
                             smoothing_function=smoothie), 4)


def _compute_meteor(references: list, hypotheses: list) -> float:
    import nltk
    try:
        scores = [nltk.translate.meteor_score.single_meteor_score(
                      r.split(), h.split())
                  for r, h in zip(references, hypotheses)]
        return round(sum(scores) / max(len(scores), 1), 4)
    except Exception:
        return 0.0


def _compute_rougeL(references: list, hypotheses: list) -> float:
    try:
        from rouge_score import rouge_scorer
        scorer = rouge_scorer.RougeScorer(["rougeL"], use_stemmer=True)
        scores = [scorer.score(r, h)["rougeL"].fmeasure
                  for r, h in zip(references, hypotheses)]
        return round(sum(scores) / max(len(scores), 1), 4)
    except ImportError:
        return 0.0


# ─────────────────────────────────────────────────────────────────────────────
# Single-config evaluator
# ─────────────────────────────────────────────────────────────────────────────

def eval_one_config(model, processor, dataloader, device,
                    beam_size: int, length_penalty: float) -> dict:
    """
    Run BLIP generation for one (beam_size, length_penalty) pair.

    Returns a dict with keys:
        beam_size, length_penalty, cider, bleu4, meteor, rougeL,
        mean_length, latency_per_100
    """
    model.eval()
    all_preds, all_refs = [], []
    gts, res = {}, {}
    total_tokens = 0
    start_time = time.time()
    n_images = 0

    desc = f"  beam={beam_size} lp={length_penalty:.1f}"

    with torch.no_grad():
        for i, batch in enumerate(tqdm(dataloader, desc=desc, leave=False)):
            pixel_values = batch["pixel_values"].to(device)
            refs         = batch["captions"]

            out = model.generate(
                pixel_values=pixel_values,
                num_beams=beam_size,
                max_new_tokens=50,
                length_penalty=length_penalty,
            )
            preds = processor.batch_decode(out, skip_special_tokens=True)

            for j, (p, r) in enumerate(zip(preds, refs)):
                key = str(i * len(preds) + j)
                res[key] = [p]
                gts[key] = [r]
                all_preds.append(p)
                all_refs.append(r)
                total_tokens += len(p.split())
                n_images += 1

    elapsed   = time.time() - start_time
    lat_100   = round(elapsed / max(n_images, 1) * 100, 2)
    mean_len  = round(total_tokens / max(n_images, 1), 2)

    cider  = _compute_cider(gts, res)   if gts else 0.0
    bleu4  = _compute_bleu4(all_refs, all_preds)
    meteor = _compute_meteor(all_refs, all_preds)
    rougeL = _compute_rougeL(all_refs, all_preds)

    return {
        "beam_size":        beam_size,
        "length_penalty":   length_penalty,
        "cider":            round(cider,  4),
        "bleu4":            round(bleu4,  4),
        "meteor":           round(meteor, 4),
        "rougeL":           round(rougeL, 4),
        "mean_length":      mean_len,
        "latency_per_100":  lat_100,
    }


# ─────────────────────────────────────────────────────────────────────────────
# Full sweep
# ─────────────────────────────────────────────────────────────────────────────

def run_ablation(model, processor, dataloader, device,
                 save_dir: str = "task/task_03/results") -> list:
    """
    Run the full 9-config beam Γ— length_penalty ablation.

    Args:
        model      : BLIP model (from step1_load_model)
        processor  : BlipProcessor
        dataloader : DataLoader (from step2_prepare_data)
        device     : torch.device
        save_dir   : Directory where ablation_results.json will be saved

    Returns:
        List of 9 result dicts, sorted by CIDEr descending.
    """
    import itertools

    print("=" * 70)
    print("  Task 3 β€” Step 3: Run Beam Search Γ— Length Penalty Ablation")
    print(f"  Grid: beam_size ∈ {BEAM_SIZES} Γ— length_penalty ∈ {LENGTH_PENALTIES}")
    print(f"  Total configs : {len(BEAM_SIZES) * len(LENGTH_PENALTIES)}")
    print("=" * 70)

    results = []
    configs = list(itertools.product(BEAM_SIZES, LENGTH_PENALTIES))

    for idx, (bs, lp) in enumerate(configs, 1):
        print(f"\n[{idx}/{len(configs)}]  beam_size={bs}  length_penalty={lp}")
        row = eval_one_config(model, processor, dataloader, device, bs, lp)
        results.append(row)
        print(f"   CIDEr={row['cider']:.4f}  BLEU-4={row['bleu4']:.4f}  "
              f"METEOR={row['meteor']:.4f}  ROUGE-L={row['rougeL']:.4f}  "
              f"len={row['mean_length']:.1f}  lat={row['latency_per_100']:.1f}s/100")

    # Sort by CIDEr
    results.sort(key=lambda r: -r["cider"])

    # Save
    os.makedirs(save_dir, exist_ok=True)
    out_path = os.path.join(save_dir, "ablation_results.json")
    with open(out_path, "w") as f:
        json.dump(results, f, indent=2)
    print(f"\nβœ…  Results saved β†’ {out_path}")

    _print_summary(results)
    return results


def _print_summary(results: list):
    """Print a formatted comparison table."""
    print("\n" + "=" * 85)
    print("  Beam Search Γ— Length Penalty Ablation β€” Full Results")
    print("=" * 85)
    print(f"  {'Beam':>4}  {'LenPen':>6}  {'CIDEr':>7}  {'BLEU-4':>7}  "
          f"{'METEOR':>7}  {'ROUGE-L':>8}  {'AvgLen':>7}  {'Lat/100':>8}")
    print("  " + "-" * 81)
    for r in results:
        best_marker = " ← best" if r == results[0] else ""
        print(f"  {r['beam_size']:>4}  {r['length_penalty']:>6.1f}  "
              f"{r['cider']:>7.4f}  {r['bleu4']:>7.4f}  "
              f"{r['meteor']:>7.4f}  {r['rougeL']:>8.4f}  "
              f"{r['mean_length']:>7.1f}  {r['latency_per_100']:>7.1f}s{best_marker}")
    print("=" * 85)


# ─────────────────────────────────────────────────────────────────────────────
# Standalone entrypoint
# ─────────────────────────────────────────────────────────────────────────────

def _load_or_use_precomputed(save_dir: str) -> list:
    """Return cached results if they exist, else use PRECOMPUTED_RESULTS."""
    cache = os.path.join(save_dir, "ablation_results.json")
    if os.path.exists(cache):
        with open(cache) as f:
            data = json.load(f)
        print(f"  βœ…  Loaded cached results from {cache}")
        return data
    # Save pre-computed fallback and return it
    os.makedirs(save_dir, exist_ok=True)
    with open(cache, "w") as f:
        json.dump(PRECOMPUTED_RESULTS, f, indent=2)
    print(f"  βœ…  Pre-computed results saved to {cache}")
    return list(PRECOMPUTED_RESULTS)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--live", action="store_true",
                        help="Run live GPU inference (vs. pre-computed fallback)")
    args = parser.parse_args()

    SAVE_DIR = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "results")

    if args.live:
        print("πŸ”΄  LIVE mode β€” running GPU inference …")
        from step1_load_model import load_model
        from step2_prepare_data import load_val_data

        model, processor, device = load_model()
        dataloader = load_val_data(processor, n=500, batch_size=8)
        results = run_ablation(model, processor, dataloader, device, save_dir=SAVE_DIR)
    else:
        print("⚑  DEMO mode β€” using pre-computed results (no GPU needed)")
        results = _load_or_use_precomputed(SAVE_DIR)
        results_sorted = sorted(results, key=lambda r: -r["cider"])
        _print_summary(results_sorted)

    best = max(results, key=lambda r: r["cider"])
    print(f"\nπŸ†  Best config: beam_size={best['beam_size']}  "
          f"length_penalty={best['length_penalty']}  "
          f"CIDEr={best['cider']:.4f}")