Spaces:
Sleeping
Sleeping
Download src/models/volume.py from farrell236/OCT_Segmentation_a2: direct link, hf CLI and curl.
- Browser
- Download file 11.3 kB
-
https://huggingface.co/spaces/farrell236/OCT_Segmentation_a2/resolve/main/src/models/volume.py
- Command line
-
hf download hf://spaces/farrell236/OCT_Segmentation_a2/src/models/volume.py
-
curl -L -o volume.py https://huggingface.co/spaces/farrell236/OCT_Segmentation_a2/resolve/main/src/models/volume.py
11.3 kB
| """Whole-volume refinement, frozen teacher features, and geometric queries.""" | |
| import numpy as np | |
| import torch | |
| from torch import nn | |
| from torch.nn import functional as F | |
| from src.inference.density import PLAIN | |
| from src.models.unet import probability_median | |
| def teacher_components(net, cfg): | |
| if cfg.get("tta_horizontal", False): | |
| raise ValueError("Volume teacher must disable horizontal TTA") | |
| if cfg["architecture"] in PLAIN: | |
| if cfg.get("decode_method") != "posterior_median": | |
| raise ValueError("Volume teacher needs posterior-median decoding") | |
| return [(net, cfg, 1.0)] | |
| if ( | |
| cfg["architecture"] == "boundary_ensemble_v1" | |
| and cfg.get("reduce") == "probability_mean" | |
| and not cfg.get("density_native_columns", False) | |
| ): | |
| components = list(zip(net.nets, cfg["components"])) | |
| if any( | |
| c["config"]["architecture"] not in PLAIN | |
| or c["config"].get("tta_horizontal", False) | |
| or c["config"].get("prediction_policy") | |
| or c.get("post_policy") | |
| for _, c in components | |
| ): | |
| raise ValueError("Unsupported volume probability component") | |
| return [(m, c["config"], c["weight"]) for m, c in components] | |
| raise ValueError("Unsupported frozen volume teacher") | |
| def feature_channels(cfg, family): | |
| components = ( | |
| cfg["components"] | |
| if cfg["architecture"] == "boundary_ensemble_v1" | |
| else [{"config": cfg}] | |
| ) | |
| return 26 * sum(c["config"]["base"] for c in components) + 4 * len( | |
| cfg["boundary_keys"][family] | |
| ) | |
| def normalized_positive(probability): | |
| positive = probability[:, :, 1:, :] | |
| mass = positive.sum(2, keepdim=True) | |
| return torch.where( | |
| mass > 1e-12, | |
| positive / mass.clamp_min(1e-12), | |
| torch.full_like(positive, 1 / positive.shape[2]), | |
| ) | |
| def teacher_forward( | |
| net, cfg, x, family, feature_width=32, output_width=512, return_probability=False | |
| ): | |
| """Read image activations and predictions; no supplied label values enter.""" | |
| radius = cfg.get("context_radius", 0) | |
| hidden = [] | |
| mixture = None | |
| if x.shape[1] != 2 * radius + 1: | |
| raise ValueError("Teacher image context mismatch") | |
| for model, child, weight in teacher_components(net, cfg): | |
| r = child.get("context_radius", 0) | |
| part = x[:, radius - r : radius + r + 1] | |
| captures = {} | |
| base = child["base"] | |
| def deep_hook(module, inputs): | |
| captures["deep"] = inputs[0][:, : 12 * base] | |
| def fine_hook(module, inputs): | |
| captures["fine"] = inputs[0] | |
| handles = [ | |
| model.decoder[0].register_forward_pre_hook(deep_hook), | |
| model.heads[family].register_forward_pre_hook(fine_hook), | |
| ] | |
| try: | |
| logits = model(part, family) | |
| finally: | |
| for handle in handles: | |
| handle.remove() | |
| if captures["deep"].shape[1] != 12 * base or captures["fine"].shape[1] != base: | |
| raise ValueError("Unexpected teacher activation geometry") | |
| fields = [] | |
| for key in ["fine", "deep"]: | |
| value = captures[key].float() | |
| stats = torch.cat([value.mean(2), value.amax(2)], dim=1) | |
| fields.append( | |
| F.interpolate( | |
| stats, size=feature_width, mode="linear", align_corners=True | |
| ) | |
| ) | |
| hidden.append(torch.cat(fields, dim=1)) | |
| p = logits.float().softmax(2) * float(weight) | |
| mixture = p if mixture is None else mixture + p | |
| full = probability_median(mixture) | |
| positive = probability_median(normalized_positive(mixture)) + 1 | |
| p0 = mixture[:, :, 0, :] | |
| endpoint = torch.logit(p0.clamp(1e-6, 1 - 1e-6)) | |
| entropy = -(mixture * mixture.clamp_min(1e-12).log()).sum(2) / np.log( | |
| max(mixture.shape[2], 2) | |
| ) | |
| resize = lambda v: F.interpolate( | |
| v, size=feature_width, mode="linear", align_corners=True | |
| ) | |
| stats = [ | |
| resize(full) / (cfg["height"] - 1), | |
| resize(positive) / (cfg["height"] - 1), | |
| resize(p0), | |
| resize(entropy), | |
| ] | |
| features = torch.cat(hidden + stats, dim=1).to(torch.float16) | |
| if ( | |
| features.shape[1] != feature_channels(cfg, family) | |
| or not torch.isfinite(features).all() | |
| ): | |
| raise ValueError("Invalid image-derived volume features") | |
| return ( | |
| features, | |
| F.interpolate(positive, size=output_width, mode="linear", align_corners=True), | |
| F.interpolate(endpoint, size=output_width, mode="linear", align_corners=True), | |
| mixture if return_probability else None, | |
| ) | |
| def canonical_features(features, frame_fraction, frames=16): | |
| """Interpolate sampled encoded B locations onto a fixed geometric grid.""" | |
| values = np.asarray(features, dtype="float32") | |
| fraction = np.asarray(frame_fraction, dtype="float64") | |
| if ( | |
| values.ndim != 3 | |
| or len(values) != len(fraction) | |
| or not len(fraction) | |
| or not np.isfinite(values).all() | |
| or not np.isfinite(fraction).all() | |
| or np.any(np.diff(fraction) <= 0) | |
| ): | |
| raise ValueError("Invalid sampled feature geometry") | |
| if len(fraction) == 1: | |
| result = np.repeat(values, frames, axis=0) | |
| else: | |
| query = np.linspace(0, 1, frames).clip(fraction[0], fraction[-1]) | |
| right = np.searchsorted(fraction, query, side="right").clip( | |
| 1, len(fraction) - 1 | |
| ) | |
| left = right - 1 | |
| weight = ((query - fraction[left]) / (fraction[right] - fraction[left])).astype( | |
| "float32" | |
| )[:, None, None] | |
| result = values[left] * (1 - weight) + values[right] * weight | |
| return result.transpose(1, 0, 2).astype("float16") | |
| def load_cached_images(paths): | |
| """The image cache may also contain labels; only its image field is loaded.""" | |
| images = [] | |
| for path in paths: | |
| with np.load(path, allow_pickle=False) as z: | |
| images.append(z["image"]) | |
| result = np.stack(images).astype("float32") | |
| if not np.isfinite(result).all(): | |
| raise ValueError("Nonfinite cached OCT pixels") | |
| return result | |
| def sample_field(field, frame_fraction, width): | |
| n, _, _, _ = field.shape | |
| fractions = frame_fraction.to(device=field.device, dtype=torch.float32) | |
| columns = torch.linspace(-1, 1, width, device=field.device) | |
| grid = torch.stack( | |
| [ | |
| columns[None, None, :].expand(n, fractions.shape[1], width), | |
| (fractions[:, :, None] * 2 - 1).expand(n, fractions.shape[1], width), | |
| ], | |
| dim=-1, | |
| ) | |
| return F.grid_sample( | |
| field.float(), grid, mode="bilinear", padding_mode="border", align_corners=True | |
| ) | |
| def calibrate_positions(probability, positive_delta, endpoint_delta): | |
| """Anchored endpoint-mass calibration; zero corrections preserve the teacher.""" | |
| original = probability[:, :, 0, :] | |
| bounded = original.clamp(1e-6, 1 - 1e-6) | |
| odds = endpoint_delta.exp() | |
| calibrated = bounded * odds / (1 + bounded * (odds - 1)) | |
| mass = (original + (calibrated - bounded)).clamp(0, 1) | |
| positive = normalized_positive(probability) | |
| anchor = torch.cat( | |
| [original[:, :, None, :], positive * (1 - original[:, :, None, :])], dim=2 | |
| ) | |
| updated = torch.cat( | |
| [mass[:, :, None, :], positive * (1 - mass[:, :, None, :])], dim=2 | |
| ) | |
| result = ( | |
| probability_median(probability) | |
| + (probability_median(updated) - probability_median(anchor)) | |
| + torch.where(mass < 0.5, positive_delta, torch.zeros_like(positive_delta)) | |
| ) | |
| return result.clamp(0, probability.shape[2] - 1) | |
| class ContextBlock(nn.Module): | |
| def __init__(self, width, dilation): | |
| super().__init__() | |
| self.net = nn.Sequential( | |
| nn.Conv2d(width, width, 3, padding=dilation, dilation=dilation, bias=False), | |
| nn.GroupNorm(8, width), | |
| nn.SiLU(), | |
| nn.Conv2d(width, width, 3, padding=dilation, dilation=dilation, bias=False), | |
| nn.GroupNorm(8, width), | |
| ) | |
| def forward(self, x): | |
| return F.silu(x + self.net(x)) | |
| class VolumeRefiner(nn.Module): | |
| def __init__(self, cfg): | |
| super().__init__() | |
| from src.models import make_model | |
| self.coarse = make_model(cfg["coarse_config"]) | |
| self.coarse.requires_grad_(False) | |
| self.coarse.eval() | |
| width = cfg.get("volume_width", 64) | |
| keys = cfg["boundary_keys"] | |
| self.projections = nn.ModuleDict( | |
| { | |
| d: nn.Conv2d( | |
| feature_channels(cfg["coarse_config"], d), width, 1, bias=False | |
| ) | |
| for d in keys | |
| } | |
| ) | |
| self.context = nn.Sequential( | |
| nn.GroupNorm(8, width), | |
| nn.SiLU(), | |
| *[ContextBlock(width, 2**i) for i in range(cfg.get("volume_blocks", 3))], | |
| ) | |
| self.heads = nn.ModuleDict( | |
| {d: nn.Conv2d(width, 2 * len(k), 1) for d, k in keys.items()} | |
| ) | |
| self.config = cfg | |
| for head in self.heads.values(): | |
| nn.init.zeros_(head.weight) | |
| nn.init.zeros_(head.bias) | |
| def train(self, mode=True): | |
| super().train(mode) | |
| self.coarse.eval() | |
| return self | |
| def forward(self, features, family): | |
| return self.heads[family](self.context(self.projections[family](features))) | |
| def corrections(self, features, family, frame_fraction, width): | |
| raw = self(features, family).float() | |
| k = raw.shape[1] // 2 | |
| positive = self.config.get("volume_radius", 16.0) * raw[:, :k].tanh() | |
| radius = self.config.get("endpoint_radius", 30.0) | |
| endpoint = radius * (raw[:, k:] / radius).tanh() | |
| sampled = sample_field( | |
| torch.cat([positive, endpoint], dim=1), frame_fraction, self.config["width"] | |
| ) | |
| if width != self.config["width"]: | |
| n, c, b, a = sampled.shape | |
| sampled = F.interpolate( | |
| sampled.reshape(n, c * b, a), | |
| size=width, | |
| mode="linear", | |
| align_corners=True, | |
| ).reshape(n, c, b, width) | |
| return sampled[:, :k], sampled[:, k:] | |
| def volume_loss( | |
| positive_delta, | |
| endpoint_delta, | |
| base_positive, | |
| base_endpoint, | |
| target_positive, | |
| zero_fraction, | |
| valid, | |
| height, | |
| coordinate_weight=0.01, | |
| regularization=0.0001, | |
| ): | |
| positive = (base_positive + positive_delta).clamp(0, height - 1) | |
| logit = base_endpoint + endpoint_delta | |
| mass = zero_fraction.float() | |
| q = logit.sigmoid() | |
| distance = (positive - target_positive).abs() | |
| point = ( | |
| F.binary_cross_entropy_with_logits(logit, mass, reduction="none") | |
| + (1 - mass) * distance | |
| ) | |
| risk = q * (1 - mass) * target_positive + (1 - q) * ( | |
| mass * positive + (1 - mass) * distance | |
| ) | |
| point = ( | |
| point | |
| + coordinate_weight * risk | |
| + regularization * (positive_delta.square() + endpoint_delta.square()) | |
| ) | |
| counts = valid.sum((-1, -2)) | |
| term = (point * valid).sum((-1, -2)) / counts.clamp_min(1) | |
| return (term * (counts > 0)).sum() / (counts > 0).sum().clamp_min(1) | |