gen_user_study_images.ipynb 9.32 KB
Newer Older
Nianchen Deng's avatar
sync    
Nianchen Deng committed
1
2
3
4
5
6
7
8
9
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
Nianchen Deng's avatar
Nianchen Deng committed
10
     "name": "stdout",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
     "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",
    "sys.path.append(os.path.abspath(sys.path[0] + '/../../'))\n",
    "__package__ = \"deep_view_syn.notebook\"\n",
    "torch.cuda.set_device(2)\n",
    "print(\"Set CUDA:%d as current device.\" % torch.cuda.current_device())\n",
    "\n",
    "from ..data.spherical_view_syn import *\n",
    "from ..configs.spherical_view_syn import SphericalViewSynConfig\n",
    "from ..my import netio\n",
    "from ..my import util\n",
    "from ..my import device\n",
    "from ..my import view\n",
    "from ..my.foveation import Foveation\n",
Nianchen Deng's avatar
Nianchen Deng committed
35
    "from ..my.gen_final import GenFinal\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    "\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",
    "    net = config.create_net().to(device.GetDevice())\n",
    "    netio.LoadNet(path, net)\n",
    "    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",
    "            data_desc['view_centers'], device=device.GetDevice()).view(samples + [3])\n",
    "        view_rots = torch.tensor(\n",
    "            data_desc['view_rots'], device=device.GetDevice()).view(samples + [3, 3])\n",
    "        return view.Trans(view_centers, view_rots)\n",
    "\n",
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
66
67
68
69
70
71
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
    "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",
    "    util.PlotImageTensor(left_images['fovea_raw'])\n",
    "    plt.subplot(122)\n",
    "    util.PlotImageTensor(right_images['fovea_raw'])\n",
    "\n",
    "    # Plot Fovea\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
    "    util.PlotImageTensor(left_images['fovea'])\n",
    "    fovea_res = left_images['fovea'].size()[-2:]\n",
    "    plot_cross((0, 0), fovea_res)\n",
    "    plt.subplot(122)\n",
    "    util.PlotImageTensor(right_images['fovea'])\n",
    "    plot_cross((0, 0), fovea_res)\n",
    "\n",
    "    #plt.subplot(1, 4, 2)\n",
    "    # util.PlotImageTensor(fovea_refined)\n",
    "\n",
    "    # Plot Mid\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
    "    util.PlotImageTensor(left_images['mid'])\n",
    "    plt.subplot(122)\n",
    "    util.PlotImageTensor(right_images['mid'])\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
116
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
117
118
119
120
121
122
    "    # Plot Periph\n",
    "    plt.figure(figsize=(8, 4))\n",
    "    plt.subplot(121)\n",
    "    util.PlotImageTensor(left_images['periph'])\n",
    "    plt.subplot(122)\n",
    "    util.PlotImageTensor(right_images['periph'])\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
123
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
124
125
126
127
128
129
130
131
132
    "    # Plot Blended\n",
    "    plt.figure(figsize=(12, 6))\n",
    "    plt.subplot(121)\n",
    "    util.PlotImageTensor(left_images['blended'])\n",
    "    full_res = left_images['blended'].size()[-2:]\n",
    "    plot_cross(left_center, full_res)\n",
    "    plt.subplot(122)\n",
    "    util.PlotImageTensor(right_images['blended'])\n",
    "    plot_cross(right_center, full_res)\n"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
