Skip to content
Snippets Groups Projects
Commit dcba5844 authored by Nianchen Deng's avatar Nianchen Deng
Browse files

update run_spherical_view_syn.py

parent 2a1d7973
Branches
No related merge requests found
%% Cell type:code id: tags:
```
import sys
import os
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
rootdir = os.path.abspath(sys.path[0] + '/../')
sys.path.append(rootdir)
torch.cuda.set_device(0)
print("Set CUDA:%d as current device." % torch.cuda.current_device())
torch.autograd.set_grad_enabled(False)
from data.spherical_view_syn import *
from configs.spherical_view_syn import SphericalViewSynConfig
from utils import netio
from utils import img
from utils import device
from utils.view import *
from components.fnr import FoveatedNeuralRenderer
def load_net(path):
config = SphericalViewSynConfig()
config.from_id(os.path.splitext(path)[0])
config.SAMPLE_PARAMS['perturb_sample'] = False
net = config.create_net().to(device.default())
netio.load(path, net)
return net
def find_file(prefix):
for path in os.listdir():
if path.startswith(prefix):
return path
return None
def load_views(data_desc_file) -> Trans:
with open(data_desc_file, 'r', encoding='utf-8') as file:
data_desc = json.loads(file.read())
view_centers = torch.tensor(
data_desc['view_centers'], device=device.default()).view(-1, 3)
view_rots = torch.tensor(
data_desc['view_rots'], device=device.default()).view(-1, 3, 3)
return Trans(view_centers, view_rots)
def plot_images(images):
plt.figure(figsize=(12, 4))
plt.subplot(131)
img.plot(images['layers_img'][0])
plt.subplot(132)
img.plot(images['layers_img'][1])
plt.subplot(133)
img.plot(images['layers_img'][2])
plt.figure(figsize=(12, 12))
img.plot(images['blended'])
scenes = {
'classroom': 'classroom_all',
'stones': 'stones_all',
'barbershop': 'barbershop_all',
'lobby': 'lobby_all'
}
fov_list = [20, 45, 110]
res_list = [(256, 256), (256, 256), (256, 230)]
res_full = (1600, 1440)
```
%% Output
Set CUDA:0 as current device.
%% Cell type:code id: tags:
```
scene = 'lobby'
scene = 'barbershop'
os.chdir(f'{rootdir}/data/__new/{scenes[scene]}')
print('Change working directory to ', os.getcwd())
fovea_net = load_net(find_file('fovea'))
periph_net = load_net(find_file('periph'))
renderer = FoveatedNeuralRenderer(fov_list, res_list, nn.ModuleList([fovea_net, periph_net, periph_net]),
res_full, device=device.default())
res_full, using_mask=False, device=device.default())
```
%% Output
Change working directory to /home/dengnc/dvs/data/__new/lobby_all
Load net from fovea@snerffast4-rgb_e6_fc512x4_d2.00-50.00_s64_~p.pth ...
Load net from periph@snerffast4-rgb_e6_fc256x4_d2.00-50.00_s64_~p.pth ...
Change working directory to /home/dengnc/dvs/data/__new/barbershop_all
Load net from fovea@snerffast4-rgb_e6_fc512x4_d1.20-6.00_s64_~p.pth ...
Load net from periph@snerffast4-rgb_e6_fc256x4_d1.20-6.00_s64_~p.pth ...
%% Cell type:code id: tags:
```
params = {
'classroom': [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, -53, 0, 0, 0],
[0, 0, 0, 20, -20, 0, 0]
],
'stones': [
[0, 0, 0, 0, 10, -300, -50],
[0, 0, 0, 0, 10, 200, -50]
],
'barbershop': [
[0, 0, 0, 20, 0, -300, 50],
[0, 0, 0, -140, -30, 150, -250],
[0, 0, 0, -60, -30, 75, -125],
[0, 0, 0, 0, 0, 0, 0],
#[0, 0, 0, 20, 0, -300, 50],
#[0, 0, 0, -140, -30, 150, -250],
#[0, 0, 0, -60, -30, 75, -125],
],
'lobby': [
#[0, 0, 0, 0, 0, 75, 0],
#[0, 0, 0, 0, 0, 5, 150],
[0, 0, 0, -120, 0, 75, 50],
]
}
for i, param in enumerate(params[scene]):
view = Trans(torch.tensor(param[:3], device=device.default()),
torch.tensor(euler_to_matrix([-param[4], param[3], 0]), device=device.default()).view(3, 3))
images = renderer(view, param[-2:])
if False:
outputdir = '../__demo/mono/'
misc.create_dir(outputdir)
img.save(images['layers_img'][0], f'{outputdir}{scene}_{i}_fovea.png')
img.save(images['layers_img'][1], f'{outputdir}{scene}_{i}_mid.png')
img.save(images['layers_img'][2], f'{outputdir}{scene}_{i}_periph.png')
img.save(images['blended'], f'{outputdir}{scene}_{i}_blended.png')
else:
images = plot_images(images)
```
%% Output
/home/dengnc/miniconda3/lib/python3.8/site-packages/torch/nn/functional.py:3828: UserWarning: Default grid_sample and affine_grid behavior has changed to align_corners=False since 1.3.0. Please specify align_corners=True if the old behavior is desired. See the documentation of grid_sample for details.
warnings.warn(
%% Cell type:code id: tags:
```
# Load Dataset
views = load_views('train.json')
print('Dataset loaded.')
print('views:', views.size())
for view_idx in range(views.size()[0]):
center = (0, 0)
test_view = views.get(view_idx)
render_view(test_view, center)
'''
images = gen(center, test_view)
outputdir = '../__2_demo/layer_blend/'
misc.create_dir(outputdir)
for key in images:
img.save(images[key], outputdir + '%s_view%04d_%s.png' % (scene, view_idx, key))
'''
'''
images = gen(
center, test_view,
mono_trans=Trans(test_view.trans_point(
torch.tensor([0.03, 0, 0], device=device.default())
), test_view.r))
outputdir = '../__2_demo/output_mono/ref_as_right_eye/'
misc.create_dir(outputdir)
for key in images:
key = 'blended'
img.save(images[key], outputdir + '%s_view%04d_%s.png' % (scene, view_idx, key))
'''
'''
left_images = gen(center,
Trans(
test_view.trans_point(
torch.tensor([-0.03, 0, 0], device=device.default())
),
test_view.r),
mono_trans=test_view)
right_images = gen(center, Trans(
test_view.trans_point(
torch.tensor([0.03, 0, 0], device=device.default())
), test_view.r), mono_trans=test_view)
outputdir = '../__2_demo/mono_periph/stereo/'
misc.create_dir(outputdir)
key = 'blended'
img.save(left_images[key], outputdir + '%s_view%04d_%s_l.png' % (scene, view_idx, key))
img.save(right_images[key], outputdir + '%s_view%04d_%s_r.png' % (scene, view_idx, key))
'''
```
......
......@@ -406,7 +406,8 @@ def test():
out[key][global_idx] = ret[key]
if args.output_flags['perf']:
perf_times[i] = perf.checkpoint()
progress_bar(i, n, 'Inferring...')
if not args.log_redirect:
progress_bar(i, n, 'Inferring...')
i += 1
global_offset += n_rays
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment