File size: 10,848 Bytes
3b72cdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qJDJLE3v0HNr"
      },
      "source": [
        "# Fetch Codebase"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "cellView": "form",
        "id": "JqiWKjpFa0ov"
      },
      "outputs": [
        {
          "ename": "FileNotFoundError",
          "evalue": "[Errno 2] No such file or directory: '/content'",
          "output_type": "error",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
            "\u001b[1;32m/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb Cell 2'\u001b[0m in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      <a href='vscode-notebook-cell:/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb#ch0000001?line=0'>1</a>\u001b[0m \u001b[39m#@title\u001b[39;00m\n\u001b[1;32m      <a href='vscode-notebook-cell:/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb#ch0000001?line=1'>2</a>\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mos\u001b[39;00m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb#ch0000001?line=2'>3</a>\u001b[0m os\u001b[39m.\u001b[39;49mchdir(\u001b[39m'\u001b[39;49m\u001b[39m/content\u001b[39;49m\u001b[39m'\u001b[39;49m)\n\u001b[1;32m      <a href='vscode-notebook-cell:/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb#ch0000001?line=3'>4</a>\u001b[0m CODE_DIR \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39msefa\u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m      <a href='vscode-notebook-cell:/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb#ch0000001?line=4'>5</a>\u001b[0m \u001b[39m#!git clone https://github.com/genforce/sefa.git $CODE_DIR\u001b[39;00m\n",
            "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/content'"
          ]
        }
      ],
      "source": [
        "#@title\n",
        "import os\n",
        "CODE_DIR = 'sefa'\n",
        "#!git clone https://github.com/genforce/sefa.git $CODE_DIR\n",
        "os.chdir(f'..')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hQ_IXBZr8YcJ"
      },
      "source": [
        "# Define Utility Functions"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "cellView": "form",
        "id": "ijKTlG5GeTd3"
      },
      "outputs": [
        {
          "ename": "ModuleNotFoundError",
          "evalue": "No module named 'models'",
          "output_type": "error",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
            "\u001b[1;32m/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb Cell 4'\u001b[0m in \u001b[0;36m<cell line: 11>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      <a href='vscode-notebook-cell:/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb#ch0000003?line=6'>7</a>\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mPIL\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mImage\u001b[39;00m\n\u001b[1;32m      <a href='vscode-notebook-cell:/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb#ch0000003?line=8'>9</a>\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mtorch\u001b[39;00m\n\u001b[0;32m---> <a href='vscode-notebook-cell:/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb#ch0000003?line=10'>11</a>\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mmodels\u001b[39;00m \u001b[39mimport\u001b[39;00m parse_gan_type\n\u001b[1;32m     <a href='vscode-notebook-cell:/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb#ch0000003?line=11'>12</a>\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mutils\u001b[39;00m \u001b[39mimport\u001b[39;00m to_tensor\n\u001b[1;32m     <a href='vscode-notebook-cell:/home/johannes/Projects/Vision/sefa/docs/SeFa.ipynb#ch0000003?line=12'>13</a>\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mutils\u001b[39;00m \u001b[39mimport\u001b[39;00m postprocess\n",
            "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'models'"
          ]
        }
      ],
      "source": [
        "#@title\n",
        "import os.path\n",
        "import io\n",
        "import IPython.display\n",
        "import numpy as np\n",
        "import cv2\n",
        "import PIL.Image\n",
        "\n",
        "import torch\n",
        "\n",
        "from models import parse_gan_type\n",
        "from utils import to_tensor\n",
        "from utils import postprocess\n",
        "from utils import load_generator\n",
        "from utils import factorize_weight\n",
        "\n",
        "\n",
        "def sample(generator, gan_type, num=1, seed=0):\n",
        "  \"\"\"Samples latent codes.\"\"\"\n",
        "  torch.manual_seed(seed)\n",
        "  codes = torch.randn(num, generator.z_space_dim).cuda()\n",
        "  if gan_type == 'pggan':\n",
        "    codes = generator.layer0.pixel_norm(codes)\n",
        "  elif gan_type == 'stylegan':\n",
        "    codes = generator.mapping(codes)['w']\n",
        "    codes = generator.truncation(codes, trunc_psi=0.7, trunc_layers=8)\n",
        "  elif gan_type == 'stylegan2':\n",
        "    codes = generator.mapping(codes)['w']\n",
        "    codes = generator.truncation(codes, trunc_psi=0.5, trunc_layers=18)\n",
        "  codes = codes.detach().cpu().numpy()\n",
        "  return codes\n",
        "\n",
        "\n",
        "def synthesize(generator, gan_type, codes):\n",
        "  \"\"\"Synthesizes images with the give codes.\"\"\"\n",
        "  if gan_type == 'pggan':\n",
        "    images = generator(to_tensor(codes))['image']\n",
        "  elif gan_type in ['stylegan', 'stylegan2']:\n",
        "    images = generator.synthesis(to_tensor(codes))['image']\n",
        "  images = postprocess(images)\n",
        "  return images\n",
        "\n",
        "\n",
        "def imshow(images, col, viz_size=256):\n",
        "  \"\"\"Shows images in one figure.\"\"\"\n",
        "  num, height, width, channels = images.shape\n",
        "  assert num % col == 0\n",
        "  row = num // col\n",
        "\n",
        "  fused_image = np.zeros((viz_size * row, viz_size * col, channels), dtype=np.uint8)\n",
        "\n",
        "  for idx, image in enumerate(images):\n",
        "    i, j = divmod(idx, col)\n",
        "    y = i * viz_size\n",
        "    x = j * viz_size\n",
        "    if height != viz_size or width != viz_size:\n",
        "      image = cv2.resize(image, (viz_size, viz_size))\n",
        "    fused_image[y:y + viz_size, x:x + viz_size] = image\n",
        "\n",
        "  fused_image = np.asarray(fused_image, dtype=np.uint8)\n",
        "  data = io.BytesIO()\n",
        "  PIL.Image.fromarray(fused_image).save(data, 'jpeg')\n",
        "  im_data = data.getvalue()\n",
        "  disp = IPython.display.display(IPython.display.Image(im_data))\n",
        "  return disp"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Q7gkmrVW8eR1"
      },
      "source": [
        "# Select a Model"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "NoWI4fPQ6Gnf"
      },
      "outputs": [],
      "source": [
        "#@title { display-mode: \"form\", run: \"auto\" }\n",
        "model_name = \"stylegan_animeface512\" #@param ['stylegan_animeface512', 'stylegan_car512', 'stylegan_cat256', 'pggan_celebahq1024', 'stylegan_bedroom256']\n",
        "\n",
        "generator = load_generator(model_name)\n",
        "gan_type = parse_gan_type(generator)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zDStH1O5t1KC"
      },
      "source": [
        "# Sample Latent Codes"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "qlRGKZbJt9hA"
      },
      "outputs": [],
      "source": [
        "#@title { display-mode: \"form\", run: \"auto\" }\n",
        "\n",
        "num_samples = 3 #@param {type:\"slider\", min:1, max:8, step:1}\n",
        "noise_seed = 0 #@param {type:\"slider\", min:0, max:1000, step:1}\n",
        "\n",
        "codes = sample(generator, gan_type, num_samples, noise_seed)\n",
        "images = synthesize(generator, gan_type, codes)\n",
        "imshow(images, col=num_samples)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MmRPN3xz8jCH"
      },
      "source": [
        "# Factorize & Edit"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ccONBF60mVir"
      },
      "outputs": [],
      "source": [
        "#@title { display-mode: \"form\", run: \"auto\" }\n",
        "\n",
        "layer_idx = \"0-1\" #@param ['all', '0-1', '2-5', '6-13']\n",
        "semantic_1 = 0 #@param {type:\"slider\", min:-3.0, max:3.0, step:0.1}\n",
        "semantic_2 = 0 #@param {type:\"slider\", min:-3.0, max:3.0, step:0.1}\n",
        "semantic_3 = 0 #@param {type:\"slider\", min:-3.0, max:3.0, step:0.1}\n",
        "semantic_4 = 0 #@param {type:\"slider\", min:-3.0, max:3.0, step:0.1}\n",
        "semantic_5 = 0 #@param {type:\"slider\", min:-3.0, max:3.0, step:0.1}\n",
        "\n",
        "# Fast implementation to factorize the weight by SeFa.\n",
        "layers, boundaries, _ = factorize_weight(generator, layer_idx)\n",
        "\n",
        "new_codes = codes.copy()\n",
        "for sem_idx in range(5):\n",
        "  boundary = boundaries[sem_idx:sem_idx + 1]\n",
        "  step = eval(f'semantic_{sem_idx + 1}')\n",
        "  if gan_type == 'pggan':\n",
        "    new_codes += boundary * step\n",
        "  elif gan_type in ['stylegan', 'stylegan2']:\n",
        "    new_codes[:, layers, :] += boundary * step\n",
        "new_images = synthesize(generator, gan_type, new_codes)\n",
        "imshow(new_images, col=num_samples)"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "collapsed_sections": [],
      "name": "SeFa",
      "provenance": [],
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "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.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}