File size: 11,293 Bytes
a2f55b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Produce stratified train/val/test splits from the master manifest.

Stratification preserves the per-class ratio across splits — important when
the classes are imbalanced (and they always are, eventually).

Determinism: a fixed seed plus sha256 sorting means re-running on the same
manifest produces identical splits. This matters for reproducibility.

Usage
-----
    python scripts/dataset/split.py \
        --manifest data/manifest.csv \
        --out-dir data \
        --val 0.1 --test 0.1

Generator-aware Stage 3A split:

    python scripts/dataset/split.py \
        --manifest data/manifest.csv \
        --out-dir data \
        --stratify-by class-generator

Held-out generator evaluation split:

    python scripts/dataset/split.py \
        --manifest data/manifest.csv \
        --out-dir data \
        --stratify-by class-generator \
        --holdout-generator sdxl

Content-disjoint held-out split (recommended for generalisation claims):

    python scripts/dataset/split.py \
        --manifest data/manifest.csv \
        --out-dir data/splits_v2 \
        --stratify-by class-generator \
        --holdout-generator sdxl \
        --group-by content-id

Why --group-by content-id exists
--------------------------------
Output filenames are content-addressed from the (prompt, seed) pair, and every
Stage 3A generator consumed the same `prompts.txt` with the same deterministic
seed derivation. So `<stem>.png` denotes the same prompt and seed in the SDXL,
SD 3.5 and AuraFlow sets.

`--holdout-generator` alone takes *every* row of that generator, which means
the held-out set can depict prompts that a sibling generator already
contributed to training. That is exactly what happened in Stage 3A: all 20,000
held-out SDXL images shared their (prompt, seed) with an SD 3.5 image in
train/val/test, so the split measured "unseen generator, **seen** content"
while being described as a true generalisation test.

With `--group-by content-id`, a (prompt, seed) identity is assigned to exactly
one partition, and two held-out files are written:

  heldout.csv               unseen generator AND unseen content (honest test)
  heldout_seen_content.csv  unseen generator, seen content (legacy-comparable)

Rows of non-held-out generators that fall in the reserved held-out content
partition are dropped from train/val/test and reported, since including them
would reintroduce the leak.

