-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathdecoder.py
174 lines (147 loc) · 6.3 KB
/
decoder.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Package imports
import torch
import torch.nn as nn
import tinycudann as tcnn
class ColorNet(nn.Module):
def __init__(self, config, input_ch=4, geo_feat_dim=15,
hidden_dim_color=64, num_layers_color=3):
super(ColorNet, self).__init__()
self.config = config
self.input_ch = input_ch
self.geo_feat_dim = geo_feat_dim
self.hidden_dim_color = hidden_dim_color
self.num_layers_color = num_layers_color
self.model = self.get_model(config['decoder']['tcnn_network'])
def forward(self, input_feat):
# h = torch.cat([embedded_dirs, geo_feat], dim=-1)
return self.model(input_feat)
def get_model(self, tcnn_network=False):
if tcnn_network:
print('Color net: using tcnn')
return tcnn.Network(
n_input_dims=self.input_ch + self.geo_feat_dim,
n_output_dims=3,
network_config={
"otype": "FullyFusedMLP",
"activation": "ReLU",
"output_activation": "None",
"n_neurons": self.hidden_dim_color,
"n_hidden_layers": self.num_layers_color - 1,
},
#dtype=torch.float
)
color_net = []
for l in range(self.num_layers_color):
if l == 0:
in_dim = self.input_ch + self.geo_feat_dim
else:
in_dim = self.hidden_dim_color
if l == self.num_layers_color - 1:
out_dim = 3 # 3 rgb
else:
out_dim = self.hidden_dim_color
color_net.append(nn.Linear(in_dim, out_dim, bias=False))
if l != self.num_layers_color - 1:
color_net.append(nn.ReLU(inplace=True))
return nn.Sequential(*nn.ModuleList(color_net))
class SDFNet(nn.Module):
def __init__(self, config, input_ch=3, geo_feat_dim=15, hidden_dim=64, num_layers=2):
super(SDFNet, self).__init__()
self.config = config
self.input_ch = input_ch
self.geo_feat_dim = geo_feat_dim
self.hidden_dim = hidden_dim
self.num_layers = num_layers
self.model = self.get_model(tcnn_network=config['decoder']['tcnn_network'])
def forward(self, x, return_geo=True):
out = self.model(x)
if return_geo: # return feature
return out
else:
return out[..., :1]
def get_model(self, tcnn_network=False):
if tcnn_network:
print('SDF net: using tcnn')
return tcnn.Network(
n_input_dims=self.input_ch,
n_output_dims=1 + self.geo_feat_dim,
network_config={
"otype": "FullyFusedMLP",
"activation": "ReLU",
"output_activation": "None",
"n_neurons": self.hidden_dim,
"n_hidden_layers": self.num_layers - 1,
},
#dtype=torch.float
)
else:
sdf_net = []
for l in range(self.num_layers):
if l == 0:
in_dim = self.input_ch
else:
in_dim = self.hidden_dim
if l == self.num_layers - 1:
out_dim = 1 + self.geo_feat_dim # 1 sigma + 15 SH features for color
else:
out_dim = self.hidden_dim
sdf_net.append(nn.Linear(in_dim, out_dim, bias=False))
if l != self.num_layers - 1:
sdf_net.append(nn.ReLU(inplace=True))
return nn.Sequential(*nn.ModuleList(sdf_net))
class ColorSDFNet(nn.Module):
'''
Color grid + SDF grid
'''
def __init__(self, config, input_ch=3, input_ch_pos=12):
super(ColorSDFNet, self).__init__()
self.config = config
self.color_net = ColorNet(config,
input_ch=input_ch+input_ch_pos,
geo_feat_dim=config['decoder']['geo_feat_dim'],
hidden_dim_color=config['decoder']['hidden_dim_color'],
num_layers_color=config['decoder']['num_layers_color'])
self.sdf_net = SDFNet(config,
input_ch=input_ch+input_ch_pos,
geo_feat_dim=config['decoder']['geo_feat_dim'],
hidden_dim=config['decoder']['hidden_dim'],
num_layers=config['decoder']['num_layers'])
def forward(self, embed, embed_pos, embed_color):
if embed_pos is not None:
h = self.sdf_net(torch.cat([embed, embed_pos], dim=-1), return_geo=True)
else:
h = self.sdf_net(embed, return_geo=True)
sdf, geo_feat = h[...,:1], h[...,1:]
if embed_pos is not None:
rgb = self.color_net(torch.cat([embed_pos, embed_color, geo_feat], dim=-1))
else:
rgb = self.color_net(torch.cat([embed_color, geo_feat], dim=-1))
return torch.cat([rgb, sdf], -1)
class ColorSDFNet_v2(nn.Module):
'''
No color grid
'''
def __init__(self, config, input_ch=3, input_ch_pos=12):
super(ColorSDFNet_v2, self).__init__()
self.config = config
self.color_net = ColorNet(config,
input_ch=input_ch_pos,
geo_feat_dim=config['decoder']['geo_feat_dim'],
hidden_dim_color=config['decoder']['hidden_dim_color'],
num_layers_color=config['decoder']['num_layers_color'])
self.sdf_net = SDFNet(config,
input_ch=input_ch+input_ch_pos,
geo_feat_dim=config['decoder']['geo_feat_dim'],
hidden_dim=config['decoder']['hidden_dim'],
num_layers=config['decoder']['num_layers'])
def forward(self, embed, embed_pos):
if embed_pos is not None:
h = self.sdf_net(torch.cat([embed, embed_pos], dim=-1), return_geo=True)
else:
h = self.sdf_net(embed, return_geo=True)
sdf, geo_feat = h[...,:1], h[...,1:]
if embed_pos is not None:
rgb = self.color_net(torch.cat([embed_pos, geo_feat], dim=-1))
else:
rgb = self.color_net(torch.cat([geo_feat], dim=-1))
return torch.cat([rgb, sdf], -1)