File size: 12,554 Bytes
65fb4ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import math
import os
from functools import partial

import numpy as np
import torch
import torch.nn.functional as F
import transformers
from torch.utils.data import DataLoader

from configs import args
from datasets import REFAVS
from decoder_invariance_check import build_model, set_seed
from load_model import collate_fn, dict_to_cuda


def make_loader(tokenizer):
    dataset = REFAVS(args.eval_split, args, tokenizer, input_type="refer")
    return DataLoader(
        dataset,
        batch_size=1,
        shuffle=False,
        num_workers=0,
        collate_fn=partial(collate_fn, tokenizer=tokenizer),
    )


def build_tokenizer():
    tokenizer = transformers.AutoTokenizer.from_pretrained(
        args.mllm,
        cache_dir=None,
        model_max_length=2048,
        padding_side="right",
        use_fast=False,
    )
    tokenizer.pad_token = tokenizer.unk_token
    tokenizer.add_tokens("[SEG]")
    seg_token_idx = tokenizer("[SEG]", add_special_tokens=False).input_ids[0]
    return tokenizer, seg_token_idx


def get_q(model, batch):
    with torch.cuda.amp.autocast(dtype=torch.bfloat16):
        output = model.forward(
            images=batch["images"],
            images_clip=batch["images_clip"],
            audio_features=batch["audio_feats"],
            image_features=batch["image_feats"],
            input_ids=batch["input_ids"],
            labels=batch["labels"],
            attention_masks=batch["attention_masks"],
            masks_list=batch["masks"],
            resize_list=batch["resizes"],
            orgsize_list=batch["orgsizes"],
            conversation_list=batch["convs"],
            refs_num=batch["refs_num"],
            fids=batch["fids"],
            vids=batch["vids"],
            contrast=args.ct_weight,
            ref_ids=batch["ref_ids"],
            inference=True,
        )
    return output["seg_embeddings"][0][0].float()


def decode_low_res(model, batch, q):
    visual_model = model.get_model().visual_model
    sparse, dense = visual_model.prompt_encoder(
        points=None,
        boxes=None,
        masks=None,
        text_embeds=q.view(1, 1, -1).to(next(visual_model.parameters()).dtype),
    )
    sparse = sparse.to(q.dtype)
    dense = dense.to(q.dtype)

    with torch.cuda.amp.autocast(dtype=torch.bfloat16):
        low_res_masks, iou_predictions = visual_model.mask_decoder(
            image_embeddings=batch["image_feats"][0],
            image_pe=visual_model.prompt_encoder.get_dense_pe(),
            sparse_prompt_embeddings=sparse,
            dense_prompt_embeddings=dense,
            multimask_output=False,
        )
    return low_res_masks.float(), iou_predictions.float().squeeze(-1)


def masks_to_64(mask_logits_or_binary):
    if mask_logits_or_binary.ndim == 3:
        mask_logits_or_binary = mask_logits_or_binary.unsqueeze(1)
    return F.interpolate(
        mask_logits_or_binary.float(),
        size=(64, 64),
        mode="bilinear",
        align_corners=False,
    ).clamp(0.0, 1.0)


def d2_scores(image_embeddings, mask64, q, beta):
    feats = image_embeddings.float()
    if mask64.shape[0] != feats.shape[0]:
        raise ValueError(f"Mask/frame mismatch: {mask64.shape} vs {feats.shape}")

    q = F.normalize(q.float().view(1, -1), dim=-1)
    mask = mask64.float()
    comp = 1.0 - mask

    z_in = (feats * mask).sum(dim=(2, 3)) / mask.sum(dim=(2, 3)).clamp_min(1e-6)
    z_out = (feats * comp).sum(dim=(2, 3)) / comp.sum(dim=(2, 3)).clamp_min(1e-6)

    z_in = F.normalize(z_in, dim=-1)
    z_out = F.normalize(z_out, dim=-1)
    return (z_in @ q.T).squeeze(-1) - beta * (z_out @ q.T).squeeze(-1)


def frame_iou(pred_logits, gt_masks):
    pred = (torch.sigmoid(pred_logits.float()) > 0.4).float()
    gt = gt_masks.float()
    if pred.ndim == 4:
        pred = pred.squeeze(1)
    inter = (pred * gt).sum(dim=(1, 2))
    union = torch.maximum(pred, gt).sum(dim=(1, 2))
    num_pixels = pred.shape[-1] * pred.shape[-2]
    no_obj = gt.sum(dim=(1, 2)) == 0
    inter_no_obj = ((1.0 - pred) * (1.0 - gt)).sum(dim=(1, 2))
    inter = torch.where(no_obj, inter_no_obj, inter)
    union = torch.where(no_obj, torch.full_like(union, float(num_pixels)), union)
    return inter / union.clamp_min(1e-7)


