-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
310 lines (234 loc) · 8.76 KB
/
model.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
# Positional Embeddings
def get_1d_embed_from_grid(dim, pos):
"""
dim: embedding dim
pos: list of positions to be encoded (M,)
output: (M,D)
"""
assert dim % 2 == 0
omega = np.arange(dim // 2, dtype=np.float32)
omega /= dim / 2.0
omega = 1.0 / 10000**omega # (D/2,)
pos = pos.reshape(-1)
out = np.einsum("m,d->md", pos, omega) # outer product (M,D/2)
sin = np.sin(out)
cos = np.cos(out)
out = np.concatenate([sin, cos], axis=1) # (M/D)
return out
def get_2d_sincos_pos_embed_from_grid(dim, grid):
assert dim % 2 == 0
# use half of dimensions to encode grid_h
emb_h = get_1d_embed_from_grid(dim // 2, grid[0]) # (H*W, D/2)
emb_w = get_1d_embed_from_grid(dim // 2, grid[1]) # (H*W, D/2)
emb = np.concatenate([emb_h, emb_w], axis=1) # (H*W, D)
return emb
def get_2d_sincos_pos_embed(dim, grid_size):
"""
grid_size: int of the grid height and width
return:
pos_embed: [grid_size*grid_size, dim]
"""
grid_h = np.arange(grid_size, dtype=np.float32)
grid_w = np.arange(grid_size, dtype=np.float32)
grid = np.meshgrid(grid_w, grid_h) # here w goes first
grid = np.stack(grid, axis=0)
grid = grid.reshape([2, 1, grid_size, grid_size])
pos_embed = get_2d_sincos_pos_embed_from_grid(dim, grid)
return pos_embed
# MODEL
class PatchEmbedding(nn.Module):
def __init__(self, **config):
super().__init__()
self.patch_size = config["patch_size"]
self.img_size = config["img_size"]
self.grid_size = self.img_size // self.patch_size
self.num_patches = self.grid_size**2
self.dim = config["dim"]
self.proj = nn.Conv2d(
config["num_channels"],
self.dim,
kernel_size=self.patch_size,
stride=self.patch_size,
)
def forward(self, x):
x = self.proj(x)
x = x.flatten(2).permute(0, 2, 1) # b,dim,h,w -> b,h*w,dim
return x
class ViTBlock(nn.Module):
def __init__(self, is_decoder=False, **config):
super().__init__()
dim = config["decoder_dim"] if is_decoder else config["dim"]
assert dim % config["num_heads"] == 0, "dim%num_heads!=0"
self.dim = dim
self.num_heads = config["num_heads"]
self.attn = nn.MultiheadAttention(
embed_dim=self.dim,
num_heads=self.num_heads,
batch_first=True,
dropout=config["attn_drop"],
)
self.mlp = nn.Sequential(
nn.Linear(self.dim, self.dim * 4),
nn.GELU(),
nn.Dropout(config["mlp_drop"]),
nn.Linear(self.dim * 4, self.dim),
)
self.ln1 = nn.LayerNorm(self.dim)
self.ln2 = nn.LayerNorm(self.dim)
def forward(self, x):
x = self.ln1(x)
x = x + self.attn(x, x, x)[0]
x = x + self.mlp(self.ln2(x))
return x
class ViTMAE(nn.Module):
def __init__(self, **config):
super().__init__()
self.config = config
self.patch_embed = PatchEmbedding(**config)
num_patches = self.patch_embed.num_patches
self.pos_embed = nn.Parameter(
torch.zeros(1, num_patches, config["dim"]), requires_grad=False
)
self.blocks = nn.ModuleList(
[ViTBlock(**config) for _ in range(config["encoder_depth"])]
)
self.encoder_norm = nn.LayerNorm(config["dim"])
# MAE stuff
self.decoder_embed = nn.Linear(config["dim"], config["decoder_dim"])
self.mask_token = nn.Parameter(torch.zeros(1, 1, config["decoder_dim"]))
self.decoder_pos_embed = nn.Parameter(
torch.zeros(1, num_patches, config["decoder_dim"]), requires_grad=False
)
self.decoder_blocks = nn.ModuleList(
[
ViTBlock(is_decoder=True, **config)
for _ in range(config["decoder_depth"])
]
)
self.decoder_norm = nn.LayerNorm(config["decoder_dim"])
out_features = config["patch_size"] ** 2 * config["num_channels"]
self.decoder_head = nn.Linear(config["decoder_dim"], out_features)
self.initialize_weights()
def initialize_weights(self):
pos_embed = get_2d_sincos_pos_embed(
self.pos_embed.shape[-1], int(self.patch_embed.num_patches**0.5)
)
self.pos_embed.data.copy_(torch.from_numpy(pos_embed).float().unsqueeze(0))
decoder_pos_embed = get_2d_sincos_pos_embed(
self.decoder_pos_embed.shape[-1], int(self.patch_embed.num_patches**0.5)
)
self.decoder_pos_embed.data.copy_(
torch.from_numpy(decoder_pos_embed).float().unsqueeze(0)
)
torch.nn.init.normal_(self.mask_token, std=0.02)
self.apply(self._init_weights)
def _init_weights(self, m):
if isinstance(m, nn.Linear):
torch.nn.init.xavier_uniform_(m.weight)
if isinstance(m, nn.Linear) and m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.bias, 0)
nn.init.constant_(m.weight, 1.0)
def random_masking(self, x, mask_ratio=0.75):
N, L, D = x.shape # batch, length, dim
len_keep = int(L * (1 - mask_ratio))
noise = torch.rand(N, L, device=x.device) # noise in [0, 1]
# sort noise for each sample
ids_shuffle = torch.argsort(
noise, dim=1
) # ascend: small is keep, large is remove
ids_restore = torch.argsort(ids_shuffle, dim=1)
# keep the first subset
ids_keep = ids_shuffle[:, :len_keep]
x_masked = torch.gather(x, dim=1, index=ids_keep.unsqueeze(-1).repeat(1, 1, D))
# generate the binary mask: 0 is keep, 1 is remove
mask = torch.ones([N, L], device=x.device)
mask[:, :len_keep] = 0
# unshuffle to get the binary mask
mask = torch.gather(mask, dim=1, index=ids_restore)
return x_masked, mask, ids_restore
def patchify(self, imgs):
"""
imgs: (N, 3, H, W)
x: (N, L, patch_size**2 *3)
"""
c = self.config["num_channels"]
p = self.patch_embed.patch_size
assert imgs.shape[2] == imgs.shape[3] and imgs.shape[2] % p == 0
h = w = imgs.shape[2] // p
x = imgs.reshape(shape=(imgs.shape[0], c, h, p, w, p))
x = torch.einsum("nchpwq->nhwpqc", x)
x = x.reshape(shape=(imgs.shape[0], h * w, p**2 * c))
return x
def unpatchify(self, x):
"""
x: (N, L, patch_size**2 *3)
imgs: (N, 3, H, W)
"""
c = self.config["num_channels"]
p = self.patch_embed.patch_size[0]
h = w = int(x.shape[1] ** 0.5)
assert h * w == x.shape[1]
x = x.reshape(shape=(x.shape[0], h, w, p, p, c))
x = torch.einsum("nhwpqc->nchpwq", x)
imgs = x.reshape(shape=(x.shape[0], c, h * p, h * p))
return imgs
def forward_encoder(self, x):
x = self.patch_embed(x)
x = x + self.pos_embed
x, mask, ids_restore = self.random_masking(x)
for block in self.blocks:
x = block(x)
x = self.encoder_norm(x)
return x, mask, ids_restore
def forward_decoder(self, x, ids_restore):
x = self.decoder_embed(x)
mask_tokens = self.mask_token.repeat(
x.shape[0], ids_restore.shape[1] - x.shape[1], 1
)
x_ = torch.cat([x, mask_tokens], dim=1)
# unshuffle
x = torch.gather(
x_, dim=1, index=ids_restore.unsqueeze(-1).repeat(1, 1, x.shape[2])
)
x = x + self.decoder_pos_embed
for block in self.decoder_blocks:
x = block(x)
x = self.decoder_head(x)
return x
def forward_loss(self, imgs, pred, mask):
"""
imgs: [N, 3, H, W]
pred: [N, L, p*p*3]
mask: [N, L], 0 is keep, 1 is remove,
"""
target = self.patchify(imgs)
loss = (pred - target) ** 2
loss = loss.mean(dim=-1) # [N, L], mean loss per patch
loss = (loss * mask).sum() / mask.sum() # mean loss on removed patches
return loss
def forward(self, imgs):
latent, mask, ids_restore = self.forward_encoder(imgs)
preds = self.forward_decoder(latent, ids_restore)
loss = self.forward_loss(imgs, preds, mask)
return loss, preds, mask
if __name__ == "__main__":
config = {
"patch_size": 16,
"img_size": 224,
"dim": 256,
"num_channels": 3,
"num_heads": 8,
"attn_drop": 0.1,
"mlp_drop": 0.1,
"pos_drop": 0.1,
"encoder_depth": 6,
"decoder_depth": 4,
"decoder_dim": 128,
}
model = ViTMAE(**config)