"""Original-coordinate inference for local and whole-volume refiners.""" import numpy as np import torch from torch.nn import functional as F from src.io import context_batch from src.models.unet import decode_logits from src.models.volume import calibrate_positions, canonical_features, teacher_forward @torch.inference_mode() def predict_cascade(row, prepared, net, cfg, device, batch_size, tta): if not prepared or any(len(p) != 4 for p in prepared): raise ValueError("Missing fine OCT pixel packets") H, A = prepared[0][1:3] if any(p[1:3] != (H, A) for p in prepared): raise ValueError("Inconsistent concatenated cascade geometry") coarse_arrays = [p[0] for p in prepared] fine_arrays = [p[3] for p in prepared] combine = lambda xs: ( xs[0] if len(xs) == 1 else ( np.concatenate(xs, axis=0) if isinstance(xs[0], np.ndarray) else torch.cat(xs, dim=0) ) ) coarse = combine(coarse_arrays) fine = combine(fine_arrays) child = cfg["coarse_config"] radius = child.get("context_radius", 0) family = row["device"] outs = [] if len(coarse) != len(fine): raise ValueError("Inconsistent coarse/fine OCT frame count") def run(x, high): p = decode_logits( net.coarse(x, family), child.get("local_radius", 6), child.get("decode_method", "local_centroid"), ) center = F.interpolate( p, size=cfg["fine_width"], mode="linear", align_corners=True ) * ((cfg["fine_height"] - 1) / (child["height"] - 1)) delta = net.refiner.residual(net.refiner(high, center, family)) # Interpolate the residual separately: zero initialization preserves the # coarse original-grid prediction without an extra interpolation of it. base = F.interpolate(p, size=A, mode="linear", align_corners=True) * ( (H - 1) / (child["height"] - 1) ) correction = F.interpolate(delta, size=A, mode="linear", align_corners=True) * ( (H - 1) / (cfg["fine_height"] - 1) ) return base + correction for start in range(0, len(coarse), batch_size): end = min(start + batch_size, len(coarse)) x = context_batch(coarse, start, end, radius, device) high = fine[start:end] high = (torch.from_numpy(high) if isinstance(high, np.ndarray) else high).to( device=device, dtype=torch.float32 ) with torch.autocast( device_type="cuda", dtype=torch.float16, enabled=device.type == "cuda" ): p = run(x, high) if tta: p = (p + run(x.flip(-1), high.flip(-1)).flip(-1)) / 2 outs.append(p.clamp(0, H - 1).cpu().numpy().astype("float32")) return np.concatenate(outs, axis=0).transpose(1, 0, 2), np.asarray( cfg["boundary_keys"][family] ) def feature_frame_indices(count, mode, frames): if mode not in ["dense", "stratified_centers"]: raise ValueError("Unknown volume feature-frame mode") if ( not isinstance(frames, int) or isinstance(frames, bool) or frames < 1 or count < 1 ): raise ValueError("Invalid volume feature-frame count") if mode == "dense" or frames >= count: return np.arange(count, dtype="int64") return np.floor((np.arange(frames, dtype="float64") + 0.5) * count / frames).astype( "int64" ) def inference_feature_grid(features, cfg): count = len(features) fraction = np.arange(count, dtype="float32") / max(1, count - 1) indices = feature_frame_indices( count, cfg.get("volume_feature_frame_mode", "dense"), cfg.get("frames_per_volume", 8), ) return canonical_features( features[indices], fraction[indices], cfg.get("volume_frames", 16) ) @torch.inference_mode() def predict_volume(row, prepared, net, cfg, device, batch_size, tta): if tta: raise ValueError("Volume refinement currently requires horizontal TTA disabled") scale = cfg.get("volume_correction_scale", 1.0) if isinstance(scale, bool) or not np.isfinite(scale) or not 0 <= scale <= 1: raise ValueError("Invalid volume correction scale") feature_frame_indices( 1, cfg.get("volume_feature_frame_mode", "dense"), cfg.get("frames_per_volume", 8), ) packets = [p[:3] for p in prepared] H, A = packets[0][1:] if any(p[1:] != (H, A) for p in packets): raise ValueError("Inconsistent original volume grids") arrays = [p[0] for p in packets] pixels = ( arrays[0] if len(arrays) == 1 else ( np.concatenate(arrays, 0) if isinstance(arrays[0], np.ndarray) else torch.cat(arrays, 0) ) ) radius = cfg.get("context_radius", 0) family = row["device"] features = [] probabilities = [] for start in range(0, len(pixels), batch_size): end = min(start + batch_size, len(pixels)) x = context_batch(pixels, start, end, radius, device) with torch.autocast( device_type="cuda", dtype=torch.float16, enabled=device.type == "cuda" ): f, _, _, p = teacher_forward( net.coarse, cfg["coarse_config"], x, family, cfg.get("volume_columns", 32), cfg["width"], True, ) features.append(f.cpu().numpy()) probabilities.append(p) probability = torch.cat(probabilities, 0) del probabilities fraction = np.arange(len(pixels), dtype="float32") / max(1, len(pixels) - 1) grid = inference_feature_grid(np.concatenate(features, 0), cfg) inputs = torch.from_numpy(grid.astype("float32")).unsqueeze(0).to(device) query = torch.from_numpy(fraction).unsqueeze(0).to(device) with torch.autocast( device_type="cuda", dtype=torch.float16, enabled=device.type == "cuda" ): positive, endpoint = net.corrections(inputs, family, query, cfg["width"]) if scale != 1.0: positive = positive * scale endpoint = endpoint * scale positions = calibrate_positions( probability, positive[0].permute(1, 0, 2), endpoint[0].permute(1, 0, 2) ) positions = F.interpolate(positions, size=A, mode="linear", align_corners=True) * ( (H - 1) / (cfg["height"] - 1) ) return positions.clamp(0, H - 1).cpu().numpy().astype("float32").transpose( 1, 0, 2 ), np.asarray(cfg["boundary_keys"][family])