test_mono_view.ipynb 11.6 KB
Newer Older
Nianchen Deng's avatar
Nianchen Deng committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Set CUDA:2 as current device.\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import torch\n",
    "import matplotlib.pyplot as plt\n",
    "import torchvision.transforms.functional as trans_f\n",
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
23
    "sys.path.append(os.path.abspath(sys.path[0] + '/../'))\n",
Nianchen Deng's avatar
Nianchen Deng committed
24
25
26
    "torch.cuda.set_device(2)\n",
    "print(\"Set CUDA:%d as current device.\" % torch.cuda.current_device())\n",
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
27
28
29
30
31
32
33
34
35
    "from data.spherical_view_syn import *\n",
    "from configs.spherical_view_syn import SphericalViewSynConfig\n",
    "from utils import netio\n",
    "from utils import misc\n",
    "from utils import img\n",
    "from utils import device\n",
    "from utils import view\n",
    "from components.foveation import Foveation\n",
    "from components.gen_final import GenFinal\n",
Nianchen Deng's avatar
Nianchen Deng committed
36
37
38
39
40
41
42
    "\n",
    "\n",
    "def load_net(path):\n",
    "    config = SphericalViewSynConfig()\n",
    "    config.from_id(path[:-4])\n",
    "    config.SAMPLE_PARAMS['perturb_sample'] = False\n",
    "    config.print()\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
43
44
    "    net = config.create_net().to(device.default())\n",
    "    netio.load(path, net)\n",
Nianchen Deng's avatar
Nianchen Deng committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    "    return net\n",
    "\n",
    "\n",
    "def find_file(prefix):\n",
    "    for path in os.listdir():\n",
    "        if path.startswith(prefix):\n",
    "            return path\n",
    "    return None\n",
    "\n",
    "\n",
    "def load_views(data_desc_file) -> view.Trans:\n",
    "    with open(data_desc_file, 'r', encoding='utf-8') as file:\n",
    "        data_desc = json.loads(file.read())\n",
    "        samples = data_desc['samples'] if 'samples' in data_desc else [-1]\n",
    "        view_centers = torch.tensor(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
60
    "            data_desc['view_centers'], device=device.default()).view(samples + [3])\n",
Nianchen Deng's avatar
Nianchen Deng committed
61
    "        view_rots = torch.tensor(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
62
    "            data_desc['view_rots'], device=device.default()).view(samples + [3, 3])\n",
Nianchen Deng's avatar
Nianchen Deng committed
63
64
65
66
67
68
    "        return view.Trans(view_centers, view_rots)\n",
    "\n",
    "\n",
    "def read_ref_images(idx):\n",
    "    patt = 'ref/view_%04d.png'\n",
    "    if isinstance(idx, torch.Tensor) and len(idx.size()) > 0:\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
69
    "        return img.load([patt % i for i in idx])\n",
Nianchen Deng's avatar
Nianchen Deng committed
70
    "    else:\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
71
    "        return img.load(patt % idx)\n",
