gen_demo_stereo.ipynb 9.78 KB
Newer Older
Nianchen Deng's avatar
Nianchen Deng committed
1
2
3
4
{
 "cells": [
  {
   "cell_type": "code",
Nianchen Deng's avatar
Nianchen Deng committed
5
   "execution_count": 2,
Nianchen Deng's avatar
Nianchen Deng committed
6
   "metadata": {},
Nianchen Deng's avatar
sync    
Nianchen Deng committed
7
8
9
10
11
12
13
14
15
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Set CUDA:0 as current device.\n"
     ]
    }
   ],
Nianchen Deng's avatar
Nianchen Deng committed
16
17
18
19
   "source": [
    "import sys\n",
    "import os\n",
    "import torch\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
20
    "import torch.nn as nn\n",
Nianchen Deng's avatar
Nianchen Deng committed
21
22
    "import matplotlib.pyplot as plt\n",
    "\n",
23
24
    "rootdir = os.path.abspath(sys.path[0] + '/../')\n",
    "sys.path.append(rootdir)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
25
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
26
    "torch.cuda.set_device(0)\n",
Nianchen Deng's avatar
Nianchen Deng committed
27
28
29
    "print(\"Set CUDA:%d as current device.\" % torch.cuda.current_device())\n",
    "torch.autograd.set_grad_enabled(False)\n",
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
30
31
32
33
34
    "from data.spherical_view_syn import *\n",
    "from configs.spherical_view_syn import SphericalViewSynConfig\n",
    "from utils import netio\n",
    "from utils import img\n",
    "from utils import device\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
35
    "from utils.view import *\n",
36
    "from components.fnr import FoveatedNeuralRenderer\n",
Nianchen Deng's avatar
Nianchen Deng committed
37
38
39
40
    "\n",
    "\n",
    "def load_net(path):\n",
    "    config = SphericalViewSynConfig()\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
41
    "    config.from_id(os.path.splitext(path)[0])\n",
Nianchen Deng's avatar
Nianchen Deng committed
42
    "    config.sa['perturb_sample'] = False\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
    "    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",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
55
    "def load_views(data_desc_file) -> Trans:\n",
Nianchen Deng's avatar
Nianchen Deng committed
56
57
58
    "    with open(data_desc_file, 'r', encoding='utf-8') as file:\n",
    "        data_desc = json.loads(file.read())\n",
    "        view_centers = torch.tensor(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
59
    "            data_desc['view_centers'], device=device.default()).view(-1, 3)\n",
Nianchen Deng's avatar
Nianchen Deng committed
60
    "        view_rots = torch.tensor(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
61
    "            data_desc['view_rots'], device=device.default()).view(-1, 3, 3)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
62
    "        return Trans(view_centers, view_rots)\n",
Nianchen Deng's avatar
Nianchen Deng committed
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
    "\n",
    "\n",
    "def plot_cross(center, res):\n",
    "    plt.plot(\n",
    "        [\n",
    "            (res[1] - 1) / 2 + center[0] - 5,\n",
    "            (res[1] - 1) / 2 + center[0] + 5\n",
    "        ],\n",
    "        [\n",
    "            (res[0] - 1) / 2 + center[1],\n",
    "            (res[0] - 1) / 2 + center[1]\n",
    "        ],\n",
    "        color=[0, 1, 0])\n",
    "    plt.plot(\n",
    "        [\n",
    "            (res[1] - 1) / 2 + center[0],\n",
    "            (res[1] - 1) / 2 + center[0]\n",
    "        ],\n",
    "        [\n",
    "            (res[0] - 1) / 2 + center[1] - 5,\n",
    "            (res[0] - 1) / 2 + center[1] + 5\n",
    "        ],\n",
    "        color=[0, 1, 0])\n",
    "\n",
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
88
89
90
    "def plot_figures(left_images, right_images, left_center, right_center):\n",
    "    # Plot Fovea\n",
    "    plt.figure(figsize=(12, 6))\n",
Nianchen Deng's avatar
Nianchen Deng committed
91
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
92
93
    "    img.plot(left_images['layers_img'][0])\n",
    "    fovea_res = left_images['layers_img'][0].size()[-2:]\n",
Nianchen Deng's avatar
Nianchen Deng committed
94
95
    "    plot_cross((0, 0), fovea_res)\n",
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
96
    "    img.plot(right_images['layers_img'][0])\n",
Nianchen Deng's avatar
Nianchen Deng committed
97
98
    "    plot_cross((0, 0), fovea_res)\n",
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    "    # Plot Mid\n",
    "    plt.figure(figsize=(12, 6))\n",
    "    plt.subplot(121)\n",
    "    img.plot(left_images['layers_img'][1])\n",
    "    plt.subplot(122)\n",
    "    img.plot(right_images['layers_img'][1])\n",
    "\n",
    "    # Plot Periph\n",
    "    plt.figure(figsize=(12, 6))\n",
    "    plt.subplot(121)\n",
    "    img.plot(left_images['layers_img'][2])\n",
    "    plt.subplot(122)\n",
    "    img.plot(right_images['layers_img'][2])\n",
    "\n",
    "    # Plot Blended\n",
    "    plt.figure(figsize=(12, 6))\n",
    "    plt.subplot(121)\n",
    "    img.plot(left_images['blended'])\n",
    "    full_res = left_images['blended'].size()[-2:]\n",
    "    plot_cross(left_center, full_res)\n",
    "    plt.subplot(122)\n",
    "    img.plot(right_images['blended'])\n",
    "    plot_cross(right_center, full_res)\n",
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
123
124
    "\n",
    "scenes = {\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
125
126
127
128
    "    'classroom': 'classroom_all',\n",
    "    'stones': 'stones_all',\n",
    "    'barbershop': 'barbershop_all',\n",
    "    'lobby': 'lobby_all'\n",
Nianchen Deng's avatar
Nianchen Deng committed
129
130
    "}\n",
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
131
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
132
    "fov_list = [20, 45, 110]\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
133
    "res_list = [(256, 256), (256, 256), (256, 230)]\n",
Nianchen Deng's avatar
Nianchen Deng committed
134
135
136
137
138
    "res_full = (1600, 1440)\n"
   ]
  },
  {
   "cell_type": "code",
Nianchen Deng's avatar
Nianchen Deng committed
139
   "execution_count": 15,
Nianchen Deng's avatar
Nianchen Deng committed
140
   "metadata": {},
Nianchen Deng's avatar
sync    
Nianchen Deng committed
141
142
143
144
145
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Nianchen Deng's avatar
Nianchen Deng committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
      "Change working directory to  /home/dengnc/dvs/data/__new/barbershop_all\n",
      "Load net from fovea200@snerffast4-rgb_e6_fc512x4_d1.20-6.00_s64_~p.pth ...\n",
      "Load net from periph200@snerffast2-rgb_e6_fc256x4_d1.20-6.00_s32_~p.pth ...\n",
      "barbershop 0 Saved\n",
      "barbershop 1 Saved\n",
      "barbershop 2 Saved\n",
      "barbershop 3 Saved\n",
      "barbershop 4 Saved\n",
      "barbershop 0 Saved\n",
      "barbershop 1 Saved\n",
      "barbershop 2 Saved\n",
      "barbershop 3 Saved\n",
      "barbershop 4 Saved\n",
      "barbershop 0 Saved\n",
      "barbershop 1 Saved\n",
      "barbershop 2 Saved\n",
      "barbershop 3 Saved\n",
      "barbershop 4 Saved\n",
      "barbershop 0 Saved\n",
      "barbershop 1 Saved\n",
      "barbershop 2 Saved\n",
      "barbershop 3 Saved\n",
      "barbershop 4 Saved\n"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
169
170
171
     ]
    }
   ],