def frame_fscore_proxy(pred_logits, gt_masks):
    pred = (torch.sigmoid(pred_logits.float()) > 0.4).float()
    gt = gt_masks.float()
    if pred.ndim == 4:
        pred = pred.squeeze(1)
    tp = (pred * gt).sum(dim=(1, 2))
    precision = tp / pred.sum(dim=(1, 2)).clamp_min(1e-7)
    recall = tp / gt.sum(dim=(1, 2)).clamp_min(1e-7)
    beta2 = 0.3
    fscore = (1 + beta2) * precision * recall / (beta2 * precision + recall).clamp_min(1e-7)
    no_obj = gt.sum(dim=(1, 2)) == 0
    return torch.where(no_obj, torch.zeros_like(fscore), fscore)


def parse_betas():
    raw = os.environ.get("D2_BETAS", "0.5")
    return [float(x.strip()) for x in raw.split(",") if x.strip()]


def collect_q_pool(model, tokenizer, limit):
    q_pool = []
    loader = make_loader(tokenizer)
    for sample_idx, batch in enumerate(loader):
        if sample_idx >= limit:
            break
        batch = dict_to_cuda(batch)
        q = get_q(model, batch)
        q_pool.append(
            {
                "sample_idx": sample_idx,
                "vid": batch["vids"][0],
                "ref": batch["refs"][0][0],
                "fid": int(batch["fids"][0][0]),
                "q": q.cpu(),
            }
        )
        print(f"Collected q {sample_idx}: vid={q_pool[-1]['vid']} ref={q_pool[-1]['ref']}")
    if not q_pool:
        raise RuntimeError("No q vectors collected. Is the selected split empty?")
    return q_pool


def choose_shuffled_idx(sample_idx, q_pool):
    if len(q_pool) <= 1:
        return None
    return (sample_idx + 1) % len(q_pool)


def choose_wrong_ref_idx(sample_idx, q_pool):
    current = q_pool[sample_idx]
    for item in q_pool:
        if item["sample_idx"] == sample_idx:
            continue
        if item["vid"] == current["vid"] and item["fid"] != current["fid"]:
            return item["sample_idx"]
    for item in q_pool:
        if item["sample_idx"] == sample_idx:
            continue
        if item["vid"] == current["vid"] and item["ref"] != current["ref"]:
            return item["sample_idx"]
    return None


