feat(heads): decompose subjective head and add contrastive projection
Browse filesSplit subjective output into seasonality logits, gender scalar, and
wearability logits. Add L2-normalized contrastive projection head for
InfoNCE-style perceptual training.
- src/pino/heads.py +51 -10
src/pino/heads.py
CHANGED
|
@@ -32,16 +32,17 @@ class ObjectiveOdorHead(nn.Module):
|
|
| 32 |
|
| 33 |
class SubjectiveSemanticHead(nn.Module):
|
| 34 |
"""
|
| 35 |
-
Head B:
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
"""
|
| 40 |
|
| 41 |
def __init__(self, hidden_dim: int = 256, num_classes_sub: int = 7) -> None:
|
| 42 |
super().__init__()
|
| 43 |
self.num_classes_sub = num_classes_sub
|
| 44 |
-
# Global average pooling across time and ingredient dimensions.
|
| 45 |
self.pool = nn.AdaptiveAvgPool2d((1, hidden_dim))
|
| 46 |
self.projection = nn.Sequential(
|
| 47 |
nn.Linear(hidden_dim, hidden_dim),
|
|
@@ -50,29 +51,69 @@ class SubjectiveSemanticHead(nn.Module):
|
|
| 50 |
nn.Linear(hidden_dim, num_classes_sub),
|
| 51 |
)
|
| 52 |
|
| 53 |
-
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 54 |
"""
|
| 55 |
x: (B, T, S, hidden_dim)
|
| 56 |
-
Returns: (B,
|
| 57 |
"""
|
| 58 |
pooled = x.mean(dim=(1, 2)) # (B, H)
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
class PIMTHeads(nn.Module):
|
| 63 |
-
"""Dual-headed multi-task output layer for the PIMT model."""
|
| 64 |
|
| 65 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
super().__init__()
|
| 67 |
self.objective = ObjectiveOdorHead(hidden_dim, objective_dim)
|
| 68 |
self.subjective = SubjectiveSemanticHead(hidden_dim, num_classes_sub)
|
|
|
|
| 69 |
|
| 70 |
def forward(self, x: torch.Tensor) -> dict[str, torch.Tensor]:
|
| 71 |
"""
|
| 72 |
x: (B, T, S, hidden_dim)
|
| 73 |
-
Returns dict with 'objective' and '
|
| 74 |
"""
|
| 75 |
return {
|
| 76 |
"objective": self.objective(x), # (B, T, 138)
|
| 77 |
"subjective": self.subjective(x), # (B, 7)
|
|
|
|
| 78 |
}
|
|
|
|
| 32 |
|
| 33 |
class SubjectiveSemanticHead(nn.Module):
|
| 34 |
"""
|
| 35 |
+
Head B: subjective semantic outputs via global time-pooling.
|
| 36 |
|
| 37 |
+
Splits the 7-D vector into:
|
| 38 |
+
- seasonality: 4 logits (cross-entropy target)
|
| 39 |
+
- gender_profile: 1 scalar (regression target)
|
| 40 |
+
- wearability: 2 logits (binary multi-label target)
|
| 41 |
"""
|
| 42 |
|
| 43 |
def __init__(self, hidden_dim: int = 256, num_classes_sub: int = 7) -> None:
|
| 44 |
super().__init__()
|
| 45 |
self.num_classes_sub = num_classes_sub
|
|
|
|
| 46 |
self.pool = nn.AdaptiveAvgPool2d((1, hidden_dim))
|
| 47 |
self.projection = nn.Sequential(
|
| 48 |
nn.Linear(hidden_dim, hidden_dim),
|
|
|
|
| 51 |
nn.Linear(hidden_dim, num_classes_sub),
|
| 52 |
)
|
| 53 |
|
| 54 |
+
def forward(self, x: torch.Tensor) -> dict[str, torch.Tensor]:
|
| 55 |
"""
|
| 56 |
x: (B, T, S, hidden_dim)
|
| 57 |
+
Returns: dict with 'seasonality' (B,4), 'gender_profile' (B,1), 'wearability' (B,2).
|
| 58 |
"""
|
| 59 |
pooled = x.mean(dim=(1, 2)) # (B, H)
|
| 60 |
+
out = self.projection(pooled) # (B, 7)
|
| 61 |
+
return {
|
| 62 |
+
"seasonality": out[:, :4], # (B, 4) logits
|
| 63 |
+
"gender_profile": out[:, 4:5], # (B, 1)
|
| 64 |
+
"wearability": out[:, 5:7], # (B, 2) logits
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class ContrastiveProjectionHead(nn.Module):
|
| 69 |
+
"""
|
| 70 |
+
Projection head for the perceptual contrastive loss.
|
| 71 |
+
|
| 72 |
+
Mean-pools the (B, T, S, hidden_dim) latent representation and maps it to a
|
| 73 |
+
compact embedding that is L2-normalized for InfoNCE / triplet losses.
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
def __init__(self, hidden_dim: int = 256, proj_dim: int = 128) -> None:
|
| 77 |
+
super().__init__()
|
| 78 |
+
self.pool = nn.AdaptiveAvgPool2d((1, hidden_dim))
|
| 79 |
+
self.projection = nn.Sequential(
|
| 80 |
+
nn.Linear(hidden_dim, proj_dim),
|
| 81 |
+
nn.ReLU(),
|
| 82 |
+
nn.Linear(proj_dim, proj_dim),
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 86 |
+
"""
|
| 87 |
+
x: (B, T, S, hidden_dim)
|
| 88 |
+
Returns: (B, proj_dim) L2-normalized embeddings.
|
| 89 |
+
"""
|
| 90 |
+
pooled = x.mean(dim=(1, 2)) # (B, hidden_dim)
|
| 91 |
+
z = self.projection(pooled) # (B, proj_dim)
|
| 92 |
+
return nn.functional.normalize(z, p=2, dim=1)
|
| 93 |
|
| 94 |
|
| 95 |
class PIMTHeads(nn.Module):
|
| 96 |
+
"""Dual-headed multi-task output layer plus contrastive projection for the PIMT model."""
|
| 97 |
|
| 98 |
+
def __init__(
|
| 99 |
+
self,
|
| 100 |
+
hidden_dim: int = 256,
|
| 101 |
+
objective_dim: int = 138,
|
| 102 |
+
num_classes_sub: int = 7,
|
| 103 |
+
proj_dim: int = 128,
|
| 104 |
+
) -> None:
|
| 105 |
super().__init__()
|
| 106 |
self.objective = ObjectiveOdorHead(hidden_dim, objective_dim)
|
| 107 |
self.subjective = SubjectiveSemanticHead(hidden_dim, num_classes_sub)
|
| 108 |
+
self.contrastive = ContrastiveProjectionHead(hidden_dim, proj_dim)
|
| 109 |
|
| 110 |
def forward(self, x: torch.Tensor) -> dict[str, torch.Tensor]:
|
| 111 |
"""
|
| 112 |
x: (B, T, S, hidden_dim)
|
| 113 |
+
Returns dict with 'objective', 'subjective', and 'contrastive' outputs.
|
| 114 |
"""
|
| 115 |
return {
|
| 116 |
"objective": self.objective(x), # (B, T, 138)
|
| 117 |
"subjective": self.subjective(x), # (B, 7)
|
| 118 |
+
"contrastive": self.contrastive(x), # (B, proj_dim)
|
| 119 |
}
|