-
Notifications
You must be signed in to change notification settings - Fork 15
/
vit.py
417 lines (356 loc) · 20.4 KB
/
vit.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# Trainer for MaskGIT
import os
import random
import time
import math
import numpy as np
from tqdm import tqdm
from collections import deque
from omegaconf import OmegaConf
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.utils as vutils
from torch.nn.parallel import DistributedDataParallel as DDP
from Trainer.trainer import Trainer
from Network.transformer import MaskTransformer
from Network.Taming.models.vqgan import VQModel
class MaskGIT(Trainer):
def __init__(self, args):
""" Initialization of the model (VQGAN and Masked Transformer), optimizer, criterion, etc."""
super().__init__(args)
self.args = args # Main argument see main.py
self.patch_size = self.args.img_size // 16 # Number of vizual token (+1 for the class)
self.scaler = torch.cuda.amp.GradScaler() # Init Scaler for multi GPUs
self.vit = self.get_network("vit") # Load Masked Bidirectional Transformer
self.ae = self.get_network("autoencoder") # Load VQGAN
self.criterion = self.get_loss("cross_entropy", label_smoothing=0.1) # Get cross entropy loss
self.optim = self.get_optim(self.vit, self.args.lr, betas=(0.9, 0.96)) # Get Adam Optimizer with weight decay
# Load data if aim to train or test the model
if not self.args.debug:
self.train_data, self.test_data = self.get_data()
# Initialize evaluation object if testing
if self.args.test_only:
from Metrics.sample_and_eval import SampleAndEval
self.sae = SampleAndEval(device=self.args.device, num_images=50_000)
def get_network(self, archi):
""" return the network, load checkpoint if self.args.resume == True
:param
archi -> str: vit|autoencoder, the architecture to load
:return
model -> nn.Module: the network
"""
if archi == "vit":
model = MaskTransformer(
img_size=self.args.img_size, hidden_dim=768, codebook_size=1024, depth=24, heads=16, mlp_dim=3072, dropout=0.1 # Small
# img_size=self.args.img_size, hidden_dim=1024, codebook_size=1024, depth=32, heads=16, mlp_dim=3072, dropout=0.1 # Big
# img_size=self.args.img_size, hidden_dim=1024, codebook_size=1024, depth=48, heads=16, mlp_dim=3072, dropout=0.1 # Huge
)
if self.args.resume:
ckpt = self.args.vit_folder
ckpt += "current.pth" if os.path.isdir(self.args.vit_folder) else ""
if self.args.is_master:
print("load ckpt from:", ckpt)
# Read checkpoint file
checkpoint = torch.load(ckpt, map_location='cpu')
# Update the current epoch and iteration
self.args.iter += checkpoint['iter']
self.args.global_epoch += checkpoint['global_epoch']
# Load network
model.load_state_dict(checkpoint['model_state_dict'], strict=False)
model = model.to(self.args.device)
if self.args.is_multi_gpus: # put model on multi GPUs if available
model = DDP(model, device_ids=[self.args.device])
elif archi == "autoencoder":
# Load config
config = OmegaConf.load(self.args.vqgan_folder + "model.yaml")
model = VQModel(**config.model.params)
checkpoint = torch.load(self.args.vqgan_folder + "last.ckpt", map_location="cpu")["state_dict"]
# Load network
model.load_state_dict(checkpoint, strict=False)
model = model.eval()
model = model.to(self.args.device)
if self.args.is_multi_gpus: # put model on multi GPUs if available
model = DDP(model, device_ids=[self.args.device])
model = model.module
else:
model = None
if self.args.is_master:
print(f"Size of model {archi}: "
f"{sum(p.numel() for p in model.parameters() if p.requires_grad) / 10 ** 6:.3f}M")
return model
@staticmethod
def get_mask_code(code, mode="arccos", value=None):
""" Replace the code token by *value* according the the *mode* scheduler
:param
code -> torch.LongTensor(): bsize * 16 * 16, the unmasked code
mode -> str: the rate of value to mask
value -> int: mask the code by the value
:return
masked_code -> torch.LongTensor(): bsize * 16 * 16, the masked version of the code
mask -> torch.LongTensor(): bsize * 16 * 16, the binary mask of the mask
"""
r = torch.rand(code.size(0))
if mode == "linear": # linear scheduler
val_to_mask = r
elif mode == "square": # square scheduler
val_to_mask = (r ** 2)
elif mode == "cosine": # cosine scheduler
val_to_mask = torch.cos(r * math.pi * 0.5)
elif mode == "arccos": # arc cosine scheduler
val_to_mask = torch.arccos(r) / (math.pi * 0.5)
else:
val_to_mask = None
mask_code = code.detach().clone()
# Sample the amount of tokens + localization to mask
mask = torch.rand(size=code.size()) < val_to_mask.view(code.size(0), 1, 1)
if value > 0: # Mask the selected token by the value
mask_code[mask] = torch.full_like(mask_code[mask], value)
else: # Replace by a randon token
mask_code[mask] = torch.randint_like(mask_code[mask], 0, 1024)
return mask_code, mask
def adap_sche(self, step, mode="arccos", leave=False):
""" Create a sampling scheduler
:param
step -> int: number of prediction during inference
mode -> str: the rate of value to unmask
leave -> bool: tqdm arg on either to keep the bar or not
:return
scheduler -> torch.LongTensor(): the list of token to predict at each step
"""
r = torch.linspace(1, 0, step)
if mode == "root": # root scheduler
val_to_mask = 1 - (r ** .5)
elif mode == "linear": # linear scheduler
val_to_mask = 1 - r
elif mode == "square": # square scheduler
val_to_mask = 1 - (r ** 2)
elif mode == "cosine": # cosine scheduler
val_to_mask = torch.cos(r * math.pi * 0.5)
elif mode == "arccos": # arc cosine scheduler
val_to_mask = torch.arccos(r) / (math.pi * 0.5)
else:
return
# fill the scheduler by the ratio of tokens to predict at each step
sche = (val_to_mask / val_to_mask.sum()) * (self.patch_size * self.patch_size)
sche = sche.round()
sche[sche == 0] = 1 # add 1 to predict a least 1 token / step
sche[-1] += (self.patch_size * self.patch_size) - sche.sum() # need to sum up nb of code
return tqdm(sche.int(), leave=leave)
def train_one_epoch(self, log_iter=2500):
""" Train the model for 1 epoch """
self.vit.train()
cum_loss = 0.
window_loss = deque(maxlen=self.args.grad_cum)
bar = tqdm(self.train_data, leave=False) if self.args.is_master else self.train_data
n = len(self.train_data)
# Start training for 1 epoch
for x, y in bar:
x = x.to(self.args.device)
y = y.to(self.args.device)
x = 2 * x - 1 # normalize from x in [0,1] to [-1,1] for VQGAN
# Drop xx% of the condition for cfg
drop_label = torch.empty(y.size()).uniform_(0, 1) < self.args.drop_label
# VQGAN encoding to img tokens
with torch.no_grad():
emb, _, [_, _, code] = self.ae.encode(x)
code = code.reshape(x.size(0), self.patch_size, self.patch_size)
# Mask the encoded tokens
masked_code, mask = self.get_mask_code(code, value=self.args.mask_value)
with torch.cuda.amp.autocast(): # half precision
pred = self.vit(masked_code, y, drop_label=drop_label) # The unmasked tokens prediction
# Cross-entropy loss
loss = self.criterion(pred.reshape(-1, 1024 + 1), code.view(-1)) / self.args.grad_cum
# update weight if accumulation of gradient is done
update_grad = self.args.iter % self.args.grad_cum == self.args.grad_cum - 1
if update_grad:
self.optim.zero_grad()
self.scaler.scale(loss).backward() # rescale to get more precise loss
if update_grad:
self.scaler.unscale_(self.optim) # rescale loss
nn.utils.clip_grad_norm_(self.vit.parameters(), 1.0) # Clip gradient
self.scaler.step(self.optim)
self.scaler.update()
cum_loss += loss.cpu().item()
window_loss.append(loss.data.cpu().numpy().mean())
# logs
if update_grad and self.args.is_master:
self.log_add_scalar('Train/Loss', np.array(window_loss).sum(), self.args.iter)
if self.args.iter % log_iter == 0 and self.args.is_master:
# Generate sample for visualization
gen_sample = self.sample(nb_sample=10)[0]
gen_sample = vutils.make_grid(gen_sample, nrow=10, padding=2, normalize=True)
self.log_add_img("Images/Sampling", gen_sample, self.args.iter)
# Show reconstruction
unmasked_code = torch.softmax(pred, -1).max(-1)[1]
reco_sample = self.reco(x=x[:10], code=code[:10], unmasked_code=unmasked_code[:10], mask=mask[:10])
reco_sample = vutils.make_grid(reco_sample.data, nrow=10, padding=2, normalize=True)
self.log_add_img("Images/Reconstruction", reco_sample, self.args.iter)
# Save Network
self.save_network(model=self.vit, path=self.args.vit_folder+"current.pth",
iter=self.args.iter, optimizer=self.optim, global_epoch=self.args.global_epoch)
self.args.iter += 1
return cum_loss / n
def fit(self):
""" Train the model """
if self.args.is_master:
print("Start training:")
start = time.time()
# Start training
for e in range(self.args.global_epoch, self.args.epoch):
# synch every GPUs
if self.args.is_multi_gpus:
self.train_data.sampler.set_epoch(e)
# Train for one epoch
train_loss = self.train_one_epoch()
# Synch loss
if self.args.is_multi_gpus:
train_loss = self.all_gather(train_loss, torch.cuda.device_count())
# Save model
if e % 10 == 0 and self.args.is_master:
self.save_network(model=self.vit, path=self.args.vit_folder + f"epoch_{self.args.global_epoch:03d}.pth",
iter=self.args.iter, optimizer=self.optim, global_epoch=self.args.global_epoch)
# Clock time
clock_time = (time.time() - start)
if self.args.is_master:
self.log_add_scalar('Train/GlobalLoss', train_loss, self.args.global_epoch)
print(f"\rEpoch {self.args.global_epoch},"
f" Iter {self.args.iter :},"
f" Loss {train_loss:.4f},"
f" Time: {clock_time // 3600:.0f}h {(clock_time % 3600) // 60:.0f}min {clock_time % 60:.2f}s")
self.args.global_epoch += 1
def eval(self):
""" Evaluation of the model"""
self.vit.eval()
if self.args.is_master:
print(f"Evaluation with hyper-parameter ->\n"
f"scheduler: {self.args.sched_mode}, number of step: {self.args.step}, "
f"softmax temperature: {self.args.sm_temp}, cfg weight: {self.args.cfg_w}, "
f"gumbel temperature: {self.args.r_temp}")
# Evaluate the model
m = self.sae.compute_and_log_metrics(self)
self.vit.train()
return m
def reco(self, x=None, code=None, masked_code=None, unmasked_code=None, mask=None):
""" For visualization, show the model ability to reconstruct masked img
:param
x -> torch.FloatTensor: bsize x 3 x 256 x 256, the real image
code -> torch.LongTensor: bsize x 16 x 16, the encoded image tokens
masked_code -> torch.LongTensor: bsize x 16 x 16, the masked image tokens
unmasked_code -> torch.LongTensor: bsize x 16 x 16, the prediction of the transformer
mask -> torch.LongTensor: bsize x 16 x 16, the binary mask of the encoded image
:return
l_visual -> torch.LongTensor: bsize x 3 x (256 x ?) x 256, the visualization of the images
"""
l_visual = [x]
with torch.no_grad():
if code is not None:
code = code.view(code.size(0), self.patch_size, self.patch_size)
# Decoding reel code
_x = self.ae.decode_code(torch.clamp(code, 0, 1023))
if mask is not None:
# Decoding reel code with mask to hide
mask = mask.view(code.size(0), 1, self.patch_size, self.patch_size).float()
__x2 = _x * (1 - F.interpolate(mask, (self.args.img_size, self.args.img_size)).to(self.args.device))
l_visual.append(__x2)
if masked_code is not None:
# Decoding masked code
masked_code = masked_code.view(code.size(0), self.patch_size, self.patch_size)
__x = self.ae.decode_code(torch.clamp(masked_code, 0, 1023))
l_visual.append(__x)
if unmasked_code is not None:
# Decoding predicted code
unmasked_code = unmasked_code.view(code.size(0), self.patch_size, self.patch_size)
___x = self.ae.decode_code(torch.clamp(unmasked_code, 0, 1023))
l_visual.append(___x)
return torch.cat(l_visual, dim=0)
def sample(self, init_code=None, nb_sample=50, labels=None, sm_temp=1, w=3,
randomize="linear", r_temp=4.5, sched_mode="arccos", step=12):
""" Generate sample with the MaskGIT model
:param
init_code -> torch.LongTensor: nb_sample x 16 x 16, the starting initialization code
nb_sample -> int: the number of image to generated
labels -> torch.LongTensor: the list of classes to generate
sm_temp -> float: the temperature before softmax
w -> float: scale for the classifier free guidance
randomize -> str: linear|warm_up|random|no, either or not to add randomness
r_temp -> float: temperature for the randomness
sched_mode -> str: root|linear|square|cosine|arccos, the shape of the scheduler
step: -> int: number of step for the decoding
:return
x -> torch.FloatTensor: nb_sample x 3 x 256 x 256, the generated images
code -> torch.LongTensor: nb_sample x step x 16 x 16, the code corresponding to the generated images
"""
self.vit.eval()
l_codes = [] # Save the intermediate codes predicted
l_mask = [] # Save the intermediate masks
with torch.no_grad():
if labels is None: # Default classes generated
# goldfish, chicken, tiger cat, hourglass, ship, dog, race car, airliner, teddy bear, random
labels = [1, 7, 282, 604, 724, 179, 751, 404, 850, random.randint(0, 999)] * (nb_sample // 10)
labels = torch.LongTensor(labels).to(self.args.device)
drop = torch.ones(nb_sample, dtype=torch.bool).to(self.args.device)
if init_code is not None: # Start with a pre-define code
code = init_code
mask = (init_code == 1024).float().view(nb_sample, self.patch_size*self.patch_size)
else: # Initialize a code
if self.args.mask_value < 0: # Code initialize with random tokens
code = torch.randint(0, 1024, (nb_sample, self.patch_size, self.patch_size)).to(self.args.device)
else: # Code initialize with masked tokens
code = torch.full((nb_sample, self.patch_size, self.patch_size), self.args.mask_value).to(self.args.device)
mask = torch.ones(nb_sample, self.patch_size*self.patch_size).to(self.args.device)
# Instantiate scheduler
if isinstance(sched_mode, str): # Standard ones
scheduler = self.adap_sche(step, mode=sched_mode)
else: # Custom one
scheduler = sched_mode
# Beginning of sampling, t = number of token to predict a step "indice"
for indice, t in enumerate(scheduler):
if mask.sum() < t: # Cannot predict more token than 16*16 or 32*32
t = int(mask.sum().item())
if mask.sum() == 0: # Break if code is fully predicted
break
with torch.cuda.amp.autocast(): # half precision
if w != 0:
# Model Prediction
logit = self.vit(torch.cat([code.clone(), code.clone()], dim=0),
torch.cat([labels, labels], dim=0),
torch.cat([~drop, drop], dim=0))
logit_c, logit_u = torch.chunk(logit, 2, dim=0)
_w = w * (indice / len(scheduler))
# Classifier Free Guidance
logit = (1 + _w) * logit_c - _w * logit_u
else:
logit = self.vit(code.clone(), labels, drop_label=~drop)
prob = torch.softmax(logit * sm_temp, -1)
# Sample the code from the softmax prediction
distri = torch.distributions.Categorical(probs=prob)
pred_code = distri.sample()
conf = torch.gather(prob, 2, pred_code.view(nb_sample, self.patch_size*self.patch_size, 1))
if randomize == "linear": # add gumbel noise decreasing over the sampling process
ratio = (indice / len(scheduler))
rand = r_temp * np.random.gumbel(size=(nb_sample, self.patch_size*self.patch_size)) * (1 - ratio)
conf = torch.log(conf.squeeze()) + torch.from_numpy(rand).to(self.args.device)
elif randomize == "warm_up": # chose random sample for the 2 first steps
conf = torch.rand_like(conf) if indice < 2 else conf
elif randomize == "random": # chose random prediction at each step
conf = torch.rand_like(conf)
# do not predict on already predicted tokens
conf[~mask.bool()] = -math.inf
# chose the predicted token with the highest confidence
tresh_conf, indice_mask = torch.topk(conf.view(nb_sample, -1), k=t, dim=-1)
tresh_conf = tresh_conf[:, -1]
# replace the chosen tokens
conf = (conf >= tresh_conf.unsqueeze(-1)).view(nb_sample, self.patch_size, self.patch_size)
f_mask = (mask.view(nb_sample, self.patch_size, self.patch_size).float() * conf.view(nb_sample, self.patch_size, self.patch_size).float()).bool()
code[f_mask] = pred_code.view(nb_sample, self.patch_size, self.patch_size)[f_mask]
# update the mask
for i_mask, ind_mask in enumerate(indice_mask):
mask[i_mask, ind_mask] = 0
l_codes.append(pred_code.view(nb_sample, self.patch_size, self.patch_size).clone())
l_mask.append(mask.view(nb_sample, self.patch_size, self.patch_size).clone())
# decode the final prediction
_code = torch.clamp(code, 0, 1023)
x = self.ae.decode_code(_code)
self.vit.train()
return x, l_codes, l_mask