MCPcopy Create free account
hub / github.com/bic-L/MaxFormer / Max_Former

Class Max_Former

event/max_former.py:13–97  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11__all__ = ['max_former']
12
13class Max_Former(nn.Module):
14 def __init__(self,
15 in_channels=2, num_classes=10,
16 embed_dims=[64, 128, 256], mlp_ratios=[4, 4, 4],
17 depths=[6, 8, 6], T = 4
18 ):
19 super().__init__()
20
21 self.num_classes = num_classes
22 self.depths = depths
23 self.T = T
24
25 patch_embed1 = Embed_Max_plus( in_channels=in_channels,
26 embed_dims=embed_dims // 2)
27
28 stage1 = nn.ModuleList(
29 [Block_DWC3(
30 dim=embed_dims // 2, mlp_ratio=mlp_ratios)
31 for j in range(1)]
32 )
33
34
35 patch_embed2 = Embed_Max( in_channels=embed_dims // 2,
36 embed_dims=embed_dims)
37
38 stage2 = nn.ModuleList([Block_SSA(
39 dim=embed_dims, mlp_ratio=mlp_ratios, num_heads = 16)
40 for j in range(1)])
41
42 setattr(self, f"patch_embed1", patch_embed1)
43 setattr(self, f"patch_embed2", patch_embed2)
44 setattr(self, f"stage1", stage1)
45 setattr(self, f"stage2", stage2)
46
47 self.head_lif = MultiStepLIFNode(tau=2.0, detach_reset=True)
48
49 # classification head
50 self.head = nn.Linear(embed_dims, num_classes) if num_classes > 0 else nn.Identity()
51 self.apply(self._init_weights)
52
53 @torch.jit.ignore
54 def _get_pos_embed(self, pos_embed, patch_embed, H, W):
55 if H * W == self.patch_embed1.num_patches:
56 return pos_embed
57 else:
58 return F.interpolate(
59 pos_embed.reshape(1, patch_embed.H, patch_embed.W, -1).permute(0, 3, 1, 2),
60 size=(H, W), mode="bilinear").reshape(1, -1, H * W).permute(0, 2, 1)
61
62 def _init_weights(self, m):
63 if isinstance(m, nn.Linear):
64 trunc_normal_(m.weight, std=.02)
65 if isinstance(m, nn.Linear) and m.bias is not None:
66 nn.init.constant_(m.bias, 0)
67 elif isinstance(m, nn.LayerNorm):
68 nn.init.constant_(m.bias, 0)
69 nn.init.constant_(m.weight, 1.0)
70

Callers 2

max_formerFunction · 0.70
max_former.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected