Update glam_efficientnet_model.py
Browse files- glam_efficientnet_model.py +95 -106
glam_efficientnet_model.py
CHANGED
@@ -1,106 +1,95 @@
|
|
1 |
-
import torch
|
2 |
-
import torch.nn as nn
|
3 |
-
import torch.nn.functional as F
|
4 |
-
from
|
5 |
-
from typing import Optional, Union
|
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 |
-
# β
Final Pooling & Classifier
|
98 |
-
pooled = F.adaptive_avg_pool2d(F_out, (1, 1)).view(B, -1)
|
99 |
-
logits = self.classifier(self.dropout(pooled))
|
100 |
-
|
101 |
-
loss = None
|
102 |
-
if labels is not None:
|
103 |
-
loss = F.cross_entropy(logits, labels)
|
104 |
-
|
105 |
-
return {"loss": loss, "logits": logits}
|
106 |
-
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from torchvision import models
|
5 |
+
from typing import Optional, Union
|
6 |
+
|
7 |
+
from glam_module import GLAM
|
8 |
+
from swin_module import SwinWindowAttention
|
9 |
+
|
10 |
+
|
11 |
+
class GLAMEfficientNetConfig:
|
12 |
+
"""Hugging Face-style configuration for GLAM EfficientNet."""
|
13 |
+
def __init__(self,
|
14 |
+
num_classes: int = 3,
|
15 |
+
embed_dim: int = 512,
|
16 |
+
num_heads: int = 8,
|
17 |
+
window_size: int = 7,
|
18 |
+
reduction_ratio: int = 8,
|
19 |
+
dropout: float = 0.5,
|
20 |
+
**kwargs):
|
21 |
+
super().__init__(**kwargs)
|
22 |
+
self.num_classes = num_classes
|
23 |
+
self.embed_dim = embed_dim
|
24 |
+
self.num_heads = num_heads
|
25 |
+
self.window_size = window_size
|
26 |
+
self.reduction_ratio = reduction_ratio
|
27 |
+
self.dropout = dropout
|
28 |
+
|
29 |
+
|
30 |
+
class GLAMEfficientNetForClassification(nn.Module):
|
31 |
+
"""EfficientNet (torchvision) + GLAM + Swin Architecture for Classification."""
|
32 |
+
def __init__(self, config: GLAMEfficientNetConfig, glam_module_cls, swin_module_cls):
|
33 |
+
super().__init__()
|
34 |
+
|
35 |
+
# β
1) Torchvision EfficientNet Backbone
|
36 |
+
efficientnet = models.efficientnet_b0(pretrained=False) # No Hugging Face!
|
37 |
+
self.features = efficientnet.features
|
38 |
+
|
39 |
+
# β
1x1 conv for channel adjustment
|
40 |
+
self.conv1x1 = nn.Conv2d(1280, config.embed_dim, kernel_size=1)
|
41 |
+
|
42 |
+
# β
2) Swin Attention Block
|
43 |
+
self.swin_attn = swin_module_cls(
|
44 |
+
embed_dim=config.embed_dim,
|
45 |
+
window_size=config.window_size,
|
46 |
+
num_heads=config.num_heads,
|
47 |
+
dropout=config.dropout
|
48 |
+
)
|
49 |
+
self.pre_attn_norm = nn.LayerNorm(config.embed_dim)
|
50 |
+
self.post_attn_norm = nn.LayerNorm(config.embed_dim)
|
51 |
+
|
52 |
+
# β
3) GLAM Block
|
53 |
+
self.glam = glam_module_cls(in_channels=config.embed_dim, reduction_ratio=config.reduction_ratio)
|
54 |
+
|
55 |
+
# β
4) Self-Adaptive Gating
|
56 |
+
self.gate_fc = nn.Linear(config.embed_dim, 1)
|
57 |
+
|
58 |
+
# β
Final classification
|
59 |
+
self.dropout = nn.Dropout(config.dropout)
|
60 |
+
self.classifier = nn.Linear(config.embed_dim, config.num_classes)
|
61 |
+
|
62 |
+
def forward(self, pixel_values, labels=None, **kwargs):
|
63 |
+
"""Perform forward pass."""
|
64 |
+
# β
1) EfficientNet Backbone
|
65 |
+
feats = self.features(pixel_values) # [B, 1280, H', W']
|
66 |
+
feats = self.conv1x1(feats) # [B, embed_dim, H', W']
|
67 |
+
B, C, H, W = feats.shape
|
68 |
+
|
69 |
+
# β
2) Transformer Branch
|
70 |
+
x_perm = feats.permute(0, 2, 3, 1).contiguous() # [B, H', W', C]
|
71 |
+
x_norm = self.pre_attn_norm(x_perm).permute(0, 3, 1, 2).contiguous()
|
72 |
+
x_norm = self.dropout(x_norm)
|
73 |
+
|
74 |
+
T_out = self.swin_attn(x_norm) # [B, C, H', W']
|
75 |
+
T_out = self.post_attn_norm(T_out.permute(0, 2, 3, 1).contiguous())
|
76 |
+
T_out = T_out.permute(0, 3, 1, 2).contiguous()
|
77 |
+
|
78 |
+
# β
3) GLAM Branch
|
79 |
+
G_out = self.glam(feats)
|
80 |
+
|
81 |
+
# β
4) Self-Adaptive Gating
|
82 |
+
gap_feats = F.adaptive_avg_pool2d(feats, (1, 1)).view(B, C)
|
83 |
+
g = torch.sigmoid(self.gate_fc(gap_feats)).view(B, 1, 1, 1)
|
84 |
+
|
85 |
+
F_out = g * T_out + (1 - g) * G_out
|
86 |
+
|
87 |
+
# β
Final Pooling & Classifier
|
88 |
+
pooled = F.adaptive_avg_pool2d(F_out, (1, 1)).view(B, -1)
|
89 |
+
logits = self.classifier(self.dropout(pooled))
|
90 |
+
|
91 |
+
loss = None
|
92 |
+
if labels is not None:
|
93 |
+
loss = F.cross_entropy(logits, labels)
|
94 |
+
|
95 |
+
return {"loss": loss, "logits": logits}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|