forked from EIFY/mup-vit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_vit.py
440 lines (369 loc) · 16.1 KB
/
simple_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import math
from collections import OrderedDict
from functools import partial
from typing import Callable, Optional
import torch
import torch.nn as nn
from torchvision.models.vision_transformer import MLPBlock
import torch.nn.functional as F
from typing import List
# # Taken from https://github.com/lucidrains/vit-pytorch, likely ported from https://github.com/google-research/big_vision/
# def posemb_sincos_2d(h, w, dim, temperature: int = 10000, dtype = torch.float32):
# y, x = torch.meshgrid(torch.arange(h), torch.arange(w), indexing="ij")
# assert (dim % 4) == 0, "feature dimension must be multiple of 4 for sincos emb"
# omega = torch.arange(dim // 4) / (dim // 4 - 1)
# omega = 1.0 / (temperature ** omega)
# y = y.flatten()[:, None] * omega[None, :]
# x = x.flatten()[:, None] * omega[None, :]
# pe = torch.cat((x.sin(), x.cos(), y.sin(), y.cos()), dim=1)
# return pe.type(dtype)
##NEW CODE
class PatchExtractor(nn.Module):
def __init__(self,image_size = 32,patch_size = 16,in_channels=3,embed_dim=384):
super(PatchExtractor, self).__init__()
self.projection = nn.Conv2d(in_channels=3, out_channels=embed_dim, kernel_size=16, stride=1, padding=0, bias=False)
self.embed_dim = embed_dim
def forward(self, x):
# x shape: (batch_size, 3, 32, 32)
# Extract 4 patches of 3x16x16
patches = x.unfold(2, 16, 16).unfold(3, 16, 16)
patches = patches.permute(0, 2, 3, 1, 4, 5).contiguous()
patches = patches.view(x.size(0), 4, 3, 16, 16)
# Downsample the original image to 3x16x16 using average pooling
downsampled = F.avg_pool2d(x, kernel_size=2, stride=2)
# Combine the 4 patches and the downsampled image
combined = torch.cat([patches, downsampled.unsqueeze(1)], dim=1)
#print(combined.shape)
# Reshape to (batch_size, 5, 3,16,16)
output = combined.view(x.size(0), 5, 3, 16, 16)
#print(output.shape)
# x shape: (batch_size, 5, 3, 16, 16)
batch_size, num_patches, channels, height, width = output.size()
# Reshape to (batch_size * num_patches, channels, height, width)
output = output.view(batch_size * num_patches, channels, height, width)
#print(output.shape)
# Apply convolutional layer
output = self.projection(output)
#print(output.shape)
# Reshape back to (batch_size, num_patches,embed_dim, 1, 1)
output = output.view(batch_size, num_patches,self.embed_dim, 1, 1)
#print(output.shape)
# Squeeze to get (batch_size, num_patches,embed_dim)
output = output.squeeze(-1).squeeze(-1)
#print(output.shape)
return output
class EncoderBlock(nn.Module):
"""Transformer encoder block."""
def __init__(
self,
num_heads: int,
hidden_dim: int,
mlp_dim: int,
dropout: float,
attention_dropout: float,
norm_layer: Callable[..., torch.nn.Module] = partial(nn.LayerNorm, eps=1e-6),
):
super().__init__()
self.num_heads = num_heads
# Attention block
self.ln_1 = norm_layer(hidden_dim)
self.self_attention = nn.MultiheadAttention(hidden_dim, num_heads, dropout=attention_dropout, batch_first=True)
self.dropout = nn.Dropout(dropout)
# MLP block
self.ln_2 = norm_layer(hidden_dim)
self.mlp = MLPBlock(hidden_dim, mlp_dim, dropout)
# Fix init discrepancy between nn.MultiheadAttention and that of big_vision
bound = math.sqrt(3 / hidden_dim)
nn.init.uniform_(self.self_attention.in_proj_weight, -bound, bound)
nn.init.uniform_(self.self_attention.out_proj.weight, -bound, bound)
def forward(self, input: torch.Tensor):
torch._assert(input.dim() == 3, f"Expected (batch_size, seq_length, hidden_dim) got {input.shape}")
x = self.ln_1(input)
x, _ = self.self_attention(x, x, x, need_weights=False)
x = self.dropout(x)
x = x + input
y = self.ln_2(x)
y = self.mlp(y)
return x + y
class Encoder(nn.Module):
"""Transformer Model Encoder for sequence to sequence translation."""
def __init__(
self,
seq_length: int,
num_layers: int,
num_heads: int,
hidden_dim: int,
mlp_dim: int,
dropout: float,
attention_dropout: float,
norm_layer: Callable[..., torch.nn.Module] = partial(nn.LayerNorm, eps=1e-6),
):
super().__init__()
self.dropout = nn.Dropout(dropout)
layers: OrderedDict[str, nn.Module] = OrderedDict()
for i in range(num_layers):
layers[f"encoder_layer_{i}"] = EncoderBlock(
num_heads,
hidden_dim,
mlp_dim,
dropout,
attention_dropout,
norm_layer,
)
self.layers = nn.Sequential(layers)
self.ln = norm_layer(hidden_dim)
def forward(self, input: torch.Tensor):
torch._assert(input.dim() == 3, f"Expected (batch_size, seq_length, hidden_dim) got {input.shape}")
return self.ln(self.layers(self.dropout(input)))
class SimpleVisionTransformer(nn.Module):
"""Vision Transformer modified per https://arxiv.org/abs/2205.01580."""
def __init__(
self,
image_size: int,
patch_size: int,
num_layers: int,
num_heads: int,
hidden_dim: int,
mlp_dim: int,
dropout: float = 0.0,
attention_dropout: float = 0.0,
num_classes: int = 10,
seq_length: int = 5, # 4 + 1
representation_size: Optional[int] = None,
norm_layer: Callable[..., torch.nn.Module] = partial(nn.LayerNorm, eps=1e-6),
):
super().__init__()
torch._assert(image_size % patch_size == 0, "Input shape indivisible by patch size!")
self.image_size = image_size
self.patch_size = patch_size
self.hidden_dim = hidden_dim
self.mlp_dim = mlp_dim
self.attention_dropout = attention_dropout
self.dropout = dropout
self.num_classes = num_classes
self.representation_size = representation_size
self.norm_layer = norm_layer
# Add PatchExtractor
self.patch_extractor = PatchExtractor(image_size=image_size, patch_size=patch_size, in_channels=3, embed_dim=hidden_dim)
# Update seq_length to match the number of tokens from TokenGenerator
self.seq_length = seq_length
self.encoder = Encoder(
seq_length,
num_layers,
num_heads,
hidden_dim,
mlp_dim,
dropout,
attention_dropout,
norm_layer,
)
self.seq_length = seq_length
heads_layers: OrderedDict[str, nn.Module] = OrderedDict()
if representation_size is None:
heads_layers["head"] = nn.Linear(hidden_dim, num_classes)
else:
heads_layers["pre_logits"] = nn.Linear(hidden_dim, representation_size)
heads_layers["act"] = nn.Tanh()
heads_layers["head"] = nn.Linear(representation_size, num_classes)
self.heads = nn.Sequential(heads_layers)
# Initialize weights for the heads
if hasattr(self.heads, "pre_logits") and isinstance(self.heads.pre_logits, nn.Linear):
fan_in = self.heads.pre_logits.in_features
nn.init.trunc_normal_(self.heads.pre_logits.weight, std=math.sqrt(1 / fan_in))
nn.init.zeros_(self.heads.pre_logits.bias)
if isinstance(self.heads.head, nn.Linear):
nn.init.zeros_(self.heads.head.weight)
nn.init.zeros_(self.heads.head.bias)
def forward(self, x: torch.Tensor):
# Use TokenGenerator to get tokens
#print(x.shape)
x = self.patch_extractor(x) # Shape: [B,5, hidden_dim]
#print(x.shape)
x = self.encoder(x)
#print(x.shape)
x = x.mean(dim = 1)
#print(x.shape)
x = self.heads(x)
#print(x.shape)
return x
## OLD CODE
class EncoderBlock(nn.Module):
"""Transformer encoder block."""
def __init__(
self,
num_heads: int,
hidden_dim: int,
mlp_dim: int,
dropout: float,
attention_dropout: float,
norm_layer: Callable[..., torch.nn.Module] = partial(nn.LayerNorm, eps=1e-6),
):
super().__init__()
self.num_heads = num_heads
# Attention block
self.ln_1 = norm_layer(hidden_dim)
self.self_attention = nn.MultiheadAttention(hidden_dim, num_heads, dropout=attention_dropout, batch_first=True)
self.dropout = nn.Dropout(dropout)
# MLP block
self.ln_2 = norm_layer(hidden_dim)
self.mlp = MLPBlock(hidden_dim, mlp_dim, dropout)
# Fix init discrepancy between nn.MultiheadAttention and that of big_vision
bound = math.sqrt(3 / hidden_dim)
nn.init.uniform_(self.self_attention.in_proj_weight, -bound, bound)
nn.init.uniform_(self.self_attention.out_proj.weight, -bound, bound)
def forward(self, input: torch.Tensor):
torch._assert(input.dim() == 3, f"Expected (batch_size, seq_length, hidden_dim) got {input.shape}")
x = self.ln_1(input)
x, _ = self.self_attention(x, x, x, need_weights=False)
x = self.dropout(x)
x = x + input
y = self.ln_2(x)
y = self.mlp(y)
return x + y
class Encoder(nn.Module):
"""Transformer Model Encoder for sequence to sequence translation."""
def __init__(
self,
seq_length: int,
num_layers: int,
num_heads: int,
hidden_dim: int,
mlp_dim: int,
dropout: float,
attention_dropout: float,
norm_layer: Callable[..., torch.nn.Module] = partial(nn.LayerNorm, eps=1e-6),
):
super().__init__()
self.dropout = nn.Dropout(dropout)
layers: OrderedDict[str, nn.Module] = OrderedDict()
for i in range(num_layers):
layers[f"encoder_layer_{i}"] = EncoderBlock(
num_heads,
hidden_dim,
mlp_dim,
dropout,
attention_dropout,
norm_layer,
)
self.layers = nn.Sequential(layers)
self.ln = norm_layer(hidden_dim)
def forward(self, input: torch.Tensor):
torch._assert(input.dim() == 3, f"Expected (batch_size, seq_length, hidden_dim) got {input.shape}")
return self.ln(self.layers(self.dropout(input)))
class DilatedConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, dilation):
super().__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=dilation, dilation=dilation)
self.bn = nn.BatchNorm2d(out_channels)
self.activation = nn.ReLU()
def forward(self, x):
return self.activation(self.bn(self.conv(x)))
class TokenGenerator(nn.Module):
def __init__(self, image_size=256, patch_size=16, in_channels=3, embed_dim=256):
super().__init__()
self.patch_embed = nn.Conv2d(in_channels, embed_dim, kernel_size=patch_size, stride=patch_size)
self.stage1 = DilatedConvBlock(embed_dim, embed_dim, dilation=1)
self.stage2 = DilatedConvBlock(embed_dim, embed_dim, dilation=4)
self._init_weights()
def _init_weights(self):
# Initialize patch_embed (equivalent to conv_proj in the original code)
fan_in = self.patch_embed.in_channels * self.patch_embed.kernel_size[0] * self.patch_embed.kernel_size[1]
std = math.sqrt(1 / fan_in) / .87962566103423978
nn.init.trunc_normal_(self.patch_embed.weight, std=std, a=-2 * std, b=2 * std)
if self.patch_embed.bias is not None:
nn.init.zeros_(self.patch_embed.bias)
# Initialize dilated convolutions (similar to conv_last in the original code)
for m in [self.stage1.conv, self.stage2.conv]:
nn.init.normal_(m.weight, mean=0.0, std=math.sqrt(2.0 / m.out_channels))
if m.bias is not None:
nn.init.zeros_(m.bias)
# Initialize batch norm layers
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x):
# Initial patching: 16x16 patches of 16x16 resolution
print(x.shape)
x = self.patch_embed(x) # Shape: [B, embed_dim, 16, 16]
tokens_stage1 = x.flatten(2).transpose(1, 2) # Shape: [B, 256, embed_dim]
print(tokens_stage1.shape)
# Stage 1: 4x4 patches with 64x64 receptive field
x = self.stage1(x)
x = F.avg_pool2d(x, kernel_size=4) # Shape: [B, embed_dim, 4, 4]
tokens_stage2 = x.flatten(2).transpose(1, 2) # Shape: [B, 16, embed_dim]
print(tokens_stage2.shape)
# Stage 2: 1x1 patch with global receptive field
x = self.stage2(x)
x = F.adaptive_avg_pool2d(x, 1) # Shape: [B, embed_dim, 1, 1]
tokens_stage3 = x.flatten(2).transpose(1, 2) # Shape: [B, 1, embed_dim]
print(tokens_stage3.shape)
# Combine tokens from all stages
tokens = torch.cat([tokens_stage1, tokens_stage2, tokens_stage3], dim=1) # Shape: [B, 273, embed_dim]
return tokens
class SimpleVisionTransformer(nn.Module):
"""Vision Transformer modified per https://arxiv.org/abs/2205.01580."""
def __init__(
self,
image_size: int,
patch_size: int,
num_layers: int,
num_heads: int,
hidden_dim: int,
mlp_dim: int,
dropout: float = 0.0,
attention_dropout: float = 0.0,
num_classes: int = 1000,
seq_length: int = 273, # 256 + 16 + 1
representation_size: Optional[int] = None,
norm_layer: Callable[..., torch.nn.Module] = partial(nn.LayerNorm, eps=1e-6),
):
super().__init__()
torch._assert(image_size % patch_size == 0, "Input shape indivisible by patch size!")
self.image_size = image_size
self.patch_size = patch_size
self.hidden_dim = hidden_dim
self.mlp_dim = mlp_dim
self.attention_dropout = attention_dropout
self.dropout = dropout
self.num_classes = num_classes
self.representation_size = representation_size
self.norm_layer = norm_layer
# Add TokenGenerator
self.token_generator = TokenGenerator(image_size=image_size, patch_size=patch_size, in_channels=3, embed_dim=hidden_dim)
# Update seq_length to match the number of tokens from TokenGenerator
self.seq_length = seq_length
self.encoder = Encoder(
seq_length,
num_layers,
num_heads,
hidden_dim,
mlp_dim,
dropout,
attention_dropout,
norm_layer,
)
heads_layers: OrderedDict[str, nn.Module] = OrderedDict()
if representation_size is None:
heads_layers["head"] = nn.Linear(hidden_dim, num_classes)
else:
heads_layers["pre_logits"] = nn.Linear(hidden_dim, representation_size)
heads_layers["act"] = nn.Tanh()
heads_layers["head"] = nn.Linear(representation_size, num_classes)
self.heads = nn.Sequential(heads_layers)
# Initialize weights for the heads
if hasattr(self.heads, "pre_logits") and isinstance(self.heads.pre_logits, nn.Linear):
fan_in = self.heads.pre_logits.in_features
nn.init.trunc_normal_(self.heads.pre_logits.weight, std=math.sqrt(1 / fan_in))
nn.init.zeros_(self.heads.pre_logits.bias)
if isinstance(self.heads.head, nn.Linear):
nn.init.zeros_(self.heads.head.weight)
nn.init.zeros_(self.heads.head.bias)
def forward(self, x: torch.Tensor):
# Use TokenGenerator to get tokens
x = self.token_generator(x) # Shape: [B, 273, hidden_dim]
# No need for position embeddings as they're implicitly handled by TokenGenerator
x = self.encoder(x)
x = x.mean(dim=1)
x = self.heads(x)
return x