test_mono_view.ipynb 10.1 KB
Newer Older
Nianchen Deng's avatar
Nianchen Deng committed
1
2
3
4
{
 "cells": [
  {
   "cell_type": "code",
5
   "execution_count": null,
Nianchen Deng's avatar
Nianchen Deng committed
6
   "metadata": {},
7
   "outputs": [],
Nianchen Deng's avatar
Nianchen Deng committed
8
9
10
11
12
13
14
   "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",
15
16
    "rootdir = os.path.abspath(sys.path[0] + '/../')\n",
    "sys.path.append(rootdir)\n",
Nianchen Deng's avatar
Nianchen Deng committed
17
18
19
    "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
20
21
22
23
24
25
26
27
28
    "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
29
30
31
32
33
34
35
    "\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
36
37
    "    net = config.create_net().to(device.default())\n",
    "    netio.load(path, net)\n",
Nianchen Deng's avatar
Nianchen Deng committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    "    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
53
    "            data_desc['view_centers'], device=device.default()).view(samples + [3])\n",
Nianchen Deng's avatar
Nianchen Deng committed
54
    "        view_rots = torch.tensor(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
55
    "            data_desc['view_rots'], device=device.default()).view(samples + [3, 3])\n",
Nianchen Deng's avatar
Nianchen Deng committed
56
57
58
59
60
61
    "        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
62
    "        return img.load([patt % i for i in idx])\n",
Nianchen Deng's avatar
Nianchen Deng committed
63
    "    else:\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
64
    "        return img.load(patt % idx)\n",
Nianchen Deng's avatar
Nianchen Deng committed
65
66
67
68
69
70
71
72
73
74
75
76
77
    "\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",
78
   "execution_count": null,
Nianchen Deng's avatar
Nianchen Deng committed
79
   "metadata": {},
80
   "outputs": [],
Nianchen Deng's avatar
Nianchen Deng committed
81
   "source": [
82
83
84
    "os.chdir(os.path.join('data/__0_user_study/us_gas_all_in_one'))\n",
    "#os.chdir(os.path.join('data/__0_user_study/us_mc_all_in_one'))\n",
    "#os.chdir(os.path.join('data/bedroom_all_in_one'))\n",
Nianchen Deng's avatar
Nianchen Deng committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    "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
103
    "gen = GenFinal(fov_list, res_list, res_full, fovea_net, periph_net, device.default())"
Nianchen Deng's avatar
Nianchen Deng committed
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
   ]
  },
  {
   "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
145
    "    }, res_list[i]).to(device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
146
147
148
149
150
151
152
153
154
155
    "    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
156
    "foveation = Foveation(fov_list, res_full, device=device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
157
158
159
160
161
162
    "\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
163
    "    img.plot(left_images['fovea_raw'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
164
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
165
    "    img.plot(right_images['fovea_raw'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
166
167
168
169
    "\n",
    "    # Plot Fovea\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
170
    "    img.plot(left_images['fovea'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
171
172
173
174
175
176
177
    "    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
178
    "    img.plot(right_images['fovea'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
179
180
181
182
183
184
185
186
    "    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
187
    "    # img.plot(fovea_refined)\n",
Nianchen Deng's avatar
Nianchen Deng committed
188
189
190
191
    "\n",
    "    # Plot Mid\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
192
    "    img.plot(left_images['mid'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
193
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
194
    "    img.plot(right_images['mid'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
195
196
197
198
    "\n",
    "    # Plot Periph\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
199
    "    img.plot(left_images['periph'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
200
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
201
    "    img.plot(right_images['periph'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
202
203
204
205
    "\n",
    "    # Plot Blended\n",
    "    plt.figure(figsize=(12, 6))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
206
    "    img.plot(left_images['blended'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
207
208
209
210
211
212
213
214
215
    "    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
216
    "    img.plot(right_images['blended'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    "    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
231
    "            torch.tensor([-0.03, 0, 0], device=device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
232
233
234
235
236
237
238
239
240
241
    "        ),\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
242
    "            torch.tensor([0.03, 0, 0], device=device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
243
244
245
246
247
248
249
250
251
    "        ),\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
252
    "misc.create_dir('output/mono_test')\n",
Nianchen Deng's avatar
Nianchen Deng committed
253
    "for key in left_images:\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
254
    "    img.save(\n",
Nianchen Deng's avatar
Nianchen Deng committed
255
256
    "        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
257
    "    img.save(\n",
Nianchen Deng's avatar
Nianchen Deng committed
258
259
260
261
262
263
264
    "        right_images[key], 'output/mono_test/set%d_%s_r.png' % (set_id, key))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.9 64-bit ('pytorch': conda)",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
265
   "name": "python379jvsc74a57bd0660ca2a75467d3af74a68fcc6f40bc78ab96b99ff17d2f100b5ca821fbb183f2"
Nianchen Deng's avatar
Nianchen Deng committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
  },
  "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
}