File size: 9,039 Bytes
59839e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Fit the final v801 structured probe with a local DINOv3 correction."""

from __future__ import annotations

import argparse
import csv
import glob
import hashlib
import json
import platform
import time
from pathlib import Path

import joblib
import numpy as np
from iterstrat.ml_stratifiers import MultilabelStratifiedKFold
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

from experiment_v801_knn import knn_oof
from train_v801 import (
    Spec,
    all_labelsets,
    choose_thresholds,
    fit_binary,
    labelset_ids,
    make_variant,
    meta_features,
    oof_probabilities,
    report_metrics,
    sha256,
    spec_dict,
    structured_crossfit,
)


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--features", type=Path, required=True)
    parser.add_argument("--development", type=Path, required=True)
    parser.add_argument("--model", type=Path, required=True)
    parser.add_argument("--robust-pattern", required=True)
    parser.add_argument("--historical-report", type=Path, required=True)
    parser.add_argument("--output-dir", type=Path, required=True)
    parser.add_argument("--seed", type=int, default=801208)
    args = parser.parse_args()
    started = time.time()

    bundle = np.load(args.features)
    metadata = json.loads(args.features.with_suffix(".json").read_text())
    names = bundle["file_names"].astype(str)
    y = bundle["labels"].astype(np.uint8)
    classes = bundle["classes"]
    lookup = {name: index for index, name in enumerate(names)}
    with args.development.open(newline="", encoding="utf-8-sig") as handle:
        rows = list(csv.DictReader(handle))
    development_idx = np.asarray([lookup[row["file_name"]] for row in rows])
    origins = np.full(len(names), "excluded", dtype="U32")
    for row in rows:
        origins[lookup[row["file_name"]]] = row["origin"]
    anchor_idx = development_idx[origins[development_idx] == "old440"]
    target_idx = development_idx[origins[development_idx] != "old440"]
    if (len(development_idx), len(anchor_idx), len(target_idx)) != (736, 440, 296):
        raise ValueError("expected development/anchor/target sizes 736/440/296")
    folds = list(
        MultilabelStratifiedKFold(n_splits=5, shuffle=True, random_state=args.seed).split(
            np.zeros(len(target_idx)), y[target_idx]
        )
    )
    legacy_x = make_variant(bundle, "legacy")
    patch_x = make_variant(bundle, "meanmax_clspatchmax")
    spec = Spec("legacy", 0.003, class_weight=None)
    legacy_oof = oof_probabilities(
        legacy_x, y, origins, anchor_idx, target_idx, folds, spec
    )
    neighbour_oof = knn_oof(
        patch_x, y, anchor_idx, target_idx, folds, k=11, temperature=0.1
    )
    mixed_oof = 0.8 * legacy_oof + 0.2 * neighbour_oof
    decoder = {
        "threshold_bias_scale": 0.0,
        "labelset_prior_weight": 0.5,
        "cardinality_weight": 0.25,
        "prior_smoothing": 1.0,
    }
    predictions = structured_crossfit(
        y[target_idx], mixed_oof, folds,
        q=decoder["threshold_bias_scale"],
        prior_weight=decoder["labelset_prior_weight"],
        cardinality_weight=decoder["cardinality_weight"],
    )
    metrics = report_metrics(y[target_idx], mixed_oof, predictions)
    recent = origins[target_idx] == "incremental100"
    metrics["incremental100_exact_match_accuracy"] = float(
        accuracy_score(y[target_idx][recent], predictions[recent])
    )

    thresholds = choose_thresholds(y[target_idx], mixed_oof)
    labelsets = all_labelsets(y.shape[1])
    target_set_ids = labelset_ids(y[target_idx], labelsets)
    counts = np.bincount(target_set_ids, minlength=len(labelsets)).astype(np.float64)
    labelset_log_prior = np.log((counts + 1.0) / (len(target_idx) + len(labelsets)))
    cardinality_model = LogisticRegression(
        C=1.0, max_iter=2000, class_weight="balanced", random_state=801
    ).fit(meta_features(mixed_oof), y[target_idx].sum(axis=1))

    probe = fit_binary(
        legacy_x[development_idx], y[development_idx], spec,
        np.ones(len(development_idx), dtype=np.float64),
    )
    reference_features = np.asarray(patch_x[development_idx], dtype=np.float32)
    reference_labels = np.asarray(y[development_idx], dtype=np.uint8)
    reference_digest = hashlib.sha256(reference_features.tobytes()).hexdigest()
    artifact = {
        "format_version": 3,
        "version": "v801",
        "method": "DINOv3 structured exact-match probe with local k-NN correction",
        "classes": classes,
        "components": [
            {
                "weight": 0.8,
                "spec": spec_dict(spec),
                "feature_dimension": int(legacy_x.shape[1]),
                "scaler": probe["scaler"],
                "models": probe["models"],
            }
        ],
        "local_correction": {
            "weight": 0.2,
            "variant": "meanmax_clspatchmax",
            "feature_dimension": int(reference_features.shape[1]),
            "k": 11,
            "temperature": 0.1,
            "reference_scope": "development736",
            "reference_file_names": names[development_idx],
            "reference_features": reference_features,
            "reference_labels": reference_labels,
            "reference_features_sha256": reference_digest,
        },
        "thresholds": thresholds,
        "labelsets": labelsets,
        "labelset_log_prior": labelset_log_prior,
        "cardinality_model": cardinality_model,
        "decoder": decoder,
        "feature_model_path": str(args.model.resolve()),
        "feature_model_sha256": metadata["model_sha256"],
        "feature_model_file_sha256": metadata["model_file_sha256"],
        "feature_type": metadata["feature_type"],
        "feature_file_sha256": sha256(args.features),
        "descriptor_names": metadata["descriptor_names"],
        "min_labels": 1,
        "max_labels": 3,
    }

    robust_reports = []
    fixed_name = "q0-prior0.5-card0.25"
    for path_text in sorted(glob.glob(args.robust_pattern)):
        path = Path(path_text)
        report = json.loads(path.read_text())
        robust_reports.append(
            {
                "path": str(path),
                "seed": report["seed"],
                "metrics": report["fixed_decoders"][fixed_name]["metrics"],
            }
        )
    robust_exact = [
        report["metrics"]["exact_match_accuracy"] for report in robust_reports
    ]
    robust_recent = [
        report["metrics"]["incremental100_exact_match_accuracy"]
        for report in robust_reports
    ]
    historical = json.loads(args.historical_report.read_text())
    historical_selected = historical["local_candidates"][fixed_name]["historical_metrics"]
    development_report = {
        "version": "v801",
        "protocol": (
            "Final configuration uses development736 only for fitting. Local correction and "
            "fixed decoder were selected from four target-domain OOF seeds plus the non-independent "
            "historical32 reference. new_holdout32 was not used for this configuration."
        ),
        "samples": {"development": 736, "old_anchors": 440, "target_oof": 296},
        "selected": {
            "base": {"weight": 0.8, "spec": spec_dict(spec)},
            "local_correction": {
                "weight": 0.2,
                "variant": "meanmax_clspatchmax",
                "k": 11,
                "temperature": 0.1,
            },
            "decoder": decoder,
            "seed_801208_fixed_decoder_metrics": metrics,
            "four_seed_fixed_decoder": {
                "mean_exact_match_accuracy": float(np.mean(robust_exact)),
                "min_exact_match_accuracy": float(np.min(robust_exact)),
                "mean_incremental100_exact_match_accuracy": float(np.mean(robust_recent)),
                "reports": robust_reports,
            },
            "historical_reference_metrics": historical_selected,
            "thresholds_fit_on_all_target_oof": thresholds.tolist(),
        },
        "feature_file_sha256": sha256(args.features),
        "reference_features_sha256": reference_digest,
        "seed": args.seed,
        "seconds": round(time.time() - started, 3),
        "python": platform.python_version(),
    }
    args.output_dir.mkdir(parents=True, exist_ok=True)
    joblib.dump(artifact, args.output_dir / "classifier.joblib", compress=3)
    (args.output_dir / "development_metrics.json").write_text(
        json.dumps(development_report, indent=2) + "\n", encoding="utf-8"
    )
    np.savez_compressed(
        args.output_dir / "selected_oof_predictions.npz",
        file_names=names[target_idx],
        labels=y[target_idx],
        probabilities=mixed_oof,
        predictions=predictions,
        legacy_probabilities=legacy_oof,
        neighbour_probabilities=neighbour_oof,
        classes=classes,
    )
    print(json.dumps(development_report["selected"], indent=2), flush=True)


if __name__ == "__main__":
    main()