|
| 1 | +import json |
| 2 | +import os |
| 3 | +import random |
| 4 | + |
| 5 | +import torch |
| 6 | +from torch.utils.data import Dataset |
| 7 | + |
| 8 | + |
| 9 | +class LatentDataset(Dataset): |
| 10 | + |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + json_path, |
| 14 | + num_latent_t, |
| 15 | + cfg_rate, |
| 16 | + ) -> None: |
| 17 | + # data_merge_path: video_dir, latent_dir, prompt_embed_dir, json_path |
| 18 | + self.json_path = json_path |
| 19 | + self.cfg_rate = cfg_rate |
| 20 | + self.datase_dir_path = os.path.dirname(json_path) |
| 21 | + self.video_dir = os.path.join(self.datase_dir_path, "video") |
| 22 | + self.latent_dir = os.path.join(self.datase_dir_path, "latent") |
| 23 | + self.prompt_embed_dir = os.path.join(self.datase_dir_path, |
| 24 | + "prompt_embed") |
| 25 | + self.prompt_attention_mask_dir = os.path.join(self.datase_dir_path, |
| 26 | + "prompt_attention_mask") |
| 27 | + with open(self.json_path) as f: |
| 28 | + self.data_anno = json.load(f) |
| 29 | + # json.load(f) already keeps the order |
| 30 | + # self.data_anno = sorted(self.data_anno, key=lambda x: x['latent_path']) |
| 31 | + self.num_latent_t = num_latent_t |
| 32 | + |
| 33 | + self.uncond_prompt_embed = torch.zeros(256, 4096).to(torch.float32) |
| 34 | + |
| 35 | + self.uncond_prompt_mask = torch.zeros(256).bool() |
| 36 | + self.lengths = [ |
| 37 | + data_item.get("length", 1) for data_item in self.data_anno |
| 38 | + ] |
| 39 | + |
| 40 | + def __getitem__(self, idx): |
| 41 | + latent_file = self.data_anno[idx]["latent_path"] |
| 42 | + prompt_embed_file = self.data_anno[idx]["prompt_embed_path"] |
| 43 | + prompt_attention_mask_file = self.data_anno[idx][ |
| 44 | + "prompt_attention_mask"] |
| 45 | + # load |
| 46 | + latent = torch.load( |
| 47 | + os.path.join(self.latent_dir, latent_file), |
| 48 | + map_location="cpu", |
| 49 | + weights_only=True, |
| 50 | + ) |
| 51 | + latent = latent.squeeze(0)[:, -self.num_latent_t:] |
| 52 | + if random.random() < self.cfg_rate: |
| 53 | + prompt_embed = self.uncond_prompt_embed |
| 54 | + prompt_attention_mask = self.uncond_prompt_mask |
| 55 | + else: |
| 56 | + prompt_embed = torch.load( |
| 57 | + os.path.join(self.prompt_embed_dir, prompt_embed_file), |
| 58 | + map_location="cpu", |
| 59 | + weights_only=True, |
| 60 | + ) |
| 61 | + prompt_attention_mask = torch.load( |
| 62 | + os.path.join(self.prompt_attention_mask_dir, |
| 63 | + prompt_attention_mask_file), |
| 64 | + map_location="cpu", |
| 65 | + weights_only=True, |
| 66 | + ) |
| 67 | + return latent, prompt_embed, prompt_attention_mask |
| 68 | + |
| 69 | + def __len__(self): |
| 70 | + return len(self.data_anno) |
| 71 | + |
| 72 | + |
| 73 | +def latent_collate_function(batch): |
| 74 | + # return latent, prompt, latent_attn_mask, text_attn_mask |
| 75 | + # latent_attn_mask: # b t h w |
| 76 | + # text_attn_mask: b 1 l |
| 77 | + # needs to check if the latent/prompt' size and apply padding & attn mask |
| 78 | + latents, prompt_embeds, prompt_attention_masks = zip(*batch) |
| 79 | + # calculate max shape |
| 80 | + max_t = max([latent.shape[1] for latent in latents]) |
| 81 | + max_h = max([latent.shape[2] for latent in latents]) |
| 82 | + max_w = max([latent.shape[3] for latent in latents]) |
| 83 | + |
| 84 | + # padding |
| 85 | + latent_list: list[torch.Tensor] = [ |
| 86 | + torch.nn.functional.pad( |
| 87 | + latent, |
| 88 | + ( |
| 89 | + 0, |
| 90 | + max_t - latent.shape[1], |
| 91 | + 0, |
| 92 | + max_h - latent.shape[2], |
| 93 | + 0, |
| 94 | + max_w - latent.shape[3], |
| 95 | + ), |
| 96 | + ) for latent in latents |
| 97 | + ] |
| 98 | + # attn mask |
| 99 | + latent_attn_mask = torch.ones(len(latent_list), max_t, max_h, max_w) |
| 100 | + # set to 0 if padding |
| 101 | + for i, latent in enumerate(latent_list): |
| 102 | + latent_attn_mask[i, latent.shape[1]:, :, :] = 0 |
| 103 | + latent_attn_mask[i, :, latent.shape[2]:, :] = 0 |
| 104 | + latent_attn_mask[i, :, :, latent.shape[3]:] = 0 |
| 105 | + |
| 106 | + prompt_embeds = torch.stack(prompt_embeds, dim=0) |
| 107 | + prompt_attention_masks = torch.stack(prompt_attention_masks, dim=0) |
| 108 | + latents = torch.stack(latent_list, dim=0) |
| 109 | + return latents, prompt_embeds, latent_attn_mask, prompt_attention_masks |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + dataset = LatentDataset("data/Mochi-Synthetic-Data/merge.txt", |
| 114 | + num_latent_t=28, |
| 115 | + cfg_rate=0.0) |
| 116 | + dataloader = torch.utils.data.DataLoader(dataset, |
| 117 | + batch_size=2, |
| 118 | + shuffle=False, |
| 119 | + collate_fn=latent_collate_function) |
| 120 | + for latent, prompt_embed, latent_attn_mask, prompt_attention_mask in dataloader: |
| 121 | + print( |
| 122 | + latent.shape, |
| 123 | + prompt_embed.shape, |
| 124 | + latent_attn_mask.shape, |
| 125 | + prompt_attention_mask.shape, |
| 126 | + ) |
| 127 | + import pdb |
| 128 | + |
| 129 | + pdb.set_trace() |
0 commit comments