-
Notifications
You must be signed in to change notification settings - Fork 3
/
multiplane_helpers_generalized.py
139 lines (109 loc) · 4.75 KB
/
multiplane_helpers_generalized.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import torch
class RenderNetwork(torch.nn.Module):
def __init__(
self,
input_size,
dir_count
):
super().__init__()
self.input_size = 3*input_size + input_size*3
self.layers_main = torch.nn.Sequential(
torch.nn.Linear(self.input_size, 2048),
torch.nn.ReLU(),
torch.nn.Linear(2048, 512),
torch.nn.ReLU(),
torch.nn.Linear(512, 512),
torch.nn.ReLU(),
)
self.layers_main_2 = torch.nn.Sequential(
torch.nn.Linear(512 + self.input_size, 256),
torch.nn.ReLU(),
torch.nn.Linear(256, 256),
torch.nn.ReLU(),
)
self.layers_sigma = torch.nn.Sequential(
torch.nn.Linear(256 + self.input_size, 256),
torch.nn.ReLU(),
torch.nn.Linear(256, 1)
)
self.layers_rgb = torch.nn.Sequential(
torch.nn.Linear(256 + self.input_size + dir_count, 256),
torch.nn.ReLU(),
torch.nn.Linear(256, 256),
torch.nn.ReLU(),
torch.nn.Linear(256, 3),
)
def forward(self, triplane_code, viewdir):
x = self.layers_main(triplane_code)
x1 = torch.concat([x, triplane_code], dim=1)
x = self.layers_main_2(x1)
xs = torch.concat([x, triplane_code], dim=1)
sigma = self.layers_sigma(xs)
x = torch.concat([x, triplane_code, viewdir], dim=1)
rgb = self.layers_rgb(x)
return torch.concat([rgb, sigma], dim=1)
class ImagePlane(torch.nn.Module):
def __init__(self, focal, poses, images, count, device='cuda'):
super(ImagePlane, self).__init__()
self.pose_matrices = []
self.K_matrices = []
self.images = []
self.centroids = []
self.focal = focal
for i in range(min(count, poses.shape[0])):
M = poses[i]
M = torch.from_numpy(M)
M = M @ torch.Tensor([[-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]).to(M.device)
self.centroids.append(M[0:3, 3])
M = torch.inverse(M)
M = M[0:3]
self.pose_matrices.append(M)
image = images[i]
image = torch.from_numpy(image)
self.images.append(image.permute(2,0,1))
self.size = float(image.shape[0])
K = torch.Tensor([[self.focal.item(), 0, 0.5*image.shape[0]], [0, self.focal.item(), 0.5*image.shape[0]], [0, 0, 1]])
self.K_matrices.append(K)
self.pose_matrices = torch.stack(self.pose_matrices).to(device)
self.K_matrices = torch.stack(self.K_matrices).to(device)
self.image_plane = torch.stack(self.images).to(device)
self.centroids = torch.stack(self.centroids).to(device)
def forward(self, points=None):
if points.shape[0] == 1:
points = points[0]
points = torch.concat([points, torch.ones(points.shape[0], 1).to(points.device)], 1).to(points.device)
ps = self.K_matrices @ self.pose_matrices @ points.T
pixels = (ps/ps[:,None,2])[:,0:2,:]
pixels = pixels / self.size
pixels = torch.clamp(pixels, 0, 1)
pixels = pixels * 2.0 - 1.0
pixels = pixels.permute(0,2,1)
feats = []
for img in range(self.image_plane.shape[0]):
feat = torch.nn.functional.grid_sample(
self.image_plane[img].unsqueeze(0),
pixels[img].unsqueeze(0).unsqueeze(0), mode='bilinear', padding_mode='zeros', align_corners=False)
feats.append(feat)
feats = torch.stack(feats).squeeze(1)
pixels = pixels.permute(1,0,2)
pixels = pixels.flatten(1)
feats = feats.permute(2,3,0,1)
feats = feats.flatten(2)
cposes = self.centroids.flatten()
feats = feats[0]
feats = torch.cat((feats, cposes.unsqueeze(0).repeat(feats.shape[0], 1)), dim=1)
return feats
class MultiImageNeRF(torch.nn.Module):
def __init__(self, image_plane, count, dir_count):
super(MultiImageNeRF, self).__init__()
self.image_plane = image_plane
self.render_network = RenderNetwork(count, dir_count)
self.input_ch_views = dir_count
def parameters(self):
return self.render_network.parameters()
def set_image_plane(self, ip):
self.image_plane = ip
def forward(self, x):
input_pts, input_views = torch.split(x, [3, self.input_ch_views], dim=-1)
x = self.image_plane(input_pts)
return self.render_network(x, input_views)