File size: 3,664 Bytes
dadf189 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | from __future__ import annotations
import torch
import torch.nn as nn
# Shared concept name registry
_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(),
)
# k independent projections — each learns which spatial regions matter
# for its concept (reduces entanglement vs. a single shared Linear→k)
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) # [B, N, hidden_dim]
# Each concept proj: [B, N, 1] → mean over N → [B, 1]
concepts = torch.cat(
[proj(h).mean(dim=1) for proj in self.concept_projs], # k × [B, 1]
dim=1,
) # [B, k]
return torch.sigmoid(concepts)
|