From fef30b5ef4aaf7cc4c48cf32bfea0dbd7f712aa4 Mon Sep 17 00:00:00 2001 From: maxime Date: Mon, 16 Nov 2026 19:22:53 +0100 Subject: [PATCH] Step 4: M2, multiple-instance model in PyTorch (shared sensor MLP, attention per family, averaged seeds); the time-split report shared with M1 Co-Authored-By: Claude Opus 5.5 --- evaluate.py | 22 +++++++++ m1.py | 22 +++------ m2.py | 123 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 + 4 files changed, 153 insertions(+), 16 deletions(-) create mode 100644 m2.py diff --git a/evaluate.py b/evaluate.py index d84a75f..303c9a6 100644 --- a/evaluate.py +++ b/evaluate.py @@ -75,6 +75,28 @@ def show(title, base, new): print(f" {f:<22}{cell(b, 0):>21}{cell(n, 0):>21}{cell(b, 1):>21}{cell(n, 1):>21}") +CUT = 0.7 # the criterion: trained on the first 70 % of every station, tested on the last 30 % + + +def report_time_split(cache, data, proba, families, threshold=0.5): + """The criterion: {station: proba [last 30 %, pilot family]} against the rules on the same instants -> the + regressions station by station and the total, printed; returns (rules, model) counts per station.""" + base, new = {}, {} + for s, d in data.items(): + cut = int(CUT * len(d["y"])) + rules_yhat = np.load(Path(cache) / "rules" / f"{s}.npz")["yhat"][cut:] + yhat = rules_yhat.copy() + for j, f in enumerate(families): + yhat[:, FAMILIES.index(f)] = proba[s][:, j] >= threshold + keep = lambda c: {f: v for f, v in c.items() if f in families} # noqa: E731 + base[s], new[s] = keep(counts(d["y"][cut:], rules_yhat)), keep(counts(d["y"][cut:], yhat)) + for f, name, pb, pn in regressions(base[s], new[s]): + print(f" regression: {s}, {f}, {name} {pb:.0f} % -> {pn:.0f} %" + f" (rules {base[s][f]}, model {new[s].get(f)}: labelled, flagged, both)") + show(f"Last {round(100 * (1 - CUT))} % of every station", total(base), total(new)) + return base, new + + def rules(a): sys.path[:0] = [str(Path(a.detectors) / "scripts"), str(Path(a.detectors) / "bench")] import score # noqa: E402 diff --git a/m1.py b/m1.py index db2ad1a..41aaad3 100644 --- a/m1.py +++ b/m1.py @@ -49,28 +49,18 @@ def fit(x, y, w=None): def split_time(a, data, rows, families): """Every station seen in training: does the information lie in the features, apart from moving to a new station?""" - from evaluate import counts, regressions, show, total - cut = {s: int(0.7 * len(r)) for s, r in rows.items()} + from evaluate import CUT, report_time_split + cut = {s: int(CUT * len(r)) for s, r in rows.items()} x = np.concatenate([rows[s][:cut[s]] for s in data]) - base, new = {}, {} - yhat = {s: np.load(Path(a.cache) / "rules" / f"{s}.npz")["yhat"][cut[s]:].copy() for s in data} - for f in families: + proba = {s: np.zeros((len(r) - cut[s], len(families))) for s, r in rows.items()} + for j, f in enumerate(families): k = FAMILIES.index(f) y = np.concatenate([data[s]["y"][:cut[s], k] for s in data]) w = np.concatenate([np.full(cut[s], 1 / cut[s]) for s in data]) if a.balance else None model = fit(x, y, w) for s in data: - yhat[s][:, k] = model.predict_proba(rows[s][cut[s]:])[:, 1] >= a.threshold - for s in data: - y = data[s]["y"][cut[s]:] - base[s] = counts(y, np.load(Path(a.cache) / "rules" / f"{s}.npz")["yhat"][cut[s]:]) - new[s] = counts(y, yhat[s]) - keep = lambda c: {f: v for f, v in c.items() if f in families} # noqa: E731 - for s in data: - for f, name, pb, pn in regressions(keep(base[s]), keep(new[s])): - print(f" regression: {s}, {f}, {name} {pb:.0f} % -> {'—' if pn is None else f'{pn:.0f} %'}" - f" (rules {base[s][f]}, model {new[s].get(f)}: labelled, flagged, both)") - show("Last 30 % of every station", keep(total(base)), keep(total(new))) + proba[s][:, j] = model.predict_proba(rows[s][cut[s]:])[:, 1] + report_time_split(a.cache, data, proba, families, a.threshold) def main(): diff --git a/m2.py b/m2.py new file mode 100644 index 0000000..97a94d8 --- /dev/null +++ b/m2.py @@ -0,0 +1,123 @@ +"""M2, the model of the pilot: multiple-instance learning. A label names a station and a range, not its sensors, and +each station has its own number of sensors: one small MLP, shared by every sensor, reads a sensor's features with the +station's; per family, an attention over the sensors pools their scores into the station's. The attention weights give +the sensors (step 6). + +Inputs: every feature through sign(x) log(1 + |x|) (jumps reach thousands of noises), NaN set to 0 with a presence mask. +Training: binary cross-entropy per family on the reviewed labels (fine-tuning only: the pre-training is step 5), each +station drawn as often as the others. + default: the criterion, trained on the first 70 % of every station, scored on the last 30 % (evaluate.CUT); + --leave-out: the indicator, each station predicted by the model trained on the others; one .npz per station + in --out, with yhat [instant, family] (the rules outside the pilot families), proba and attention [instant, sensor, family]. +Usage: python m2.py --cache [--leave-out --out ] [--families Visibility,Obstruction] [--epochs 200 --seeds 5] +""" +import argparse +from pathlib import Path + +import numpy as np +import torch +from torch import nn + +from dataset import FAMILIES +from evaluate import CUT, report_time_split + +BATCHES, BATCH = 8, 128 # per station and per epoch: every station weighs the same + + +def prepare(d): + """A station of the cache -> tensors xs [T, S, 2 Fs], xc [T, 2 Fc]: the squashed features and their presence.""" + def squash(x): + m = np.isfinite(x) + v = np.where(m, x, 0) + return np.concatenate([np.sign(v) * np.log1p(np.abs(v)), m], axis=-1).astype(np.float32) + return torch.from_numpy(squash(d["xs"])), torch.from_numpy(squash(d["xc"])) + + +class MIL(nn.Module): + def __init__(self, fs, fc, k, hidden=64, dropout=0.1): + super().__init__() + self.encoder = nn.Sequential(nn.Linear(fs + fc, hidden), nn.ReLU(), nn.Dropout(dropout), + nn.Linear(hidden, hidden), nn.ReLU()) + self.attention = nn.Linear(hidden, k) # per family: how much a sensor counts for the station + self.score = nn.Linear(hidden, k) # per family: the sensor's own score + + def forward(self, xs, xc): + """xs [B, S, Fs], xc [B, Fc] -> station logits [B, K], attention [B, S, K].""" + h = self.encoder(torch.cat([xs, xc[:, None, :].expand(-1, xs.shape[1], -1)], dim=-1)) + attention = torch.softmax(self.attention(h), dim=1) + return (attention * self.score(h)).sum(dim=1), attention + + +def train(parts, k_index, epochs, seed=0, proportional=False): + """parts: [(xs, xc, y [T, K])], one per station -> the trained model. Each station drawn as often as the others, or + in proportion to its instants.""" + torch.manual_seed(seed) + rng = np.random.default_rng(seed) + sizes = np.array([len(p[2]) for p in parts], float) + model = MIL(parts[0][0].shape[-1], parts[0][1].shape[-1], len(k_index)) + opt = torch.optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-4) + loss = nn.BCEWithLogitsLoss() + model.train() + for _ in range(epochs): + for _ in range(BATCHES): + order = (rng.choice(len(parts), len(parts), p=sizes / sizes.sum()) if proportional + else rng.permutation(len(parts))) + for i in order: + xs, xc, y = parts[i] + b = torch.from_numpy(rng.integers(0, len(y), BATCH)) + logits, _ = model(xs[b], xc[b]) + opt.zero_grad() + loss(logits, y[b][:, k_index]).backward() + opt.step() + return model.eval() + + +@torch.no_grad() +def predict(model, xs, xc): + out = [model(xs[i:i + 1024], xc[i:i + 1024]) for i in range(0, len(xs), 1024)] + return torch.sigmoid(torch.cat([o[0] for o in out])).numpy(), torch.cat([o[1] for o in out]).numpy() + + +def main(): + p = argparse.ArgumentParser(usage=__doc__) + p.add_argument("--cache", required=True) + p.add_argument("--families", default="Visibility,Obstruction") + p.add_argument("--epochs", type=int, default=200) + p.add_argument("--seeds", type=int, default=5, help="models averaged: one run alone varies by 10 to 20 points") + p.add_argument("--threshold", type=float, default=0.5) + p.add_argument("--proportional", action="store_true", help="stations drawn in proportion to their instants") + p.add_argument("--leave-out", action="store_true") + p.add_argument("--out") + a = p.parse_args() + families = a.families.split(",") + k_index = torch.tensor([FAMILIES.index(f) for f in families]) + data = {q.stem: np.load(q) for q in sorted(Path(a.cache).glob("*.npz"))} + tensors = {s: (*prepare(d), torch.from_numpy(d["y"].astype(np.float32))) for s, d in data.items()} + + def ensemble(parts): + return [train(parts, k_index, a.epochs, seed, a.proportional) for seed in range(a.seeds)] + + def averaged(models, xs, xc): + out = [predict(m, xs, xc) for m in models] + return np.mean([o[0] for o in out], axis=0), np.mean([o[1] for o in out], axis=0) + + if not a.leave_out: + cut = {s: int(CUT * len(d["y"])) for s, d in data.items()} + models = ensemble([tuple(t[:cut[s]] for t in tensors[s]) for s in data]) + proba = {s: averaged(models, tensors[s][0][cut[s]:], tensors[s][1][cut[s]:])[0] for s in data} + report_time_split(a.cache, data, proba, families, a.threshold) + return + out = Path(a.out) + out.mkdir(parents=True, exist_ok=True) + for test in data: + proba, attention = averaged(ensemble([tensors[s] for s in data if s != test]), *tensors[test][:2]) + yhat = np.load(Path(a.cache) / "rules" / f"{test}.npz")["yhat"].copy() + for j, f in enumerate(families): + yhat[:, FAMILIES.index(f)] = proba[:, j] >= a.threshold + np.savez_compressed(out / f"{test}.npz", yhat=yhat, proba=proba, attention=attention.astype(np.float16), + families=np.array(families)) + print(test, {f: int(yhat[:, FAMILIES.index(f)].sum()) for f in families}, flush=True) + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt index ad5443d..ce20631 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ numpy pandas scikit-learn +# CPU build: uv pip install -p .venv torch --index-url https://download.pytorch.org/whl/cpu +torch