summaryrefslogtreecommitdiff
path: root/worldalign/synth_cc_battery.py
blob: be28a0130970e0204e430104b54b7605f0ef7685 (plain)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""Upper-bound set battery: connected-component sprites as vision sets.

The learned towers have not yet produced object states, which leaves two
hypotheses entangled: the set-kernel machinery could be wrong, or only
the towers could be short. This battery separates them. On this world
the background is flat, so connected bright components ARE the objects;
per-component centered sprites are model-free object states of the
minimal-world-knowledge class (like the color anchors on natural data:
declared, fixed, no learning). If set-kernel relation fields built from
these pass the gate and support recovery, the machinery is validated and
the remaining gap is exactly "an SSL objective that discovers objects".

Text sets are the per-group phrase states of the set battery. Hidden
pairs score orderings only.
"""

from __future__ import annotations

import argparse
import json
from pathlib import Path

import numpy as np
import torch
import torch.nn.functional as F
from scipy import ndimage
from tqdm import tqdm

from .common import read_json, seed_everything, write_json
from .manifold_gate import standardize_relation
from .ricci_control import run_gates
from .synth_set_battery import (
    parse_group_phrases,
    set_similarity_field,
    text_group_sets,
)
from .synth_towers import load_image


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument("--data-dir", default="artifacts/synth_v0")
    parser.add_argument("--text-tower", default="artifacts/synth_v0/text_tower.pt")
    parser.add_argument("--split", choices=["val", "test"], default="test")
    parser.add_argument("--samples", type=int, default=512)
    parser.add_argument("--sprite-window", type=int, default=56)
    parser.add_argument("--merge-distance", type=float, default=30.0)
    parser.add_argument(
        "--features", choices=["pixels", "descriptors", "onehot"], default="descriptors"
    )
    parser.add_argument("--text-mode", choices=["lm", "bow"], default="bow")
    parser.add_argument(
        "--kernel", choices=["matching", "moment"], default="matching",
        help="moment: symmetric-tensor (Fock) set kernel, no matching step",
    )
    parser.add_argument("--vision-views", type=int, default=4)
    parser.add_argument("--random-perms", type=int, default=300)
    parser.add_argument("--descent-restarts", type=int, default=5)
    parser.add_argument("--descent-max-steps", type=int, default=200000)
    parser.add_argument(
        "--descent-objective", default="mse", choices=["mse", "m30_total"]
    )
    parser.add_argument("--descent-verify-top", type=int, default=64)
    parser.add_argument("--device", default="cuda:3")
    parser.add_argument("--seed", type=int, default=20260731)
    parser.add_argument(
        "--output", default="artifacts/synth_v0/cc_battery_gate.json"
    )
    return parser.parse_args()


def component_sprites(
    image: torch.Tensor, window: int, merge_distance: float
) -> tuple[torch.Tensor, torch.Tensor]:
    """Centered sprites of bright connected components, ring-merged.

    Same-group objects sit on a small ring; merging nearby components
    reassembles groups so a sprite carries multiplicity as pattern.
    """
    array = image.permute(1, 2, 0).numpy()
    background = np.median(array.reshape(-1, 3), axis=0)
    foreground = (np.abs(array - background).sum(-1) > 0.12)
    labels, count = ndimage.label(foreground)
    if count == 0:
        return torch.zeros(1, 3 * window * window), torch.ones(1)
    centers = np.array(ndimage.center_of_mass(foreground, labels, range(1, count + 1)))
    sizes = ndimage.sum(foreground, labels, range(1, count + 1))
    # Merge components whose centers are close (ring members).
    parent = list(range(count))

    def find(a: int) -> int:
        while parent[a] != a:
            parent[a] = parent[parent[a]]
            a = parent[a]
        return a

    for a in range(count):
        for b in range(a + 1, count):
            if np.linalg.norm(centers[a] - centers[b]) < merge_distance:
                parent[find(a)] = find(b)
    groups: dict[int, list[int]] = {}
    for a in range(count):
        groups.setdefault(find(a), []).append(a)

    height, width = foreground.shape
    half = window // 2
    padded = np.pad(array, ((half, half), (half, half), (0, 0)))
    padded_mask = np.pad(foreground, half)
    sprites, weights = [], []
    for members in groups.values():
        member_mask = np.isin(labels, [m + 1 for m in members])
        mass = float(member_mask.sum())
        ys, xs = np.nonzero(member_mask)
        cy, cx = int(ys.mean()), int(xs.mean())
        patch = padded[cy : cy + window, cx : cx + window].copy()
        mask_patch = padded_mask[cy : cy + window, cx : cx + window]
        patch[~mask_patch] = 0.0
        sprites.append(torch.from_numpy(patch).float().flatten())
        weights.append(mass)
    weights = torch.tensor(weights)
    return torch.stack(sprites), weights / weights.sum().clamp_min(1e-8)


HUE_CENTERS = {
    "red": 0.0, "orange": 30.0, "yellow": 60.0, "green": 120.0,
    "cyan": 180.0, "blue": 220.0, "purple": 275.0, "pink": 330.0,
}


def component_descriptors(
    image: torch.Tensor, merge_distance: float
) -> tuple[torch.Tensor, torch.Tensor]:
    """Rotation-invariant group descriptors: color classes, log-area,
    member multiplicity, and normalized single-member area."""
    import colorsys

    array = image.permute(1, 2, 0).numpy()
    background = np.median(array.reshape(-1, 3), axis=0)
    foreground = np.abs(array - background).sum(-1) > 0.12
    labels, count = ndimage.label(foreground)
    if count == 0:
        return torch.zeros(1, 19), torch.ones(1)
    centers = np.array(
        ndimage.center_of_mass(foreground, labels, range(1, count + 1))
    )
    parent = list(range(count))

    def find(a: int) -> int:
        while parent[a] != a:
            parent[a] = parent[parent[a]]
            a = parent[a]
        return a

    for a in range(count):
        for b in range(a + 1, count):
            if np.linalg.norm(centers[a] - centers[b]) < merge_distance:
                parent[find(a)] = find(b)
    groups: dict[int, list[int]] = {}
    for a in range(count):
        groups.setdefault(find(a), []).append(a)
    descriptors, weights = [], []
    total = foreground.size
    for members in groups.values():
        member_mask = np.isin(labels, [m + 1 for m in members])
        pixels = array[member_mask]
        mean_rgb = pixels.mean(0)
        h, s_, v = colorsys.rgb_to_hsv(*mean_rgb.tolist())
        hue = h * 360.0
        vector = np.zeros(16)
        if v > 0.75 and s_ < 0.25:
            vector[8] = 1.0  # white
        elif v < 0.25:
            vector[9] = 1.0  # black
        elif s_ < 0.25:
            vector[10] = 1.0  # gray
        elif abs(hue - 30.0) < 25.0 and v < 0.55:
            vector[11] = 1.0  # brown
        else:
            for index, center in enumerate(HUE_CENTERS.values()):
                distance = min(abs(hue - center), 360.0 - abs(hue - center))
                if distance < 25.0:
                    vector[index] = 1.0
                    break
        member_count = len(members)
        area = float(member_mask.sum()) / total
        vector[12] = np.log(area + 1e-6) / 6.0
        vector[13] = (member_count - 1) / 3.0
        vector[14] = np.log(area / member_count + 1e-6) / 6.0
        vector[15] = 1.0
        # Rotation-invariant shape features of the largest single member:
        # compactness, convexity, and inertia eccentricity separate the
        # six shapes without orientation.
        largest = max(members, key=lambda m: (labels == m + 1).sum())
        single = labels == largest + 1
        area_px = float(single.sum())
        eroded = ndimage.binary_erosion(single)
        perimeter = float((single & ~eroded).sum())
        compactness = 4.0 * np.pi * area_px / max(perimeter, 1.0) ** 2
        ys, xs = np.nonzero(single)
        ys = ys - ys.mean(); xs = xs - xs.mean()
        cov = np.cov(np.stack([xs, ys])) + 1e-6 * np.eye(2)
        eigenvalues = np.linalg.eigvalsh(cov)
        eccentricity = float(1.0 - eigenvalues[0] / eigenvalues[1])
        hull_span = (xs.max() - xs.min() + 1) * (ys.max() - ys.min() + 1)
        boxfill = area_px / max(hull_span, 1.0)
        shape_vector = np.array([compactness, eccentricity, boxfill])
        vector = np.concatenate([vector, shape_vector])
        descriptors.append(torch.tensor(vector, dtype=torch.float32))
        weights.append(float(member_mask.sum()))
    weights = torch.tensor(weights)
    return torch.stack(descriptors), weights / weights.sum().clamp_min(1e-8)


def onehot_descriptors(
    raw_sets: list[torch.Tensor],
) -> list[torch.Tensor]:
    """Factor-mirrored one-hot recoding of descriptor sets.

    Sizes are binned by corpus terciles of single-member log-area with the
    middle bin unmarked, mirroring the text side where medium size has no
    word; shapes are k-means clusters of the rotation-invariant shape
    features. Both statistics come from the evaluated corpus itself,
    unimodally. Output channels mirror the bag-of-words support: color
    (12), small/large (2), count (4), shape cluster (6).
    """
    from sklearn.cluster import KMeans

    all_groups = torch.cat(raw_sets)
    # The renderer shrinks radii with member count, so raw single-member
    # area confounds size class with multiplicity; residualize log-area on
    # member count before binning (unimodal statistics).
    counts_all = (all_groups[:, 13] * 3.0).round().clamp(0, 3)
    area_all = all_groups[:, 14]
    count_means = {}
    for value in (0.0, 1.0, 2.0, 3.0):
        chosen = counts_all == value
        count_means[value] = float(area_all[chosen].mean()) if chosen.any() else 0.0
    adjusted_all = area_all - torch.tensor(
        [count_means[float(v)] for v in counts_all]
    )
    low, high = adjusted_all.quantile(1.0 / 3.0), adjusted_all.quantile(2.0 / 3.0)
    shape_features = all_groups[:, 16:19].numpy()
    clusters = KMeans(n_clusters=6, n_init=10, random_state=0).fit(shape_features)
    recoded = []
    for groups in raw_sets:
        vectors = torch.zeros(len(groups), 24)
        vectors[:, :12] = groups[:, :12]
        counts_here = (groups[:, 13] * 3.0).round().clamp(0, 3)
        adjusted = groups[:, 14] - torch.tensor(
            [count_means[float(v)] for v in counts_here]
        )
        vectors[:, 12] = (adjusted <= low).float()   # small
        vectors[:, 13] = (adjusted >= high).float()  # large
        counts = (groups[:, 13] * 3.0).round().long().clamp(0, 3)
        vectors[torch.arange(len(groups)), 14 + counts] = 1.0
        labels = clusters.predict(groups[:, 16:19].numpy())
        vectors[torch.arange(len(groups)), 18 + labels] = 1.0
        recoded.append(vectors)
    return recoded


def moment_field(sets: list[torch.Tensor]) -> torch.Tensor:
    """Symmetric-tensor (second-quantized) set kernel, matching-free.

    phi(S) concatenates the degree-1 and degree-2 moments of the set; the
    field is the Gram matrix of normalized phi. No assignment step, so no
    matching-value or set-size bias can enter.
    """
    phis = []
    for members in sets:
        m1 = members.mean(0)
        m2 = (members[:, :, None] * members[:, None, :]).mean(0).flatten()
        phi = torch.cat([m1, m2])
        phis.append(phi / phi.norm().clamp_min(1e-9))
    stacked = torch.stack(phis)
    return stacked @ stacked.T


def phrase_bow_sets(
    rows: list[int], captions: list[list[str]], vocabulary: list[str]
) -> list[torch.Tensor]:
    index = {word: i for i, word in enumerate(vocabulary)}
    sets = []
    for row in rows:
        phrases = parse_group_phrases(captions[row][0])
        vectors = torch.zeros(len(phrases), len(index))
        for p, phrase in enumerate(phrases):
            for token in phrase.split():
                if token in index:
                    vectors[p, index[token]] += 1.0
        sets.append(F.normalize(vectors, dim=-1))
    return sets


def main() -> None:
    args = parse_args()
    seed_everything(args.seed)
    manifest = read_json(Path(args.data_dir, "manifest.json"))
    captions = read_json(Path(args.data_dir, "captions.json"))["captions"]
    rows = manifest[args.split][: args.samples]
    image_dir = Path(manifest["image_dir"])

    per_view_fields = []
    set_sizes = []
    for view in range(args.vision_views):
        vision_sets = []
        raw_sets = []
        for row in tqdm(rows, desc=f"cc sprites v{view}"):
            image = load_image(image_dir / f"scene{row:06d}_v{view}.png")
            if args.features in ("descriptors", "onehot"):
                sprites, _ = component_descriptors(image, args.merge_distance)
            else:
                sprites, _ = component_sprites(
                    image, args.sprite_window, args.merge_distance
                )
            raw_sets.append(sprites)
            if view == 0:
                set_sizes.append(len(sprites))
        if args.features == "onehot":
            vision_sets = [
                F.normalize(v, dim=-1) for v in onehot_descriptors(raw_sets)
            ]
        else:
            vision_sets = [F.normalize(v, dim=-1) for v in raw_sets]
        if args.kernel == "moment":
            per_view_fields.append(moment_field(vision_sets))
        else:
            per_view_fields.append(set_similarity_field(vision_sets))

    if args.text_mode == "bow":
        text_sets = phrase_bow_sets(rows, captions, manifest["vocabulary"])
    else:
        text_sets = text_group_sets(rows, captions, args)
    if args.kernel == "moment":
        text_field = moment_field(text_sets)
    else:
        text_field = set_similarity_field(text_sets)
    # Mass weighting inside the matching corrupts similarity grading
    # (0.16 vs 0.45 against soft truth); match unweighted. Averaging the
    # per-view fields cancels segmentation errors across resampled layouts.
    visual_field = torch.stack(per_view_fields).mean(0)
    visual_channels = standardize_relation(visual_field.double())[0][None]
    text_channels = standardize_relation(text_field.double())[0][None]
    generator = torch.Generator().manual_seed(args.seed)
    report = {
        "protocol": (
            "Vision sets are model-free connected-component sprites "
            "(declared minimal world knowledge); text sets are per-group "
            "phrase states. Hidden pairs score orderings only."
        ),
        "split": args.split,
        "samples": len(rows),
        "mean_vision_set_size": float(np.mean(set_sizes)),
        **run_gates(text_channels, visual_channels, args, generator),
    }
    verdict = {
        "true_z_mse": report["gate_a"]["random"]["mse"]["true_z"],
        "improving_fraction": report["gate_b"]["improving_fraction"],
        "descent_keeps": report["descent_from_true"]["final_accuracy"],
        "true_mse": report["gate_a"]["true"]["mse"],
        "best_random_descent": min(
            (r["final_objective"] for r in report["descent_from_random"]),
            default=None,
        ),
    }
    verdict["counterfeit_found"] = bool(
        verdict["best_random_descent"] is not None
        and verdict["best_random_descent"] < verdict["true_mse"]
    )
    report["verdict"] = verdict
    print(json.dumps({"verdict": verdict}))
    write_json(args.output, report)
    print(f"Wrote {args.output}")


if __name__ == "__main__":
    main()