Nianchen Deng's avatar
Nianchen Deng committed
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
    "\n",
    "\n",
    "def adjust_cam(cam, vr_cam, gaze_center):\n",
    "    fovea_offset = (\n",
    "        (gaze_center[0]) / vr_cam.f[0].item() * cam.f[0].item(),\n",
    "        (gaze_center[1]) / vr_cam.f[1].item() * cam.f[1].item()\n",
    "    )\n",
    "    cam.c[0] = cam.res[1] / 2 - fovea_offset[0]\n",
    "    cam.c[1] = cam.res[0] / 2 - fovea_offset[1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Change working directory to  /home/dengnc/deep_view_syn/data/__0_user_study/us_gas_all_in_one\n",
      "==== Config fovea ====\n",
      "Net type:  nmsl\n",
      "Encode dim:  10\n",
      "Optimizer decay:  0\n",
      "Normalize:  False\n",
      "Direction as input:  False\n",
      "Full-connected network parameters: {'nf': 128, 'n_layers': 4, 'skips': []}\n",
      "Sample parameters {'spherical': True, 'depth_range': (1.0, 50.0), 'n_samples': 32, 'perturb_sample': False, 'lindisp': True, 'inverse_r': True}\n",
      "==========================\n",
      "Load net from fovea@nmsl-rgb_e10_fc128x4_d1-50_s32.pth ...\n",
      "==== Config periph ====\n",
      "Net type:  nnmsl\n",
      "Encode dim:  10\n",
      "Optimizer decay:  0\n",
      "Normalize:  False\n",
      "Direction as input:  False\n",
      "Full-connected network parameters: {'nf': 64, 'n_layers': 4, 'skips': []}\n",
      "Sample parameters {'spherical': True, 'depth_range': (1.0, 50.0), 'n_samples': 16, 'perturb_sample': False, 'lindisp': True, 'inverse_r': True}\n",
      "==========================\n",
      "Load net from periph@nnmsl-rgb_e10_fc64x4_d1-50_s16.pth ...\n",
      "Dataset loaded.\n",
      "views: [5, 5, 5, 5, 5]\n"
     ]
    }
   ],
   "source": [
    "os.chdir(sys.path[0] + '/../data/__0_user_study/us_gas_all_in_one')\n",
    "#os.chdir(sys.path[0] + '/../data/__0_user_study/us_mc_all_in_one')\n",
    "#os.chdir(sys.path[0] + '/../data/bedroom_all_in_one')\n",
    "print('Change working directory to ', os.getcwd())\n",
    "torch.autograd.set_grad_enabled(False)\n",
    "\n",
    "fovea_net = load_net(find_file('fovea'))\n",
    "periph_net = load_net(find_file('periph'))\n",
    "\n",
    "# Load Dataset\n",
    "views = load_views('views.json')\n",
    "#ref_dataset = SphericalViewSynDataset('ref.json', load_images=False, calculate_rays=False)\n",
    "print('Dataset loaded.')\n",
    "\n",
    "print('views:', views.size())\n",
    "#print('ref views:', ref_dataset.samples)\n",
    "\n",
    "fov_list = [20, 45, 110]\n",
    "res_list = [(128, 128), (256, 256), (256, 230)]  # (192,256)]\n",
    "res_full = (1600, 1440)\n",
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
140
    "gen = GenFinal(fov_list, res_list, res_full, fovea_net, periph_net, device.default())"
Nianchen Deng's avatar
Nianchen Deng committed
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
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ==gas==\n",
    "set_id = 0\n",
    "left_center = (-137, 64)\n",
    "right_center = (-142, 64)\n",
    "set_id = 1\n",
    "left_center = (133, -44)\n",
    "right_center = (130, -44)\n",
    "set_id = 2\n",
    "left_center = (-20, -5)\n",
    "right_center = (-25, -5)\n",
    "# ==mc==\n",
    "#set_id = 3\n",
    "#left_center = (-107, 80)\n",
    "#right_center = (-112, 80)\n",
    "#set_id = 4\n",
    "#left_center = (-17, -90)\n",
    "#right_center = (-22, -90)\n",
    "#set_id = 5\n",
    "#left_center = (95, 30)\n",
    "#right_center = (91, 30)\n",
    "\n",
    "view_coord = [0, 0, 0, 0, 0]\n",
    "for i, val in enumerate(views.size()):\n",
    "    view_coord[i] += val // 2\n",
    "print('view_coord:', view_coord)\n",
    "test_view = views.get(*view_coord)\n",
    "\n",
    "cams = [\n",
    "    view.CameraParam({\n",
    "        \"fov\": fov_list[i],\n",
    "        \"cx\": 0.5,\n",
    "        \"cy\": 0.5,\n",
    "        \"normalized\": True\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
182
    "    }, res_list[i]).to(device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
183
184
185
186
187
188
189
190
191
192
    "    for i in range(len(fov_list))\n",
    "]\n",
    "fovea_cam, mid_cam, periph_cam = cams[0], cams[1], cams[2]\n",
    "#guide_cam = ref_dataset.cam_params\n",
    "vr_cam = view.CameraParam({\n",
    "    'fov': fov_list[-1],\n",
    "    'cx': 0.5,\n",
    "    'cy': 0.5,\n",
    "    'normalized': True\n",
    "}, res_full)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
193
    "foveation = Foveation(fov_list, res_full, device=device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
194
195
196
197
198
199
    "\n",
    "\n",
    "def plot_figures(left_images, right_images, left_center, right_center):\n",
    "    # Plot Fovea raw\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
200
    "    img.plot(left_images['fovea_raw'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
201
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
202
    "    img.plot(right_images['fovea_raw'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
203
204
205
206
    "\n",
    "    # Plot Fovea\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
207
    "    img.plot(left_images['fovea'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
208
209
210
211
212
213
214
    "    plt.plot([(fovea_cam.res[1] - 1) / 2 - 5, (fovea_cam.res[1] - 1) / 2 + 5],\n",
    "             [(fovea_cam.res[0] - 1) / 2, (fovea_cam.res[0] - 1) / 2],\n",
    "             color=[0, 1, 0])\n",
    "    plt.plot([(fovea_cam.res[1] - 1) / 2, (fovea_cam.res[1] - 1) / 2],\n",
    "             [(fovea_cam.res[0] - 1) / 2 - 5, (fovea_cam.res[0] - 1) / 2 + 5],\n",
    "             color=[0, 1, 0])\n",
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
215
    "    img.plot(right_images['fovea'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
216
217
218
219
220
221
222
223
    "    plt.plot([(fovea_cam.res[1] - 1) / 2 - 5, (fovea_cam.res[1] - 1) / 2 + 5],\n",
    "             [(fovea_cam.res[0] - 1) / 2, (fovea_cam.res[0] - 1) / 2],\n",
    "             color=[0, 1, 0])\n",
    "    plt.plot([(fovea_cam.res[1] - 1) / 2, (fovea_cam.res[1] - 1) / 2],\n",
    "             [(fovea_cam.res[0] - 1) / 2 - 5, (fovea_cam.res[0] - 1) / 2 + 5],\n",
    "             color=[0, 1, 0])\n",
    "\n",
    "    #plt.subplot(1, 4, 2)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
224
    "    # img.plot(fovea_refined)\n",
Nianchen Deng's avatar
Nianchen Deng committed
225
226
227
228
    "\n",
    "    # Plot Mid\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
229
    "    img.plot(left_images['mid'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
230
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
231
    "    img.plot(right_images['mid'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
232
233
234
235
    "\n",
    "    # Plot Periph\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
236
    "    img.plot(left_images['periph'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
237
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
238
    "    img.plot(right_images['periph'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
239
240
241
242
    "\n",
    "    # Plot Blended\n",
    "    plt.figure(figsize=(12, 6))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
243
    "    img.plot(left_images['blended'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
244
245
246
247
248
249
250
251
252
    "    plt.plot([(res_full[1] - 1) / 2 + left_center[0] - 5, (res_full[1] - 1) / 2 + left_center[0] + 5],\n",
    "             [(res_full[0] - 1) / 2 + left_center[1],\n",
    "              (res_full[0] - 1) / 2 + left_center[1]],\n",
    "             color=[0, 1, 0])\n",
    "    plt.plot([(res_full[1] - 1) / 2 + left_center[0], (res_full[1] - 1) / 2 + left_center[0]],\n",
    "             [(res_full[0] - 1) / 2 + left_center[1] - 5,\n",
    "              (res_full[0] - 1) / 2 + left_center[1] + 5],\n",
    "             color=[0, 1, 0])\n",
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
253
    "    img.plot(right_images['blended'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
    "    plt.plot([(res_full[1] - 1) / 2 + right_center[0] - 5, (res_full[1] - 1) / 2 + right_center[0] + 5],\n",
    "             [(res_full[0] - 1) / 2 + right_center[1],\n",
    "              (res_full[0] - 1) / 2 + right_center[1]],\n",
    "             color=[0, 1, 0])\n",
    "    plt.plot([(res_full[1] - 1) / 2 + right_center[0], (res_full[1] - 1) / 2 + right_center[0]],\n",
    "             [(res_full[0] - 1) / 2 + right_center[1] - 5,\n",
    "              (res_full[0] - 1) / 2 + right_center[1] + 5],\n",
    "             color=[0, 1, 0])\n",
    "\n",
    "\n",
    "left_images = gen(\n",
    "    left_center,\n",
    "    view.Trans(\n",
    "        test_view.trans_point(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
268
    "            torch.tensor([-0.03, 0, 0], device=device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
269
270
271
272
273
274
275
276
277
278
    "        ),\n",
    "        test_view.r\n",
    "    ),\n",
    "    ret_raw=True,\n",
    "    mono_trans=test_view,\n",
    "    shift=0)\n",
    "right_images = gen(\n",
    "    right_center,\n",
    "    view.Trans(\n",
    "        test_view.trans_point(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
279
    "            torch.tensor([0.03, 0, 0], device=device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
280
281
282
283
284
285
286
287
288
    "        ),\n",
    "        test_view.r\n",
    "    ),\n",
    "    ret_raw=True,\n",
    "    mono_trans=test_view,\n",
    "    shift=0)\n",
    "\n",
    "plot_figures(left_images, right_images, left_center, right_center)\n",
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
289
    "misc.create_dir('output/mono_test')\n",
Nianchen Deng's avatar
Nianchen Deng committed
290
    "for key in left_images:\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
291
    "    img.save(\n",
Nianchen Deng's avatar
Nianchen Deng committed
292
293
    "        left_images[key], 'output/mono_test/set%d_%s_l.png' % (set_id, key))\n",
    "for key in right_images:\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
294
    "    img.save(\n",
Nianchen Deng's avatar
Nianchen Deng committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
    "        right_images[key], 'output/mono_test/set%d_%s_r.png' % (set_id, key))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.9 64-bit ('pytorch': conda)",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
309
   "name": "python379jvsc74a57bd0660ca2a75467d3af74a68fcc6f40bc78ab96b99ff17d2f100b5ca821fbb183f2"
Nianchen Deng's avatar
Nianchen Deng committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  },
  "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.9"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}