File size: 13,827 Bytes
a11eff5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
0aab987
a11eff5
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
0aab987
a11eff5
0aab987
a11eff5
 
 
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
0aab987
a11eff5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aab987
a11eff5
 
 
 
 
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f6d33374",
   "metadata": {},
   "source": [
    "# Test notebook with CLIP scoring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6eb74941-bb4d-4d7e-97f1-d5a3a07672bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install flax transformers\n",
    "# !git clone https://github.com/patil-suraj/vqgan-jax.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41db7534-f589-4b63-9165-9c9799e1b06e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "import jax\n",
    "import flax.linen as nn\n",
    "from flax.training.common_utils import shard\n",
    "from flax.jax_utils import replicate, unreplicate\n",
    "\n",
    "from transformers.models.bart.modeling_flax_bart import *\n",
    "from transformers import BartTokenizer, FlaxBartForConditionalGeneration\n",
    "\n",
    "import io\n",
    "\n",
    "import requests\n",
    "from PIL import Image\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import torch\n",
    "import torchvision.transforms as T\n",
    "import torchvision.transforms.functional as TF\n",
    "from torchvision.transforms import InterpolationMode\n",
    "\n",
    "jax.devices()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d408065c",
   "metadata": {},
   "source": [
    "`dalle_mini` is a local package that contains the VQGAN-JAX model by Suraj, and other utilities. You can also `cd` to the directory that contains your checkout of [`vqgan-jax`](https://github.com/patil-suraj/vqgan-jax.git)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09295910",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dalle_mini.vqgan_jax.modeling_flax_vqgan import VQModel\n",
    "#%cd /content/vqgan-jax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6a3462a-9004-4121-b365-3ae3aaf94dd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# TODO: set those args in a config file\n",
    "OUTPUT_VOCAB_SIZE = 16384 + 1  # encoded image token space + 1 for bos\n",
    "OUTPUT_LENGTH = 256 + 1  # number of encoded tokens + 1 for bos\n",
    "BOS_TOKEN_ID = 16384\n",
    "BASE_MODEL = 'facebook/bart-large-cnn'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbef1afb-0b36-44a5-83f7-643d7e2c0e30",
   "metadata": {},
   "outputs": [],
   "source": [
    "class CustomFlaxBartModule(FlaxBartModule):\n",
    "    def setup(self):\n",
    "        # we keep shared to easily load pre-trained weights\n",
    "        self.shared = nn.Embed(\n",
    "            self.config.vocab_size,\n",
    "            self.config.d_model,\n",
    "            embedding_init=jax.nn.initializers.normal(self.config.init_std, self.dtype),\n",
    "            dtype=self.dtype,\n",
    "        )\n",
    "        # a separate embedding is used for the decoder\n",
    "        self.decoder_embed = nn.Embed(\n",
    "            OUTPUT_VOCAB_SIZE,\n",
    "            self.config.d_model,\n",
    "            embedding_init=jax.nn.initializers.normal(self.config.init_std, self.dtype),\n",
    "            dtype=self.dtype,\n",
    "        )\n",
    "        self.encoder = FlaxBartEncoder(self.config, dtype=self.dtype, embed_tokens=self.shared)\n",
    "\n",
    "        # the decoder has a different config\n",
    "        decoder_config = BartConfig(self.config.to_dict())\n",
    "        decoder_config.max_position_embeddings = OUTPUT_LENGTH\n",
    "        decoder_config.vocab_size = OUTPUT_VOCAB_SIZE\n",
    "        self.decoder = FlaxBartDecoder(decoder_config, dtype=self.dtype, embed_tokens=self.decoder_embed)\n",
    "\n",
    "class CustomFlaxBartForConditionalGenerationModule(FlaxBartForConditionalGenerationModule):\n",
    "    def setup(self):\n",
    "        self.model = CustomFlaxBartModule(config=self.config, dtype=self.dtype)\n",
    "        self.lm_head = nn.Dense(\n",
    "            OUTPUT_VOCAB_SIZE,\n",
    "            use_bias=False,\n",
    "            dtype=self.dtype,\n",
    "            kernel_init=jax.nn.initializers.normal(self.config.init_std, self.dtype),\n",
    "        )\n",
    "        self.final_logits_bias = self.param(\"final_logits_bias\", self.bias_init, (1, OUTPUT_VOCAB_SIZE))\n",
    "\n",
    "class CustomFlaxBartForConditionalGeneration(FlaxBartForConditionalGeneration):\n",
    "    module_class = CustomFlaxBartForConditionalGenerationModule"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "879320b7-eaa0-4dc9-bbf2-c81efc53301d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import wandb\n",
    "run = wandb.init()\n",
    "artifact = run.use_artifact('wandb/hf-flax-dalle-mini/model-3h3x3565:latest', type='bart_model')\n",
    "artifact_dir = artifact.download()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8bcff33-e95b-4c01-b162-ee857a55c3e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# create our model\n",
    "tokenizer = BartTokenizer.from_pretrained(BASE_MODEL)\n",
    "model = CustomFlaxBartForConditionalGeneration.from_pretrained(artifact_dir)\n",
    "model.config.force_bos_token_to_be_generated = False\n",
    "model.config.forced_bos_token_id = None\n",
    "model.config.forced_eos_token_id = None\n",
    "\n",
    "# we verify that the shape has not been modified\n",
    "model.params['final_logits_bias'].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d5e0f14-2502-470e-9553-daee6748601f",
   "metadata": {},
   "outputs": [],
   "source": [
    "vqgan = VQModel.from_pretrained(\"flax-community/vqgan_f16_16384\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cca395a-93c2-49bc-a3be-98287e4403d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def custom_to_pil(x):\n",
    "    x = np.clip(x, 0., 1.)\n",
    "    x = (255*x).astype(np.uint8)\n",
    "    x = Image.fromarray(x)\n",
    "    if not x.mode == \"RGB\":\n",
    "        x = x.convert(\"RGB\")\n",
    "    return x\n",
    "\n",
    "def generate(input, rng, params):\n",
    "  return model.generate(\n",
    "      **input,\n",
    "      max_length=257,\n",
    "      num_beams=1,\n",
    "      do_sample=True,\n",
    "      prng_key=rng,\n",
    "      eos_token_id=50000,\n",
    "      pad_token_id=50000,\n",
    "      params=params\n",
    "  )\n",
    "\n",
    "def get_images(indices, params):\n",
    "    return vqgan.decode_code(indices, params=params)\n",
    "\n",
    "\n",
    "def plot_images(images):\n",
    "    fig = plt.figure(figsize=(40, 20))\n",
    "    columns = 4\n",
    "    rows = 2\n",
    "    plt.subplots_adjust(hspace=0, wspace=0)\n",
    "\n",
    "    for i in range(1, columns*rows +1):\n",
    "        fig.add_subplot(rows, columns, i)\n",
    "        plt.imshow(images[i-1])\n",
    "    plt.gca().axes.get_yaxis().set_visible(False)\n",
    "    plt.show()\n",
    "    \n",
    "def stack_reconstructions(images):\n",
    "    w, h = images[0].size[0], images[0].size[1]\n",
    "    img = Image.new(\"RGB\", (len(images)*w, h))\n",
    "    for i, img_ in enumerate(images):\n",
    "        img.paste(img_, (i*w,0))\n",
    "    return img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1bec3d2-ef17-4feb-aa0d-b51ed2fdcd3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "p_generate = jax.pmap(generate, \"batch\")\n",
    "p_get_images = jax.pmap(get_images, \"batch\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a539823a-a775-4d92-96a5-dc8b1eef69c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "bart_params = replicate(model.params)\n",
    "vqgan_params = replicate(vqgan.params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8b268d8-6992-422a-8373-95651474ae70",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompts = [\n",
    "    \"man in blue jacket walking on pathway in between trees during daytime\",\n",
    "    'white snow covered mountain under blue sky during daytime',\n",
    "    'white snow covered mountain under blue sky during night',\n",
    "    \"orange tabby cat on persons hand\",\n",
    "    \"aerial view of beach during daytime\",\n",
    "    \"chess pieces on chess board\",\n",
    "    \"laptop on brown wooden table\",\n",
    "    \"white bus on road near high rise buildings\",\n",
    "]\n",
    "\n",
    "\n",
    "prompt = [prompts[1]] * jax.device_count()\n",
    "inputs = tokenizer(prompt, return_tensors='jax', padding=\"max_length\", truncation=True, max_length=128).data\n",
    "inputs = shard(inputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68638cfa-9a4d-4e6a-8630-91aefb627bbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "for i in range(8):\n",
    "    key = random.randint(0, 1e7)\n",
    "    rng = jax.random.PRNGKey(key)\n",
    "    rngs = jax.random.split(rng, jax.local_device_count())\n",
    "    indices = p_generate(inputs, rngs, bart_params).sequences\n",
    "    indices = indices[:, :, 1:]\n",
    "\n",
    "    images = p_get_images(indices, vqgan_params)\n",
    "    images = np.squeeze(np.asarray(images), 1)\n",
    "    imges = [custom_to_pil(image) for image in images]\n",
    "\n",
    "    plt.figure(figsize=(40, 20))\n",
    "    plt.imshow(stack_reconstructions(imges))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6e1060f",
   "metadata": {},
   "source": [
    "## CLIP Scoring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c68724bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import CLIPProcessor, FlaxCLIPModel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17158e5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "clip = FlaxCLIPModel.from_pretrained(\"openai/clip-vit-base-patch32\")\n",
    "processor = CLIPProcessor.from_pretrained(\"openai/clip-vit-base-patch32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1b37b6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hallucinate(prompt, num_images=64):\n",
    "    prompt = [prompt] * jax.device_count()\n",
    "    inputs = tokenizer(prompt, return_tensors='jax', padding=\"max_length\", truncation=True, max_length=128).data\n",
    "    inputs = shard(inputs)\n",
    "\n",
    "    all_images = []\n",
    "    for i in range(num_images // jax.device_count()):\n",
    "        key = random.randint(0, 1e7)\n",
    "        rng = jax.random.PRNGKey(key)\n",
    "        rngs = jax.random.split(rng, jax.local_device_count())\n",
    "        indices = p_generate(inputs, rngs, bart_params).sequences\n",
    "        indices = indices[:, :, 1:]\n",
    "\n",
    "        images = p_get_images(indices, vqgan_params)\n",
    "        images = np.squeeze(np.asarray(images), 1)\n",
    "        for image in images:\n",
    "            all_images.append(custom_to_pil(image))\n",
    "    return all_images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "831c715f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def clip_top_k(prompt, images, k=8):\n",
    "    inputs = processor(text=prompt, images=images, return_tensors=\"np\", padding=True)\n",
    "    outputs = clip(**inputs)\n",
    "    logits = outputs.logits_per_text\n",
    "    scores = np.array(logits[0]).argsort()[-k:][::-1]\n",
    "    return [images[score] for score in scores]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00605e13",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"white snow covered mountain under blue sky during daytime\"\n",
    "images = hallucinate(prompt)\n",
    "selected = clip_top_k(prompt, images, k=8)\n",
    "stack_reconstructions(selected)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc745da2",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"aerial view of beach at night\"\n",
    "images = hallucinate(prompt)\n",
    "selected = clip_top_k(prompt, images, k=8)\n",
    "stack_reconstructions(selected)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9cc0b1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"an armchair in the shape of an avocado\"\n",
    "images = hallucinate(prompt)\n",
    "selected = clip_top_k(prompt, images, k=8)\n",
    "stack_reconstructions(selected)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "574e9433",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"young woman riding her bike into a forest\"\n",
    "images = hallucinate(prompt)\n",
    "selected = clip_top_k(prompt, images, k=8)\n",
    "stack_reconstructions(selected)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4762c91e",
   "metadata": {},
   "source": [
    "`Forest` seems to dominate. Interesting cubist interpretation in the fourth image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af30608a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}