gen_demo_stereo.ipynb 7.47 KB
Newer Older
Nianchen Deng's avatar
Nianchen Deng committed
1
2
3
4
{
 "cells": [
  {
   "cell_type": "code",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
5
   "execution_count": 5,
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.SAMPLE_PARAMS['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
sync    
Nianchen Deng committed
139
   "execution_count": 26,
Nianchen Deng's avatar
Nianchen Deng committed
140
   "metadata": {},
Nianchen Deng's avatar
sync    
Nianchen Deng committed
141
142
143
144
145
146
147
148
149
150
151
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Change working directory to  /home/dengnc/dvs/data/__new/classroom_all\n",
      "Load net from fovea@snerffast4-rgb_e6_fc256x8_d1.00-7.00_s64_~p.pth ...\n",
      "Load net from periph@snerffast4-rgb_e6_fc128x4_d1.00-7.00_s64_~p.pth ...\n"
     ]
    }
   ],
Nianchen Deng's avatar
Nianchen Deng committed
152
   "source": [
Nianchen Deng's avatar
sync    
Nianchen Deng committed
153
154
155
156
157
158
    "params = {\n",
    "    'classroom': [\n",
    "        [(0, 0, 0, 0, 0), (1, -83), (-5, -83)],\n",
    "        [(0, 0, 0, 0, 0), (-171, 55), (-178, 55)],\n",
    "        [(0, 0, 0, 0, 0), (60, 55), (55, 55)],\n",
    "        [(0, 0, 0, 0, 0), (138, 160), (130, 160)]\n",
Nianchen Deng's avatar
Nianchen Deng committed
159
160
    "    ],\n",
    "}\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
161
162
    "scene = 'classroom'\n",
    "os.chdir(f'{rootdir}/data/__new/{scenes[scene]}')\n",
Nianchen Deng's avatar
Nianchen Deng committed
163
164
165
166
    "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",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
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
    "renderer = FoveatedNeuralRenderer(fov_list, res_list,\n",
    "                                  nn.ModuleList([fovea_net, periph_net, periph_net]),\n",
    "                                  res_full, device=device.default())\n",
    "\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, using_mask=False, ret_raw=False)\n",
    "    if True:\n",
    "        outputdir = '../__demo/stereo/'\n",
    "        misc.create_dir(outputdir)\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",
    "    else:\n",
    "        plot_figures(left_images, right_images, param[1], param[2])\n"
Nianchen Deng's avatar
Nianchen Deng committed
192
193
194
195
   ]
  }
 ],
 "metadata": {
Nianchen Deng's avatar
sync    
Nianchen Deng committed
196
197
198
  "interpreter": {
   "hash": "82066b63b621a9e3d15e3b7c11ca76da6238eff3834294910d715044bd0561e5"
  },
Nianchen Deng's avatar
Nianchen Deng committed
199
  "kernelspec": {
Nianchen Deng's avatar
sync    
Nianchen Deng committed
200
201
   "display_name": "Python 3.8.5 64-bit ('base': conda)",
   "name": "python3"
Nianchen Deng's avatar
Nianchen Deng committed
202
203
204
205
206
207
208
209
210
211
212
  },
  "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
213
   "version": "3.8.5"
Nianchen Deng's avatar
Nianchen Deng committed
214
215
216
217
218
219
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}