gen_user_study_images.ipynb 7.46 KB
Newer Older
Nianchen Deng's avatar
sync    
Nianchen Deng committed
1
2
3
4
{
 "cells": [
  {
   "cell_type": "code",
5
   "execution_count": null,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
6
   "metadata": {},
7
   "outputs": [],
Nianchen Deng's avatar
sync    
Nianchen Deng committed
8
9
10
11
12
13
   "source": [
    "import sys\n",
    "import os\n",
    "import torch\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
14
15
    "rootdir = os.path.abspath(sys.path[0] + '/../')\n",
    "sys.path.append(rootdir)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
16
17
18
    "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
19
20
21
22
23
24
25
26
    "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.gen_final import GenFinal\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
27
28
29
30
31
32
33
    "\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
34
35
    "    net = config.create_net().to(device.default())\n",
    "    netio.load(path, net)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    "    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
51
    "            data_desc['view_centers'], device=device.default()).view(samples + [3])\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
52
    "        view_rots = torch.tensor(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
53
    "            data_desc['view_rots'], device=device.default()).view(samples + [3, 3])\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
54
55
56
    "        return view.Trans(view_centers, view_rots)\n",
    "\n",
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
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
    "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",
    "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
84
    "    img.plot(left_images['fovea_raw'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
85
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
86
    "    img.plot(right_images['fovea_raw'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
87
88
89
90
    "\n",
    "    # Plot Fovea\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
91
    "    img.plot(left_images['fovea'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
92
93
94
    "    fovea_res = left_images['fovea'].size()[-2:]\n",
    "    plot_cross((0, 0), fovea_res)\n",
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
95
    "    img.plot(right_images['fovea'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
96
97
98
    "    plot_cross((0, 0), fovea_res)\n",
    "\n",
    "    #plt.subplot(1, 4, 2)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
99
    "    # img.plot(fovea_refined)\n",
Nianchen Deng's avatar
Nianchen Deng committed
100
101
102
103
    "\n",
    "    # Plot Mid\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
104
    "    img.plot(left_images['mid'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
105
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
106
    "    img.plot(right_images['mid'])\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
107
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
108
109
110
    "    # Plot Periph\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
111
    "    img.plot(left_images['periph'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
112
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
113
    "    img.plot(right_images['periph'])\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
114
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
115
116
117
    "    # Plot Blended\n",
    "    plt.figure(figsize=(12, 6))\n",
    "    plt.subplot(121)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
118
    "    img.plot(left_images['blended'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
119
120
121
    "    full_res = left_images['blended'].size()[-2:]\n",
    "    plot_cross(left_center, full_res)\n",
    "    plt.subplot(122)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
122
    "    img.plot(right_images['blended'])\n",
Nianchen Deng's avatar
Nianchen Deng committed
123
    "    plot_cross(right_center, full_res)\n"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
124
125
126
127
   ]
  },
  {
   "cell_type": "code",
128
   "execution_count": null,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
129
   "metadata": {},
130
   "outputs": [],
Nianchen Deng's avatar
sync    
Nianchen Deng committed
131
   "source": [
132
133
134
    "#os.chdir(os.path.join(rootdir, 'data/__0_user_study/us_gas_all_in_one'))\n",
    "os.chdir(os.path.join(rootdir, 'data/__0_user_study/us_mc_all_in_one'))\n",
    "#os.chdir(os.path.join(rootdir, 'data/__0_user_study/bedroom_all_in_one'))\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
135
136
137
138
139
140
141
142
143
144
145
146
    "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",
Nianchen Deng's avatar
Nianchen Deng committed
147
148
149
150
151
152
    "#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
153
    "gen = GenFinal(fov_list, res_list, res_full, fovea_net, periph_net, device.default())"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
154
155
156
157
   ]
  },
  {
   "cell_type": "code",
158
   "execution_count": null,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
159
   "metadata": {},
160
   "outputs": [],
Nianchen Deng's avatar
sync    
Nianchen Deng committed
161
   "source": [
Nianchen Deng's avatar
Nianchen Deng committed
162
163
164
165
166
167
168
169
170
171
172
    "centers = [\n",
    "    # ==gas==\n",
    "    [(-137, 64), (-142, 64)],\n",
    "    [(133, -44), (130, -44)],\n",
    "    [(-20, -5), (-25, -5)],\n",
    "    # ==mc==\n",
    "    [(-107, 80), (-112, 80)],\n",
    "    [(-17, -90), (-22, -90)],\n",
    "    [(95, 30), (91, 30)]\n",
    "]\n",
    "set_id = 5\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
173
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
174
    "view_coord = [0, 0, 0, 0, 0]\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
175
176
177
178
179
    "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",
Nianchen Deng's avatar
Nianchen Deng committed
180
181
    "left_images = gen(centers[set_id][0], view.Trans(\n",
    "    test_view.trans_point(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
182
    "        torch.tensor([-0.03, 0, 0], device=device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
183
184
185
    "    ), test_view.r), mono_trans=test_view, ret_raw=True)\n",
    "right_images = gen(centers[set_id][1], view.Trans(\n",
    "    test_view.trans_point(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
186
    "        torch.tensor([0.03, 0, 0], device=device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
187
    "    ), test_view.r), mono_trans=test_view, ret_raw=True)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
188
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
189
    "#plot_figures(left_images, right_images, centers[set_id][0], centers[set_id][1])\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
190
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
191
    "misc.create_dir('output')\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
192
    "for key in left_images:\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
193
    "    img.save(\n",
Nianchen Deng's avatar
Nianchen Deng committed
194
    "        left_images[key], 'output/set%d_%s_l.png' % (set_id, key))\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
195
    "for key in right_images:\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
196
    "    img.save(\n",
Nianchen Deng's avatar
Nianchen Deng committed
197
    "        right_images[key], 'output/set%d_%s_r.png' % (set_id, key))\n"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
198
199
200
201
202
203
204
205
206
207
208
209
210
   ]
  },
  {
   "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
211
   "name": "python379jvsc74a57bd0660ca2a75467d3af74a68fcc6f40bc78ab96b99ff17d2f100b5ca821fbb183f2"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
212
213
214
215
216
217
218
219
220
221
222
  },
  "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
223
   "version": "3.7.9"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
224
225
226
227
228
229
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}