gen_for_eval.ipynb 5.34 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
   "source": [
    "import sys\n",
    "import os\n",
    "import torch\n",
    "\n",
13
14
    "rootdir = os.path.abspath(sys.path[0] + '/../')\n",
    "sys.path.append(rootdir)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
15
16
    "torch.cuda.set_device(2)\n",
    "print(\"Set CUDA:%d as current device.\" % torch.cuda.current_device())\n",
Nianchen Deng's avatar
Nianchen Deng committed
17
    "torch.autograd.set_grad_enabled(False)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
18
    "\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 img\n",
    "from utils import misc\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
    "\n",
    "\n",
    "def load_net(path):\n",
    "    config = SphericalViewSynConfig()\n",
    "    config.from_id(path[:-4])\n",
Nianchen Deng's avatar
Nianchen Deng committed
32
    "    config.sa['perturb_sample'] = False\n",
Nianchen Deng's avatar
Nianchen Deng committed
33
    "    #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
    "    return net\n",
    "\n",
    "\n",
    "def find_file(prefix):\n",
    "    for path in os.listdir():\n",
Nianchen Deng's avatar
Nianchen Deng committed
41
    "        if path.startswith(prefix + '@'):\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
42
43
44
45
46
47
48
49
    "            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",
    "        view_centers = torch.tensor(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
50
    "            data_desc['view_centers'], device=device.default()).view(-1, 3)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
51
    "        view_rots = torch.tensor(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
52
    "            data_desc['view_rots'], device=device.default()).view(-1, 3, 3)\n",
Nianchen Deng's avatar
Nianchen Deng committed
53
54
55
56
57
58
59
60
61
62
63
64
65
    "        return view.Trans(view_centers, view_rots)\n",
    "\n",
    "scenes = {\n",
    "    'gas': '__0_user_study/us_gas_all_in_one',\n",
    "    'mc': '__0_user_study/us_mc_all_in_one',\n",
    "    'bedroom': 'bedroom_all_in_one',\n",
    "    'gallery': 'gallery_all_in_one',\n",
    "    'lobby': 'lobby_all_in_one'\n",
    "}\n",
    "\n",
    "fov_list = [20, 45, 110]\n",
    "res_list = [(128, 128), (256, 256), (256, 230)]\n",
    "res_full = (1600, 1440)"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
66
67
68
69
   ]
  },
  {
   "cell_type": "code",
70
   "execution_count": null,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
71
   "metadata": {},
72
   "outputs": [],
Nianchen Deng's avatar
sync    
Nianchen Deng committed
73
   "source": [
Nianchen Deng's avatar
Nianchen Deng committed
74
    "scene = 'mc'\n",
75
    "os.chdir(os.path.join(rootdir, 'data/' + scenes[scene]))\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
76
77
78
79
80
81
82
83
84
85
    "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",
    "\n",
    "# Load Dataset\n",
    "views = load_views('nerf_views.json')\n",
    "print('Dataset loaded.')\n",
    "print('views:', views.size())\n",
    "gen = GenFinal(fov_list, res_list, res_full, fovea_net, periph_net,\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
86
    "               device=device.default())"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
87
88
89
90
   ]
  },
  {
   "cell_type": "code",
Nianchen Deng's avatar
Nianchen Deng committed
91
   "execution_count": 4,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
92
93
94
   "metadata": {},
   "outputs": [],
   "source": [
Nianchen Deng's avatar
Nianchen Deng committed
95
    "for view_idx in range(20):\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
96
97
    "    center = (0, 0)\n",
    "    test_view = views.get(view_idx)\n",
Nianchen Deng's avatar
Nianchen Deng committed
98
99
    "    images = gen.gen(center, test_view,\n",
    "                     mono_trans=view.Trans(test_view.trans_point(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
100
    "                         torch.tensor([-0.03, 0, 0], device=device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
101
102
103
    "                     ), test_view.r))\n",
    "    #left_images = gen(center, view.Trans(\n",
    "    #    test_view.trans_point(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
104
    "    #        torch.tensor([-0.03, 0, 0], device=device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
105
106
107
    "    #    ), test_view.r), mono_trans=test_view, ret_raw=True)\n",
    "    #right_images = gen(center, view.Trans(\n",
    "    #    test_view.trans_point(\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
108
    "    #        torch.tensor([0.03, 0, 0], device=device.default())\n",
Nianchen Deng's avatar
Nianchen Deng committed
109
    "    #    ), test_view.r), mono_trans=test_view, ret_raw=True)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
110
111
    "    #plot_figures(images, center)\n",
    "\n",
112
    "    outputdir = '../__1_eval/output_mono_periph/ref_as_right_eye/%s/' % scene\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
113
    "    misc.create_dir(outputdir)\n",
Nianchen Deng's avatar
Nianchen Deng committed
114
115
    "    #for key in images:\n",
    "    key = 'blended'\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
116
    "    img.save(images[key], outputdir + 'view%04d_%s.png' % (view_idx, key))\n"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
117
118
119
120
   ]
  },
  {
   "cell_type": "code",
121
   "execution_count": null,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
122
   "metadata": {},
123
   "outputs": [],
Nianchen Deng's avatar
Nianchen Deng committed
124
125
126
127
128
129
130
131
132
133
   "source": [
    "import numpy as np\n",
    "gaze_idx = 0\n",
    "for y in np.linspace(-200, 200, 11):\n",
    "    for x in np.linspace(-200, 200, 11):\n",
    "        center = (int(x), int(y))\n",
    "        test_view = views.get(0)\n",
    "        images = gen.gen(center, test_view, True)\n",
    "        #plot_figures(images, center)\n",
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
134
    "        misc.create_dir('output/eval_gaze')\n",
Nianchen Deng's avatar
Nianchen Deng committed
135
    "        out_path = 'output/eval_gaze/gaze%03d_%d,%d.png' % (gaze_idx, x, y)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
136
    "        img.save(images['blended'], out_path)\n",
Nianchen Deng's avatar
Nianchen Deng committed
137
138
139
    "        print('Output ' + out_path)\n",
    "        gaze_idx += 1"
   ]
Nianchen Deng's avatar
sync    
Nianchen Deng committed
140
141
142
143
  }
 ],
 "metadata": {
  "kernelspec": {
Nianchen Deng's avatar
Nianchen Deng committed
144
   "display_name": "Python 3.7.9 64-bit ('pytorch': conda)",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
145
   "name": "python379jvsc74a57bd0660ca2a75467d3af74a68fcc6f40bc78ab96b99ff17d2f100b5ca821fbb183f2"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
146
147
148
149
150
151
152
153
154
155
156
  },
  "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
157
   "version": "3.7.9"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
158
159
160
161
162
163
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}