File size: 11,445 Bytes
519ab98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""AY refine: A from BCE head + fixed Y → Top-K / r rejection CSV."""

from __future__ import annotations

import json
import os
from typing import Dict, List, Optional, Sequence, Tuple

import numpy as np
import pandas as pd
import torch
from sklearn.preprocessing import LabelEncoder


def load_y_logits_json(path: str) -> Tuple[List[str], np.ndarray]:
    """Load deep_ellipse_logits.json → (image_ids, Y[N,C])."""
    with open(path, "r", encoding="utf-8") as f:
        rows = json.load(f)
    image_ids = [str(r["image_id"]) for r in rows]
    y = np.asarray([r["logits"] for r in rows], dtype=np.float32)
    return image_ids, y


def otsu_threshold_1d(values: np.ndarray) -> float:
    """Otsu on 1-D float array (off-diagonal affinities)."""
    v = np.asarray(values, dtype=np.float64).ravel()
    v = v[np.isfinite(v)]
    if v.size == 0:
        return 0.0
    vmin, vmax = float(v.min()), float(v.max())
    if vmax - vmin < 1e-12:
        return vmin
    hist, bin_edges = np.histogram(v, bins=256, range=(vmin, vmax))
    hist = hist.astype(np.float64)
    total = hist.sum()
    if total <= 0:
        return 0.5 * (vmin + vmax)
    bin_centers = 0.5 * (bin_edges[:-1] + bin_edges[1:])
    weight1 = np.cumsum(hist)
    weight2 = total - weight1
    sum_all = np.cumsum(hist * bin_centers)
    mean1 = sum_all / np.maximum(weight1, 1e-12)
    mean2 = (sum_all[-1] - sum_all) / np.maximum(weight2, 1e-12)
    inter = weight1[:-1] * weight2[:-1] * (mean1[:-1] - mean2[:-1]) ** 2
    idx = int(np.argmax(inter))
    return float(bin_centers[idx])


def build_a_norm(
    a: np.ndarray,
    *,
    power_k: float = 2.0,
    alpha: Optional[float] = None,
    sample_offdiag: int = 2_000_000,
    clamp_neg: bool = True,
) -> Tuple[np.ndarray, float]:
    """
    A'[i,j] = max(0, A[i,j] - α) for i != j; diagonal restored as A[i,i];
    A_norm = row-normalize (A')^k.
    α from Otsu on off-diagonal (optionally subsampled).

    clamp_neg=True (default): truncate the shifted affinities at 0, i.e. the
    current paper/code construction A' = max(0, A - α) on the off-diagonal.
    clamp_neg=False: keep the signed shift A' = A - α (diagonal still restored),
    which is the literal main-text formula A' = (A - α) + αI. Because the power
    ^k is applied AFTER the shift, negative entries fold back to positive for
    even k (e.g. k=2: A_ij=0.01 and 0.99 both give 0.2401). A non-integer
    power_k with clamp_neg=False would raise (negative base → NaN), so it is
    rejected up front.
    """
    a = np.asarray(a, dtype=np.float32)
    n = a.shape[0]
    assert a.shape == (n, n)
    if not clamp_neg and float(power_k) != int(power_k):
        raise ValueError(
            f"clamp_neg=False requires an integer power_k (got {power_k!r}); "
            "a fractional power of a negative base is undefined."
        )

    if alpha is None:
        # sample off-diagonal for Otsu
        if n * (n - 1) > sample_offdiag:
            rng = np.random.default_rng(0)
            i = rng.integers(0, n, size=sample_offdiag)
            j = rng.integers(0, n, size=sample_offdiag)
            mask = i != j
            off = a[i[mask], j[mask]]
        else:
            eye = np.eye(n, dtype=bool)
            off = a[~eye]
        alpha = otsu_threshold_1d(off)

    ap = a - float(alpha)
    np.fill_diagonal(ap, a.diagonal())  # restore diag: (A-α)+αI → diag stays A_ii
    # Ensure non-negative for power (clamp) unless the un-clamped variant is asked
    if clamp_neg:
        ap = np.maximum(ap, 0.0)
    if power_k != 1.0:
        ap = np.power(ap, power_k)
    row_sum = ap.sum(axis=1, keepdims=True)
    row_sum = np.maximum(row_sum, 1e-12)
    a_norm = ap / row_sum
    return a_norm.astype(np.float32), float(alpha)


def row_knn_lambda_thresholds(a: np.ndarray, *, knn_n: int = 50) -> np.ndarray:
    """
    Per-row lambda = (knn_n+1)-th largest off-diagonal affinity (1-indexed rank).

    For knn_n=50, lambda_i is the 51st largest value in row i (excluding diagonal).
    """
    a = np.asarray(a, dtype=np.float32)
    n = a.shape[0]
    assert a.shape == (n, n)
    rank_idx = int(knn_n)  # 0-based index of (knn_n+1)-th largest
    lambdas = np.zeros(n, dtype=np.float32)
    for i in range(n):
        row = a[i].copy()
        row[i] = -np.inf
        valid = row[np.isfinite(row) & (row > -np.inf)]
        if valid.size <= rank_idx:
            lambdas[i] = float(np.min(valid)) if valid.size else 0.0
        else:
            sorted_desc = np.sort(valid)[::-1]
            lambdas[i] = float(sorted_desc[rank_idx])
    return lambdas


def build_a_norm_knn_row_lambda(
    a: np.ndarray,
    *,
    knn_n: int = 50,
    power_k: float = 2.0,
) -> Tuple[np.ndarray, np.ndarray]:
    """
    A'[i,j] = max(0, A[i,j] - lambda_i) for j != i; diag stays A_ii;
    A_norm = row-normalize (A')^k with per-row lambda from row_knn_lambda_thresholds.
    """
    a = np.asarray(a, dtype=np.float32)
    n = a.shape[0]
    assert a.shape == (n, n)

    lambdas = row_knn_lambda_thresholds(a, knn_n=knn_n)
    ap = a - lambdas[:, None]
    np.fill_diagonal(ap, a.diagonal())
    ap = np.maximum(ap, 0.0)
    if power_k != 1.0:
        ap = np.power(ap, power_k)
    row_sum = ap.sum(axis=1, keepdims=True)
    row_sum = np.maximum(row_sum, 1e-12)
    a_norm = ap / row_sum
    return a_norm.astype(np.float32), lambdas


def ay_refine(
    y_raw: np.ndarray, a_norm: np.ndarray, *, graph_hops: int = 1
) -> np.ndarray:
    """Y_refined = A_norm^graph_hops @ Y_raw."""
    if graph_hops < 1:
        raise ValueError(f"graph_hops must be >= 1, got {graph_hops!r}")
    y = y_raw
    for _ in range(graph_hops):
        y = a_norm @ y
    return y


def decode_writer_id(class_idx: int, label_encoder: LabelEncoder) -> str:
    return str(label_encoder.classes_[class_idx])


def known_class_indices(label_encoder: LabelEncoder) -> np.ndarray:
    """Indices of known writers: every class slot except the unknown '-1'."""
    idxs = [i for i, c in enumerate(label_encoder.classes_) if str(c) != "-1"]
    return np.asarray(idxs, dtype=np.int64)


def scores_and_pred_from_y(
    y_refined: np.ndarray,
    label_encoder: LabelEncoder,
    *,
    reject_score: str = "logit",
) -> Tuple[np.ndarray, np.ndarray]:
    """
    Returns (scores[N], pred_cls[N]).
    reject_score='logit': max over known cols of Y_ref
    reject_score='softmax': max softmax prob over known cols
    """
    if reject_score not in ("logit", "softmax"):
        raise ValueError(f"reject_score must be logit|softmax, got {reject_score!r}")
    kidx = known_class_indices(label_encoder)
    y_k = np.asarray(y_refined, dtype=np.float64)[:, kidx]
    if reject_score == "softmax":
        y_k = y_k - y_k.max(axis=1, keepdims=True)
        exp = np.exp(y_k)
        probs = exp / np.maximum(exp.sum(axis=1, keepdims=True), 1e-12)
        local = probs.argmax(axis=1)
        scores = probs.max(axis=1)
    else:
        local = y_k.argmax(axis=1)
        scores = y_k.max(axis=1)
    pred_cls = kidx[local]
    return scores.astype(np.float64), pred_cls.astype(np.int64)


def apply_topk_reject(
    writers: Sequence[str],
    conf: Sequence[float],
    *,
    reject_ratio: float = 0.65,
    reject_score: str = "logit",
) -> Tuple[List[str], List[Optional[str]], Optional[float]]:
    """
    Apply Top-r rejection on precomputed argmax writers + confidence scores.
    Returns (final_writer_ids, reject_reasons, dyn_thresh).
    """
    n = len(writers)
    target_unknown = int(n * reject_ratio)
    final = [str(w) for w in writers]
    conf_arr = np.asarray(conf, dtype=np.float64)
    reject_reason: List[Optional[str]] = [None] * n
    n_natural = 0
    for i in range(n):
        if final[i] == "-1":
            reject_reason[i] = "invalid_prediction"
            n_natural += 1

    remaining = [i for i in range(n) if final[i] != "-1"]
    remaining_sorted = sorted(remaining, key=lambda i: conf_arr[i])
    n_dyn = max(0, target_unknown - n_natural)
    n_dyn = min(n_dyn, len(remaining_sorted))
    thresh: Optional[float] = None
    reason_tag = "low_confidence" if reject_score == "softmax" else "low_logit"
    for i in remaining_sorted[:n_dyn]:
        final[i] = "-1"
        reject_reason[i] = reason_tag
        thresh = float(conf_arr[i])
    return final, reject_reason, thresh


def _write_reject_csv(
    image_ids: Sequence[str],
    final: Sequence[str],
    conf: Sequence[float],
    reject_reason: Sequence[Optional[str]],
    *,
    out_path: str,
    reject_score: str,
    thresh: Optional[float],
) -> pd.DataFrame:
    n = len(image_ids)
    df = pd.DataFrame(
        {
            "image_id": list(image_ids),
            "writer_id": list(final),
            "confidence": np.asarray(conf, dtype=np.float64),
            "reject_reason": list(reject_reason),
        }
    )
    os.makedirs(os.path.dirname(out_path) or ".", exist_ok=True)
    df.to_csv(out_path, index=False)

    clean = out_path.replace(".csv", "_submit.csv")
    if clean == out_path:
        clean = out_path[:-4] + "_submit.csv"
    df[["image_id", "writer_id"]].to_csv(clean, index=False)

    n_unk = sum(1 for w in final if w == "-1")
    print(
        f"AY Top-K CSV ({reject_score}): n={n}, unknown={n_unk} "
        f"({n_unk / n * 100:.1f}%), "
        f"dyn_thresh={thresh if thresh is not None else 'N/A'}"
    )
    print(f"  saved: {out_path}")
    print(f"  submit: {clean}")
    return df


def resweep_reject_from_scored(
    image_ids: Sequence[str],
    writers: Sequence[str],
    conf: Sequence[float],
    *,
    reject_ratio: float = 0.65,
    out_path: str,
    reject_score: str = "logit",
) -> pd.DataFrame:
    """Re-apply Top-r on fixed confidence ranking (r-grid without re-inference)."""
    final, reject_reason, thresh = apply_topk_reject(
        writers, conf, reject_ratio=reject_ratio, reject_score=reject_score
    )
    return _write_reject_csv(
        image_ids,
        final,
        conf,
        reject_reason,
        out_path=out_path,
        reject_score=reject_score,
        thresh=thresh,
    )


def topk_reject_csv(
    image_ids: Sequence[str],
    y_refined: np.ndarray,
    label_encoder: LabelEncoder,
    *,
    reject_ratio: float = 0.65,
    out_path: str,
    reject_score: str = "logit",
) -> pd.DataFrame:
    """
    Score by max logit or max softmax (known classes only); reject lowest
    reject_ratio as -1; else argmax → writer_id.
    """
    scores, pred_cls = scores_and_pred_from_y(
        y_refined, label_encoder, reject_score=reject_score
    )
    writers = [decode_writer_id(int(c), label_encoder) for c in pred_cls]
    final, reject_reason, thresh = apply_topk_reject(
        writers, scores, reject_ratio=reject_ratio, reject_score=reject_score
    )
    return _write_reject_csv(
        image_ids,
        final,
        scores,
        reject_reason,
        out_path=out_path,
        reject_score=reject_score,
        thresh=thresh,
    )


def align_y_to_ids(
    y_ids: Sequence[str],
    y: np.ndarray,
    target_ids: Sequence[str],
) -> np.ndarray:
    """Reorder Y rows to match target_ids order."""
    idx = {str(i): k for k, i in enumerate(y_ids)}
    missing = [i for i in target_ids if str(i) not in idx]
    if missing:
        raise KeyError(f"Y logits missing {len(missing)} ids, e.g. {missing[:3]}")
    order = [idx[str(i)] for i in target_ids]
    return y[order]