"""Bounded native-detail residual probe; no coordinates or location-specific rules.""" def network(seed): import torch from torch import nn torch.manual_seed(seed) net = nn.Sequential( nn.Conv2d(4, 16, 3, padding=1), nn.ReLU(), nn.Conv2d(16, 16, 3, padding=1), nn.ReLU(), nn.Conv2d(16, 1, 1), ) nn.init.zeros_(net[-1].weight) nn.init.zeros_(net[-1].bias) return net def inputs(coarse, rgb, arm): import torch if arm not in {"coarse_only", "native_rgb"}: raise ValueError("Unknown refinement arm") if ( coarse.ndim != 4 or coarse.shape[1] != 1 or rgb.shape != (coarse.shape[0], 3, *coarse.shape[-2:]) or coarse.dtype != torch.float32 or rgb.dtype != torch.uint8 or coarse.requires_grad or coarse.device != rgb.device or not torch.isfinite(coarse).all() ): raise ValueError("Aligned detached float32 scores and native uint8 RGB required") fine = ( rgb.float().div(127.5).sub(1) if arm == "native_rgb" else torch.zeros_like(rgb, dtype=torch.float32) ) return torch.cat((coarse, fine), dim=1) def residual(net, x, tile=None): """Two 3x3 layers require a two-pixel halo. No resize or boundary averaging.""" if tile is None: return net(x) if not isinstance(tile, int) or isinstance(tile, bool) or tile < 1: raise ValueError("Positive integer tile size required") import torch h, w = x.shape[-2:] out = torch.empty((x.shape[0], 1, h, w), dtype=x.dtype, device=x.device) for y in range(0, h, tile): for xx in range(0, w, tile): y1, x1 = min(y + tile, h), min(xx + tile, w) sy, sx, ey, ex = max(0, y - 2), max(0, xx - 2), min(h, y1 + 2), min(w, x1 + 2) patch = net(x[..., sy:ey, sx:ex]) out[..., y:y1, xx:x1] = patch[..., y - sy : y1 - sy, xx - sx : x1 - sx] return out def prediction(net, coarse, x, tile=None): """Add once after C4 residual averaging, preserving exact zero identity.""" import torch correction = torch.stack( [ torch.rot90(residual(net, torch.rot90(x, k, [-2, -1]), tile), -k, [-2, -1]) for k in range(4) ] ).mean(0) return coarse + correction def validate_groups(records, held, groups, parent_groups): expected = set(groups) - {held} if held not in groups or len(set(groups)) != len(groups) or set(parent_groups) != expected: raise ValueError("Parent must exclude the whole held street") ids = [r["sample_id"] for r in records] if ( not records or len(set(ids)) != len(ids) or not all(ids) or any(r["split"] != "train" or r["group"] == held for r in records) or {r["group"] for r in records} != expected ): raise ValueError("Unique TRAIN records from exactly the allowed streets required")