-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maskgit.py
288 lines (210 loc) · 7.33 KB
/
maskgit.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
import torch.nn as nn
import torch
import copy
import torch.nn.functional as F
from einops import rearrange, repeat, pack
import math
from models.transformer import Encoder
from models.muse import filter_logits
from tqdm import tqdm
from typing import Optional
import numpy as np
import cv2
def cosine_schedule(t):
return torch.cos(t * math.pi / 2)
class LayerNorm(nn.Module):
def __init__(self, dim):
super().__init__()
self.gamma = nn.Parameter(torch.ones(dim))
# we don't want to update this
self.register_buffer("beta", torch.zeros(dim))
def forward(self, x):
return F.layer_norm(x, x.shape[-1:], self.gamma, self.beta)
def weights_init(m):
classname = m.__class__.__name__
if "Linear" in classname or "Embedding" == classname:
print(f"Initializing Module {classname}.")
nn.init.trunc_normal_(m.weight.data, 0.0, 0.02)
# elif "Parameter" in classname:
# return nn.init.trunc_normal_(m, 0.0, 0.02)
def restore(x):
# x = (x + 1) * 0.5
x = x.clamp(0,1)
x = x.permute(1,2,0).detach().cpu().numpy()
x = (255*x).astype(np.uint8)
return x
def exists(val):
return val is not None
class BiDirectionalTransformer(nn.Module):
def __init__(
self,
dim,
vocab_size=8192,
num_patches=256,
n_heads=8,
d_head=64,
dec_depth=6,
mult=4,
dropout=0.1
):
super().__init__()
self.input_proj = nn.Embedding(vocab_size+1, dim)
# self.pos_enc = nn.Parameter(torch.randn(1, num_patches, dim))
self.pos_enc = nn.init.trunc_normal_(nn.Parameter(torch.zeros(1, num_patches, dim)), 0., 0.02)
self.mask_token_id = vocab_size
self.init_norm = LayerNorm(dim)
self.decoder = Encoder(
dim=dim, n_heads=n_heads, d_head=d_head, depth=dec_depth, mult=mult, dropout=dropout
)
self.final_norm = LayerNorm(dim)
self.linear = nn.Linear(dim, vocab_size, bias=False)
self.apply(weights_init)
def forward(self, x):
x = self.input_proj(x)
x += self.pos_enc
# transformer decoder
x = self.init_norm(x)
dec_out = self.decoder(x)
dec_out = self.final_norm(dec_out)
output = self.linear(dec_out)
return output
class MaskGitTransformer(nn.Module):
def __init__(
self,
dim,
vq,
vocab_size=8192,
n_heads=8,
d_head=64,
dec_depth=6,
mult=4,
dropout=0.1
):
super().__init__()
self.vq = vq
self.bidirectional_transformer = BiDirectionalTransformer (
dim=dim, vocab_size=vocab_size, num_patches=vq.num_patches,
n_heads=n_heads, d_head=d_head, dec_depth=dec_depth, mult=mult, dropout=dropout
)
self.mask_token_id = vocab_size
# freeze vq
self.vq.eval()
self.vq.requires_grad_(False)
def fill_mask(self, x):
b , n = x.shape
timesteps = torch.random(b)
num_tokens_masked = cosine_schedule(timesteps) * n
num_tokens_masked = num_tokens_masked.clamp(min = 1.).int()
# create mask
randm_perm = torch.rand(x.shape).argsort(dim = -1)
mask = randm_perm < num_tokens_masked
mask = mask.cuda()
# fill x with mask_id, ignore the tokens that are not masked while computing loss
tgt = x.masked_fill(~mask, -1)
x = x.masked_fill(mask, self.mask_token_id)
return x, tgt, mask
def fill_custom_mask(self, x, num_masked = 200):
T = 8 # max number of timesteps during inference
# sample the timestep from uniform distribution
b , n = x.shape
t = torch.tensor([7])
num_tokens_masked = cosine_schedule(t / T) * n
num_tokens_masked = num_tokens_masked.clamp(min = 1.).int()
# create mask
randm_perm = torch.rand(x.shape).argsort(dim = -1)
mask = randm_perm < num_tokens_masked
mask = torch.zeros(x.shape).bool()
# put first 200 as True
# mask[:, :num_masked] = True
# put last 200 as True
# mask[:, -num_masked:] = True
mask[:, :num_masked] = True
mask = mask.cuda()
# fill x with mask_id, ignore the tokens that are not masked while computing loss
tgt = x.masked_fill(~mask, -1)
# x = x.masked_fill(mask, self.mask_token_id)
return x, tgt, mask
def forward(self, imgs):
# quantize images
x = self.vq.encode_imgs(imgs)
x, tgt, mask = self.fill_mask(x)
# transformer decoder
output = self.bidirectional_transformer(x)
# x = self.init_norm(x)
# dec_out = self.decoder(x, context=None)
# dec_out = self.final_norm(dec_out)
# output = self.linear(dec_out)
if not self.training:
pred_ids = torch.softmax(output, dim=-1).argmax(dim=-1)
# replace the mask with pred_ids
x[mask] = pred_ids[mask]
decoded_imgs = self.vq.decode_indices(x)
return decoded_imgs
# compute loss
output = rearrange(output, 'b t c -> b c t')
loss = torch.nn.functional.cross_entropy(output, tgt, ignore_index=-1)
return loss
def generate(self, imgs : Optional[torch.Tensor] = None, num_masked=200, timesteps = 18):
num_patches = self.vq.num_patches
device = "cuda" if torch.cuda.is_available() else "cpu"
b = 1
# initialize decoder inputs
ids = torch.ones(b, num_patches, dtype=torch.long, device=device) * self.mask_token_id
scores = torch.zeros_like(ids).float().to(device)
mask = torch.zeros_like(ids).bool().to(device)
if exists(imgs):
# quantize images
ids = self.vq.encode_imgs(imgs)
ids , _, mask = self.fill_custom_mask(ids, num_masked)
# print number of true
print(f"Number of Tokens Masked: {mask.sum()}")
scores = torch.zeros_like(ids).float().to(device)
b , n = ids.shape
ids2 = ids.masked_fill(mask, 100)
decoded_imgs = self.vq.decode_indices(ids2)
# display
img = restore(decoded_imgs[0])
img = img[:, :, ::-1]
cv2.imwrite('outputs/maskgit/test_outputs/masked.jpg', img)
outputs = []
for timestep, steps_until_x0 in tqdm(zip(torch.linspace(0, 1, timesteps, device = device), reversed(range(timesteps))), total = timesteps):
rand_mask_prob = cosine_schedule(timestep)
num_tokens_masked = max(int((rand_mask_prob * num_masked).item()), 1)
print(num_tokens_masked)
low_probs_indices = torch.argsort(scores, dim = -1)
# indices of tokens to mask
masked_indices = low_probs_indices[:, :num_tokens_masked]
# True where the tokens are masked, False otherwise
mask.scatter_(1, masked_indices, True)
ids2 = ids.masked_fill(mask, 100)
decoded_imgs_1 = self.vq.decode_indices(ids2)
# display
img_1 = restore(decoded_imgs_1[0])
img_1 = img_1[:, :, ::-1]
x = ids.masked_fill(mask, self.mask_token_id)
# decoder forward
logits = self.bidirectional_transformer(x)
probs = F.softmax(logits, dim = -1)
# decaying temperature
temperature = 1 * (steps_until_x0 / timesteps) # temperature is annealed
# sample with gumbel softmax
logits = filter_logits(logits, p=0.9)
pred_ids = F.gumbel_softmax(logits, tau = temperature, hard = False, dim = -1).argmax(dim = -1)
# fill the masked tokens with predicted tokens
ids[mask] = pred_ids[mask]
# update scores
scores = probs.gather(2, rearrange(pred_ids, 'b t -> b t 1'))
scores = rearrange(scores, 'b t 1 -> b t')
# scores = scores.masked_fill(~mask, 1.0)
scores = scores.masked_fill(~mask, 1.0)
mask = torch.zeros_like(ids).bool().to(device)
decoded_imgs = self.vq.decode_indices(ids)
# display
img = restore(decoded_imgs[0])
img = img[:, :, ::-1]
img_combined = np.vstack([img, img_1])
outputs.append(img_combined)
outputs = np.hstack(outputs)
cv2.imwrite('outputs/maskgit/test_outputs/iterations.jpg', outputs)
imgs = self.vq.decode_indices(ids)
return imgs