Spaces:
Running
Running
feat: cleanup encode_dataset
Browse files
tools/dataset/{make_dataset.ipynb → encode_dataset.ipynb}
RENAMED
|
@@ -5,7 +5,7 @@
|
|
| 5 |
"id": "d0b72877",
|
| 6 |
"metadata": {},
|
| 7 |
"source": [
|
| 8 |
-
"#
|
| 9 |
]
|
| 10 |
},
|
| 11 |
{
|
|
@@ -15,7 +15,11 @@
|
|
| 15 |
"source": [
|
| 16 |
"This notebook shows how to pre-encode images to token sequences using JAX, VQGAN and a dataset in the [`webdataset` format](https://webdataset.github.io/webdataset/).\n",
|
| 17 |
"\n",
|
| 18 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
]
|
| 20 |
},
|
| 21 |
{
|
|
@@ -25,19 +29,15 @@
|
|
| 25 |
"metadata": {},
|
| 26 |
"outputs": [],
|
| 27 |
"source": [
|
| 28 |
-
"
|
| 29 |
-
"from tqdm import tqdm\n",
|
| 30 |
"\n",
|
| 31 |
-
"import torch\n",
|
| 32 |
"import torchvision.transforms as T\n",
|
| 33 |
-
"import torchvision.transforms.functional as TF\n",
|
| 34 |
-
"from torchvision.transforms import InterpolationMode\n",
|
| 35 |
-
"import math\n",
|
| 36 |
"\n",
|
| 37 |
"import webdataset as wds\n",
|
| 38 |
"\n",
|
| 39 |
"import jax\n",
|
| 40 |
-
"
|
|
|
|
| 41 |
]
|
| 42 |
},
|
| 43 |
{
|
|
@@ -45,184 +45,110 @@
|
|
| 45 |
"id": "c7c4c1e6",
|
| 46 |
"metadata": {},
|
| 47 |
"source": [
|
| 48 |
-
"##
|
| 49 |
-
]
|
| 50 |
-
},
|
| 51 |
-
{
|
| 52 |
-
"cell_type": "markdown",
|
| 53 |
-
"id": "9822850f",
|
| 54 |
-
"metadata": {},
|
| 55 |
-
"source": [
|
| 56 |
-
"The following is the list of shards we'll process. We hardcode the length of data so that we can see nice progress bars using `tqdm`."
|
| 57 |
]
|
| 58 |
},
|
| 59 |
{
|
| 60 |
"cell_type": "code",
|
| 61 |
-
"execution_count":
|
| 62 |
"id": "1265dbfe",
|
| 63 |
"metadata": {},
|
| 64 |
"outputs": [],
|
| 65 |
"source": [
|
| 66 |
-
"shards =
|
| 67 |
-
"
|
| 68 |
-
]
|
| 69 |
-
},
|
| 70 |
-
{
|
| 71 |
-
"cell_type": "markdown",
|
| 72 |
-
"id": "7e38fa14",
|
| 73 |
-
"metadata": {},
|
| 74 |
-
"source": [
|
| 75 |
-
"If we are extra cautious or our server is unreliable, we can enable retries by providing a custom `curl` retrieval command:"
|
| 76 |
-
]
|
| 77 |
-
},
|
| 78 |
-
{
|
| 79 |
-
"cell_type": "code",
|
| 80 |
-
"execution_count": null,
|
| 81 |
-
"id": "4c8c5960",
|
| 82 |
-
"metadata": {},
|
| 83 |
-
"outputs": [],
|
| 84 |
-
"source": [
|
| 85 |
-
"# Enable curl retries to try to work around temporary network / server errors.\n",
|
| 86 |
-
"# This shouldn't be necessary when using reliable servers.\n",
|
| 87 |
-
"# shards = f'pipe:curl -s --retry 5 --retry-delay 5 -L {shards} || true'"
|
| 88 |
-
]
|
| 89 |
-
},
|
| 90 |
-
{
|
| 91 |
-
"cell_type": "code",
|
| 92 |
-
"execution_count": null,
|
| 93 |
-
"id": "13c6631b",
|
| 94 |
-
"metadata": {},
|
| 95 |
-
"outputs": [],
|
| 96 |
-
"source": [
|
| 97 |
-
"from pathlib import Path\n",
|
| 98 |
"\n",
|
| 99 |
-
"
|
| 100 |
-
"
|
|
|
|
|
|
|
| 101 |
"\n",
|
| 102 |
-
"
|
| 103 |
-
"
|
|
|
|
|
|
|
|
|
|
| 104 |
]
|
| 105 |
},
|
| 106 |
{
|
| 107 |
"cell_type": "code",
|
| 108 |
-
"execution_count":
|
| 109 |
-
"id": "
|
| 110 |
-
"metadata": {},
|
| 111 |
-
"outputs": [
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
]
|
| 116 |
},
|
| 117 |
{
|
| 118 |
"cell_type": "markdown",
|
| 119 |
-
"id": "
|
| 120 |
"metadata": {},
|
| 121 |
"source": [
|
| 122 |
-
"
|
| 123 |
-
]
|
| 124 |
-
},
|
| 125 |
-
{
|
| 126 |
-
"cell_type": "code",
|
| 127 |
-
"execution_count": null,
|
| 128 |
-
"id": "669b35df",
|
| 129 |
-
"metadata": {},
|
| 130 |
-
"outputs": [],
|
| 131 |
-
"source": [
|
| 132 |
-
"def center_crop(image, max_size=256):\n",
|
| 133 |
-
" # Note: we allow upscaling too. We should exclude small images. \n",
|
| 134 |
-
" image = TF.resize(image, max_size, interpolation=InterpolationMode.LANCZOS)\n",
|
| 135 |
-
" image = TF.center_crop(image, output_size=2 * [max_size])\n",
|
| 136 |
-
" return image\n",
|
| 137 |
-
"\n",
|
| 138 |
-
"preprocess_image = T.Compose([\n",
|
| 139 |
-
" center_crop,\n",
|
| 140 |
-
" T.ToTensor(),\n",
|
| 141 |
-
" lambda t: t.permute(1, 2, 0) # Reorder, we need dimensions last\n",
|
| 142 |
-
"])"
|
| 143 |
]
|
| 144 |
},
|
| 145 |
{
|
| 146 |
"cell_type": "markdown",
|
| 147 |
-
"id": "
|
| 148 |
"metadata": {},
|
| 149 |
"source": [
|
| 150 |
-
"
|
| 151 |
-
"\n",
|
| 152 |
-
"Note that we receive the contents of the `json` structure, which will be replaced by the string we return.\n",
|
| 153 |
-
"If we want to keep other fields inside `json`, we can add `caption` as a new field."
|
| 154 |
]
|
| 155 |
},
|
| 156 |
{
|
| 157 |
"cell_type": "code",
|
| 158 |
"execution_count": null,
|
| 159 |
-
"id": "
|
| 160 |
"metadata": {},
|
| 161 |
"outputs": [],
|
| 162 |
"source": [
|
| 163 |
-
"
|
| 164 |
-
"
|
| 165 |
-
"
|
| 166 |
-
"
|
| 167 |
-
"
|
|
|
|
| 168 |
]
|
| 169 |
},
|
| 170 |
{
|
| 171 |
"cell_type": "markdown",
|
| 172 |
-
"id": "
|
| 173 |
"metadata": {},
|
| 174 |
"source": [
|
| 175 |
-
"
|
| 176 |
-
"
|
| 177 |
-
"
|
| 178 |
-
"
|
| 179 |
-
"We can also create our custom exception handler as demonstrated here:"
|
| 180 |
]
|
| 181 |
},
|
| 182 |
{
|
| 183 |
-
"cell_type": "
|
| 184 |
-
"
|
| 185 |
-
"id": "369d9719",
|
| 186 |
-
"metadata": {},
|
| 187 |
-
"outputs": [],
|
| 188 |
-
"source": [
|
| 189 |
-
"# UNUSED - Log exceptions to a file\n",
|
| 190 |
-
"def ignore_and_log(exn):\n",
|
| 191 |
-
" with open('errors.txt', 'a') as f:\n",
|
| 192 |
-
" f.write(f'{repr(exn)}\\n')\n",
|
| 193 |
-
" return True"
|
| 194 |
-
]
|
| 195 |
-
},
|
| 196 |
-
{
|
| 197 |
-
"cell_type": "code",
|
| 198 |
-
"execution_count": null,
|
| 199 |
-
"id": "27de1414",
|
| 200 |
-
"metadata": {},
|
| 201 |
-
"outputs": [],
|
| 202 |
-
"source": [
|
| 203 |
-
"# Or simply use `wds.ignore_and_continue`\n",
|
| 204 |
-
"exception_handler = wds.warn_and_continue"
|
| 205 |
-
]
|
| 206 |
-
},
|
| 207 |
-
{
|
| 208 |
-
"cell_type": "code",
|
| 209 |
-
"execution_count": null,
|
| 210 |
-
"id": "5149b6d5",
|
| 211 |
"metadata": {},
|
| 212 |
-
"outputs": [],
|
| 213 |
"source": [
|
| 214 |
-
"
|
| 215 |
-
" length=batches, # Hint so `len` is implemented\n",
|
| 216 |
-
" shardshuffle=False, # Keep same order for encoded files for easier bookkeeping. Set to `True` for training.\n",
|
| 217 |
-
" handler=exception_handler, # Ignore read errors instead of failing.\n",
|
| 218 |
-
")\n",
|
| 219 |
-
"\n",
|
| 220 |
-
"dataset = (dataset \n",
|
| 221 |
-
" .decode('pil') # decode image with PIL\n",
|
| 222 |
-
"# .map_dict(jpg=preprocess_image, json=create_caption, handler=exception_handler) # Process fields with functions defined above\n",
|
| 223 |
-
" .map_dict(jpg=preprocess_image, json=create_caption) # Process fields with functions defined above\n",
|
| 224 |
-
" .to_tuple('__key__', 'jpg', 'json') # filter to keep only key (for reference), image, caption.\n",
|
| 225 |
-
" .batched(bs)) # better to batch in the dataset (but we could also do it in the dataloader) - this arg does not affect speed and we could remove it"
|
| 226 |
]
|
| 227 |
},
|
| 228 |
{
|
|
@@ -235,7 +161,7 @@
|
|
| 235 |
"outputs": [],
|
| 236 |
"source": [
|
| 237 |
"%%time\n",
|
| 238 |
-
"
|
| 239 |
]
|
| 240 |
},
|
| 241 |
{
|
|
@@ -251,54 +177,50 @@
|
|
| 251 |
{
|
| 252 |
"cell_type": "code",
|
| 253 |
"execution_count": null,
|
| 254 |
-
"id": "
|
| 255 |
"metadata": {},
|
| 256 |
"outputs": [],
|
| 257 |
"source": [
|
| 258 |
-
"
|
| 259 |
-
]
|
| 260 |
-
},
|
| 261 |
-
{
|
| 262 |
-
"cell_type": "markdown",
|
| 263 |
-
"id": "44d50a51",
|
| 264 |
-
"metadata": {},
|
| 265 |
-
"source": [
|
| 266 |
-
"### Torch DataLoader"
|
| 267 |
]
|
| 268 |
},
|
| 269 |
{
|
| 270 |
"cell_type": "code",
|
| 271 |
"execution_count": null,
|
| 272 |
-
"id": "
|
| 273 |
"metadata": {},
|
| 274 |
"outputs": [],
|
| 275 |
"source": [
|
| 276 |
-
"
|
| 277 |
]
|
| 278 |
},
|
| 279 |
{
|
| 280 |
"cell_type": "markdown",
|
| 281 |
-
"id": "
|
| 282 |
"metadata": {},
|
| 283 |
"source": [
|
| 284 |
-
"
|
| 285 |
]
|
| 286 |
},
|
| 287 |
{
|
| 288 |
"cell_type": "code",
|
| 289 |
"execution_count": null,
|
| 290 |
-
"id": "
|
| 291 |
"metadata": {},
|
| 292 |
"outputs": [],
|
| 293 |
"source": [
|
| 294 |
-
"
|
|
|
|
|
|
|
| 295 |
]
|
| 296 |
},
|
| 297 |
{
|
| 298 |
"cell_type": "markdown",
|
| 299 |
-
"id": "
|
| 300 |
"metadata": {},
|
| 301 |
"source": [
|
|
|
|
|
|
|
| 302 |
"We'll use a VQGAN trained with Taming Transformers and converted to a JAX model."
|
| 303 |
]
|
| 304 |
},
|
|
@@ -311,7 +233,11 @@
|
|
| 311 |
},
|
| 312 |
"outputs": [],
|
| 313 |
"source": [
|
| 314 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
]
|
| 316 |
},
|
| 317 |
{
|
|
@@ -327,18 +253,7 @@
|
|
| 327 |
"id": "20357f74",
|
| 328 |
"metadata": {},
|
| 329 |
"source": [
|
| 330 |
-
"Encoding is really simple using `shard` to automatically distribute
|
| 331 |
-
]
|
| 332 |
-
},
|
| 333 |
-
{
|
| 334 |
-
"cell_type": "code",
|
| 335 |
-
"execution_count": null,
|
| 336 |
-
"id": "6686b004",
|
| 337 |
-
"metadata": {},
|
| 338 |
-
"outputs": [],
|
| 339 |
-
"source": [
|
| 340 |
-
"from flax.training.common_utils import shard\n",
|
| 341 |
-
"from functools import partial"
|
| 342 |
]
|
| 343 |
},
|
| 344 |
{
|
|
@@ -348,21 +263,17 @@
|
|
| 348 |
"metadata": {},
|
| 349 |
"outputs": [],
|
| 350 |
"source": [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
"@partial(jax.pmap, axis_name=\"batch\")\n",
|
| 352 |
-
"def
|
| 353 |
" # Not sure if we should `replicate` params, does not seem to have any effect\n",
|
| 354 |
-
" _, indices =
|
| 355 |
" return indices"
|
| 356 |
]
|
| 357 |
},
|
| 358 |
-
{
|
| 359 |
-
"cell_type": "markdown",
|
| 360 |
-
"id": "14375a41",
|
| 361 |
-
"metadata": {},
|
| 362 |
-
"source": [
|
| 363 |
-
"### Encoding loop"
|
| 364 |
-
]
|
| 365 |
-
},
|
| 366 |
{
|
| 367 |
"cell_type": "code",
|
| 368 |
"execution_count": null,
|
|
@@ -370,49 +281,48 @@
|
|
| 370 |
"metadata": {},
|
| 371 |
"outputs": [],
|
| 372 |
"source": [
|
| 373 |
-
"import os\n",
|
| 374 |
"import pandas as pd\n",
|
| 375 |
"\n",
|
| 376 |
-
"def encode_captioned_dataset(dataloader, output_dir, save_every=14):\n",
|
| 377 |
-
" output_dir.mkdir(parents=True, exist_ok=True)\n",
|
| 378 |
"\n",
|
| 379 |
-
"
|
| 380 |
-
"
|
| 381 |
-
"
|
| 382 |
-
"
|
| 383 |
-
"
|
| 384 |
-
" for
|
| 385 |
-
"
|
| 386 |
-
"
|
| 387 |
-
"
|
| 388 |
-
"
|
| 389 |
-
"
|
| 390 |
-
"\n",
|
| 391 |
-
"
|
| 392 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
" encoded = encoded.reshape(-1, encoded.shape[-1])\n",
|
|
|
|
|
|
|
| 394 |
"\n",
|
| 395 |
-
"
|
| 396 |
-
"
|
| 397 |
-
"
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
"
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
"metadata": {},
|
| 413 |
-
"outputs": [],
|
| 414 |
-
"source": [
|
| 415 |
-
"save_every = 318"
|
| 416 |
]
|
| 417 |
},
|
| 418 |
{
|
|
@@ -422,7 +332,7 @@
|
|
| 422 |
"metadata": {},
|
| 423 |
"outputs": [],
|
| 424 |
"source": [
|
| 425 |
-
"
|
| 426 |
]
|
| 427 |
},
|
| 428 |
{
|
|
@@ -453,7 +363,7 @@
|
|
| 453 |
"name": "python",
|
| 454 |
"nbconvert_exporter": "python",
|
| 455 |
"pygments_lexer": "ipython3",
|
| 456 |
-
"version": "3.
|
| 457 |
}
|
| 458 |
},
|
| 459 |
"nbformat": 4,
|
|
|
|
| 5 |
"id": "d0b72877",
|
| 6 |
"metadata": {},
|
| 7 |
"source": [
|
| 8 |
+
"# Pre-encoding a dataset for DALLE·mini"
|
| 9 |
]
|
| 10 |
},
|
| 11 |
{
|
|
|
|
| 15 |
"source": [
|
| 16 |
"This notebook shows how to pre-encode images to token sequences using JAX, VQGAN and a dataset in the [`webdataset` format](https://webdataset.github.io/webdataset/).\n",
|
| 17 |
"\n",
|
| 18 |
+
"Adapt it to your own dataset and image encoder.\n",
|
| 19 |
+
"\n",
|
| 20 |
+
"At the end you should have a dataset of pairs:\n",
|
| 21 |
+
"* a caption defined as a string\n",
|
| 22 |
+
"* an encoded image defined as a list of int."
|
| 23 |
]
|
| 24 |
},
|
| 25 |
{
|
|
|
|
| 29 |
"metadata": {},
|
| 30 |
"outputs": [],
|
| 31 |
"source": [
|
| 32 |
+
"from tqdm.notebook import tqdm\n",
|
|
|
|
| 33 |
"\n",
|
|
|
|
| 34 |
"import torchvision.transforms as T\n",
|
|
|
|
|
|
|
|
|
|
| 35 |
"\n",
|
| 36 |
"import webdataset as wds\n",
|
| 37 |
"\n",
|
| 38 |
"import jax\n",
|
| 39 |
+
"import braceexpand\n",
|
| 40 |
+
"from pathlib import Path"
|
| 41 |
]
|
| 42 |
},
|
| 43 |
{
|
|
|
|
| 45 |
"id": "c7c4c1e6",
|
| 46 |
"metadata": {},
|
| 47 |
"source": [
|
| 48 |
+
"## Configuration Parameters"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
]
|
| 50 |
},
|
| 51 |
{
|
| 52 |
"cell_type": "code",
|
| 53 |
+
"execution_count": 3,
|
| 54 |
"id": "1265dbfe",
|
| 55 |
"metadata": {},
|
| 56 |
"outputs": [],
|
| 57 |
"source": [
|
| 58 |
+
"shards = \"my_images/shard-{0000..0008}.tar\" # defined using braceexpand format as used by webdataset\n",
|
| 59 |
+
"encoded_output = Path(\"encoded_data\") # where we will save our encoded data\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
"\n",
|
| 61 |
+
"VQGAN_REPO, VQGAN_COMMIT_ID = (\n",
|
| 62 |
+
" \"dalle-mini/vqgan_imagenet_f16_16384\",\n",
|
| 63 |
+
" \"85eb5d3b51a1c62a0cc8f4ccdee9882c0d0bd384\",\n",
|
| 64 |
+
")\n",
|
| 65 |
"\n",
|
| 66 |
+
"# good defaults for a TPU v3-8\n",
|
| 67 |
+
"batch_size = 128 # Per device\n",
|
| 68 |
+
"num_workers = 8 # For parallel processing\n",
|
| 69 |
+
"total_bs = batch_size * jax.device_count() # You can use a smaller size while testing\n",
|
| 70 |
+
"save_frequency = 128 # Number of batches to create a new file (180MB for f16 and 720MB for f8 per file)"
|
| 71 |
]
|
| 72 |
},
|
| 73 |
{
|
| 74 |
"cell_type": "code",
|
| 75 |
+
"execution_count": 5,
|
| 76 |
+
"id": "cd956ec6-7d98-4d4d-a454-f80fe857eadd",
|
| 77 |
+
"metadata": {},
|
| 78 |
+
"outputs": [
|
| 79 |
+
{
|
| 80 |
+
"data": {
|
| 81 |
+
"text/plain": [
|
| 82 |
+
"['XXX/shard-0000.tar',\n",
|
| 83 |
+
" 'XXX/shard-0001.tar',\n",
|
| 84 |
+
" 'XXX/shard-0002.tar',\n",
|
| 85 |
+
" 'XXX/shard-0003.tar',\n",
|
| 86 |
+
" 'XXX/shard-0004.tar',\n",
|
| 87 |
+
" 'XXX/shard-0005.tar',\n",
|
| 88 |
+
" 'XXX/shard-0006.tar',\n",
|
| 89 |
+
" 'XXX/shard-0007.tar',\n",
|
| 90 |
+
" 'XXX/shard-0008.tar']"
|
| 91 |
+
]
|
| 92 |
+
},
|
| 93 |
+
"execution_count": 5,
|
| 94 |
+
"metadata": {},
|
| 95 |
+
"output_type": "execute_result"
|
| 96 |
+
}
|
| 97 |
+
],
|
| 98 |
+
"source": [
|
| 99 |
+
"shards = list(\n",
|
| 100 |
+
" braceexpand.braceexpand(shards)\n",
|
| 101 |
+
") # better display for tqdm with known length"
|
| 102 |
]
|
| 103 |
},
|
| 104 |
{
|
| 105 |
"cell_type": "markdown",
|
| 106 |
+
"id": "75dba8e2",
|
| 107 |
"metadata": {},
|
| 108 |
"source": [
|
| 109 |
+
"## Load data"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
]
|
| 111 |
},
|
| 112 |
{
|
| 113 |
"cell_type": "markdown",
|
| 114 |
+
"id": "a1e8fb95",
|
| 115 |
"metadata": {},
|
| 116 |
"source": [
|
| 117 |
+
"We load data using `webdataset`."
|
|
|
|
|
|
|
|
|
|
| 118 |
]
|
| 119 |
},
|
| 120 |
{
|
| 121 |
"cell_type": "code",
|
| 122 |
"execution_count": null,
|
| 123 |
+
"id": "9ef5de9e",
|
| 124 |
"metadata": {},
|
| 125 |
"outputs": [],
|
| 126 |
"source": [
|
| 127 |
+
"ds = (\n",
|
| 128 |
+
" wds.WebDataset(shards, handler=wds.warn_and_continue)\n",
|
| 129 |
+
" .decode(\"rgb\", handler=wds.warn_and_continue)\n",
|
| 130 |
+
" .to_tuple(\"jpg\", \"txt\") # assumes image is in `jpg` and caption in `txt`\n",
|
| 131 |
+
" .batched(total_bs) # load in batch per worker (faster)\n",
|
| 132 |
+
")"
|
| 133 |
]
|
| 134 |
},
|
| 135 |
{
|
| 136 |
"cell_type": "markdown",
|
| 137 |
+
"id": "90981824",
|
| 138 |
"metadata": {},
|
| 139 |
"source": [
|
| 140 |
+
"Note:\n",
|
| 141 |
+
"* you can also shuffle shards and items using `shardshuffle` and `shuffle` if necessary.\n",
|
| 142 |
+
"* you may need to resize images in your pipeline (with `map_dict` for example), we assume they are already set to 256x256.\n",
|
| 143 |
+
"* you can also filter out some items using `select`."
|
|
|
|
| 144 |
]
|
| 145 |
},
|
| 146 |
{
|
| 147 |
+
"cell_type": "markdown",
|
| 148 |
+
"id": "129c377d",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
"metadata": {},
|
|
|
|
| 150 |
"source": [
|
| 151 |
+
"We can now inspect our data."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
]
|
| 153 |
},
|
| 154 |
{
|
|
|
|
| 161 |
"outputs": [],
|
| 162 |
"source": [
|
| 163 |
"%%time\n",
|
| 164 |
+
"images, captions = next(iter(ds))"
|
| 165 |
]
|
| 166 |
},
|
| 167 |
{
|
|
|
|
| 177 |
{
|
| 178 |
"cell_type": "code",
|
| 179 |
"execution_count": null,
|
| 180 |
+
"id": "5acfc4d8",
|
| 181 |
"metadata": {},
|
| 182 |
"outputs": [],
|
| 183 |
"source": [
|
| 184 |
+
"captions[:10]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
]
|
| 186 |
},
|
| 187 |
{
|
| 188 |
"cell_type": "code",
|
| 189 |
"execution_count": null,
|
| 190 |
+
"id": "c24693c0",
|
| 191 |
"metadata": {},
|
| 192 |
"outputs": [],
|
| 193 |
"source": [
|
| 194 |
+
"T.ToPILImage()(images[0].permute(2, 0, 1))"
|
| 195 |
]
|
| 196 |
},
|
| 197 |
{
|
| 198 |
"cell_type": "markdown",
|
| 199 |
+
"id": "3059ffb1",
|
| 200 |
"metadata": {},
|
| 201 |
"source": [
|
| 202 |
+
"Finally we create our dataloader."
|
| 203 |
]
|
| 204 |
},
|
| 205 |
{
|
| 206 |
"cell_type": "code",
|
| 207 |
"execution_count": null,
|
| 208 |
+
"id": "c227c551",
|
| 209 |
"metadata": {},
|
| 210 |
"outputs": [],
|
| 211 |
"source": [
|
| 212 |
+
"dl = (\n",
|
| 213 |
+
" wds.WebLoader(ds, batch_size=None, num_workers=8).unbatched().batched(total_bs)\n",
|
| 214 |
+
") # avoid partial batch at the end of each worker"
|
| 215 |
]
|
| 216 |
},
|
| 217 |
{
|
| 218 |
"cell_type": "markdown",
|
| 219 |
+
"id": "a354472b",
|
| 220 |
"metadata": {},
|
| 221 |
"source": [
|
| 222 |
+
"## Image encoder\n",
|
| 223 |
+
"\n",
|
| 224 |
"We'll use a VQGAN trained with Taming Transformers and converted to a JAX model."
|
| 225 |
]
|
| 226 |
},
|
|
|
|
| 233 |
},
|
| 234 |
"outputs": [],
|
| 235 |
"source": [
|
| 236 |
+
"from vqgan_jax.modeling_flax_vqgan import VQModel\n",
|
| 237 |
+
"from flax.jax_utils import replicate\n",
|
| 238 |
+
"\n",
|
| 239 |
+
"vqgan = VQModel.from_pretrained(\"flax-community/vqgan_f16_16384\")\n",
|
| 240 |
+
"vqgan_params = replicate(vqgan.params)"
|
| 241 |
]
|
| 242 |
},
|
| 243 |
{
|
|
|
|
| 253 |
"id": "20357f74",
|
| 254 |
"metadata": {},
|
| 255 |
"source": [
|
| 256 |
+
"Encoding is really simple using `shard` to automatically distribute batches across devices and `pmap`."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
]
|
| 258 |
},
|
| 259 |
{
|
|
|
|
| 263 |
"metadata": {},
|
| 264 |
"outputs": [],
|
| 265 |
"source": [
|
| 266 |
+
"from flax.training.common_utils import shard\n",
|
| 267 |
+
"from functools import partial\n",
|
| 268 |
+
"\n",
|
| 269 |
+
"\n",
|
| 270 |
"@partial(jax.pmap, axis_name=\"batch\")\n",
|
| 271 |
+
"def p_encode(batch, params):\n",
|
| 272 |
" # Not sure if we should `replicate` params, does not seem to have any effect\n",
|
| 273 |
+
" _, indices = vqgan.encode(batch, params=params)\n",
|
| 274 |
" return indices"
|
| 275 |
]
|
| 276 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
{
|
| 278 |
"cell_type": "code",
|
| 279 |
"execution_count": null,
|
|
|
|
| 281 |
"metadata": {},
|
| 282 |
"outputs": [],
|
| 283 |
"source": [
|
|
|
|
| 284 |
"import pandas as pd\n",
|
| 285 |
"\n",
|
|
|
|
|
|
|
| 286 |
"\n",
|
| 287 |
+
"def encode_dataset(dataloader, output_dir, save_frequency):\n",
|
| 288 |
+
" output_dir.mkdir(parents=True, exist_ok=True)\n",
|
| 289 |
+
" all_captions = []\n",
|
| 290 |
+
" all_encoding = []\n",
|
| 291 |
+
" n_file = 1\n",
|
| 292 |
+
" for idx, (images, captions) in enumerate(tqdm(dataloader)):\n",
|
| 293 |
+
" images = images.numpy()\n",
|
| 294 |
+
" n = len(images) // 8 * 8\n",
|
| 295 |
+
" if n != len(images):\n",
|
| 296 |
+
" # get the max number of images we can (multiple of 8)\n",
|
| 297 |
+
" print(f\"Different sizes {n} vs {len(images)}\")\n",
|
| 298 |
+
" images = images[:n]\n",
|
| 299 |
+
" captions = captions[:n]\n",
|
| 300 |
+
" if not len(captions):\n",
|
| 301 |
+
" print(f\"No images/captions in batch...\")\n",
|
| 302 |
+
" continue\n",
|
| 303 |
+
" images = shard(images)\n",
|
| 304 |
+
" encoded = p_encode(images, vqgan_params)\n",
|
| 305 |
" encoded = encoded.reshape(-1, encoded.shape[-1])\n",
|
| 306 |
+
" all_captions.extend(captions)\n",
|
| 307 |
+
" all_encoding.extend(encoded.tolist())\n",
|
| 308 |
"\n",
|
| 309 |
+
" # save files\n",
|
| 310 |
+
" if (idx + 1) % save_frequency == 0:\n",
|
| 311 |
+
" print(f\"Saving file {n_file}\")\n",
|
| 312 |
+
" batch_df = pd.DataFrame.from_dict(\n",
|
| 313 |
+
" {\"caption\": all_captions, \"encoding\": all_encoding}\n",
|
| 314 |
+
" )\n",
|
| 315 |
+
" batch_df.to_parquet(f\"{output_dir}/{n_file:03d}.parquet\")\n",
|
| 316 |
+
" all_captions = []\n",
|
| 317 |
+
" all_encoding = []\n",
|
| 318 |
+
" n_file += 1\n",
|
| 319 |
+
"\n",
|
| 320 |
+
" if len(all_captions):\n",
|
| 321 |
+
" print(f\"Saving final file {n_file}\")\n",
|
| 322 |
+
" batch_df = pd.DataFrame.from_dict(\n",
|
| 323 |
+
" {\"caption\": all_captions, \"encoding\": all_encoding}\n",
|
| 324 |
+
" )\n",
|
| 325 |
+
" batch_df.to_parquet(f\"{output_dir}/{n_file:03d}.parquet\")"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
]
|
| 327 |
},
|
| 328 |
{
|
|
|
|
| 332 |
"metadata": {},
|
| 333 |
"outputs": [],
|
| 334 |
"source": [
|
| 335 |
+
"encode_dataset(dl, output_dir=encoded_output, save_frequency=save_frequency)"
|
| 336 |
]
|
| 337 |
},
|
| 338 |
{
|
|
|
|
| 363 |
"name": "python",
|
| 364 |
"nbconvert_exporter": "python",
|
| 365 |
"pygments_lexer": "ipython3",
|
| 366 |
+
"version": "3.9.7"
|
| 367 |
}
|
| 368 |
},
|
| 369 |
"nbformat": 4,
|