nijohnson commited on
Commit
7ea1190
·
verified ·
1 Parent(s): f8b8975

Upload 2 files

Browse files
README.md ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - cross-attention
5
+ - efficientformer
6
+ - linear
7
+ - lion
8
+ - onecycle
9
+ - orthogonal
10
+ - relu
11
+ - retrieval
12
+ - scalenorm
13
+ - xlarge
14
+ ---
15
+
16
+ # model_395445763_efficientformer_xlarge.py
17
+
18
+ ## Model Overview
19
+
20
+ A **xlarge**-scale implementation of the **efficientformer** architecture, built for **retrieval** tasks.
21
+
22
+ ## Architecture
23
+
24
+ - **Architecture**: efficientformer
25
+ - **Scale**: xlarge
26
+ - **Attention**: linear
27
+ - **Fusion strategy**: cross attention
28
+ - **Task head**: retrieval
29
+ - **Activation**: relu
30
+ - **Normalization**: scalenorm
31
+ - **Initialization**: orthogonal
32
+
33
+ ## Training
34
+
35
+ - **Optimizer**: lion
36
+ - **LR scheduler**: onecycle
37
+
38
+ ## Files
39
+
40
+ - `model_395445763_efficientformer_xlarge.py` — main artifact of this repository
41
+
42
+ ## License
43
+
44
+ See the license field above.
model_395445763_efficientformer_xlarge.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch, torch.nn as nn, math
2
+
3
+ class M(nn.Module):
4
+ def __init__(self, d=512, L=10, H=8, nc=10):
5
+ super().__init__()
6
+ self.pe = nn.Conv2d(3, d, 16, 16)
7
+ self.cls = nn.Parameter(torch.randn(1,1,d)*.02)
8
+ self.pos = nn.Parameter(torch.randn(1,197,d)*.02)
9
+ self.blks = nn.ModuleList([nn.TransformerEncoderLayer(d, H, d*4, .1, activation='gelu', batch_first=True, norm_first=True) for _ in range(L)])
10
+ self.ln = nn.LayerNorm(d)
11
+ self.te = nn.Embedding(30522, d, padding_idx=0)
12
+ self.tpos = nn.Parameter(torch.randn(1,128,d)*.02)
13
+ self.tblks = nn.ModuleList([nn.TransformerEncoderLayer(d, H, d*4, .1, activation='gelu', batch_first=True, norm_first=True) for _ in range(L)])
14
+ self.tln = nn.LayerNorm(d)
15
+ self.fuse = nn.ModuleList([nn.TransformerEncoderLayer(d, H, d*4, .1, activation='gelu', batch_first=True, norm_first=True) for _ in range(2)])
16
+ self.fln = nn.LayerNorm(d)
17
+ self.head = nn.Sequential(nn.Linear(d,d), nn.ReLU(), nn.Dropout(.1), nn.Linear(d,nc))
18
+ self._init()
19
+
20
+ def _init(self):
21
+ for m in self.modules():
22
+ if isinstance(m, nn.Linear):
23
+ nn.init.orthogonal_(m.weight)
24
+ if m.bias is not None: nn.init.zeros_(m.bias)
25
+
26
+ def enc_img(self, x):
27
+ x = self.pe(x).flatten(2).transpose(1,2)
28
+ x = torch.cat([self.cls.expand(x.size(0),-1,-1), x], 1)
29
+ x = x + self.pos
30
+ for b in self.blks: x = b(x)
31
+ return self.ln(x)
32
+
33
+ def enc_txt(self, ids, mask=None):
34
+ x = self.te(ids) + self.tpos[:, :ids.size(1)]
35
+ m = (mask == 0) if mask is not None else None
36
+ for b in self.tblks: x = b(x, src_key_padding_mask=m)
37
+ return self.tln(x)
38
+
39
+ def forward(self, img, ids, mask=None, lbl=None):
40
+ fi = self.enc_img(img)
41
+ ft = self.enc_txt(ids, mask)
42
+ x = ft
43
+ for f in self.fuse: x = f(x)
44
+ x = self.fln(x[:, 0])
45
+ logits = self.head(x)
46
+ loss = nn.functional.cross_entropy(logits, lbl) if lbl is not None else None
47
+ return {'logits': logits, 'loss': loss}
48
+
49
+ if __name__ == '__main__':
50
+ m = M()
51
+ print(f'Params: {sum(p.numel() for p in m.parameters()):,}')
52
+ o = m(torch.randn(2,3,224,224), torch.randint(0,30522,(2,128)), torch.ones(2,128), torch.tensor([0,1]))
53
+ print(o['logits'].shape, o['loss'].item())