Default remains `--group-by none`, which reproduces the legacy behaviour
byte-for-byte.
"""
from __future__ import annotations

import argparse
import csv
import random
from collections import defaultdict
from pathlib import Path


def _content_id(row: dict[str, str]) -> str:
    """Identity of the (prompt, seed) pair behind a content-addressed filename.

    The same stem denotes the same prompt and seed across every Stage 3A
    generator, so this groups sibling renderings of identical content.
    """
    return Path(row["path"]).stem


def _generator_label(row: dict[str, str]) -> str:
    """Return the best available generator label for an AI row."""
    return row.get("generator") or row.get("source") or "unknown"


def _split_key(row: dict[str, str], stratify_by: str) -> str:
    """Return the grouping key used for stratified splitting."""
    cls = row["class"]
    if stratify_by == "class-generator" and cls == "ai_generated":
        return f"{cls}:{_generator_label(row)}"
    return cls


def _write_csv(path: Path, fieldnames: list[str], rows: list[dict[str, str]]) -> None:
    with path.open("w", newline="") as fh:
        writer = csv.DictWriter(fh, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)


def _split_content_disjoint(
    args: argparse.Namespace,
    rows: list[dict[str, str]],
    fieldnames: list[str],
    holdout_generators: set[str],
) -> None:
    """Partition by (prompt, seed) identity so held-out content is truly unseen."""
    ai_rows = [r for r in rows if r["class"] == "ai_generated"]
    real_rows = [r for r in rows if r["class"] != "ai_generated"]

    groups: dict[str, list[dict[str, str]]] = defaultdict(list)
    for r in ai_rows:
        groups[_content_id(r)].append(r)

    group_ids = sorted(groups)
    rng = random.Random(args.seed)
    rng.shuffle(group_ids)

    n_groups = len(group_ids)
    n_holdout = int(round(n_groups * args.holdout_frac)) if holdout_generators else 0
    holdout_ids = set(group_ids[:n_holdout])
    remaining = group_ids[n_holdout:]

    n_test = int(round(len(remaining) * args.test))
    n_val = int(round(len(remaining) * args.val))
    partition = {gid: "test" for gid in remaining[:n_test]}
    partition.update({gid: "val" for gid in remaining[n_test : n_test + n_val]})
    partition.update({gid: "train" for gid in remaining[n_test + n_val :]})

    buckets: dict[str, list[dict[str, str]]] = defaultdict(list)
    dropped: dict[str, int] = defaultdict(int)
    for gid, items in groups.items():
        in_holdout_content = gid in holdout_ids
        for r in items:
            is_holdout_gen = _generator_label(r) in holdout_generators
            if is_holdout_gen:
                buckets["heldout" if in_holdout_content else "heldout_seen_content"].append(r)
            elif in_holdout_content:
                # Including this would put the held-out prompt back into
                # training via a sibling generator — the exact Stage 3A leak.
                dropped[_generator_label(r)] += 1
            else:
                buckets[partition[gid]].append(r)

    # Real photos share no content identity with generated images, so they are
    # split by the ordinary stratified fractions.
    real_sorted = sorted(real_rows, key=lambda r: r["sha256"])
    rng_real = random.Random(args.seed)
    rng_real.shuffle(real_sorted)
    n = len(real_sorted)
    n_test_r = int(round(n * args.test))
    n_val_r = int(round(n * args.val))
    buckets["test"].extend(real_sorted[:n_test_r])
    buckets["val"].extend(real_sorted[n_test_r : n_test_r + n_val_r])
    buckets["train"].extend(real_sorted[n_test_r + n_val_r :])

    print(f"content-id groups: {n_groups}")
    print(f"  reserved for held-out content: {len(holdout_ids)}")
    print(f"  train/val/test groups: {len(remaining) - n_test - n_val}/{n_val}/{n_test}")
    if dropped:
        total_dropped = sum(dropped.values())
        print(
            f"  dropped {total_dropped} non-held-out rows whose content is "
            "reserved (prevents leakage):"
        )
        for gen, count in sorted(dropped.items()):
            print(f"    {gen}: {count}")

    args.out_dir.mkdir(parents=True, exist_ok=True)
    for name in ("train", "val", "test", "heldout", "heldout_seen_content"):
        rs = buckets.get(name, [])
        if not rs:
            continue
        rs.sort(key=lambda r: (r["sha256"], r["path"]))
        path = args.out_dir / f"{name}.csv"
        _write_csv(path, fieldnames, rs)
        counts: dict[str, int] = defaultdict(int)
        for r in rs:
            key = (
                _generator_label(r) if r["class"] == "ai_generated" else "authentic"
            )
            counts[key] += 1
        breakdown = "  ".join(f"{k}={v}" for k, v in sorted(counts.items()))
        print(f"  wrote {path} ({len(rs)} rows)  {breakdown}")


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--manifest", type=Path, required=True)
    parser.add_argument("--out-dir", type=Path, required=True)
    parser.add_argument("--val", type=float, default=0.1)
    parser.add_argument("--test", type=float, default=0.1)
    parser.add_argument("--seed", type=int, default=0)
    parser.add_argument(
        "--stratify-by",
        choices=["class", "class-generator"],
        default="class",
        help=(
            "Stratification mode. 'class' preserves legacy behavior; "
            "'class-generator' additionally balances AI generators."
        ),
    )
    parser.add_argument(
        "--holdout-generator",
        action="append",
        default=[],
        help=(
            "AI generator to remove from train/val/test and write to "
            "heldout.csv. Can be passed multiple times."
        ),
    )
    parser.add_argument(
        "--group-by",
        choices=["none", "content-id"],
        default="none",
        help=(
            "'none' (default) reproduces legacy behaviour. 'content-id' keeps "
            "every generator's rendering of the same (prompt, seed) in one "
            "partition, so the held-out split is content-disjoint as well as "
            "generator-disjoint."
        ),
    )
    parser.add_argument(
        "--holdout-frac",
        type=float,
        default=0.1,
        help=(
            "Fraction of content-id groups reserved for the held-out content "
            "partition. Only used with --group-by content-id."
        ),
    )
    args = parser.parse_args()

    if args.val + args.test >= 1.0:
        raise ValueError("val + test must be < 1.0")

    with args.manifest.open() as fh:
        reader = csv.DictReader(fh)
        rows = list(reader)
        fieldnames = reader.fieldnames or []

    holdout_generators = set(args.holdout_generator)

    if args.group_by == "content-id":
        return _split_content_disjoint(args, rows, fieldnames, holdout_generators)

    split_candidates: list[dict[str, str]] = []
    holdout_rows: list[dict[str, str]] = []
    for r in rows:
        if r["class"] == "ai_generated" and _generator_label(r) in holdout_generators:
            holdout_rows.append(r)
        else:
            split_candidates.append(r)

    if holdout_generators and not holdout_rows:
        raise ValueError(
            "No rows matched --holdout-generator values: "
            f"{sorted(holdout_generators)}"
        )

    by_group: dict[str, list[dict[str, str]]] = defaultdict(list)
    for r in split_candidates:
        by_group[_split_key(r, args.stratify_by)].append(r)

    rng = random.Random(args.seed)
    train_rows, val_rows, test_rows = [], [], []
    for group, items in sorted(by_group.items()):
        # Sort by sha256 for determinism, then shuffle with seeded RNG.
        items.sort(key=lambda r: r["sha256"])
        rng.shuffle(items)
        n = len(items)
        n_test = int(round(n * args.test))
        n_val = int(round(n * args.val))
        test_rows.extend(items[:n_test])
        val_rows.extend(items[n_test : n_test + n_val])
        train_rows.extend(items[n_test + n_val :])
        print(
            f"  {group}: total={n}  "
            f"train={n - n_test - n_val}  val={n_val}  test={n_test}"
        )

    args.out_dir.mkdir(parents=True, exist_ok=True)
    for name, rs in [("train", train_rows), ("val", val_rows), ("test", test_rows)]:
        path = args.out_dir / f"{name}.csv"
        _write_csv(path, fieldnames, rs)
        print(f"  wrote {path} ({len(rs)} rows)")

    if holdout_generators:
        path = args.out_dir / "heldout.csv"
        holdout_rows.sort(key=lambda r: (r["sha256"], r["path"]))
        _write_csv(path, fieldnames, holdout_rows)
        print(
            f"  wrote {path} ({len(holdout_rows)} rows) "
            f"for held-out generators: {sorted(holdout_generators)}"
        )


if __name__ == "__main__":
    main()