test_spherical_view_syn.ipynb 7.78 KB
Newer Older
1
2
3
4
{
 "cells": [
  {
   "cell_type": "code",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
5
   "execution_count": null,
6
   "metadata": {},
Nianchen Deng's avatar
sync    
Nianchen Deng committed
7
   "outputs": [],
8
9
   "source": [
    "import sys\n",
BobYeah's avatar
BobYeah committed
10
    "import os\n",
11
12
13
    "\n",
    "rootdir = os.path.abspath('../')\n",
    "sys.path.append(rootdir)\n",
14
15
16
17
    "\n",
    "import torch\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
18
19
20
    "from utils import img\n",
    "from utils import sphere\n",
    "from utils.constants import *\n",
Nianchen Deng's avatar
Nianchen Deng committed
21
    "from nets.msl_net import *\n",
22
23
    "\n",
    "# Select device\n",
24
    "torch.cuda.set_device(0)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
25
    "print(\"Set CUDA:%d as current device.\" % torch.cuda.current_device())\n"
26
27
28
   ]
  },
  {
Nianchen Deng's avatar
sync    
Nianchen Deng committed
29
   "cell_type": "markdown",
30
31
32
33
34
35
36
   "metadata": {},
   "source": [
    "# Test Ray-Sphere Intersection & Cartesian-Spherical Conversion"
   ]
  },
  {
   "cell_type": "code",
37
   "execution_count": null,
38
   "metadata": {},
39
   "outputs": [],
40
41
   "source": [
    "def PlotSphere(ax, r):\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
42
    "    u, v = np.mgrid[0:2 * PI:50j, 0:PI:20j]\n",
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    "    x = np.cos(u) * np.sin(v) * r\n",
    "    y = np.sin(u) * np.sin(v) * r\n",
    "    z = np.cos(v) * r\n",
    "    ax.plot_surface(x, y, z, rstride=1, cstride=1,\n",
    "                    color='b', linewidth=0.5, alpha=0.3)\n",
    "\n",
    "\n",
    "def PlotPlane(ax, r):\n",
    "    # 二元函数定义域平面\n",
    "    x = np.linspace(-r, r, 3)\n",
    "    y = np.linspace(-r, r, 3)\n",
    "    X, Y = np.meshgrid(x, y)\n",
    "    ax.plot_wireframe(X, Y, X * 0, color='g', linewidth=1)\n",
    "\n",
    "\n",
    "p = torch.tensor([[0.0, 0.0, 0.0]])\n",
BobYeah's avatar
BobYeah committed
59
    "v = torch.tensor([[0.0, -1.0, 1.0]])\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
60
    "r = torch.tensor([[0.5]])\n",
61
    "v = v / torch.norm(v) * r * 2\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
62
    "p_on_sphere_ = sphere.ray_sphere_intersect(p, v, r)[0][0]\n",
63
64
    "print(p_on_sphere_)\n",
    "print(p_on_sphere_.norm())\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
65
    "spher_coord = sphere.cartesian2spherical(p_on_sphere_)\n",
66
    "print(spher_coord[..., 1:3].rad2deg())\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
67
    "p_on_sphere = sphere.spherical2cartesian(spher_coord)\n",
Nianchen Deng's avatar
Nianchen Deng committed
68
    "print(p_on_sphere_.size())\n",
69
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
70
    "fig = plt.figure(figsize=(8, 8))\n",
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
    "ax = fig.gca(projection='3d')\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('z')\n",
    "\n",
    "PlotPlane(ax, r.item())\n",
    "PlotSphere(ax, r[0, 0].item())\n",
    "\n",
    "ax.scatter([0], [0], [0], color=\"g\", s=10)  # Center\n",
    "ax.scatter([p_on_sphere[0, 0].item()],\n",
    "           [p_on_sphere[0, 2].item()],\n",
    "           [p_on_sphere[0, 1].item()],\n",
    "           color=\"r\", s=10)  # Ray position\n",
    "ax.scatter([p_on_sphere_[0, 0].item()],\n",
    "           [p_on_sphere_[0, 2].item()],\n",
    "           [p_on_sphere_[0, 1].item()],\n",
    "           color=\"y\", s=10)  # Ray position\n",
    "\n",
    "p_ = p + v\n",
    "ax.plot([p[0, 0].item(), p_[0, 0].item()],\n",
    "        [p[0, 2].item(), p_[0, 2].item()],\n",
    "        [p[0, 1].item(), p_[0, 1].item()],\n",
    "        color=\"r\")\n",
    "\n",
    "ax.plot([p_on_sphere_[0, 0].item(), p_on_sphere_[0, 0].item()],\n",
    "        [p_on_sphere_[0, 2].item(), p_on_sphere_[0, 2].item()],\n",
    "        [0, p_on_sphere_[0, 1].item()], color=\"k\", linestyle='--', linewidth=0.5)\n",
    "\n",
    "ax.plot([p_on_sphere_[0, 0].item(), 0],\n",
    "        [p_on_sphere_[0, 2].item(), 0],\n",
    "        [0, 0],\n",
    "        linewidth=0.5, linestyle=\"--\", color=\"k\")\n",
    "\n",
    "ax.plot([p_on_sphere_[0, 0].item(), 0],\n",
    "        [p_on_sphere_[0, 2].item(), 0],\n",
    "        [p_on_sphere_[0, 1], 0],\n",
    "        linewidth=0.5, linestyle=\"--\", color=\"k\")\n",
    "\n",
    "ax.set_xlim(-r.item(), r.item())\n",
    "ax.set_ylim(-r.item(), r.item())\n",
    "ax.set_zlim(-r.item(), r.item())\n",
    "\n",
    "plt.show()\n"
   ]
  },
  {
Nianchen Deng's avatar
sync    
Nianchen Deng committed
116
   "cell_type": "markdown",
117
118
   "metadata": {},
   "source": [
Nianchen Deng's avatar
sync    
Nianchen Deng committed
119
    "# Test Dataset Loader"
120
121
   ]
  },
BobYeah's avatar
BobYeah committed
122
123
  {
   "cell_type": "code",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
124
   "execution_count": null,
BobYeah's avatar
BobYeah committed
125
   "metadata": {},
Nianchen Deng's avatar
sync    
Nianchen Deng committed
126
   "outputs": [],
BobYeah's avatar
BobYeah committed
127
   "source": [
Nianchen Deng's avatar
Nianchen Deng committed
128
129
    "from data.spherical_view_syn import SphericalViewSynDataset\n",
    "from data.loader import FastDataLoader\n",
BobYeah's avatar
BobYeah committed
130
    "\n",
131
    "DATA_DESC_FILE = f'{rootdir}/data/__new/street_fovea_r360x80_t1.0/train1.json'\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
132
    "\n",
133
    "dataset = SphericalViewSynDataset(DATA_DESC_FILE)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
134
    "data_loader = FastDataLoader(dataset, 4, shuffle=False)\n",
BobYeah's avatar
BobYeah committed
135
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
136
    "fig = plt.figure(figsize=(12, 6.5))\n",
BobYeah's avatar
BobYeah committed
137
138
139
140
    "i = 0\n",
    "for indices, patches, rays_o, rays_d in data_loader:\n",
    "    print(i, patches.size(), rays_o.size(), rays_d.size())\n",
    "    for idx in range(len(indices)):\n",
Nianchen Deng's avatar
Nianchen Deng committed
141
    "        plt.subplot(4, 7, i + 1)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
142
    "        img.plot(patches[idx])\n",
BobYeah's avatar
BobYeah committed
143
    "        i += 1\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
144
145
146
147
148
149
150
151
152
    "    if i == 4:\n",
    "        break\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Validate Dataset"
BobYeah's avatar
BobYeah committed
153
154
   ]
  },
155
156
157
158
159
160
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
Nianchen Deng's avatar
Nianchen Deng committed
161
162
    "from data.spherical_view_syn import SphericalViewSynDataset\n",
    "from data.loader import FastDataLoader\n",
163
    "\n",
Nianchen Deng's avatar
Nianchen Deng committed
164
    "\n",
165
166
167
168
169
170
    "#DATA_DESC_FILE = f'{rootdir}/data/pabellon_fovea_r40x40_t0.3/train.json'\n",
    "#DATA_DESC_FILE = f'{rootdir}/data/gas_fovea_r80x60_t0.3_2021.01.26/train.json'\n",
    "#DATA_DESC_FILE = f'{rootdir}/data/nerf_fern/train.json'\n",
    "#DATA_DESC_FILE = f'{rootdir}/data/lobby_fovea_2021.01.18/train.json'\n",
    "#DATA_DESC_FILE = f'{rootdir}/data/__new/street_fovea_r360x80_t1.0/train1.json'\n",
    "#DATA_DESC_FILE = f'{rootdir}/data/__new/stones_fovea_r360x80_t1.0/train1.json'\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
171
172
    "#DATA_DESC_FILE = f'{rootdir}/data/__new/lobby_periph_r360x180_t1.0/train1.json'\n",
    "DATA_DESC_FILE = f'{rootdir}/data/__new/classroom_all/nerf_cvt.json'\n",
173
174
    "\n",
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
175
    "dataset = SphericalViewSynDataset(DATA_DESC_FILE, load_views=range(12))\n",
Nianchen Deng's avatar
Nianchen Deng committed
176
    "dataset.set_patch_size(1)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
177
    "res = dataset.view_res\n",
178
    "data_loader = FastDataLoader(dataset, res[0] * res[1], shuffle=False)\n",
179
    "\n",
180
    "selector = torch.arange(res[0] * res[1]).reshape(res[0], res[1])[::5, ::5].flatten()\n",
181
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
182
    "for ri in range(0, 4):\n",
183
    "    r = ri * 2 + 1\n",
Nianchen Deng's avatar
Nianchen Deng committed
184
185
186
    "    p = None\n",
    "    centers = None\n",
    "    pixels = None\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
187
    "    idx_range = list(range(12)) #+ list(range(24, 30)) + list(range(42, 48))\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
188
    "    idx = 0\n",
Nianchen Deng's avatar
Nianchen Deng committed
189
    "    for indices, patches, rays_o, rays_d in data_loader:\n",
190
191
    "        if idx not in idx_range:\n",
    "            idx += 1\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
192
193
194
195
196
197
198
199
200
    "            continue\n",
    "        patches = patches[selector]\n",
    "        rays_o = rays_o[selector]\n",
    "        rays_d = rays_d[selector]\n",
    "        r = torch.tensor([[r]], device=device.default())\n",
    "        p_ = misc.torch2np(sphere.ray_sphere_intersect(rays_o, rays_d, r)[0].view(-1, 3))\n",
    "        p = p_ if p is None else np.concatenate((p, p_), axis=0)\n",
    "        pixels_ = misc.torch2np(patches)\n",
    "        pixels = pixels_ if pixels is None else np.concatenate((pixels, pixels_), axis=0)\n",
201
    "        idx += 1\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
202
    "\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
203
204
205
    "    plt.figure(facecolor='white', figsize=(20, 20))\n",
    "    ax = plt.axes(projection='3d')\n",
    "    #ax = plt.subplot(1, 2, ri % 2 + 1, projection='3d')\n",
Nianchen Deng's avatar
Nianchen Deng committed
206
207
208
209
210
    "    plt.xlabel('x')\n",
    "    plt.ylabel('z')\n",
    "    plt.title('r = %f' % r)\n",
    "    ax.scatter([0], [0], [0], color=\"k\", s=10)\n",
    "    ax.scatter(p[:, 0], p[:, 2], p[:, 1], color=pixels, s=0.5)\n",
Nianchen Deng's avatar
sync    
Nianchen Deng committed
211
    "    ax.view_init(elev=0, azim=-90)\n"
BobYeah's avatar
BobYeah committed
212
   ]
213
214
215
216
  }
 ],
 "metadata": {
  "kernelspec": {
Nianchen Deng's avatar
sync    
Nianchen Deng committed
217
218
219
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
220
221
  },
  "language_info": {
222
223
224
225
226
227
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
228
   "name": "python",
229
230
231
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
BobYeah's avatar
BobYeah committed
232
  }
233
234
 },
 "nbformat": 4,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
235
236
 "nbformat_minor": 4
}