Nianchen Deng's avatar
Nianchen Deng committed
172
   "source": [
Nianchen Deng's avatar
sync    
Nianchen Deng committed
173
174
    "params = {\n",
    "    'classroom': [\n",
Nianchen Deng's avatar
Nianchen Deng committed
175
    "        [(0, 0, 0, 0, 0), (22, -83), (16, -83)],\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
176
    "        [(0, 0, 0, 0, 0), (60, 55), (55, 55)],\n",
Nianchen Deng's avatar
Nianchen Deng committed
177
    "    ],\n",
Nianchen Deng's avatar
Nianchen Deng committed
178
179
180
181
182
183
184
185
186
187
188
189
190
    "    'lobby': [\n",
    "        [(0, 0, 0, 0, 0), (114, 0), (110, 0)],\n",
    "        [(0, 0, 0, 0, 0), (21, 150), (12, 150)]\n",
    "    ],\n",
    "    'barbershop': [\n",
    "        #[(0, 0, 0, 0, 0), (106, -67), (90, -67)],\n",
    "        #[(0, 0, 0, 0, 0), (-114, 10), (-126, 10)],\n",
    "        [(0, 0, 0, 25, 20), (189, -45), (173, -45)],\n",
    "        [(0, 0, 0, 25, 20), (-148, 130), (-163, 130)],\n",
    "        [(0.15, 0.15, 0, 43, 2), (9, 0), (-9, 0)],\n",
    "        [(0.15, 0, 0.15, -13, -5), (6, 0), (-6, 0)],\n",
    "        [(-0.15, 0.15, 0.15, -53, -21), (3, 0), (-3, 0)]\n",
    "    ]\n",
Nianchen Deng's avatar
Nianchen Deng committed
191
    "}\n",
Nianchen Deng's avatar
Nianchen Deng committed
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
    "\n",
    "#for scene in ['classroom', 'lobby', 'barbershop']:\n",
    "for scene in ['barbershop']:\n",
    "    os.chdir(f'{rootdir}/data/__new/{scenes[scene]}')\n",
    "    print('Change working directory to ', os.getcwd())\n",
    "\n",
    "    fovea_net = load_net(find_file('fovea'))\n",
    "    periph_net = load_net(find_file('periph'))\n",
    "    renderer = FoveatedNeuralRenderer(fov_list, res_list,\n",
    "                                    nn.ModuleList([fovea_net, periph_net, periph_net]),\n",
    "                                    res_full, device=device.default())\n",
    "\n",
    "    for mono_periph in range(0,4):\n",
    "        for i, param in enumerate(params[scene]):\n",
    "            view = Trans(torch.tensor(param[0][:3], device=device.default()),\n",
    "                        torch.tensor(euler_to_matrix([-param[0][4], param[0][3], 0]),\n",
    "                                    device=device.default()).view(3, 3))\n",
    "            eye_offset = torch.tensor([0.03, 0, 0], device=device.default())\n",
    "            left_view = Trans(view.trans_point(-eye_offset), view.r)\n",
    "            right_view = Trans(view.trans_point(eye_offset), view.r)\n",
    "            left_images, right_images = renderer(view, param[1], param[2],\n",
    "                                                stereo_disparity=0.06,\n",
    "                                                using_mask=True,\n",
    "                                                mono_periph_mode=mono_periph,\n",
    "                                                ret_raw=False)\n",
    "            if True:\n",
    "                outputdir = '../__demo/stereo_m%d' % mono_periph if mono_periph else '../__demo/stereo'\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
219
    "                os.makedirs(outputdir, exist_ok=True)\n",
Nianchen Deng's avatar
Nianchen Deng committed
220
221
222
223
224
225
226
227
228
229
230
    "                img.save(torch.cat([\n",
    "                    left_images['blended'],\n",
    "                    right_images['blended']\n",
    "                ], dim=-1), '%s/%s_%d.png' % (outputdir, scene, i))\n",
    "                img.save(left_images['blended'], '%s/%s_%d_l.png' % (outputdir, scene, i))\n",
    "                img.save(right_images['blended'], '%s/%s_%d_r.png' % (outputdir, scene, i))\n",
    "                stereo_overlap = torch.cat([\n",
    "                    left_images['blended'][:, 0:1],\n",
    "                    right_images['blended'][:, 1:3]\n",
    "                ], dim=1)\n",
    "                img.save(stereo_overlap, '%s/%s_%d_stereo.png' % (outputdir, scene, i))\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
231
    "                #os.makedirs(outputdir + '/mid', exist_ok=True)\n",
Nianchen Deng's avatar
Nianchen Deng committed
232
233
234
235
236
    "                #img.save(left_images['layers_img'][1], '%s/mid/%s_%d_l.png' % (outputdir, scene, i))\n",
    "                #img.save(right_images['layers_img'][1], '%s/mid/%s_%d_r.png' % (outputdir, scene, i))\n",
    "                print(\"%s %d Saved\" % (scene, i))\n",
    "            else:\n",
    "                plot_figures(left_images, right_images, param[1], param[2])\n"
Nianchen Deng's avatar
Nianchen Deng committed
237
   ]
Nianchen Deng's avatar
Nianchen Deng committed
238
239
240
241
242
243
244
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
Nianchen Deng's avatar
Nianchen Deng committed
245
246
247
  }
 ],
 "metadata": {
Nianchen Deng's avatar
sync    
Nianchen Deng committed
248
249
250
  "interpreter": {
   "hash": "82066b63b621a9e3d15e3b7c11ca76da6238eff3834294910d715044bd0561e5"
  },
Nianchen Deng's avatar
Nianchen Deng committed
251
  "kernelspec": {
Nianchen Deng's avatar
Nianchen Deng committed
252
253
   "display_name": "Python 3",
   "language": "python",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
254
   "name": "python3"
Nianchen Deng's avatar
Nianchen Deng committed
255
256
257
258
259
260
261
262
263
264
265
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
266
   "version": "3.8.5"
Nianchen Deng's avatar
Nianchen Deng committed
267
  }
Nianchen Deng's avatar
Nianchen Deng committed
268
269
 },
 "nbformat": 4,
Nianchen Deng's avatar
Nianchen Deng committed
270
271
 "nbformat_minor": 4
}