133
134
135
136
   ]
  },
  {
   "cell_type": "code",
Nianchen Deng's avatar
Nianchen Deng committed
137
   "execution_count": 6,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
138
139
140
141
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
Nianchen Deng's avatar
Nianchen Deng committed
142
     "name": "stdout",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
143
     "text": [
Nianchen Deng's avatar
Nianchen Deng committed
144
      "Change working directory to  /home/dengnc/deep_view_syn/data/__0_user_study/us_mc_all_in_one\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
145
146
147
148
149
150
      "==== Config fovea ====\n",
      "Net type:  nmsl\n",
      "Encode dim:  10\n",
      "Optimizer decay:  0\n",
      "Normalize:  False\n",
      "Direction as input:  False\n",
Nianchen Deng's avatar
Nianchen Deng committed
151
      "Full-connected network parameters: {'nf': 128, 'n_layers': 4, 'skips': []}\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
152
153
      "Sample parameters {'spherical': True, 'depth_range': (1.0, 50.0), 'n_samples': 32, 'perturb_sample': False, 'lindisp': True, 'inverse_r': True}\n",
      "==========================\n",
Nianchen Deng's avatar
Nianchen Deng committed
154
      "Load net from fovea@nmsl-rgb_e10_fc128x4_d1-50_s32.pth ...\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
155
156
157
158
159
160
161
162
163
      "==== 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",
Nianchen Deng's avatar
Nianchen Deng committed
164
      "Load net from periph@nnmsl-rgb_e10_fc64x4_d1-50_s16.pth ...\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
165
166
167
168
169
170
      "Dataset loaded.\n",
      "views: [5, 5, 5, 5, 5]\n"
     ]
    }
   ],
   "source": [
Nianchen Deng's avatar
Nianchen Deng committed
171
172
173
    "#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",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
174
175
176
177
178
179
180
181
182
183
184
185
    "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
186
187
188
189
190
191
192
    "#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",
    "gen = GenFinal(fov_list, res_list, res_full, fovea_net, periph_net, device.GetDevice())"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
193
194
195
196
   ]
  },
  {
   "cell_type": "code",
Nianchen Deng's avatar
Nianchen Deng committed
197
   "execution_count": 9,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
198
199
200
201
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
Nianchen Deng's avatar
Nianchen Deng committed
202
     "name": "stdout",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
203
     "text": [
Nianchen Deng's avatar
Nianchen Deng committed
204
      "view_coord: [2, 2, 2, 2, 2]\nshift: 3\nshift: -3\n"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
205
206
207
208
     ]
    }
   ],
   "source": [
Nianchen Deng's avatar
Nianchen Deng committed
209
210
211
212
213
214
215
216
217
218
219
    "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
220
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
221
    "view_coord = [0, 0, 0, 0, 0]\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
222
223
224
225
226
    "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
227
228
229
230
231
232
233
234
    "left_images = gen(centers[set_id][0], view.Trans(\n",
    "    test_view.trans_point(\n",
    "        torch.tensor([-0.03, 0, 0], device=device.GetDevice())\n",
    "    ), 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",
    "        torch.tensor([0.03, 0, 0], device=device.GetDevice())\n",
    "    ), test_view.r), mono_trans=test_view, ret_raw=True)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
235
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
236
    "#plot_figures(left_images, right_images, centers[set_id][0], centers[set_id][1])\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
237
238
239
    "\n",
    "util.CreateDirIfNeed('output')\n",
    "for key in left_images:\n",
Nianchen Deng's avatar
Nianchen Deng committed
240
241
    "    util.WriteImageTensor(\n",
    "        left_images[key], 'output/set%d_%s_l.png' % (set_id, key))\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
242
    "for key in right_images:\n",
Nianchen Deng's avatar
Nianchen Deng committed
243
244
    "    util.WriteImageTensor(\n",
    "        right_images[key], 'output/set%d_%s_r.png' % (set_id, key))\n"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
245
246
247
248
249
250
251
252
253
254
255
256
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
Nianchen Deng's avatar
Nianchen Deng committed
257
   "name": "python3",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
258
259
260
261
262
   "display_name": "Python 3.7.9 64-bit ('pytorch': conda)",
   "metadata": {
    "interpreter": {
     "hash": "660ca2a75467d3af74a68fcc6f40bc78ab96b99ff17d2f100b5ca821fbb183f2"
    }
Nianchen Deng's avatar
Nianchen Deng committed
263
   }
Nianchen Deng's avatar
sync    
Nianchen Deng committed
264
265
266
267
268
269
270
271
272
273
274
  },
  "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
Nianchen Deng committed
275
   "version": "3.7.9-final"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
276
277
278
279
280
281
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}