File size: 13,938 Bytes
3894c45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "     # CoMoGan\n",
    "Notebook to test our model after training."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-24T11:36:37.046598Z",
     "start_time": "2024-08-24T11:36:35.526168Z"
    }
   },
   "source": [
    "import ipywidgets as widgets\n",
    "import pytorch_lightning as pl\n",
    "import pathlib\n",
    "import torch\n",
    "import yaml\n",
    "import os\n",
    "\n",
    "from math import pi\n",
    "from PIL import Image\n",
    "from munch import Munch\n",
    "from threading import Timer\n",
    "from IPython.display import clear_output\n",
    "from torchvision.transforms import ToPILImage\n",
    "\n",
    "from data import create_dataset\n",
    "from torchvision.transforms import ToTensor\n",
    "from data.base_dataset import get_transform\n",
    "from networks import find_model_using_name, create_model"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load the model with a checkpoint\n",
    "Choose the directory that contains the checkpoint that you want."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import pathlib\n",
    "\n",
    "# Load names of directories inside /logs\n",
    "p = pathlib.Path('./logs')\n",
    "\n",
    "# Use x.name to get the directory name instead of splitting the path\n",
    "list_run_id = [x.name for x in p.iterdir() if x.is_dir()]\n",
    "\n",
    "import ipywidgets as widgets\n",
    "from IPython.display import display, clear_output\n",
    "import os\n",
    "\n",
    "w_run = widgets.Dropdown(options=list_run_id,\n",
    "                         description='Select RUN_ID',\n",
    "                         disabled=False,\n",
    "                         style=dict(description_width='initial'))\n",
    "\n",
    "\n",
    "w_check = None\n",
    "root_dir = None\n",
    "\n",
    "def on_value_change_check(change):\n",
    "    global w_check, w_run, root_dir\n",
    "    \n",
    "    clear_output(wait=True)\n",
    "    \n",
    "    root_dir = os.path.join('logs', w_run.value, 'tensorboard', 'default', 'version_0')\n",
    "    p = pathlib.Path(root_dir + '/checkpoints')\n",
    "    \n",
    "    # Load a list of checkpoints, use the last one by default\n",
    "    list_checkpoint = [x.name for x in p.iterdir() if 'iter' in x.name]\n",
    "    list_checkpoint.sort(reverse=True, key=lambda x: int(x.split('_')[1].split('.pth')[0]))\n",
    "    \n",
    "    w_check = widgets.Dropdown(options=list_checkpoint,\n",
    "                               description='Select checkpoint',\n",
    "                               disabled=False,\n",
    "                               style=dict(description_width='initial'))\n",
    "    display(widgets.HBox([w_run, w_check]))\n",
    "\n",
    "on_value_change_check({'new': w_run.value})\n",
    "w_run.observe(on_value_change_check, names='value')\n"
   ],
   "execution_count": 2,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-24T11:36:39.368141Z",
     "start_time": "2024-08-24T11:36:37.080571Z"
    }
   },
   "source": [
    "RUN_ID = w_run.value\n",
    "CHECKPOINT = w_check.value\n",
    "\n",
    "# Load parameters\n",
    "with open(os.path.join(root_dir, 'hparams.yaml')) as cfg_file:\n",
    "    opt = Munch(yaml.safe_load(cfg_file))\n",
    "\n",
    "opt.no_flip = True\n",
    "# Load parameters to the model, load the checkpoint\n",
    "model = create_model(opt)\n",
    "model = model.load_from_checkpoint((os.path.join(root_dir, 'checkpoints/', CHECKPOINT)))\n",
    "# Transfer the model to the GPU\n",
    "model.to('cpu');"
   ],
   "outputs": [],
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load the validation dataset"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-24T11:36:39.383167Z",
     "start_time": "2024-08-24T11:36:39.370142Z"
    }
   },
   "source": [
    "import pathlib\n",
    "from PIL import Image\n",
    "\n",
    "# Set opt.dataroot to the imgs_test directory\n",
    "opt.dataroot = 'imgs_test/'\n",
    "\n",
    "# Load paths of all files contained in /imgs_test\n",
    "p = pathlib.Path(opt.dataroot)\n",
    "dataset_paths = [str(x.relative_to(opt.dataroot)) for x in p.iterdir()]\n",
    "dataset_paths.sort()\n",
    "\n",
    "sequence_name = {}\n",
    "# Make a dict with each sequence name as a key and\n",
    "# a list of paths to the images of the sequence as a value\n",
    "for file in dataset_paths:\n",
    "    # Keep only the sequence part contained in the name of the image\n",
    "    strip_name = file.split('_')[0]\n",
    "    \n",
    "    if strip_name not in sequence_name:\n",
    "        sequence_name[strip_name] = [file]\n",
    "    else:\n",
    "        sequence_name[strip_name].append(file)\n"
   ],
   "outputs": [],
   "execution_count": 4
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Select a sequence, an image and the moment of the day\n",
    "Select the sequence on which you want to work before choosing which image should be used.<br>\n",
    "Select the moment of the day, by choosing the angle of the sun $\\phi$ between [0,2$\\pi$],\n",
    "which maps to a sun elevation ∈ [+30◦,−40◦]\n",
    "<ul>\n",
    "<li>0 means day</li>\n",
    "<li>$\\pi$/2 means dusk</li>\n",
    "<li>$\\pi$ means night</li>\n",
    "<li>$\\pi$ + $\\pi$/2 means dawn</li>\n",
    "<li>2$\\pi$ means day (again)</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "scrolled": true,
    "ExecuteTime": {
     "end_time": "2024-08-24T11:40:52.233134Z",
     "start_time": "2024-08-24T11:40:45.511403Z"
    }
   },
   "source": [
    "def drop_list():\n",
    "    # Select the sequence on which you want to make your test\n",
    "    return widgets.Dropdown(options=sequence_name.keys(),\n",
    "                         description='Select sequence',\n",
    "                         disabled=False,\n",
    "                         style=dict(description_width='initial'))\n",
    "def slider_img():\n",
    "    # Select an image from the sequence\n",
    "    return widgets.IntSlider(min=0, max=len(sequence_name[w_seq.value]) - 1,\n",
    "                           description='Select image')\n",
    "def slider_time():\n",
    "    # Select time\n",
    "    return widgets.FloatSlider(value=0, min=0, max=pi*2, step=0.01,\n",
    "                             description='Select time',\n",
    "                             readout_format='.2f')\n",
    "\n",
    "def debounce(wait):\n",
    "    # Decorator that will debounce the call to a function\n",
    "    def decorator(fn):\n",
    "        timer = None\n",
    "        def debounced(*args, **kwargs):\n",
    "            nonlocal timer\n",
    "            def call_it():\n",
    "                fn(*args, **kwargs)\n",
    "            if timer is not None:\n",
    "                timer.cancel()\n",
    "            timer = Timer(wait, call_it)\n",
    "            timer.start()\n",
    "        return debounced\n",
    "    return decorator\n",
    "    \n",
    "def inference(seq, index_img, phi, output = True):\n",
    "    global sequence_name, w_img_time, w_seq, opt, out\n",
    "    # Load the image\n",
    "    A_path = os.path.join(opt.dataroot, sequence_name[seq.value][index_img])\n",
    "    A_img = Image.open(A_path).convert('RGB')\n",
    "    # Apply image transformation\n",
    "    A = get_transform(opt, convert=False)(A_img)\n",
    "    # Normalization between -1 and 1\n",
    "    img_real = (((ToTensor()(A)) * 2) - 1).unsqueeze(0)\n",
    "    # Forward our image into the model with the specified ɸ\n",
    "    img_fake = model.forward(img_real.cpu(), phi.cpu()) \n",
    "    # Encapsulate the initial image beside our result\n",
    "    new_im = Image.new('RGB', (A_img.width * 2, A_img.height))\n",
    "    new_im.paste(A_img, (0, 0))\n",
    "    new_im.paste(ToPILImage()((img_fake[0].cpu() + 1) / 2), (A_img.width, 0))\n",
    "    # Clear the output and display the widgets and the images\n",
    "    if output:\n",
    "        out.clear_output(wait=True)\n",
    "        with out:\n",
    "            # Resize the output\n",
    "            O_img = new_im.resize((new_im.width // 2, new_im.height // 2))\n",
    "            display(w_img_time, O_img)\n",
    "        display(out)\n",
    "    \n",
    "    return new_im\n",
    "\n",
    "@debounce(0.2)\n",
    "def on_value_change_img(change):\n",
    "    global w_seq, w_time\n",
    "    inference(w_seq, change['new'], torch.tensor(w_time.value))\n",
    "    \n",
    "@debounce(0.2)\n",
    "def on_value_change_time(change):\n",
    "    global w_seq, w_img\n",
    "    inference(w_seq, w_img.value, torch.tensor(change['new']))\n",
    "    \n",
    "def on_value_change_seq(change):\n",
    "    global w_seq, w_img, w_time\n",
    "    w_img = slider_img()\n",
    "    w_time = slider_time()\n",
    "    inference(w_seq, w_img.value, torch.tensor(w_time.value))\n",
    "    \n",
    "w_seq = drop_list()\n",
    "w_img = slider_img()\n",
    "w_time = slider_time()\n",
    "w_img_time = widgets.VBox([w_seq, widgets.HBox([w_img, w_time])])\n",
    "# Set the size of the output cell\n",
    "out = widgets.Output(layout=widgets.Layout(width='auto', height='240px'))\n",
    "\n",
    "inference(w_seq, w_img.value, torch.tensor(w_time.value))\n",
    "w_img.observe(on_value_change_img, names='value')\n",
    "w_time.observe(on_value_change_time, names='value')\n",
    "w_seq.observe(on_value_change_seq, names='value')"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Output(layout=Layout(height='240px', width='auto'))"
      ],
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "665fdbd928f148d19f3e341ab0993ab7"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 7
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sequence to video\n",
    "The code below translates a sequence of images into a video.<br>\n",
    "By default, the 'Select time' slider is on 'Variable phi' ($\\phi$), in this case the time parameter will progressively increase from 0 to 2$\\pi$.<br>\n",
    "2$\\pi$ is reached at the end of the video. If you move the slider, you can select a fixed $\\phi$.<br>\n",
    "Require to install two more packages <code>pip install imageio imageio-ffmpeg<code>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import Video\n",
    "import numpy as np\n",
    "import imageio\n",
    "\n",
    "w_button = widgets.Button(description='Start', tooltip='Start the inference of a sequence',\n",
    "                          icon='play')\n",
    "\n",
    "phi_opt = ['Variable phi'] + [str(round(f, 2)) for f in np.arange(0, pi*2, 0.01)]\n",
    "w_vid_time = widgets.SelectionSlider(options=phi_opt, value=phi_opt[0], description='Select time : ')\n",
    "\n",
    "w_vid_seq = drop_list()\n",
    "\n",
    "w_button_seq = widgets.VBox([widgets.HBox([w_vid_seq, w_vid_time]), w_button])\n",
    "display(w_button_seq)\n",
    "\n",
    "def get_video(bt):\n",
    "    global sequence_name, w_vid_seq, w_vid_time, w_button_seq\n",
    "    \n",
    "    clear_output(wait=True)\n",
    "    display(w_button_seq)\n",
    "    seq_size = len(sequence_name[w_vid_seq.value])\n",
    "    # Display progress bar\n",
    "    w_prog = widgets.IntProgress(value=0, min=0, max=seq_size, description='Loading:')\n",
    "    display(w_prog)\n",
    "    # Create a videos directory to save our video\n",
    "    save_name = str(pathlib.Path('.').absolute()) + '/videos/'\n",
    "    os.makedirs(save_name, exist_ok=True)\n",
    "    # If variable phi\n",
    "    if w_vid_time.value == 'Variable phi':\n",
    "        # Write our video in the project folder\n",
    "        save_name += 'comogan_{}_phi_{}.mp4'.format(w_vid_seq.value.replace('segment-', 'seg_'),\n",
    "                                                   'variable')\n",
    "    else:\n",
    "        save_name += 'comogan_{}_phi_{}.mp4'.format(w_vid_seq.value.replace('segment-', 'seg_'),\n",
    "                                                   w_vid_time.value.replace('.', '_'))\n",
    "    writer = imageio.get_writer(save_name, fps=10)\n",
    "    # Loop on the images contained in the sequence\n",
    "    for i in range(seq_size):\n",
    "        if w_vid_time.value == 'Variable phi':\n",
    "            # Inference of the i image in sequence_name[key]\n",
    "            phi_var = 2*pi/seq_size * i\n",
    "            my_img = inference(w_vid_seq, i, torch.tensor(phi_var), False)\n",
    "        else:\n",
    "            my_img = inference(w_vid_seq, i, torch.tensor(float(w_vid_time.value)), False)\n",
    "        writer.append_data(np.array(my_img))\n",
    "        # Progress bar\n",
    "        w_prog.value += 1\n",
    "    \n",
    "    writer.close()\n",
    "    display(Video(save_name, embed=True))\n",
    "\n",
    "w_button.on_click(get_video)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}