"""Optional training-only ordinal interval supervision from coded boundaries. The boundary objective keeps every original valid source target. Auxiliary intervals are defined only in columns whose necessary boundaries are valid and ordered. This auxiliary support is never used by reference conversion/scoring. """ import torch from torch import nn from torch.nn import functional as F from src.models.unet import BoundaryNet ORDERS = { "heidelberg_spectralis": [ "SCT_280677004", "DCM_128289", "DCM_128290", "DCM_128291", "DCM_128292", "DCM_128293", "SCT_76710003", "DCM_128321", "DCM_128323", "DCM_128297", "DCM_128300", ], "topcon_maestro2": [ "SCT_280677004", "DCM_128289", "DCM_128290", "DCM_128291", "DCM_128293", "SCT_76710003", "DCM_128320", "DCM_128297", "DCM_128299", ], "topcon_triton": [ "SCT_280677004", "DCM_128289", "DCM_128290", "DCM_128291", "DCM_128293", "SCT_76710003", "DCM_128320", "DCM_128297", "DCM_128299", "DCM_128301", ], "zeiss_cirrus": ["SCT_280677004", "DCM_128298"], } def configure_auxiliary(config): from src.models import output_indices if config.get("input_channels", 1) != 1 or config.get("head_mode") != "device": raise ValueError( "This auxiliary experiment requires single-channel device heads" ) mapping = {} names = [] for device, keys in sorted(config["device_keys"].items()): order = ORDERS[device] if set(keys) != set(order): raise ValueError("Unreviewed auxiliary boundary vocabulary") indices = output_indices(config, device) ordered = [indices[keys.index(key)] for key in order] regions = list(range(len(names), len(names) + len(order) + 1)) intervals = ( ["above_" + order[0]] + [a + "__to__" + b for a, b in zip(order[:-1], order[1:])] + ["below_" + order[-1]] ) names.extend(device + "::" + interval for interval in intervals) mapping[device] = {"boundary_indices": ordered, "region_indices": regions} config["auxiliary_mapping"] = mapping config["auxiliary_region_names"] = names return config class AuxiliaryBoundaryNet(BoundaryNet): def __init__(self, n_boundaries, base, height, n_regions): super().__init__(n_boundaries, base) self.axial_prior = nn.Parameter(torch.zeros(1, n_boundaries, height, 1)) self.auxiliary_head = nn.Conv2d(base, n_regions, 1) nn.init.zeros_(self.auxiliary_head.weight) nn.init.zeros_(self.auxiliary_head.bias) def forward(self, image, return_aux=False): x = image skips = [] for index, layer in enumerate(self.down): if index: x = F.avg_pool2d(x, 2) x = layer(x) 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)) prior = self.axial_prior if prior.shape[-2] != x.shape[-2]: prior = F.interpolate( prior, size=(x.shape[-2], 1), mode="bilinear", align_corners=False ) boundary = self.head(x) + prior return (boundary, self.auxiliary_head(x)) if return_aux else boundary def interval_targets(positions, valid, height): """Integer ordinal interval labels plus an independent auxiliary-column mask.""" support = valid.all(1) & (positions[:, 1:] >= positions[:, :-1]).all(1) rows = torch.arange(height, device=positions.device, dtype=positions.dtype)[ None, None, :, None ] target = (rows >= positions[:, :, None, :]).sum(1) return target, support def auxiliary_loss(logits, positions, valid, config): n, _, height, width = logits.shape per_image = logits.float().flatten(1).sum(1) * 0 for mapping in config["auxiliary_mapping"].values(): indices = mapping["boundary_indices"] regions = mapping["region_indices"] target, columns = interval_targets( positions[:, indices], valid[:, indices], height ) support = columns[:, None, :].expand(-1, height, -1).float() ce = F.cross_entropy(logits[:, regions].float(), target, reduction="none") count = logits.new_zeros((n, len(regions)), dtype=torch.float32) count.scatter_add_(1, target.flatten(1), support.flatten(1)) denominator = ( count.gather(1, target.flatten(1)).reshape(n, height, width).clamp_min(1) ) per_image = per_image + (ce * support / denominator).sum((1, 2)) / ( count > 0 ).sum(1).clamp_min(1) return per_image.mean()