Spaces:
Sleeping
Sleeping
Download src/models/global_context.py from farrell236/OCT_Segmentation_a1: direct link, hf CLI and curl.
- Browser
- Download file 2.17 kB
-
https://huggingface.co/spaces/farrell236/OCT_Segmentation_a1/resolve/main/src/models/global_context.py
- Command line
-
hf download hf://spaces/farrell236/OCT_Segmentation_a1/src/models/global_context.py
-
curl -L -o global_context.py https://huggingface.co/spaces/farrell236/OCT_Segmentation_a1/resolve/main/src/models/global_context.py
2.17 kB
| """Local adjacent slices conditioned on three whole-volume OCT views.""" | |
| import torch | |
| from torch import nn | |
| from torch.nn import functional as F | |
| from src.models.context import ContextBoundaryNet | |
| from src.models.unet import Block | |
| class GlobalBoundaryNet(ContextBoundaryNet): | |
| def __init__(self, n_boundaries, base, prior_height): | |
| super().__init__(n_boundaries, base, prior_height) | |
| self.global_encoder = nn.Sequential( | |
| nn.Conv2d(3, base, 5, stride=2, padding=2, bias=False), | |
| nn.GroupNorm(min(8, base), base), | |
| nn.SiLU(), | |
| Block(base, base * 2), | |
| nn.AvgPool2d(2), | |
| Block(base * 2, base * 4), | |
| nn.AdaptiveAvgPool2d(1), | |
| nn.Flatten(), | |
| ) | |
| self.global_film = nn.Linear(base * 4, base * 12 * 2) | |
| nn.init.zeros_(self.global_film.weight) | |
| nn.init.zeros_(self.global_film.bias) | |
| def forward(self, image): | |
| if image.shape[1] != 6: | |
| raise ValueError("Expected local3 plus global3 OCT channels") | |
| global_image = F.interpolate( | |
| image[:, 3:], size=(96, 128), mode="bilinear", align_corners=False | |
| ) | |
| embedding = self.global_encoder(global_image) | |
| scale, bias = self.global_film(embedding).chunk(2, dim=1) | |
| x = image[:, :3].contiguous() | |
| skips = [] | |
| for index, layer in enumerate(self.down): | |
| if index: | |
| x = F.avg_pool2d(x, 2) | |
| x = layer(x) | |
| if index == len(self.down) - 1: | |
| x = x * (1 + scale[:, :, None, None]) + bias[:, :, None, None] | |
| skips.append(x) | |
| for index, layer in enumerate(self.up): | |
| skip = skips[-2 - index] | |
| x = F.interpolate( | |
| x, size=skip.shape[-2:], mode="bilinear", align_corners=False | |
| ) | |
| x = layer(torch.cat([x, skip], 1)) | |
| logits = self.head(x) | |
| prior = self.axial_prior | |
| if prior.shape[-2] != logits.shape[-2]: | |
| prior = F.interpolate( | |
| prior, size=(logits.shape[-2], 1), mode="bilinear", align_corners=False | |
| ) | |
| return logits + prior | |