File size: 7,973 Bytes
3b3a783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
####data util to get and preprocess data from a text and image pair to latents and text embeddings.
### all that is required is a csv file with an image url and text caption:
#!pip install datasets img2dataset accelerate diffusers
#!pip install git+https://github.com/openai/CLIP.git

import json
import os
from dataclasses import dataclass
from typing import List, Union

import clip
import h5py
import numpy as np
import pandas as pd
import torch
import torchvision.transforms as transforms
import webdataset as wds
from diffusers import AutoencoderKL
from img2dataset import download
from torch import Tensor, nn
from torch.utils.data import DataLoader
from tqdm import tqdm


@torch.no_grad()
def encode_text(label: Union[str, List[str]], model: nn.Module, device: str) -> Tensor:
    text_tokens = clip.tokenize(label, truncate=True).to(device)
    text_encoding = model.encode_text(text_tokens)
    return text_encoding.cpu()


@torch.no_grad()
def encode_image(img: Tensor, vae: AutoencoderKL) -> Tensor:
    x = img.to("cuda").to(torch.float16)

    x = x * 2 - 1  # to make it between -1 and 1.
    encoded = vae.encode(x, return_dict=False)[0].sample()
    return encoded.cpu()


@torch.no_grad()
def decode_latents(out_latents: torch.FloatTensor, vae: AutoencoderKL) -> Tensor:
    # expected to be in the unscaled latent space
    out = vae.decode(out_latents.cuda())[0].cpu()

    return ((out + 1) / 2).clip(0, 1)


def quantize_latents(lat: Tensor, clip_val: float = 20) -> Tensor:
    """scale and quantize latents to unit8"""
    lat_norm = lat.clip(-clip_val, clip_val) / clip_val
    return (((lat_norm + 1) / 2) * 255).to(torch.uint8)


def dequantize_latents(lat: Tensor, clip_val: float = 20) -> Tensor:
    lat_norm = (lat.to(torch.float16) / 255) * 2 - 1
    return lat_norm * clip_val


def append_to_dataset(dataset: h5py.File, new_data: Tensor) -> None:
    """Appends new data to an HDF5 dataset."""
    new_size = dataset.shape[0] + new_data.shape[0]
    dataset.resize(new_size, axis=0)
    dataset[-new_data.shape[0] :] = new_data


def get_text_and_latent_embeddings_hdf5(
    dataloader: DataLoader, vae: AutoencoderKL, model: nn.Module, drive_save_path: str
) -> None:
    """Process img/text inptus that outputs an latent and text embeddings and text_prompts, saving encodings as float16."""

    img_latent_path = os.path.join(drive_save_path, "image_latents.hdf5")
    text_embed_path = os.path.join(drive_save_path, "text_encodings.hdf5")
    metadata_csv_path = os.path.join(drive_save_path, "metadata.csv")

    with h5py.File(img_latent_path, "a") as img_file, h5py.File(text_embed_path, "a") as text_file:
        if "image_latents" not in img_file:
            img_ds = img_file.create_dataset(
                "image_latents",
                shape=(0, 4, 32, 32),
                maxshape=(None, 4, 32, 32),
                dtype="float16",
                chunks=True,
            )
        else:
            img_ds = img_file["image_latents"]

        if "text_encodings" not in text_file:
            text_ds = text_file.create_dataset(
                "text_encodings", shape=(0, 768), maxshape=(None, 768), dtype="float16", chunks=True
            )
        else:
            text_ds = text_file["text_encodings"]

        for img, (label, url) in tqdm(dataloader):
            text_encoding = encode_text(label, model).cpu().numpy().astype(np.float16)
            img_encoding = encode_image(img, vae).cpu().numpy().astype(np.float16)

            append_to_dataset(img_ds, img_encoding)
            append_to_dataset(text_ds, text_encoding)

            metadata_df = pd.DataFrame({"text": label, "url": url})
            if os.path.exists(metadata_csv_path):
                metadata_df.to_csv(metadata_csv_path, mode="a", header=False, index=False)
            else:
                metadata_df.to_csv(metadata_csv_path, mode="w", header=True, index=False)


