-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfcmae_model.py
206 lines (171 loc) · 6.45 KB
/
fcmae_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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import torch
import torch.nn as nn
from timm.models.layers import trunc_normal_
from sparse_model import SparseConvNeXtV2
from model import Block
class FCMAE(nn.Module):
""" Fully Convolutional Masked Autoencoder with ConvNeXtV2 backbone
"""
def __init__(
self,
img_size=224,
in_chans=3,
depths=[3, 3, 9, 3],
dims=[96, 192, 384, 768],
decoder_depth=1,
decoder_embed_dim=512,
patch_size=32,
mask_ratio=0.6,
norm_pix_loss=False):
super().__init__()
# configs
self.img_size = img_size
self.depths = depths
self.imds = dims
self.patch_size = patch_size
self.mask_ratio = mask_ratio
self.num_patches = (img_size // patch_size) ** 2
self.decoder_embed_dim = decoder_embed_dim
self.decoder_depth = decoder_depth
self.norm_pix_loss = norm_pix_loss
# encoder
self.encoder = SparseConvNeXtV2(
in_chans=in_chans, depths=depths, dims=dims, D=2)
# decoder
self.proj = nn.Conv2d(
in_channels=dims[-1],
out_channels=decoder_embed_dim,
kernel_size=1)
# mask tokens
self.mask_token = nn.Parameter(torch.zeros(1, decoder_embed_dim, 1, 1))
decoder = [Block(
dim=decoder_embed_dim,
drop_path=0.) for i in range(decoder_depth)]
self.decoder = nn.Sequential(*decoder)
# pred
self.pred = nn.Conv2d(
in_channels=decoder_embed_dim,
out_channels=patch_size ** 2 * in_chans,
kernel_size=1)
def patchify(self, imgs):
"""
imgs: (N, 3, H, W)
x: (N, L, patch_size**2 *3)
"""
p = self.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], 3, h, p, w, p))
x = torch.einsum('nchpwq->nhwpqc', x)
x = x.reshape(shape=(imgs.shape[0], h * w, p**2 * 3))
return x
def unpatchify(self, x):
"""
x: (N, L, patch_size**2 *3)
imgs: (N, 3, H, W)
"""
p = self.patch_size
h = w = int(x.shape[1]**.5)
assert h * w == x.shape[1]
x = x.reshape(shape=(x.shape[0], h, w, p, p, 3))
x = torch.einsum('nhwpqc->nchpwq', x)
imgs = x.reshape(shape=(x.shape[0], 3, h * p, h * p))
return imgs
def gen_random_mask(self, x, mask_ratio):
N = x.shape[0]
L = (x.shape[2] // self.patch_size) ** 2
len_keep = int(L * (1 - mask_ratio))
noise = torch.randn(N, L, device=x.device)
# sort noise for each sample
ids_shuffle = torch.argsort(noise, dim=1)
ids_restore = torch.argsort(ids_shuffle, dim=1)
# 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 mask
def upsample_mask(self, mask, scale):
assert len(mask.shape) == 2
p = int(mask.shape[1] ** .5)
return mask.reshape(-1, p, p).\
repeat_interleave(scale, axis=1).\
repeat_interleave(scale, axis=2)
def forward_encoder(self, imgs, mask_ratio):
# generate random masks
mask = self.gen_random_mask(imgs, mask_ratio)
# encoding
x = self.encoder(imgs, mask)
return x, mask
def forward_decoder(self, x, mask):
x = self.proj(x)
# append mask token
n, c, h, w = x.shape
mask = mask.reshape(-1, h, w).unsqueeze(1).type_as(x)
mask_token = self.mask_token.repeat(x.shape[0], 1, x.shape[2], x.shape[3])
x = x * (1. - mask) + mask_token * mask
# decoding
x = self.decoder(x)
# pred
pred = self.pred(x)
return pred
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
"""
if len(pred.shape) == 4:
n, c, _, _ = pred.shape
pred = pred.reshape(n, c, -1)
pred = torch.einsum('ncl->nlc', pred)
target = self.patchify(imgs)
if self.norm_pix_loss:
mean = target.mean(dim=-1, keepdim=True)
var = target.var(dim=-1, keepdim=True)
target = (target - mean) / (var + 1.e-6)**.5
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, labels=None, mask_ratio=0.6):
x, mask = self.forward_encoder(imgs, mask_ratio)
pred = self.forward_decoder(x, mask)
loss = self.forward_loss(imgs, pred, mask)
return loss, pred, mask
def convnextv2_atto(**kwargs):
model = FCMAE(
depths=[2, 2, 6, 2], dims=[40, 80, 160, 320], **kwargs)
return model
def convnextv2_femto(**kwargs):
model = FCMAE(
depths=[2, 2, 6, 2], dims=[48, 96, 192, 384], **kwargs)
return model
def convnextv2_pico(**kwargs):
model = FCMAE(
depths=[2, 2, 6, 2], dims=[64, 128, 256, 512], **kwargs)
return model
def convnextv2_nano(**kwargs):
model = FCMAE(
depths=[2, 2, 8, 2], dims=[80, 160, 320, 640], **kwargs)
return model
def convnextv2_tiny(**kwargs):
model = FCMAE(
depths=[3, 3, 9, 3], dims=[96, 192, 384, 768], **kwargs)
return model
def convnextv2_base(**kwargs):
model = FCMAE(
depths=[3, 3, 27, 3], dims=[128, 256, 512, 1024], **kwargs)
return model
def convnextv2_large(**kwargs):
model = FCMAE(
depths=[3, 3, 27, 3], dims=[192, 384, 768, 1536], **kwargs)
return model
def convnextv2_huge(**kwargs):
model = FCMAE(
depths=[3, 3, 27, 3], dims=[352, 704, 1408, 2816], **kwargs)
return model