File size: 6,440 Bytes
1ea7ba6 | 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | """
SpiceFusionNet β Multi-Modal Fusion CNN
β’ CNN Branch : EfficientNet-B4 (ImageNet pretrained)
β’ Texture Branch: LBP + GLCM features β MLP (β 256-d)
β’ Color Branch : HSV histogram β MLP (β 128-d)
β’ Fusion : learned attention weights across three branches
β’ Head : FC β BN β ReLU β Dropout(0.4) β Softmax
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import timm
import config
class _MLP(nn.Module):
def __init__(self, in_dim: int, hidden: int, out_dim: int, drop: float = 0.3):
super().__init__()
self.net = nn.Sequential(
nn.Linear(in_dim, hidden),
nn.BatchNorm1d(hidden),
nn.ReLU(inplace=True),
nn.Dropout(drop),
nn.Linear(hidden, out_dim),
nn.BatchNorm1d(out_dim),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.net(x)
class AttentionFusion(nn.Module):
"""Gate each branch with learned scalar weights (sum-to-1 via softmax)."""
def __init__(self, cnn_dim: int, tex_dim: int, col_dim: int):
super().__init__()
self.total = cnn_dim + tex_dim + col_dim
self.gate = nn.Sequential(
nn.Linear(self.total, 3),
nn.Softmax(dim=1),
)
def forward(self, f_cnn, f_tex, f_col):
cat = torch.cat([f_cnn, f_tex, f_col], dim=1) # (B, total)
g = self.gate(cat) # (B, 3)
out = torch.cat([
g[:, 0:1] * f_cnn,
g[:, 1:2] * f_tex,
g[:, 2:3] * f_col,
], dim=1)
return out # (B, total)
class SpiceFusionNet(nn.Module):
def __init__(
self,
num_classes: int = config.NUM_CLASSES,
pretrained: bool = config.PRETRAINED,
drop_rate: float = config.DROP_RATE,
cnn_dim: int = config.CNN_DIM,
tex_dim: int = config.TEX_DIM,
col_dim: int = config.COL_DIM,
proj_dim: int = config.PROJ_DIM,
tex_in: int = config.TEX_INPUT_DIM,
col_in: int = config.COL_INPUT_DIM,
):
super().__init__()
# ββ CNN Branch βββββββββββββββββββββββββββββββββββββββββββββββ
self.backbone = timm.create_model(
config.BACKBONE,
pretrained=pretrained,
num_classes=0,
global_pool="avg",
drop_rate=drop_rate,
)
# ββ Texture Branch βββββββββββββββββββββββββββββββββββββββββββ
self.tex_branch = _MLP(tex_in, 128, tex_dim, drop=0.3)
# ββ Color Branch βββββββββββββββββββββββββββββββββββββββββββββ
self.col_branch = _MLP(col_in, 64, col_dim, drop=0.3)
# ββ Attention Fusion βββββββββββββββββββββββββββββββββββββββββ
self.fusion = AttentionFusion(cnn_dim, tex_dim, col_dim)
total_dim = cnn_dim + tex_dim + col_dim
# ββ Projection Head (Phase 2 β SupCon) βββββββββββββββββββββββ
self.proj_head = nn.Sequential(
nn.Linear(cnn_dim, 512),
nn.ReLU(inplace=True),
nn.Linear(512, proj_dim),
)
# ββ Image-only Classifier (Phase 1) ββββββββββββββββββββββββββ
self.img_head = nn.Sequential(
nn.Linear(cnn_dim, 512),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Dropout(drop_rate),
nn.Linear(512, num_classes),
)
# ββ Fusion Classifier (Phase 3) βββββββββββββββββββββββββββββββ
self.fusion_head = nn.Sequential(
nn.Linear(total_dim, 512),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Dropout(drop_rate),
nn.Linear(512, num_classes),
)
# ββ Forward modes ββββββββββββββββββββββββββββββββββββββββββββββββ
def forward_image(self, x: torch.Tensor) -> torch.Tensor:
"""Phase 1: image β logits."""
return self.img_head(self.backbone(x))
def forward_contrastive(self, x: torch.Tensor) -> torch.Tensor:
"""Phase 2: image β L2-normalized projection features (for SupCon)."""
feats = self.backbone(x)
proj = self.proj_head(feats)
return F.normalize(proj, dim=1)
def forward_fusion(
self,
x: torch.Tensor,
tex: torch.Tensor,
col: torch.Tensor,
):
"""Phase 3: image + texture + color β (logits, proj_feats)."""
f_cnn = self.backbone(x)
f_tex = self.tex_branch(tex)
f_col = self.col_branch(col)
fused = self.fusion(f_cnn, f_tex, f_col)
logits = self.fusion_head(fused)
proj = F.normalize(self.proj_head(f_cnn), dim=1)
return logits, proj
def forward(self, x, tex=None, col=None):
"""Default forward β uses fusion if tex/col provided, else image-only."""
if tex is not None and col is not None:
logits, _ = self.forward_fusion(x, tex, col)
return logits
return self.forward_image(x)
def save_checkpoint(path, model, optimizer, epoch, best_val_acc, history):
torch.save({
"epoch": epoch,
"model_state": model.state_dict(),
"optimizer_state": optimizer.state_dict(),
"best_val_acc": best_val_acc,
"history": history,
}, path)
def load_checkpoint(path: str, device: torch.device):
ckpt = torch.load(path, map_location=device)
model = SpiceFusionNet()
model.load_state_dict(ckpt["model_state"])
model.to(device)
return model, ckpt.get("epoch", 0), ckpt.get("best_val_acc", 0.0), ckpt.get("history", {})
|