def download_and_process_data(
    latent_save_path="latents",
    raw_imgs_save_path="raw_imgs",
    csv_path="imgs.csv",
    image_size=256,
    bs=64,
    caption_col="captions",
    url_col="url",
    download_data=True,
    number_sample_per_shard=10000,
):
    if not os.path.exists(raw_imgs_save_path):
        os.mkdir(raw_imgs_save_path)

    if not os.path.exists(latent_save_path):
        os.mkdir(latent_save_path)

    if download_data:
        download(
            processes_count=8,
            thread_count=64,
            url_list=csv_path,
            image_size=image_size,
            output_folder=raw_imgs_save_path,
            output_format="webdataset",
            input_format="csv",
            url_col=url_col,
            caption_col=caption_col,
            enable_wandb=False,
            number_sample_per_shard=number_sample_per_shard,
            distributor="multiprocessing",
            resize_mode="center_crop",
        )

    files = os.listdir(raw_imgs_save_path)
    tar_files = [os.path.join(raw_imgs_save_path, file) for file in files if file.endswith(".tar")]
    print(tar_files)
    dataset = wds.WebDataset(tar_files)

    transform = transforms.Compose(
        [
            transforms.ToTensor(),
        ]
    )

    # output is (img_tensor, (caption , url_col)) per batch:
    dataset = (
        dataset.decode("pil")
        .to_tuple("jpg;png", "json")
        .map_tuple(transform, lambda x: (x["caption"], x[url_col]))
    )

    dataloader = DataLoader(dataset, batch_size=bs, shuffle=False)

    model, _ = clip.load("ViT-L/14")

    vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
    vae = vae.to("cuda")
    model.to("cuda")

    print("Starting to encode latents and text:")
    get_text_and_latent_embeddings_hdf5(dataloader, vae, model, latent_save_path)
    print("Finished encode latents and text:")


@dataclass
class DataConfiguration:
    data_link: str
    caption_col: str = "caption"
    url_col: str = "url"
    latent_save_path: str = "latents_folder"
    raw_imgs_save_path: str = "raw_imgs_folder"
    use_drive: bool = False
    initial_csv_path: str = "imgs.csv"
    number_sample_per_shard: int = 10000
    image_size: int = 256
    batch_size: int = 64
    download_data: bool = True


if __name__ == "__main__":
    use_wandb = False

    if use_wandb:
        import wandb

        os.environ["WANDB_API_KEY"] = "key"
        #!wandb login

    data_link = "https://huggingface.co/datasets/zzliang/GRIT/resolve/main/grit-20m/coyo_0_snappy.parquet?download=true"

    data_config = DataConfiguration(
        data_link=data_link,
        latent_save_path="latent_folder",
        raw_imgs_save_path="raw_imgs_folder",
        download_data=False,
        number_sample_per_shard=1000,
    )

    if use_wandb:
        wandb.init(project="image_vae_processing", entity="apapiu", config=data_config)

    if not os.path.exists(data_config.latent_save_path):
        os.mkdir(data_config.latent_save_path)

    config_file_path = os.path.join(data_config.latent_save_path, "config.json")
    with open(config_file_path, "w") as f:
        json.dump(data_config.__dict__, f)

    print("Config saved to:", config_file_path)

    df = pd.read_parquet(data_link)
    ###add additional data cleaning here...should I
    df = df.iloc[:3000]
    df[["key", "url", "caption"]].to_csv("imgs.csv", index=None)

    if data_config.use_drive:
        from google.colab import drive

        drive.mount("/content/drive")

    download_and_process_data(
        latent_save_path=data_config.latent_save_path,
        raw_imgs_save_path=data_config.raw_imgs_save_path,
        csv_path=data_config.initial_csv_path,
        image_size=data_config.image_size,
        bs=data_config.batch_size,
        caption_col=data_config.caption_col,
        url_col=data_config.url_col,
        download_data=data_config.download_data,
        number_sample_per_shard=data_config.number_sample_per_shard,
    )

    if use_wandb:
        wandb.finish()