def run_d2(model, tokenizer, q_pool, betas, limit):
    rows = []
    loader = make_loader(tokenizer)
    q_lookup = {item["sample_idx"]: item for item in q_pool}
    generator = torch.Generator(device="cuda")
    generator.manual_seed(1234)

    for sample_idx, batch in enumerate(loader):
        if sample_idx >= limit:
            break
        batch = dict_to_cuda(batch)
        item = q_lookup[sample_idx]
        real_q = item["q"].cuda()

        low_res_masks, iou_predictions = decode_low_res(model, batch, real_q)
        pred_mask64 = masks_to_64(torch.sigmoid(low_res_masks))
        gt_masks = batch["masks"][0][0].float()
        gt_mask64 = masks_to_64(gt_masks)
        image_embeddings = batch["image_feats"][0].float()

        pred_logits_hr = model.get_model().visual_model.postprocess_masks(
            low_res_masks.to(batch["image_feats"][0].dtype),
            input_size=batch["resizes"][0],
            original_size=batch["orgsizes"][0],
        ).squeeze(1)

        frame_ious = frame_iou(pred_logits_hr, gt_masks)
        frame_fscores = frame_fscore_proxy(pred_logits_hr, gt_masks)
        pred_area = (torch.sigmoid(pred_logits_hr.float()) > 0.4).float().mean(dim=(1, 2))
        gt_area = gt_masks.float().mean(dim=(1, 2))

        shuffled_idx = choose_shuffled_idx(sample_idx, q_pool)
        wrong_ref_idx = choose_wrong_ref_idx(sample_idx, q_pool)
        q_controls = [
            ("real", real_q, sample_idx),
            ("random", torch.randn(real_q.shape, device=real_q.device, generator=generator), None),
        ]
        if shuffled_idx is not None:
            q_controls.append(("shuffled", q_lookup[shuffled_idx]["q"].cuda(), shuffled_idx))
        if wrong_ref_idx is not None:
            q_controls.append(("wrong_ref", q_lookup[wrong_ref_idx]["q"].cuda(), wrong_ref_idx))

        for beta in betas:
            for q_type, q, q_source_idx in q_controls:
                pred_scores = d2_scores(image_embeddings, pred_mask64, q, beta)
                gt_scores = d2_scores(image_embeddings, gt_mask64, q, beta)
                base_info = {
                    "sample_idx": sample_idx,
                    "vid": item["vid"],
                    "ref": item["ref"],
                    "fid": item["fid"],
                    "split": args.eval_split,
                    "frame_iou": math.nan,
                    "frame_fscore_proxy": math.nan,
                    "iou_pred": math.nan,
                    "pred_area": math.nan,
                    "gt_area": math.nan,
                }
                for frame_idx in range(pred_scores.shape[0]):
                    base_info_frame = dict(base_info)
                    base_info_frame.update(
                        {
                            "frame_iou": frame_ious[frame_idx].item(),
                            "frame_fscore_proxy": frame_fscores[frame_idx].item(),
                            "iou_pred": iou_predictions[frame_idx].item(),
                            "pred_area": pred_area[frame_idx].item(),
                            "gt_area": gt_area[frame_idx].item(),
                        }
                    )
                    row = dict(base_info_frame)
                    row.update(
                        {
                            "frame": frame_idx,
                            "q_type": q_type,
                            "beta": beta,
                            "s_pred": pred_scores[frame_idx].item(),
                            "s_gt": gt_scores[frame_idx].item(),
                            "q_source_idx": q_source_idx if q_source_idx is not None else "",
                        }
                    )
                    rows.append(row)

        real_rows = [
            r for r in rows if r["sample_idx"] == sample_idx and r["q_type"] == "real" and r["beta"] == betas[0]
        ]
        s_pred_values = [r["s_pred"] for r in real_rows]
        print(
            f"D2 {sample_idx}: vid={item['vid']} ref={item['ref']} "
            f"mean_s_pred={np.mean(s_pred_values):.4f} min_s_pred={np.min(s_pred_values):.4f} "
            f"mean_iou={frame_ious.mean().item():.4f}"
        )

    return rows


def print_summary(rows):
    real_rows = [r for r in rows if r["q_type"] == "real"]
    if not real_rows:
        return
    by_beta = sorted(set(r["beta"] for r in real_rows))
    print("\nSummary")
    print(f"rows: {len(rows)}")
    for beta in by_beta:
        beta_rows = [r for r in rows if r["beta"] == beta]
        print(f"\nbeta={beta}")
        for q_type in sorted(set(r["q_type"] for r in beta_rows)):
            qr = [r for r in beta_rows if r["q_type"] == q_type]
            print(
                f"{q_type:10s} "
                f"mean_s_pred={np.mean([r['s_pred'] for r in qr]):+.4f} "
                f"mean_s_gt={np.mean([r['s_gt'] for r in qr]):+.4f}"
            )
        real_beta = [r for r in beta_rows if r["q_type"] == "real"]
        s_pred = np.array([r["s_pred"] for r in real_beta])
        frame_iou_values = np.array([r["frame_iou"] for r in real_beta])
        if len(s_pred) > 1 and np.std(s_pred) > 1e-8 and np.std(frame_iou_values) > 1e-8:
            corr = np.corrcoef(s_pred, frame_iou_values)[0, 1]
            print(f"corr(real s_pred, frame_iou)={corr:+.4f}")
        else:
            print("corr(real s_pred, frame_iou)=nan")


def main():
    set_seed(42)
    torch.set_grad_enabled(False)
    betas = parse_betas()
    tokenizer, seg_token_idx = build_tokenizer()
    limit = args.max_eval_rows if args.max_eval_rows > 0 else 30
    print(f"Split: {args.eval_split} | samples: {limit} | betas: {betas}")

    model = build_model(tokenizer, seg_token_idx)
    q_pool = collect_q_pool(model, tokenizer, limit)
    rows = run_d2(model, tokenizer, q_pool, betas, limit)
    print_summary(rows)

    csv_path = os.environ.get("D2_BASIC_CSV", f"/workspace/SimToken/d2_basic_{args.eval_split}_{limit}.csv")
    os.makedirs(os.path.dirname(os.path.abspath(csv_path)), exist_ok=True)
    with open(csv_path, "w", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=list(rows[0].keys()))
        writer.writeheader()
        writer.writerows(rows)
    print(f"\nSaved CSV: {csv_path}")


if __name__ == "__main__":
    main()