File size: 9,546 Bytes
ae754a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d0b72877",
   "metadata": {},
   "source": [
    "# Pre-encoding a dataset for DALLE·mini"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba7b31e6",
   "metadata": {},
   "source": [
    "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",
    "\n",
    "Adapt it to your own dataset and image encoder.\n",
    "\n",
    "At the end you should have a dataset of pairs:\n",
    "* a caption defined as a string\n",
    "* an encoded image defined as a list of int."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b59489e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm.notebook import tqdm\n",
    "\n",
    "import torchvision.transforms as T\n",
    "\n",
    "import webdataset as wds\n",
    "\n",
    "import jax\n",
    "import braceexpand\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7c4c1e6",
   "metadata": {},
   "source": [
    "## Configuration Parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1265dbfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "shards = \"my_images/shard-{0000..0008}.tar\"  # defined using braceexpand format as used by webdataset\n",
    "encoded_output = Path(\"encoded_data\")  # where we will save our encoded data\n",
    "\n",
    "VQGAN_REPO, VQGAN_COMMIT_ID = (\n",
    "    \"dalle-mini/vqgan_imagenet_f16_16384\",\n",
    "    \"85eb5d3b51a1c62a0cc8f4ccdee9882c0d0bd384\",\n",
    ")\n",
    "\n",
    "# good defaults for a TPU v3-8\n",
    "batch_size = 128  # Per device\n",
    "num_workers = 8  # For parallel processing\n",
    "total_bs = batch_size * jax.device_count()  # You can use a smaller size while testing\n",
    "save_frequency = 128  # Number of batches to create a new file (180MB for f16 and 720MB for f8 per file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cd956ec6-7d98-4d4d-a454-f80fe857eadd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['XXX/shard-0000.tar',\n",
       " 'XXX/shard-0001.tar',\n",
       " 'XXX/shard-0002.tar',\n",
       " 'XXX/shard-0003.tar',\n",
       " 'XXX/shard-0004.tar',\n",
       " 'XXX/shard-0005.tar',\n",
       " 'XXX/shard-0006.tar',\n",
       " 'XXX/shard-0007.tar',\n",
       " 'XXX/shard-0008.tar']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shards = list(\n",
    "    braceexpand.braceexpand(shards)\n",
    ")  # better display for tqdm with known length"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75dba8e2",
   "metadata": {},
   "source": [
    "## Load data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1e8fb95",
   "metadata": {},
   "source": [
    "We load data using `webdataset`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ef5de9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = (\n",
    "    wds.WebDataset(shards, handler=wds.warn_and_continue)\n",
    "    .decode(\"rgb\", handler=wds.warn_and_continue)\n",
    "    .to_tuple(\"jpg\", \"txt\")  # assumes image is in `jpg` and caption in `txt`\n",
    "    .batched(total_bs)  # load in batch per worker (faster)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90981824",
   "metadata": {},
   "source": [
    "Note:\n",
    "* you can also shuffle shards and items using `shardshuffle` and `shuffle` if necessary.\n",
    "* you may need to resize images in your pipeline (with `map_dict` for example), we assume they are already set to 256x256.\n",
    "* you can also filter out some items using `select`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "129c377d",
   "metadata": {},
   "source": [
    "We can now inspect our data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8cac98cb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "images, captions = next(iter(ds))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd268fbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "images.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5acfc4d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "captions[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c24693c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "T.ToPILImage()(images[0].permute(2, 0, 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3059ffb1",
   "metadata": {},
   "source": [
    "Finally we create our dataloader."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c227c551",
   "metadata": {},
   "outputs": [],
   "source": [
    "dl = (\n",
    "    wds.WebLoader(ds, batch_size=None, num_workers=8).unbatched().batched(total_bs)\n",
    ")  # avoid partial batch at the end of each worker"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a354472b",
   "metadata": {},
   "source": [
    "## Image encoder\n",
    "\n",
    "We'll use a VQGAN trained with Taming Transformers and converted to a JAX model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47a8b818",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from vqgan_jax.modeling_flax_vqgan import VQModel\n",
    "from flax.jax_utils import replicate\n",
    "\n",
    "vqgan = VQModel.from_pretrained(\"flax-community/vqgan_f16_16384\")\n",
    "vqgan_params = replicate(vqgan.params)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62ad01c3",
   "metadata": {},
   "source": [
    "## Encoding"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20357f74",
   "metadata": {},
   "source": [
    "Encoding is really simple using `shard` to automatically distribute batches across devices and `pmap`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "322a4619",
   "metadata": {},
   "outputs": [],
   "source": [
    "from flax.training.common_utils import shard\n",
    "from functools import partial\n",
    "\n",
    "\n",
    "@partial(jax.pmap, axis_name=\"batch\")\n",
    "def p_encode(batch, params):\n",
    "    # Not sure if we should `replicate` params, does not seem to have any effect\n",
    "    _, indices = vqgan.encode(batch, params=params)\n",
    "    return indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff6c10d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "\n",
    "def encode_dataset(dataloader, output_dir, save_frequency):\n",
    "    output_dir.mkdir(parents=True, exist_ok=True)\n",
    "    all_captions = []\n",
    "    all_encoding = []\n",
    "    n_file = 1\n",
    "    for idx, (images, captions) in enumerate(tqdm(dataloader)):\n",
    "        images = images.numpy()\n",
    "        n = len(images) // 8 * 8\n",
    "        if n != len(images):\n",
    "            # get the max number of images we can (multiple of 8)\n",
    "            print(f\"Different sizes {n} vs {len(images)}\")\n",
    "            images = images[:n]\n",
    "            captions = captions[:n]\n",
    "        if not len(captions):\n",
    "            print(f\"No images/captions in batch...\")\n",
    "            continue\n",
    "        images = shard(images)\n",
    "        encoded = p_encode(images, vqgan_params)\n",
    "        encoded = encoded.reshape(-1, encoded.shape[-1])\n",
    "        all_captions.extend(captions)\n",
    "        all_encoding.extend(encoded.tolist())\n",
    "\n",
    "        # save files\n",
    "        if (idx + 1) % save_frequency == 0:\n",
    "            print(f\"Saving file {n_file}\")\n",
    "            batch_df = pd.DataFrame.from_dict(\n",
    "                {\"caption\": all_captions, \"encoding\": all_encoding}\n",
    "            )\n",
    "            batch_df.to_parquet(f\"{output_dir}/{n_file:03d}.parquet\")\n",
    "            all_captions = []\n",
    "            all_encoding = []\n",
    "            n_file += 1\n",
    "\n",
    "    if len(all_captions):\n",
    "        print(f\"Saving final file {n_file}\")\n",
    "        batch_df = pd.DataFrame.from_dict(\n",
    "            {\"caption\": all_captions, \"encoding\": all_encoding}\n",
    "        )\n",
    "        batch_df.to_parquet(f\"{output_dir}/{n_file:03d}.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7704863d",
   "metadata": {},
   "outputs": [],
   "source": [
    "encode_dataset(dl, output_dir=encoded_output, save_frequency=save_frequency)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8953dd84",
   "metadata": {},
   "source": [
    "----"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "db471c52d602b4f5f40ecaf278e88ccfef85c29d0a1a07185b0d51fc7acf4e26"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}