florentgbelidji HF staff commited on
Commit
ba86388
·
1 Parent(s): c10e66b

Delete vit.py

Browse files
Files changed (1) hide show
  1. vit.py +0 -305
vit.py DELETED
@@ -1,305 +0,0 @@
1
- '''
2
- * Copyright (c) 2022, salesforce.com, inc.
3
- * All rights reserved.
4
- * SPDX-License-Identifier: BSD-3-Clause
5
- * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6
- * By Junnan Li
7
- * Based on timm code base
8
- * https://github.com/rwightman/pytorch-image-models/tree/master/timm
9
- '''
10
-
11
- import torch
12
- import torch.nn as nn
13
- import torch.nn.functional as F
14
- from functools import partial
15
-
16
- from timm.models.vision_transformer import _cfg, PatchEmbed
17
- from timm.models.registry import register_model
18
- from timm.models.layers import trunc_normal_, DropPath
19
- from timm.models.helpers import named_apply, adapt_input_conv
20
-
21
- from fairscale.nn.checkpoint.checkpoint_activations import checkpoint_wrapper
22
-
23
- class Mlp(nn.Module):
24
- """ MLP as used in Vision Transformer, MLP-Mixer and related networks
25
- """
26
- def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):
27
- super().__init__()
28
- out_features = out_features or in_features
29
- hidden_features = hidden_features or in_features
30
- self.fc1 = nn.Linear(in_features, hidden_features)
31
- self.act = act_layer()
32
- self.fc2 = nn.Linear(hidden_features, out_features)
33
- self.drop = nn.Dropout(drop)
34
-
35
- def forward(self, x):
36
- x = self.fc1(x)
37
- x = self.act(x)
38
- x = self.drop(x)
39
- x = self.fc2(x)
40
- x = self.drop(x)
41
- return x
42
-
43
-
44
- class Attention(nn.Module):
45
- def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.):
46
- super().__init__()
47
- self.num_heads = num_heads
48
- head_dim = dim // num_heads
49
- # NOTE scale factor was wrong in my original version, can set manually to be compat with prev weights
50
- self.scale = qk_scale or head_dim ** -0.5
51
- self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
52
- self.attn_drop = nn.Dropout(attn_drop)
53
- self.proj = nn.Linear(dim, dim)
54
- self.proj_drop = nn.Dropout(proj_drop)
55
- self.attn_gradients = None
56
- self.attention_map = None
57
-
58
- def save_attn_gradients(self, attn_gradients):
59
- self.attn_gradients = attn_gradients
60
-
61
- def get_attn_gradients(self):
62
- return self.attn_gradients
63
-
64
- def save_attention_map(self, attention_map):
65
- self.attention_map = attention_map
66
-
67
- def get_attention_map(self):
68
- return self.attention_map
69
-
70
- def forward(self, x, register_hook=False):
71
- B, N, C = x.shape
72
- qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
73
- q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple)
74
-
75
- attn = (q @ k.transpose(-2, -1)) * self.scale
76
- attn = attn.softmax(dim=-1)
77
- attn = self.attn_drop(attn)
78
-
79
- if register_hook:
80
- self.save_attention_map(attn)
81
- attn.register_hook(self.save_attn_gradients)
82
-
83
- x = (attn @ v).transpose(1, 2).reshape(B, N, C)
84
- x = self.proj(x)
85
- x = self.proj_drop(x)
86
- return x
87
-
88
-
89
- class Block(nn.Module):
90
-
91
- def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,
92
- drop_path=0., act_layer=nn.GELU, norm_layer=nn.LayerNorm, use_grad_checkpointing=False):
93
- super().__init__()
94
- self.norm1 = norm_layer(dim)
95
- self.attn = Attention(
96
- dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)
97
- # NOTE: drop path for stochastic depth, we shall see if this is better than dropout here
98
- self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
99
- self.norm2 = norm_layer(dim)
100
- mlp_hidden_dim = int(dim * mlp_ratio)
101
- self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)
102
-
103
- if use_grad_checkpointing:
104
- self.attn = checkpoint_wrapper(self.attn)
105
- self.mlp = checkpoint_wrapper(self.mlp)
106
-
107
- def forward(self, x, register_hook=False):
108
- x = x + self.drop_path(self.attn(self.norm1(x), register_hook=register_hook))
109
- x = x + self.drop_path(self.mlp(self.norm2(x)))
110
- return x
111
-
112
-
113
- class VisionTransformer(nn.Module):
114
- """ Vision Transformer
115
- A PyTorch impl of : `An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale` -
116
- https://arxiv.org/abs/2010.11929
117
- """
118
- def __init__(self, img_size=224, patch_size=16, in_chans=3, num_classes=1000, embed_dim=768, depth=12,
119
- num_heads=12, mlp_ratio=4., qkv_bias=True, qk_scale=None, representation_size=None,
120
- drop_rate=0., attn_drop_rate=0., drop_path_rate=0., norm_layer=None,
121
- use_grad_checkpointing=False, ckpt_layer=0):
122
- """
123
- Args:
124
- img_size (int, tuple): input image size
125
- patch_size (int, tuple): patch size
126
- in_chans (int): number of input channels
127
- num_classes (int): number of classes for classification head
128
- embed_dim (int): embedding dimension
129
- depth (int): depth of transformer
130
- num_heads (int): number of attention heads
131
- mlp_ratio (int): ratio of mlp hidden dim to embedding dim
132
- qkv_bias (bool): enable bias for qkv if True
133
- qk_scale (float): override default qk scale of head_dim ** -0.5 if set
134
- representation_size (Optional[int]): enable and set representation layer (pre-logits) to this value if set
135
- drop_rate (float): dropout rate
136
- attn_drop_rate (float): attention dropout rate
137
- drop_path_rate (float): stochastic depth rate
138
- norm_layer: (nn.Module): normalization layer
139
- """
140
- super().__init__()
141
- self.num_features = self.embed_dim = embed_dim # num_features for consistency with other models
142
- norm_layer = norm_layer or partial(nn.LayerNorm, eps=1e-6)
143
-
144
- self.patch_embed = PatchEmbed(
145
- img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim)
146
-
147
- num_patches = self.patch_embed.num_patches
148
-
149
- self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
150
- self.pos_embed = nn.Parameter(torch.zeros(1, num_patches + 1, embed_dim))
151
- self.pos_drop = nn.Dropout(p=drop_rate)
152
-
153
- dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule
154
- self.blocks = nn.ModuleList([
155
- Block(
156
- dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,
157
- drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[i], norm_layer=norm_layer,
158
- use_grad_checkpointing=(use_grad_checkpointing and i>=depth-ckpt_layer)
159
- )
160
- for i in range(depth)])
161
- self.norm = norm_layer(embed_dim)
162
-
163
- trunc_normal_(self.pos_embed, std=.02)
164
- trunc_normal_(self.cls_token, std=.02)
165
- self.apply(self._init_weights)
166
-
167
- def _init_weights(self, m):
168
- if isinstance(m, nn.Linear):
169
- trunc_normal_(m.weight, std=.02)
170
- if isinstance(m, nn.Linear) and m.bias is not None:
171
- nn.init.constant_(m.bias, 0)
172
- elif isinstance(m, nn.LayerNorm):
173
- nn.init.constant_(m.bias, 0)
174
- nn.init.constant_(m.weight, 1.0)
175
-
176
- @torch.jit.ignore
177
- def no_weight_decay(self):
178
- return {'pos_embed', 'cls_token'}
179
-
180
- def forward(self, x, register_blk=-1):
181
- B = x.shape[0]
182
- x = self.patch_embed(x)
183
-
184
- cls_tokens = self.cls_token.expand(B, -1, -1) # stole cls_tokens impl from Phil Wang, thanks
185
- x = torch.cat((cls_tokens, x), dim=1)
186
-
187
- x = x + self.pos_embed[:,:x.size(1),:]
188
- x = self.pos_drop(x)
189
-
190
- for i,blk in enumerate(self.blocks):
191
- x = blk(x, register_blk==i)
192
- x = self.norm(x)
193
-
194
- return x
195
-
196
- @torch.jit.ignore()
197
- def load_pretrained(self, checkpoint_path, prefix=''):
198
- _load_weights(self, checkpoint_path, prefix)
199
-
200
-
201
- @torch.no_grad()
202
- def _load_weights(model: VisionTransformer, checkpoint_path: str, prefix: str = ''):
203
- """ Load weights from .npz checkpoints for official Google Brain Flax implementation
204
- """
205
- import numpy as np
206
-
207
- def _n2p(w, t=True):
208
- if w.ndim == 4 and w.shape[0] == w.shape[1] == w.shape[2] == 1:
209
- w = w.flatten()
210
- if t:
211
- if w.ndim == 4:
212
- w = w.transpose([3, 2, 0, 1])
213
- elif w.ndim == 3:
214
- w = w.transpose([2, 0, 1])
215
- elif w.ndim == 2:
216
- w = w.transpose([1, 0])
217
- return torch.from_numpy(w)
218
-
219
- w = np.load(checkpoint_path)
220
- if not prefix and 'opt/target/embedding/kernel' in w:
221
- prefix = 'opt/target/'
222
-
223
- if hasattr(model.patch_embed, 'backbone'):
224
- # hybrid
225
- backbone = model.patch_embed.backbone
226
- stem_only = not hasattr(backbone, 'stem')
227
- stem = backbone if stem_only else backbone.stem
228
- stem.conv.weight.copy_(adapt_input_conv(stem.conv.weight.shape[1], _n2p(w[f'{prefix}conv_root/kernel'])))
229
- stem.norm.weight.copy_(_n2p(w[f'{prefix}gn_root/scale']))
230
- stem.norm.bias.copy_(_n2p(w[f'{prefix}gn_root/bias']))
231
- if not stem_only:
232
- for i, stage in enumerate(backbone.stages):
233
- for j, block in enumerate(stage.blocks):
234
- bp = f'{prefix}block{i + 1}/unit{j + 1}/'
235
- for r in range(3):
236
- getattr(block, f'conv{r + 1}').weight.copy_(_n2p(w[f'{bp}conv{r + 1}/kernel']))
237
- getattr(block, f'norm{r + 1}').weight.copy_(_n2p(w[f'{bp}gn{r + 1}/scale']))
238
- getattr(block, f'norm{r + 1}').bias.copy_(_n2p(w[f'{bp}gn{r + 1}/bias']))
239
- if block.downsample is not None:
240
- block.downsample.conv.weight.copy_(_n2p(w[f'{bp}conv_proj/kernel']))
241
- block.downsample.norm.weight.copy_(_n2p(w[f'{bp}gn_proj/scale']))
242
- block.downsample.norm.bias.copy_(_n2p(w[f'{bp}gn_proj/bias']))
243
- embed_conv_w = _n2p(w[f'{prefix}embedding/kernel'])
244
- else:
245
- embed_conv_w = adapt_input_conv(
246
- model.patch_embed.proj.weight.shape[1], _n2p(w[f'{prefix}embedding/kernel']))
247
- model.patch_embed.proj.weight.copy_(embed_conv_w)
248
- model.patch_embed.proj.bias.copy_(_n2p(w[f'{prefix}embedding/bias']))
249
- model.cls_token.copy_(_n2p(w[f'{prefix}cls'], t=False))
250
- pos_embed_w = _n2p(w[f'{prefix}Transformer/posembed_input/pos_embedding'], t=False)
251
- if pos_embed_w.shape != model.pos_embed.shape:
252
- pos_embed_w = resize_pos_embed( # resize pos embedding when different size from pretrained weights
253
- pos_embed_w, model.pos_embed, getattr(model, 'num_tokens', 1), model.patch_embed.grid_size)
254
- model.pos_embed.copy_(pos_embed_w)
255
- model.norm.weight.copy_(_n2p(w[f'{prefix}Transformer/encoder_norm/scale']))
256
- model.norm.bias.copy_(_n2p(w[f'{prefix}Transformer/encoder_norm/bias']))
257
- # if isinstance(model.head, nn.Linear) and model.head.bias.shape[0] == w[f'{prefix}head/bias'].shape[-1]:
258
- # model.head.weight.copy_(_n2p(w[f'{prefix}head/kernel']))
259
- # model.head.bias.copy_(_n2p(w[f'{prefix}head/bias']))
260
- # if isinstance(getattr(model.pre_logits, 'fc', None), nn.Linear) and f'{prefix}pre_logits/bias' in w:
261
- # model.pre_logits.fc.weight.copy_(_n2p(w[f'{prefix}pre_logits/kernel']))
262
- # model.pre_logits.fc.bias.copy_(_n2p(w[f'{prefix}pre_logits/bias']))
263
- for i, block in enumerate(model.blocks.children()):
264
- block_prefix = f'{prefix}Transformer/encoderblock_{i}/'
265
- mha_prefix = block_prefix + 'MultiHeadDotProductAttention_1/'
266
- block.norm1.weight.copy_(_n2p(w[f'{block_prefix}LayerNorm_0/scale']))
267
- block.norm1.bias.copy_(_n2p(w[f'{block_prefix}LayerNorm_0/bias']))
268
- block.attn.qkv.weight.copy_(torch.cat([
269
- _n2p(w[f'{mha_prefix}{n}/kernel'], t=False).flatten(1).T for n in ('query', 'key', 'value')]))
270
- block.attn.qkv.bias.copy_(torch.cat([
271
- _n2p(w[f'{mha_prefix}{n}/bias'], t=False).reshape(-1) for n in ('query', 'key', 'value')]))
272
- block.attn.proj.weight.copy_(_n2p(w[f'{mha_prefix}out/kernel']).flatten(1))
273
- block.attn.proj.bias.copy_(_n2p(w[f'{mha_prefix}out/bias']))
274
- for r in range(2):
275
- getattr(block.mlp, f'fc{r + 1}').weight.copy_(_n2p(w[f'{block_prefix}MlpBlock_3/Dense_{r}/kernel']))
276
- getattr(block.mlp, f'fc{r + 1}').bias.copy_(_n2p(w[f'{block_prefix}MlpBlock_3/Dense_{r}/bias']))
277
- block.norm2.weight.copy_(_n2p(w[f'{block_prefix}LayerNorm_2/scale']))
278
- block.norm2.bias.copy_(_n2p(w[f'{block_prefix}LayerNorm_2/bias']))
279
-
280
-
281
- def interpolate_pos_embed(pos_embed_checkpoint, visual_encoder):
282
- # interpolate position embedding
283
- embedding_size = pos_embed_checkpoint.shape[-1]
284
- num_patches = visual_encoder.patch_embed.num_patches
285
- num_extra_tokens = visual_encoder.pos_embed.shape[-2] - num_patches
286
- # height (== width) for the checkpoint position embedding
287
- orig_size = int((pos_embed_checkpoint.shape[-2] - num_extra_tokens) ** 0.5)
288
- # height (== width) for the new position embedding
289
- new_size = int(num_patches ** 0.5)
290
-
291
- if orig_size!=new_size:
292
- # class_token and dist_token are kept unchanged
293
- extra_tokens = pos_embed_checkpoint[:, :num_extra_tokens]
294
- # only the position tokens are interpolated
295
- pos_tokens = pos_embed_checkpoint[:, num_extra_tokens:]
296
- pos_tokens = pos_tokens.reshape(-1, orig_size, orig_size, embedding_size).permute(0, 3, 1, 2)
297
- pos_tokens = torch.nn.functional.interpolate(
298
- pos_tokens, size=(new_size, new_size), mode='bicubic', align_corners=False)
299
- pos_tokens = pos_tokens.permute(0, 2, 3, 1).flatten(1, 2)
300
- new_pos_embed = torch.cat((extra_tokens, pos_tokens), dim=1)
301
- print('reshape position embedding from %d to %d'%(orig_size ** 2,new_size ** 2))
302
-
303
- return new_pos_embed
304
- else:
305
- return pos_embed_checkpoint