-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgaussianimage_cholesky.py
246 lines (216 loc) · 14 KB
/
gaussianimage_cholesky.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from gsplat.project_gaussians_2d import project_gaussians_2d
from gsplat.rasterize_sum import rasterize_gaussians_sum
from utils import *
import torch
import torch.nn as nn
import numpy as np
import math
from quantize import *
from optimizer import Adan
class GaussianImage_Cholesky(nn.Module):
def __init__(self, loss_type="L2", **kwargs):
super().__init__()
self.loss_type = loss_type
self.init_num_points = kwargs["num_points"]
self.H, self.W = kwargs["H"], kwargs["W"]
self.BLOCK_W, self.BLOCK_H = kwargs["BLOCK_W"], kwargs["BLOCK_H"]
self.tile_bounds = (
(self.W + self.BLOCK_W - 1) // self.BLOCK_W,
(self.H + self.BLOCK_H - 1) // self.BLOCK_H,
1,
) #
self.device = kwargs["device"]
self._xyz = nn.Parameter(torch.atanh(2 * (torch.rand(self.init_num_points, 2) - 0.5)))
self._cholesky = nn.Parameter(torch.rand(self.init_num_points, 3))
self.register_buffer('_opacity', torch.ones((self.init_num_points, 1)))
self._features_dc = nn.Parameter(torch.rand(self.init_num_points, 3))
self.last_size = (self.H, self.W)
self.quantize = kwargs["quantize"]
self.register_buffer('background', torch.ones(3))
self.opacity_activation = torch.sigmoid
self.rgb_activation = torch.sigmoid
self.register_buffer('bound', torch.tensor([0.5, 0.5]).view(1, 2))
self.register_buffer('cholesky_bound', torch.tensor([0.5, 0, 0.5]).view(1, 3))
if self.quantize:
self.xyz_quantizer = FakeQuantizationHalf.apply
self.features_dc_quantizer = VectorQuantizer(codebook_dim=3, codebook_size=8, num_quantizers=2, vector_type="vector", kmeans_iters=5)
self.cholesky_quantizer = UniformQuantizer(signed=False, bits=6, learned=True, num_channels=3)
if kwargs["opt_type"] == "adam":
self.optimizer = torch.optim.Adam(self.parameters(), lr=kwargs["lr"])
else:
self.optimizer = Adan(self.parameters(), lr=kwargs["lr"])
self.scheduler = torch.optim.lr_scheduler.StepLR(self.optimizer, step_size=20000, gamma=0.5)
def _init_data(self):
self.cholesky_quantizer._init_data(self._cholesky)
@property
def get_xyz(self):
return torch.tanh(self._xyz)
@property
def get_features(self):
return self._features_dc
@property
def get_opacity(self):
return self._opacity
@property
def get_cholesky_elements(self):
return self._cholesky+self.cholesky_bound
def forward(self):
self.xys, depths, self.radii, conics, num_tiles_hit = project_gaussians_2d(self.get_xyz, self.get_cholesky_elements, self.H, self.W, self.tile_bounds)
out_img = rasterize_gaussians_sum(self.xys, depths, self.radii, conics, num_tiles_hit,
self.get_features, self._opacity, self.H, self.W, self.BLOCK_H, self.BLOCK_W, background=self.background, return_alpha=False)
out_img = torch.clamp(out_img, 0, 1) #[H, W, 3]
out_img = out_img.view(-1, self.H, self.W, 3).permute(0, 3, 1, 2).contiguous()
return {"render": out_img}
def train_iter(self, gt_image):
render_pkg = self.forward()
image = render_pkg["render"]
loss = loss_fn(image, gt_image, self.loss_type, lambda_value=0.7)
loss.backward()
with torch.no_grad():
mse_loss = F.mse_loss(image, gt_image)
psnr = 10 * math.log10(1.0 / mse_loss.item())
self.optimizer.step()
self.optimizer.zero_grad(set_to_none = True)
self.scheduler.step()
return loss, psnr
def forward_quantize(self):
l_vqm, m_bit = 0, 16*self.init_num_points*2
means = torch.tanh(self.xyz_quantizer(self._xyz))
cholesky_elements, l_vqs, s_bit = self.cholesky_quantizer(self._cholesky)
cholesky_elements = cholesky_elements + self.cholesky_bound
l_vqr, r_bit = 0, 0
colors, l_vqc, c_bit = self.features_dc_quantizer(self.get_features)
self.xys, depths, self.radii, conics, num_tiles_hit = project_gaussians_2d(means, cholesky_elements, self.H, self.W, self.tile_bounds)
out_img = rasterize_gaussians_sum(self.xys, depths, self.radii, conics, num_tiles_hit,
colors, self._opacity, self.H, self.W, self.BLOCK_H, self.BLOCK_W, background=self.background, return_alpha=False)
out_img = torch.clamp(out_img, 0, 1)
out_img = out_img.view(-1, self.H, self.W, 3).permute(0, 3, 1, 2).contiguous()
vq_loss = l_vqm + l_vqs + l_vqr + l_vqc
return {"render": out_img, "vq_loss": vq_loss, "unit_bit":[m_bit, s_bit, r_bit, c_bit]}
def train_iter_quantize(self, gt_image):
render_pkg = self.forward_quantize()
image = render_pkg["render"]
loss = loss_fn(image, gt_image, self.loss_type, lambda_value=0.7) + render_pkg["vq_loss"]
loss.backward()
with torch.no_grad():
mse_loss = F.mse_loss(image, gt_image)
psnr = 10 * math.log10(1.0 / mse_loss.item())
self.optimizer.step()
self.optimizer.zero_grad(set_to_none=True)
self.scheduler.step()
return loss, psnr
def compress_wo_ec(self):
means = torch.tanh(self.xyz_quantizer(self._xyz))
quant_cholesky_elements, cholesky_elements = self.cholesky_quantizer.compress(self._cholesky)
cholesky_elements = cholesky_elements + self.cholesky_bound
colors, feature_dc_index = self.features_dc_quantizer.compress(self.get_features)
return {"xyz":self._xyz.half(), "feature_dc_index": feature_dc_index, "quant_cholesky_elements": quant_cholesky_elements,}
def decompress_wo_ec(self, encoding_dict):
xyz, feature_dc_index, quant_cholesky_elements = encoding_dict["xyz"], encoding_dict["feature_dc_index"], encoding_dict["quant_cholesky_elements"]
means = torch.tanh(xyz.float())
cholesky_elements = self.cholesky_quantizer.decompress(quant_cholesky_elements)
cholesky_elements = cholesky_elements + self.cholesky_bound
colors = self.features_dc_quantizer.decompress(feature_dc_index)
self.xys, depths, self.radii, conics, num_tiles_hit = project_gaussians_2d(means, cholesky_elements, self.H, self.W, self.tile_bounds)
out_img = rasterize_gaussians_sum(self.xys, depths, self.radii, conics, num_tiles_hit,
colors, self._opacity, self.H, self.W, self.BLOCK_H, self.BLOCK_W, background=self.background, return_alpha=False)
out_img = torch.clamp(out_img, 0, 1)
out_img = out_img.view(-1, self.H, self.W, 3).permute(0, 3, 1, 2).contiguous()
return {"render":out_img}
def analysis_wo_ec(self, encoding_dict):
quant_cholesky_elements, feature_dc_index = encoding_dict["quant_cholesky_elements"], encoding_dict["feature_dc_index"]
total_bits = 0
initial_bits, codebook_bits = 0, 0
for quantizer_index, layer in enumerate(self.features_dc_quantizer.quantizer.layers):
codebook_bits += layer._codebook.embed.numel()*torch.finfo(layer._codebook.embed.dtype).bits
initial_bits += self.cholesky_quantizer.scale.numel()*torch.finfo(self.cholesky_quantizer.scale.dtype).bits
initial_bits += self.cholesky_quantizer.beta.numel()*torch.finfo(self.cholesky_quantizer.beta.dtype).bits
initial_bits += codebook_bits
total_bits += initial_bits
total_bits += self._xyz.numel()*16
feature_dc_index = feature_dc_index.int().cpu().numpy()
index_max = np.max(feature_dc_index)
max_bit = np.ceil(np.log2(index_max)) #calculate max bit for feature_dc_index
total_bits += feature_dc_index.size * max_bit #get_np_size(encoding_dict["feature_dc_index"]) * 8
quant_cholesky_elements = quant_cholesky_elements.cpu().numpy()
total_bits += quant_cholesky_elements.size * 6 #cholesky bits
position_bits = self._xyz.numel()*16
cholesky_bits, feature_dc_bits = 0, 0
cholesky_bits += self.cholesky_quantizer.scale.numel()*torch.finfo(self.cholesky_quantizer.scale.dtype).bits
cholesky_bits += self.cholesky_quantizer.beta.numel()*torch.finfo(self.cholesky_quantizer.beta.dtype).bits
cholesky_bits += quant_cholesky_elements.size * 6
feature_dc_bits += codebook_bits
feature_dc_bits += feature_dc_index.size * max_bit
bpp = total_bits/self.H/self.W
position_bpp = position_bits/self.H/self.W
cholesky_bpp = cholesky_bits/self.H/self.W
feature_dc_bpp = feature_dc_bits/self.H/self.W
return {"bpp": bpp, "position_bpp": position_bpp,
"cholesky_bpp": cholesky_bpp, "feature_dc_bpp": feature_dc_bpp}
def compress(self):
means = torch.tanh(self.xyz_quantizer(self._xyz))
quant_cholesky_elements, cholesky_elements = self.cholesky_quantizer.compress(self._cholesky)
cholesky_elements = cholesky_elements + self.cholesky_bound
colors, feature_dc_index = self.features_dc_quantizer.compress(self.get_features)
cholesky_compressed, cholesky_histogram_table, cholesky_unique = compress_matrix_flatten_categorical(quant_cholesky_elements.int().flatten().tolist())
feature_dc_compressed, feature_dc_histogram_table, feature_dc_unique = compress_matrix_flatten_categorical(feature_dc_index.int().flatten().tolist())
return {"xyz":self._xyz.half(), "feature_dc_index": feature_dc_index, "quant_cholesky_elements": quant_cholesky_elements,
"feature_dc_bitstream":[feature_dc_compressed, feature_dc_histogram_table, feature_dc_unique],
"cholesky_bitstream":[cholesky_compressed, cholesky_histogram_table, cholesky_unique]}
def decompress(self, encoding_dict):
xyz = encoding_dict["xyz"]
num_points, device = xyz.size(0), xyz.device
feature_dc_compressed, feature_dc_histogram_table, feature_dc_unique = encoding_dict["feature_dc_bitstream"]
cholesky_compressed, cholesky_histogram_table, cholesky_unique = encoding_dict["cholesky_bitstream"]
feature_dc_index = decompress_matrix_flatten_categorical(feature_dc_compressed, feature_dc_histogram_table, feature_dc_unique, num_points*2, (num_points, 2))
quant_cholesky_elements = decompress_matrix_flatten_categorical(cholesky_compressed, cholesky_histogram_table, cholesky_unique, num_points*3, (num_points, 3))
feature_dc_index = torch.from_numpy(feature_dc_index).to(device).int() #[800, 2]
quant_cholesky_elements = torch.from_numpy(quant_cholesky_elements).to(device).float() #[800, 3]
means = torch.tanh(xyz.float())
cholesky_elements = self.cholesky_quantizer.decompress(quant_cholesky_elements)
cholesky_elements = cholesky_elements + self.cholesky_bound
colors = self.features_dc_quantizer.decompress(feature_dc_index)
self.xys, depths, self.radii, conics, num_tiles_hit = project_gaussians_2d(means, cholesky_elements, self.H, self.W, self.tile_bounds)
out_img = rasterize_gaussians_sum(self.xys, depths, self.radii, conics, num_tiles_hit,
colors, self._opacity, self.H, self.W, self.BLOCK_H, self.BLOCK_W, background=self.background, return_alpha=False)
out_img = torch.clamp(out_img, 0, 1)
out_img = out_img.view(-1, self.H, self.W, 3).permute(0, 3, 1, 2).contiguous()
return {"render":out_img}
def analysis(self, encoding_dict):
quant_cholesky_elements, feature_dc_index = encoding_dict["quant_cholesky_elements"], encoding_dict["feature_dc_index"]
cholesky_compressed, cholesky_histogram_table, cholesky_unique = compress_matrix_flatten_categorical(quant_cholesky_elements.int().flatten().tolist())
feature_dc_compressed, feature_dc_histogram_table, feature_dc_unique = compress_matrix_flatten_categorical(feature_dc_index.int().flatten().tolist())
cholesky_lookup = dict(zip(cholesky_unique, cholesky_histogram_table.astype(np.float64) / np.sum(cholesky_histogram_table).astype(np.float64)))
feature_dc_lookup = dict(zip(feature_dc_unique, feature_dc_histogram_table.astype(np.float64) / np.sum(feature_dc_histogram_table).astype(np.float64)))
total_bits = 0
initial_bits, codebook_bits = 0, 0
for quantizer_index, layer in enumerate(self.features_dc_quantizer.quantizer.layers):
codebook_bits += layer._codebook.embed.numel()*torch.finfo(layer._codebook.embed.dtype).bits
initial_bits += self.cholesky_quantizer.scale.numel()*torch.finfo(self.cholesky_quantizer.scale.dtype).bits
initial_bits += self.cholesky_quantizer.beta.numel()*torch.finfo(self.cholesky_quantizer.beta.dtype).bits
initial_bits += get_np_size(cholesky_histogram_table) * 8
initial_bits += get_np_size(cholesky_unique) * 8
initial_bits += get_np_size(feature_dc_histogram_table) * 8
initial_bits += get_np_size(feature_dc_unique) * 8
initial_bits += codebook_bits
total_bits += initial_bits
total_bits += self._xyz.numel()*16
total_bits += get_np_size(cholesky_compressed) * 8
total_bits += get_np_size(feature_dc_compressed) * 8
position_bits = self._xyz.numel()*16
cholesky_bits, feature_dc_bits = 0, 0
cholesky_bits += self.cholesky_quantizer.scale.numel()*torch.finfo(self.cholesky_quantizer.scale.dtype).bits
cholesky_bits += self.cholesky_quantizer.beta.numel()*torch.finfo(self.cholesky_quantizer.beta.dtype).bits
cholesky_bits += get_np_size(cholesky_histogram_table) * 8
cholesky_bits += get_np_size(cholesky_unique) * 8
cholesky_bits += get_np_size(cholesky_compressed) * 8
feature_dc_bits += codebook_bits
feature_dc_bits += get_np_size(feature_dc_histogram_table) * 8
feature_dc_bits += get_np_size(feature_dc_unique) * 8
feature_dc_bits += get_np_size(feature_dc_compressed) * 8
bpp = total_bits/self.H/self.W
position_bpp = position_bits/self.H/self.W
cholesky_bpp = cholesky_bits/self.H/self.W
feature_dc_bpp = feature_dc_bits/self.H/self.W
return {"bpp": bpp, "position_bpp": position_bpp,
"cholesky_bpp": cholesky_bpp, "feature_dc_bpp": feature_dc_bpp,}