-
Notifications
You must be signed in to change notification settings - Fork 0
/
vit.py
140 lines (111 loc) · 4.04 KB
/
vit.py
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
from torch import nn
class PatchEmbedding(nn.Module):
def __init__(self, patch_size=16, embed_dim=768, in_channels=3):
super().__init__()
self.embedding = nn.Sequential(
nn.Conv2d(
in_channels=in_channels,
out_channels=embed_dim,
stride=patch_size,
kernel_size=patch_size,
),
nn.Flatten(start_dim=-2, end_dim=-1),
)
def forward(self, x):
return self.embedding(x).permute(0, 2, 1)
class MultiheadSelfAttentionBlock(nn.Module):
def __init__(self, embedding_dim=768, num_heads=12, attn_dropout=0.0):
super().__init__()
self.ln = nn.LayerNorm(normalized_shape=embedding_dim)
self.msa = nn.MultiheadAttention(
num_heads=num_heads,
embed_dim=embedding_dim,
dropout=attn_dropout,
batch_first=True,
)
def forward(self, x):
x_ln = self.ln(x)
x, _ = self.msa(query=x_ln, key=x_ln, value=x_ln, need_weights=False)
return x
class MLPBlock(nn.Module):
def __init__(self, embed_dim=768, dropout=0.1, mlp_size=3072):
super().__init__()
self.ln = nn.LayerNorm(normalized_shape=embed_dim)
self.layers = nn.Sequential(
nn.Linear(in_features=embed_dim, out_features=mlp_size),
nn.Dropout(p=dropout),
nn.GELU(),
nn.Linear(in_features=mlp_size, out_features=embed_dim),
nn.Dropout(p=dropout),
)
def forward(self, x):
return self.layers(self.ln(x))
class TransformerEncoderBlock(nn.Module):
def __init__(
self,
embedding_dim=768,
num_heads=12,
attn_dropout=0.0,
dropout=0.1,
mlp_size=3072,
):
super().__init__()
self.msa_block = MultiheadSelfAttentionBlock(
embedding_dim, num_heads, attn_dropout
)
self.mlp_block = MLPBlock(embedding_dim, dropout, mlp_size)
def forward(self, x):
x = self.msa_block(x) + x
x = self.mlp_block(x) + x
return x
class ViT(nn.Module):
def __init__(
self,
img_size: int = 224,
in_channels: int = 3,
patch_size: int = 16,
num_transformer_layers: int = 12,
embed_dim: int = 768,
mlp_size: int = 3072,
num_heads: int = 12,
attn_dropout: float = 0.0,
mlp_dropout: float = 0.1,
embed_dropout: float = 0.1,
num_classes: int = 1000,
):
super().__init__()
self.num_patches = (img_size * img_size) // (patch_size**2)
self.class_token = nn.Parameter(
data=torch.randn(1, 1, embed_dim), requires_grad=True
)
self.position_embedding = nn.Parameter(
data=torch.randn(1, self.num_patches + 1, embed_dim), requires_grad=True
)
self.embed_dropout = nn.Dropout(p=embed_dropout)
self.patch_embedding = PatchEmbedding(
in_channels=in_channels, patch_size=patch_size, embed_dim=embed_dim
)
self.transformer_encoder = nn.Sequential(
*[
TransformerEncoderBlock(
embedding_dim=embed_dim,
num_heads=num_heads,
mlp_size=mlp_size,
dropout=mlp_dropout,
)
for _ in range(num_transformer_layers)
]
)
self.classifier = nn.Sequential(
nn.LayerNorm(normalized_shape=embed_dim),
nn.Linear(in_features=embed_dim, out_features=num_classes),
)
def forward(self, x):
x = self.patch_embedding(x)
class_token = self.class_token.expand(x.shape[0], -1, -1)
x = torch.cat([class_token, x], dim=1)
x = self.position_embedding + x
x = self.embed_dropout(x)
x = self.transformer_encoder(x)
x = self.classifier(x[:, 0])
return x