import random import time import math 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 # Abstract class for the trainer import os import json import numpy as np from PIL import Image import torch.optim as optim from torch.utils.data import Dataset import torch from torch import nn from transformer.transformer import LayerNorm, TransformerEncoder, TransformerEncoderLayer class SinePositionalEmbedding(nn.Module): def __init__( self, embedding_dim: int, dropout: float=0.0, scale: bool=False, alpha: bool=False, ): super().__init__() self.embedding_dim = embedding_dim self.x_scale = math.sqrt(embedding_dim) if scale else 1.0 self.alpha = nn.Parameter(torch.ones(1), requires_grad=alpha) self.dropout = torch.nn.Dropout(p=dropout) self.reverse = False self.pe = None self.extend_pe(torch.tensor(0.0).expand(1, 4000)) def extend_pe(self, x): """Reset the positional encodings.""" if self.pe is not None: if self.pe.size(1) >= x.size(1): if self.pe.dtype != x.dtype or self.pe.device != x.device: self.pe = self.pe.to(dtype=x.dtype, device=x.device) return pe = torch.zeros(x.size(1), self.embedding_dim) if self.reverse: position = torch.arange( x.size(1) - 1, -1, -1.0, dtype=torch.float32).unsqueeze(1) else: position = torch.arange( 0, x.size(1), dtype=torch.float32).unsqueeze(1) div_term = torch.exp( torch.arange(0, self.embedding_dim, 2, dtype=torch.float32) * -(math.log(10000.0) / self.embedding_dim)) pe[:, 0::2] = torch.sin(position * div_term) pe[:, 1::2] = torch.cos(position * div_term) pe = pe.unsqueeze(0) self.pe = pe.to(device=x.device, dtype=x.dtype).detach() def forward(self, x: torch.Tensor) -> torch.Tensor: self.extend_pe(x) output = x.unsqueeze(-1) if x.ndim == 2 else x output = output * self.x_scale + self.alpha * self.pe[:, :x.size(1)] return self.dropout(output) class MaskTransformer(nn.Module): def __init__(self, hidden_dim=768, codebook_size=1024, depth=24, heads=8, mlp_dim=3072, dropout=0.1, cond_dim=192): """ Initialize the Transformer model. :param: img_size -> int: Input image size (default: 256) hidden_dim -> int: Hidden dimension for the transformer (default: 768) codebook_size -> int: Size of the codebook (default: 1024) depth -> int: Depth of the transformer (default: 24) heads -> int: Number of attention heads (default: 8) mlp_dim -> int: MLP dimension (default: 3072) dropout -> float: Dropout rate (default: 0.1) nclass -> int: Number of classes (default: 1000) """ super().__init__() hidden_dim = 512 self.codebook_size = codebook_size self.cond_proj = nn.Linear(cond_dim, hidden_dim) self.tok_emb = nn.Embedding(codebook_size+1, hidden_dim) # +1 for the mask of the viz token, +1 for mask of the class self.position = SinePositionalEmbedding( hidden_dim, dropout=0.1, scale=False, alpha=True) # First layer before the Transformer block norm_first = False self.encoder = TransformerEncoder( TransformerEncoderLayer( d_model=hidden_dim, nhead=16, dim_feedforward=2048, dropout=0.1, batch_first=True, norm_first=False, ), num_layers=12, norm=LayerNorm(hidden_dim) if norm_first else None, ) self.proj = nn.Linear(hidden_dim, codebook_size+1, bias=False) def forward(self, img_token, y=None, drop_label=None, return_attn=False): """ Forward. :param: img_token -> torch.LongTensor: bsize x 16 x 16, the encoded image tokens y -> torch.FloatTensor: condition to generate drop_label -> torch.BoolTensor: either or not to drop the condition return_attn -> Bool: return the attn for visualization :return: logit: -> torch.FloatTensor: bsize x path_size*path_size * 1024, the predicted logit attn: -> list(torch.FloatTensor): list of attention for visualization """ b, l = img_token.size() y[drop_label] = 0 input = img_token tok_embeddings = self.tok_emb(input) + self.cond_proj(y) x = self.position(tok_embeddings) x, _ = self.encoder( (x, None), src_key_padding_mask=None) audio_output = self.proj(x) return audio_output def custom_lamda(x): return x[:5] class ImageNetKaggle(Dataset): def __init__(self, root, split, transform=None): self.samples = [] self.targets = [] self.transform = transform self.syn_to_class = {} with open(os.path.join(root, "imagenet_class_index.json"), "rb") as f: json_file = json.load(f) for class_id, v in json_file.items(): self.syn_to_class[v[0]] = int(class_id) with open(os.path.join(root, "ILSVRC2012_val_labels.json"), "rb") as f: self.val_to_syn = json.load(f) samples_dir = os.path.join(root, "ILSVRC/Data/CLS-LOC", split) for entry in os.listdir(samples_dir): if split == "train": syn_id = entry target = self.syn_to_class[syn_id] syn_folder = os.path.join(samples_dir, syn_id) for sample in os.listdir(syn_folder): sample_path = os.path.join(syn_folder, sample) self.samples.append(sample_path) self.targets.append(target) elif split == "val": syn_id = self.val_to_syn[entry] target = self.syn_to_class[syn_id] sample_path = os.path.join(samples_dir, entry) self.samples.append(sample_path) self.targets.append(target) def __len__(self): return len(self.samples) def __getitem__(self, idx): x = Image.open(self.samples[idx]).convert("RGB") if self.transform: x = self.transform(x) return x, self.targets[idx] class MaskGIT(nn.Module): def __init__(self, args): super().__init__() """ Initialization of the model (VQGAN and Masked Transformer), optimizer, criterion, etc.""" self.args = args # Main argument see main.py # self.scaler = torch.cuda.amp.GradScaler() # Init Scaler for multi GPUs # self.ae = self.get_network("autoencoder") self.codebook_size = args.codebook_size print("Acquired codebook size:", self.codebook_size) self.vit = self.get_network("vit") # Load Masked Bidirectional Transformer self.criterion = self.get_loss("cross_entropy", label_smoothing=0.1, ignore_index=self.codebook_size) # self.optim = self.get_optim(self.vit, self.args.lr, betas=(0.9, 0.96)) # Get Adam Optimizer with weight decay @staticmethod def get_optim(net, lr, mode="AdamW", **kwargs): """ Get the optimizer Adam or Adam with weight decay """ if isinstance(net, list): params = [] for n in net: params += list(n.parameters()) else: params = net.parameters() if mode == "AdamW": return optim.AdamW(params, lr, weight_decay=1e-5, **kwargs) elif mode == "Adam": return optim.Adam(params, lr, **kwargs) return None @staticmethod def get_loss(mode="l1", **kwargs): """ return the loss """ if mode == "l1": return nn.L1Loss() elif mode == "l2": return nn.MSELoss() elif mode == "cross_entropy": return nn.CrossEntropyLoss(**kwargs) return None 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( hidden_dim=768, codebook_size=self.codebook_size, depth=12, 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 ) 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, codebook_size=256): """ 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) 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, codebook_size) return mask_code, mask def adap_sche(self, step, mode="arccos", leave=False, tgt_len=None): """ 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()) * tgt_len sche = sche.round() sche[sche == 0] = 1 # add 1 to predict a least 1 token / step sche[-1] += tgt_len - 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, codebook_size=self.codebook_size) 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, self.codebook_size + 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, self.codebook_size - 1)) 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, self.codebook_size - 1)) 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, self.codebook_size - 1)) l_visual.append(___x) return torch.cat(l_visual, dim=0) def sample(self, init_code=None, cond=None, sm_temp=5, w=3, randomize="linear", r_temp=10, 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(): nb_sample = cond.shape[0] tgt_len = cond.shape[1] drop = torch.ones(nb_sample, dtype=torch.bool).to(cond.device) if init_code is not None: # Start with a pre-define code code = init_code mask = (init_code == self.codebook_size).float() else: # Initialize a code if self.args.mask_value < 0: # Code initialize with random tokens code = torch.randint(0, self.codebook_size, (nb_sample, tgt_len)).to( cond.device) else: # Code initialize with masked tokens code = torch.full((nb_sample, tgt_len), self.args.mask_value).to( cond.device) mask = torch.ones(nb_sample, tgt_len).to(cond.device) # Instantiate scheduler if isinstance(sched_mode, str): # Standard ones scheduler = self.adap_sche(step, mode=sched_mode, tgt_len=tgt_len) else: # Custom one scheduler = sched_mode # Beginning of sampling, t = number of token to predict a step "indice" for indice, t in enumerate(tqdm(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 if w != 0: # Model Prediction logit = self.vit(torch.cat([code.clone(), code.clone()], dim=0), torch.cat([cond, cond], dim=0), torch.cat([~drop, drop], dim=0)) logit_c, logit_u = torch.chunk(logit, 2, dim=0) _w = w * (indice / (len(scheduler) - 1)) # Classifier Free Guidance logit = (1 + _w) * logit_c - _w * logit_u else: logit = self.vit(code.clone(), cond, 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, tgt_len, 1)) if randomize == "linear": # add gumbel noise decreasing over the sampling process ratio = (indice / (len(scheduler) - 1)) rand = r_temp * np.random.gumbel(size=(nb_sample, tgt_len)) * (1 - ratio) conf = torch.log(conf.squeeze()) + torch.from_numpy(rand).to(cond.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, tgt_len) f_mask = (mask.view(nb_sample, tgt_len).float() * conf.view(nb_sample,tgt_len).float()).bool() code[f_mask] = pred_code.view(nb_sample, tgt_len)[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, tgt_len).clone()) l_mask.append(mask.view(nb_sample, tgt_len).clone()) # decode the final prediction _code = torch.clamp(code, 0, self.codebook_size - 1) return _code if __name__ == '__main__': args_dict = { "sched_mode": "arccos", "mask_value": 512, "drop_label": 0.1, "debug": False, "iter": 0, "global_epoch": 0, "is_multi_gpus": False, "is_master": True, "codebook_size": 512 } args = OmegaConf.create(args_dict) maskgit = MaskGIT(args) print(maskgit) bs = 1 ph_len = 100 code = torch.randint(0, 512, (bs, ph_len)) y = torch.randn(bs, ph_len, 192) padding_mask = torch.randint(0,1, (bs, ph_len)).bool() drop_label = torch.empty(y.size()).uniform_(0, 1) < args.drop_label print(code) masked_code, mask = maskgit.get_mask_code(code, value=args.mask_value, codebook_size=maskgit.codebook_size) print(masked_code, mask) with torch.cuda.amp.autocast(): # half precision pred = maskgit.vit(masked_code, y, drop_label=drop_label) # The unmasked tokens prediction # Cross-entropy loss code_label = code.clone() code_label[padding_mask] = maskgit.codebook_size loss = maskgit.criterion(pred.reshape(-1, maskgit.codebook_size + 1), code_label.view(-1)) loss.backward() print(loss) for name, param in maskgit.named_parameters(): if param.grad is None: print('param {} has no grad'.format(name)) y_pred = maskgit.sample(None, cond=y,) print(y_pred.shape, y_pred)