| from __future__ import annotations |
|
|
| import torch |
| import torch.nn as nn |
|
|
| |
| _CONCEPT_NAMES = [ |
| "orientation_coherence", |
| "ridge_valley_clarity", |
| "continuity", |
| "noise_level", |
| "contrast_uniformity", |
| "minutiae_reliability", |
| ] |
|
|
|
|
| class ConceptHead(nn.Module): |
| """Predict concept activations in [0, 1] from globally-pooled features. |
| |
| Legacy architecture β inputs a [B, D] vector (global-average-pooled backbone |
| output). Kept for backward compatibility with checkpoints v16βv26. |
| |
| For new experiments use SpatialConceptHead which operates on the full |
| 14Γ14 spatial token map and better captures spatial quality concepts such |
| as orientation_coherence, continuity, and minutiae_reliability. |
| """ |
|
|
| CONCEPT_NAMES = _CONCEPT_NAMES |
| uses_spatial: bool = False |
|
|
| def __init__(self, in_dim: int, k: int = 6, hidden_dim: int = 256): |
| super().__init__() |
| self.mlp = nn.Sequential( |
| nn.Linear(in_dim, hidden_dim), |
| nn.LayerNorm(hidden_dim), |
| nn.GELU(), |
| nn.Linear(hidden_dim, k), |
| nn.Sigmoid(), |
| ) |
|
|
| def forward(self, features: torch.Tensor) -> torch.Tensor: |
| """Args: |
| features: [B, D] globally-pooled backbone features. |
| Returns: |
| concepts: [B, k] each β (0, 1). |
| """ |
| return self.mlp(features) |
|
|
|
|
| class SpatialConceptHead(nn.Module): |
| """Predict concept activations from spatial token features [B, N, D]. |
| |
| Architecture |
| ------------ |
| Shared trunk : Linear(D β hidden_dim) β LayerNorm β GELU β [B, N, hidden_dim] |
| Per-concept : Linear(hidden_dim β 1) β mean over N β scalar |
| Activation : Sigmoid β (0, 1) |
| |
| Using spatial tokens (instead of the globally-pooled vector) lets each |
| concept attend to different image regions: |
| |
| - orientation_coherence : local ridge flow consistency across patches |
| - continuity : ridge break locations |
| - minutiae_reliability : bifurcation / ridge-ending regions |
| |
| Separate per-concept projection weights reduce cross-concept entanglement |
| compared to a single shared MLP that outputs all k values simultaneously. |
| The shared trunk amortises the cost of the first linear projection across |
| all 196 tokens. |
| """ |
|
|
| CONCEPT_NAMES = _CONCEPT_NAMES |
| uses_spatial: bool = True |
|
|
| def __init__(self, in_dim: int, k: int = 6, hidden_dim: int = 128): |
| super().__init__() |
| self.trunk = nn.Sequential( |
| nn.Linear(in_dim, hidden_dim), |
| nn.LayerNorm(hidden_dim), |
| nn.GELU(), |
| ) |
| |
| |
| self.concept_projs = nn.ModuleList([ |
| nn.Linear(hidden_dim, 1) for _ in range(k) |
| ]) |
| self.k = k |
|
|
| def forward(self, spatial: torch.Tensor) -> torch.Tensor: |
| """Args: |
| spatial: [B, N, D] spatial token features from backbone.forward_spatial(). |
| N = 196 (14Γ14 patches for 224-px input), D = 320 for TinyViT-5M. |
| Returns: |
| concepts: [B, k] each β (0, 1), high = better quality for that concept. |
| """ |
| h = self.trunk(spatial) |
| |
| concepts = torch.cat( |
| [proj(h).mean(dim=1) for proj in self.concept_projs], |
| dim=1, |
| ) |
| return torch.sigmoid(concepts) |
|
|