Racso777 commited on
Commit
777c551
·
1 Parent(s): a25ab9b

Upload vit_model.py

Browse files
Files changed (1) hide show
  1. vit_model.py +422 -0
vit_model.py ADDED
@@ -0,0 +1,422 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ original code from rwightman:
3
+ https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/vision_transformer.py
4
+ """
5
+ from functools import partial
6
+ from collections import OrderedDict
7
+
8
+ import torch
9
+ import torch.nn as nn
10
+
11
+
12
+ def drop_path(x, drop_prob: float = 0., training: bool = False):
13
+ """
14
+ Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
15
+ This is the same as the DropConnect impl I created for EfficientNet, etc networks, however,
16
+ the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
17
+ See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for
18
+ changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use
19
+ 'survival rate' as the argument.
20
+ """
21
+ if drop_prob == 0. or not training:
22
+ return x
23
+ keep_prob = 1 - drop_prob
24
+ shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets
25
+ random_tensor = keep_prob + torch.rand(shape, dtype=x.dtype, device=x.device)
26
+ random_tensor.floor_() # binarize
27
+ output = x.div(keep_prob) * random_tensor
28
+ return output
29
+
30
+
31
+ class DropPath(nn.Module):
32
+ """
33
+ Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
34
+ """
35
+ def __init__(self, drop_prob=None):
36
+ super(DropPath, self).__init__()
37
+ self.drop_prob = drop_prob
38
+
39
+ def forward(self, x):
40
+ return drop_path(x, self.drop_prob, self.training)
41
+
42
+
43
+ class PatchEmbed(nn.Module):
44
+ """
45
+ 2D Image to Patch Embedding
46
+ """
47
+ def __init__(self, img_size=224, patch_size=16, in_c=3, embed_dim=768, norm_layer=None):
48
+ super().__init__()
49
+ img_size = (img_size, img_size)
50
+ patch_size = (patch_size, patch_size)
51
+ self.img_size = img_size
52
+ self.patch_size = patch_size
53
+ self.grid_size = (img_size[0] // patch_size[0], img_size[1] // patch_size[1])
54
+ self.num_patches = self.grid_size[0] * self.grid_size[1]
55
+
56
+ self.proj = nn.Conv2d(in_c, embed_dim, kernel_size=patch_size, stride=patch_size)
57
+ self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity()
58
+
59
+ def forward(self, x):
60
+ B, C, H, W = x.shape
61
+ assert H == self.img_size[0] and W == self.img_size[1], \
62
+ f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})."
63
+
64
+ # flatten: [B, C, H, W] -> [B, C, HW]
65
+ # transpose: [B, C, HW] -> [B, HW, C]
66
+ x = self.proj(x).flatten(2).transpose(1, 2)
67
+ x = self.norm(x)
68
+ return x
69
+
70
+
71
+ class Attention(nn.Module):
72
+ def __init__(self,
73
+ dim, # 输入token的dim
74
+ num_heads=8,
75
+ qkv_bias=False,
76
+ qk_scale=None,
77
+ attn_drop_ratio=0.,
78
+ proj_drop_ratio=0.):
79
+ super(Attention, self).__init__()
80
+ self.num_heads = num_heads
81
+ head_dim = dim // num_heads
82
+ self.scale = qk_scale or head_dim ** -0.5
83
+ self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
84
+ self.attn_drop = nn.Dropout(attn_drop_ratio)
85
+ self.proj = nn.Linear(dim, dim)
86
+ self.proj_drop = nn.Dropout(proj_drop_ratio)
87
+
88
+ def forward(self, x):
89
+ # [batch_size, num_patches + 1, total_embed_dim]
90
+ B, N, C = x.shape
91
+
92
+ # qkv(): -> [batch_size, num_patches + 1, 3 * total_embed_dim]
93
+ # reshape: -> [batch_size, num_patches + 1, 3, num_heads, embed_dim_per_head]
94
+ # permute: -> [3, batch_size, num_heads, num_patches + 1, embed_dim_per_head]
95
+ qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
96
+ # [batch_size, num_heads, num_patches + 1, embed_dim_per_head]
97
+ q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple)
98
+
99
+ # transpose: -> [batch_size, num_heads, embed_dim_per_head, num_patches + 1]
100
+ # @: multiply -> [batch_size, num_heads, num_patches + 1, num_patches + 1]
101
+ attn = (q @ k.transpose(-2, -1)) * self.scale
102
+ attn = attn.softmax(dim=-1)
103
+ attn = self.attn_drop(attn)
104
+
105
+ # @: multiply -> [batch_size, num_heads, num_patches + 1, embed_dim_per_head]
106
+ # transpose: -> [batch_size, num_patches + 1, num_heads, embed_dim_per_head]
107
+ # reshape: -> [batch_size, num_patches + 1, total_embed_dim]
108
+ x = (attn @ v).transpose(1, 2).reshape(B, N, C)
109
+ x = self.proj(x)
110
+ x = self.proj_drop(x)
111
+ return x
112
+
113
+
114
+ class Mlp(nn.Module):
115
+ """
116
+ MLP as used in Vision Transformer, MLP-Mixer and related networks
117
+ """
118
+ def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):
119
+ super().__init__()
120
+ out_features = out_features or in_features
121
+ hidden_features = hidden_features or in_features
122
+ self.fc1 = nn.Linear(in_features, hidden_features)
123
+ self.act = act_layer()
124
+ self.fc2 = nn.Linear(hidden_features, out_features)
125
+ self.drop = nn.Dropout(drop)
126
+
127
+ def forward(self, x):
128
+ x = self.fc1(x)
129
+ x = self.act(x)
130
+ x = self.drop(x)
131
+ x = self.fc2(x)
132
+ x = self.drop(x)
133
+ return x
134
+
135
+
136
+ class Block(nn.Module):
137
+ def __init__(self,
138
+ dim,
139
+ num_heads,
140
+ mlp_ratio=4.,
141
+ qkv_bias=False,
142
+ qk_scale=None,
143
+ drop_ratio=0.,
144
+ attn_drop_ratio=0.,
145
+ drop_path_ratio=0.,
146
+ act_layer=nn.GELU,
147
+ norm_layer=nn.LayerNorm):
148
+ super(Block, self).__init__()
149
+ self.norm1 = norm_layer(dim)
150
+ self.attn = Attention(dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale,
151
+ attn_drop_ratio=attn_drop_ratio, proj_drop_ratio=drop_ratio)
152
+ # NOTE: drop path for stochastic depth, we shall see if this is better than dropout here
153
+ self.drop_path = DropPath(drop_path_ratio) if drop_path_ratio > 0. else nn.Identity()
154
+ self.norm2 = norm_layer(dim)
155
+ mlp_hidden_dim = int(dim * mlp_ratio)
156
+ self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop_ratio)
157
+
158
+ def forward(self, x):
159
+ x = x + self.drop_path(self.attn(self.norm1(x)))
160
+ x = x + self.drop_path(self.mlp(self.norm2(x)))
161
+ return x
162
+
163
+
164
+ class VisionTransformer(nn.Module):
165
+ def __init__(self, img_size=224, patch_size=16, in_c=3, num_classes=1000,
166
+ embed_dim=768, depth=12, num_heads=12, mlp_ratio=4.0, qkv_bias=True,
167
+ qk_scale=None, representation_size=None, distilled=False, drop_ratio=0.,
168
+ attn_drop_ratio=0., drop_path_ratio=0., embed_layer=PatchEmbed, norm_layer=None,
169
+ act_layer=None):
170
+ """
171
+ Args:
172
+ img_size (int, tuple): input image size
173
+ patch_size (int, tuple): patch size
174
+ in_c (int): number of input channels
175
+ num_classes (int): number of classes for classification head
176
+ embed_dim (int): embedding dimension
177
+ depth (int): depth of transformer
178
+ num_heads (int): number of attention heads
179
+ mlp_ratio (int): ratio of mlp hidden dim to embedding dim
180
+ qkv_bias (bool): enable bias for qkv if True
181
+ qk_scale (float): override default qk scale of head_dim ** -0.5 if set
182
+ representation_size (Optional[int]): enable and set representation layer (pre-logits) to this value if set
183
+ distilled (bool): model includes a distillation token and head as in DeiT models
184
+ drop_ratio (float): dropout rate
185
+ attn_drop_ratio (float): attention dropout rate
186
+ drop_path_ratio (float): stochastic depth rate
187
+ embed_layer (nn.Module): patch embedding layer
188
+ norm_layer: (nn.Module): normalization layer
189
+ """
190
+ super(VisionTransformer, self).__init__()
191
+ self.num_classes = num_classes
192
+ self.num_features = self.embed_dim = embed_dim # num_features for consistency with other models
193
+ self.num_tokens = 2 if distilled else 1
194
+ norm_layer = norm_layer or partial(nn.LayerNorm, eps=1e-6)
195
+ act_layer = act_layer or nn.GELU
196
+
197
+ self.patch_embed = embed_layer(img_size=img_size, patch_size=patch_size, in_c=in_c, embed_dim=embed_dim)
198
+ num_patches = self.patch_embed.num_patches
199
+
200
+ self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
201
+ self.dist_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) if distilled else None
202
+ self.pos_embed = nn.Parameter(torch.zeros(1, num_patches + self.num_tokens, embed_dim))
203
+ self.pos_drop = nn.Dropout(p=drop_ratio)
204
+
205
+ dpr = [x.item() for x in torch.linspace(0, drop_path_ratio, depth)] # stochastic depth decay rule
206
+ self.blocks = nn.Sequential(*[
207
+ Block(dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,
208
+ drop_ratio=drop_ratio, attn_drop_ratio=attn_drop_ratio, drop_path_ratio=dpr[i],
209
+ norm_layer=norm_layer, act_layer=act_layer)
210
+ for i in range(depth)
211
+ ])
212
+ self.norm = norm_layer(embed_dim)
213
+
214
+ # Representation layer
215
+ if representation_size and not distilled:
216
+ self.has_logits = True
217
+ self.num_features = representation_size
218
+ self.pre_logits = nn.Sequential(OrderedDict([
219
+ ("fc", nn.Linear(embed_dim, representation_size)),
220
+ ("act", nn.Tanh())
221
+ ]))
222
+ else:
223
+ self.has_logits = False
224
+ self.pre_logits = nn.Identity()
225
+
226
+ # Classifier head(s)
227
+ self.head = nn.Linear(self.num_features, num_classes) if num_classes > 0 else nn.Identity()
228
+ self.head_dist = None
229
+ if distilled:
230
+ self.head_dist = nn.Linear(self.embed_dim, self.num_classes) if num_classes > 0 else nn.Identity()
231
+
232
+ # Weight init
233
+ nn.init.trunc_normal_(self.pos_embed, std=0.02)
234
+ if self.dist_token is not None:
235
+ nn.init.trunc_normal_(self.dist_token, std=0.02)
236
+
237
+ nn.init.trunc_normal_(self.cls_token, std=0.02)
238
+ self.apply(_init_vit_weights)
239
+
240
+ def forward_features(self, x):
241
+ # [B, C, H, W] -> [B, num_patches, embed_dim]
242
+ x = self.patch_embed(x) # [B, 196, 768]
243
+ # [1, 1, 768] -> [B, 1, 768]
244
+ cls_token = self.cls_token.expand(x.shape[0], -1, -1)
245
+ if self.dist_token is None:
246
+ x = torch.cat((cls_token, x), dim=1) # [B, 197, 768]
247
+ else:
248
+ x = torch.cat((cls_token, self.dist_token.expand(x.shape[0], -1, -1), x), dim=1)
249
+
250
+ x = self.pos_drop(x + self.pos_embed)
251
+ x = self.blocks(x)
252
+ x = self.norm(x)
253
+ if self.dist_token is None:
254
+ return self.pre_logits(x[:, 0])
255
+ else:
256
+ return x[:, 0], x[:, 1]
257
+
258
+ def forward(self, x):
259
+ x = self.forward_features(x)
260
+ if self.head_dist is not None:
261
+ x, x_dist = self.head(x[0]), self.head_dist(x[1])
262
+ if self.training and not torch.jit.is_scripting():
263
+ # during inference, return the average of both classifier predictions
264
+ return x, x_dist
265
+ else:
266
+ return (x + x_dist) / 2
267
+ else:
268
+ x = self.head(x)
269
+ return x
270
+
271
+
272
+ def _init_vit_weights(m):
273
+ """
274
+ ViT weight initialization
275
+ :param m: module
276
+ """
277
+ if isinstance(m, nn.Linear):
278
+ nn.init.trunc_normal_(m.weight, std=.01)
279
+ if m.bias is not None:
280
+ nn.init.zeros_(m.bias)
281
+ elif isinstance(m, nn.Conv2d):
282
+ nn.init.kaiming_normal_(m.weight, mode="fan_out")
283
+ if m.bias is not None:
284
+ nn.init.zeros_(m.bias)
285
+ elif isinstance(m, nn.LayerNorm):
286
+ nn.init.zeros_(m.bias)
287
+ nn.init.ones_(m.weight)
288
+
289
+
290
+ def vit_base_patch16_224(num_classes: int = 1000):
291
+ """
292
+ ViT-Base model (ViT-B/16) from original paper (https://arxiv.org/abs/2010.11929).
293
+ ImageNet-1k weights @ 224x224, source https://github.com/google-research/vision_transformer.
294
+ weights ported from official Google JAX impl:
295
+ 链接: https://pan.baidu.com/s/1zqb08naP0RPqqfSXfkB2EA 密码: eu9f
296
+ """
297
+ model = VisionTransformer(img_size=224,
298
+ patch_size=16,
299
+ embed_dim=768,
300
+ depth=12,
301
+ num_heads=12,
302
+ representation_size=None,
303
+ num_classes=num_classes)
304
+ return model
305
+
306
+
307
+ def vit_base_patch16_224_in21k(num_classes: int = 21843, has_logits: bool = True):
308
+ """
309
+ ViT-Base model (ViT-B/16) from original paper (https://arxiv.org/abs/2010.11929).
310
+ ImageNet-21k weights @ 224x224, source https://github.com/google-research/vision_transformer.
311
+ weights ported from official Google JAX impl:
312
+ https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-vitjx/jx_vit_base_patch16_224_in21k-e5005f0a.pth
313
+ """
314
+ model = VisionTransformer(img_size=224,
315
+ patch_size=16,
316
+ embed_dim=768,
317
+ depth=12,
318
+ num_heads=12,
319
+ representation_size=768 if has_logits else None,
320
+ num_classes=num_classes)
321
+ return model
322
+
323
+
324
+ def vit_base_patch32_224(num_classes: int = 1000):
325
+ """
326
+ ViT-Base model (ViT-B/32) from original paper (https://arxiv.org/abs/2010.11929).
327
+ ImageNet-1k weights @ 224x224, source https://github.com/google-research/vision_transformer.
328
+ weights ported from official Google JAX impl:
329
+ 链接: https://pan.baidu.com/s/1hCv0U8pQomwAtHBYc4hmZg 密码: s5hl
330
+ """
331
+ model = VisionTransformer(img_size=224,
332
+ patch_size=32,
333
+ embed_dim=768,
334
+ depth=12,
335
+ num_heads=12,
336
+ representation_size=None,
337
+ num_classes=num_classes)
338
+ return model
339
+
340
+
341
+ def vit_base_patch32_224_in21k(num_classes: int = 21843, has_logits: bool = True):
342
+ """
343
+ ViT-Base model (ViT-B/32) from original paper (https://arxiv.org/abs/2010.11929).
344
+ ImageNet-21k weights @ 224x224, source https://github.com/google-research/vision_transformer.
345
+ weights ported from official Google JAX impl:
346
+ https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-vitjx/jx_vit_base_patch32_224_in21k-8db57226.pth
347
+ """
348
+ model = VisionTransformer(img_size=224,
349
+ patch_size=32,
350
+ embed_dim=768,
351
+ depth=12,
352
+ num_heads=12,
353
+ representation_size=768 if has_logits else None,
354
+ num_classes=num_classes)
355
+ return model
356
+
357
+
358
+ def vit_large_patch16_224(num_classes: int = 1000):
359
+ """
360
+ ViT-Large model (ViT-L/16) from original paper (https://arxiv.org/abs/2010.11929).
361
+ ImageNet-1k weights @ 224x224, source https://github.com/google-research/vision_transformer.
362
+ weights ported from official Google JAX impl:
363
+ 链接: https://pan.baidu.com/s/1cxBgZJJ6qUWPSBNcE4TdRQ 密码: qqt8
364
+ """
365
+ model = VisionTransformer(img_size=224,
366
+ patch_size=16,
367
+ embed_dim=1024,
368
+ depth=24,
369
+ num_heads=16,
370
+ representation_size=None,
371
+ num_classes=num_classes)
372
+ return model
373
+
374
+
375
+ def vit_large_patch16_224_in21k(num_classes: int = 21843, has_logits: bool = True):
376
+ """
377
+ ViT-Large model (ViT-L/16) from original paper (https://arxiv.org/abs/2010.11929).
378
+ ImageNet-21k weights @ 224x224, source https://github.com/google-research/vision_transformer.
379
+ weights ported from official Google JAX impl:
380
+ https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-vitjx/jx_vit_large_patch16_224_in21k-606da67d.pth
381
+ """
382
+ model = VisionTransformer(img_size=224,
383
+ patch_size=16,
384
+ embed_dim=1024,
385
+ depth=24,
386
+ num_heads=16,
387
+ representation_size=1024 if has_logits else None,
388
+ num_classes=num_classes)
389
+ return model
390
+
391
+
392
+ def vit_large_patch32_224_in21k(num_classes: int = 21843, has_logits: bool = True):
393
+ """
394
+ ViT-Large model (ViT-L/32) from original paper (https://arxiv.org/abs/2010.11929).
395
+ ImageNet-21k weights @ 224x224, source https://github.com/google-research/vision_transformer.
396
+ weights ported from official Google JAX impl:
397
+ https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-vitjx/jx_vit_large_patch32_224_in21k-9046d2e7.pth
398
+ """
399
+ model = VisionTransformer(img_size=224,
400
+ patch_size=32,
401
+ embed_dim=1024,
402
+ depth=24,
403
+ num_heads=16,
404
+ representation_size=1024 if has_logits else None,
405
+ num_classes=num_classes)
406
+ return model
407
+
408
+
409
+ def vit_huge_patch14_224_in21k(num_classes: int = 21843, has_logits: bool = True):
410
+ """
411
+ ViT-Huge model (ViT-H/14) from original paper (https://arxiv.org/abs/2010.11929).
412
+ ImageNet-21k weights @ 224x224, source https://github.com/google-research/vision_transformer.
413
+ NOTE: converted weights not currently available, too large for github release hosting.
414
+ """
415
+ model = VisionTransformer(img_size=224,
416
+ patch_size=14,
417
+ embed_dim=1280,
418
+ depth=32,
419
+ num_heads=16,
420
+ representation_size=1280 if has_logits else None,
421
+ num_classes=num_classes)
422
+ return model