Spaces:
Running
Running
Download src/models/unet.py from farrell236/OCT_Segmentation_a1: direct link, hf CLI and curl.
- Browser
- Download file 3.8 kB
-
https://huggingface.co/spaces/farrell236/OCT_Segmentation_a1/resolve/main/src/models/unet.py
- Command line
-
hf download hf://spaces/farrell236/OCT_Segmentation_a1/src/models/unet.py
-
curl -L -o unet.py https://huggingface.co/spaces/farrell236/OCT_Segmentation_a1/resolve/main/src/models/unet.py
3.8 kB
| """Randomly initialized U-Net with per-boundary axial distributions.""" | |
| import torch | |
| from torch import nn | |
| from torch.nn import functional as F | |
| class Block(nn.Module): | |
| def __init__(self, a, b): | |
| super().__init__() | |
| g = min(8, b) | |
| self.net = nn.Sequential( | |
| nn.Conv2d(a, b, 3, padding=1, bias=False), | |
| nn.GroupNorm(g, b), | |
| nn.SiLU(), | |
| nn.Conv2d(b, b, 3, padding=1, bias=False), | |
| nn.GroupNorm(g, b), | |
| nn.SiLU(), | |
| ) | |
| def forward(self, x): | |
| return self.net(x) | |
| class BoundaryNet(nn.Module): | |
| def __init__(self, n_boundaries, base=24): | |
| super().__init__() | |
| c = [base, base * 2, base * 4, base * 8, base * 12] | |
| self.down = nn.ModuleList( | |
| [Block(1, c[0])] + [Block(c[i - 1], c[i]) for i in range(1, 5)] | |
| ) | |
| self.up = nn.ModuleList( | |
| [Block(c[i] + c[i - 1], c[i - 1]) for i in range(4, 0, -1)] | |
| ) | |
| self.head = nn.Conv2d(c[0], n_boundaries, 1) | |
| def forward(self, x): | |
| skips = [] | |
| for i, layer in enumerate(self.down): | |
| if i: | |
| x = F.avg_pool2d(x, 2) | |
| x = layer(x) | |
| skips.append(x) | |
| for i, layer in enumerate(self.up): | |
| skip = skips[-2 - i] | |
| x = F.interpolate( | |
| x, size=skip.shape[-2:], mode="bilinear", align_corners=False | |
| ) | |
| x = layer(torch.cat([x, skip], 1)) | |
| return self.head(x) | |
| def expected_positions(logits): | |
| p = logits.float().softmax(dim=-2) | |
| y = torch.arange(logits.shape[-2], device=logits.device, dtype=torch.float32) | |
| return (p * y[None, None, :, None]).sum(-2) | |
| def boundary_loss( | |
| logits, | |
| positions, | |
| valid, | |
| sigma=1.5, | |
| regression_weight=0.1, | |
| coordinate_scale=None, | |
| regression_loss="smooth_l1", | |
| active_head_packing=False, | |
| ): | |
| """Gaussian cross entropy plus a configured native-scaled position penalty.""" | |
| if active_head_packing: | |
| n, k, h, w = logits.shape | |
| support = valid.any(-1) | |
| count = max(1, int(support.sum(-1).max().item())) | |
| index = ( | |
| torch.arange(k, device=logits.device)[None] | |
| .expand(n, -1) | |
| .masked_fill(~support, k) | |
| .sort(-1) | |
| .values[:, :count] | |
| ) | |
| keep = index < k | |
| index = index.clamp_max(k - 1) | |
| logits = logits.gather(1, index[:, :, None, None].expand(-1, -1, h, w)) | |
| positions = positions.gather(1, index[:, :, None].expand(-1, -1, w)) | |
| valid = valid.gather(1, index[:, :, None].expand(-1, -1, w)) & keep[:, :, None] | |
| h = logits.shape[-2] | |
| rows = torch.arange(h, device=logits.device, dtype=torch.float32)[ | |
| None, None, :, None | |
| ] | |
| target = torch.exp(-0.5 * ((rows - positions[:, :, None, :]) / sigma) ** 2) | |
| target = target / target.sum(-2, keepdim=True).clamp_min(1e-8) | |
| ce = -(target * logits.float().log_softmax(-2)).sum(-2) | |
| if regression_loss == "smooth_l1": | |
| reg = F.smooth_l1_loss( | |
| expected_positions(logits), positions, reduction="none", beta=2 | |
| ) | |
| elif regression_loss == "l1": | |
| reg = (expected_positions(logits) - positions).abs() | |
| elif regression_loss == "expected_l1": | |
| reg = ( | |
| logits.float().softmax(-2) * (rows - positions[:, :, None, :]).abs() | |
| ).sum(-2) | |
| else: | |
| raise ValueError("Unknown position regression loss") | |
| if coordinate_scale is not None: | |
| reg = reg * coordinate_scale[:, None, None] | |
| m = valid.float() | |
| count = m.sum(-1) | |
| loss = ((ce + regression_weight * reg) * m).sum(-1) / count.clamp_min(1) | |
| included = count > 0 | |
| per_image = (loss * included).sum(1) / included.sum(1).clamp_min(1) | |
| return per_image.mean() | |