-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecompress.py
More file actions
executable file
·444 lines (362 loc) · 14.1 KB
/
decompress.py
File metadata and controls
executable file
·444 lines (362 loc) · 14.1 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import json
import os
from dataclasses import dataclass
from typing import Any, Callable, Dict
import numpy as np
import torch
import torch.nn.functional as F
from torch import Tensor
from sort import sort_splats
def log_transform(x):
return torch.sign(x) * torch.log1p(torch.abs(x))
def inverse_log_transform(y):
return torch.sign(y) * (torch.expm1(torch.abs(y)))
@dataclass
class PngCompression:
"""Uses quantization and sorting to compress splats into PNG files and uses
K-means clustering to compress the spherical harmonic coefficents.
.. warning::
This class requires the `imageio <https://pypi.org/project/imageio/>`_,
`plas <https://github.com/fraunhoferhhi/PLAS.git>`_
and `torchpq <https://github.com/DeMoriarty/TorchPQ?tab=readme-ov-file#install>`_ packages to be installed.
.. warning::
This class might throw away a few lowest opacities splats if the number of
splats is not a square number.
.. note::
The splats parameters are expected to be pre-activation values. It expects
the following fields in the splats dictionary: "means", "scales", "quats",
"opacities", "sh0", "shN". More fields can be added to the dictionary, but
they will only be compressed using NPZ compression.
References:
- `Compact 3D Scene Representation via Self-Organizing Gaussian Grids <https://arxiv.org/abs/2312.13299>`_
- `Making Gaussian Splats more smaller <https://aras-p.info/blog/2023/09/27/Making-Gaussian-Splats-more-smaller/>`_
Args:
use_sort (bool, optional): Whether to sort splats before compression. Defaults to True.
verbose (bool, optional): Whether to print verbose information. Default to True.
"""
use_sort: bool = True
verbose: bool = True
def _get_compress_fn(self, param_name: str) -> Callable:
compress_fn_map = {
"means": _compress_png_16bit,
"scales": _compress_png,
"quats": _compress_png,
"opacities": _compress_png,
"sh0": _compress_png,
"shN": _compress_kmeans,
}
if param_name in compress_fn_map:
return compress_fn_map[param_name]
else:
return _compress_npz
def _get_decompress_fn(self, param_name: str) -> Callable:
decompress_fn_map = {
"means": _decompress_png_16bit,
"scales": _decompress_png,
"quats": _decompress_png,
"opacities": _decompress_png,
"sh0": _decompress_png,
"shN": _decompress_kmeans,
}
if param_name in decompress_fn_map:
return decompress_fn_map[param_name]
else:
return _decompress_npz
def compress(self, compress_dir: str, splats: Dict[str, Tensor]) -> None:
"""Run compression
Args:
compress_dir (str): directory to save compressed files
splats (Dict[str, Tensor]): Gaussian splats to compress
"""
target_n = 65536
n_gs = len(splats["means"])
if n_gs < target_n:
pad = target_n - n_gs
device = splats["means"].device
dtype = splats["means"].dtype
idx = torch.randint(0, n_gs, (pad,), device=device)
for k, v in splats.items():
if k == "opacities":
# fully transparent -> safe
pad_vals = torch.zeros((pad,), device=device, dtype=v.dtype)
else:
pad_vals = v[idx]
splats[k] = torch.cat([v, pad_vals], dim=0)
# Param-specific preprocessing
splats["means"] = log_transform(splats["means"])
splats["quats"] = F.normalize(splats["quats"], dim=-1)
n_gs = len(splats["means"])
n_sidelen = int(n_gs**0.5)
n_crop = n_gs - n_sidelen**2
if n_crop != 0:
splats = _crop_n_splats(splats, n_crop)
print(
f"Warning: Number of Gaussians was not square. Removed {n_crop} Gaussians."
)
if self.use_sort:
splats = sort_splats(splats)
meta = {}
for param_name in splats.keys():
compress_fn = self._get_compress_fn(param_name)
kwargs = {
"n_sidelen": n_sidelen,
"verbose": self.verbose,
}
meta[param_name] = compress_fn(
compress_dir, param_name, splats[param_name], **kwargs
)
with open(os.path.join(compress_dir, "meta.json"), "w") as f:
json.dump(meta, f)
def decompress(self, compress_dir: str) -> Dict[str, Tensor]:
"""Run decompression
Args:
compress_dir (str): directory that contains compressed files
Returns:
Dict[str, Tensor]: decompressed Gaussian splats
"""
with open(os.path.join(compress_dir, "meta.json"), "r") as f:
meta = json.load(f)
splats = {}
for param_name, param_meta in meta.items():
decompress_fn = self._get_decompress_fn(param_name)
splats[param_name] = decompress_fn(compress_dir, param_name, param_meta)
# Param-specific postprocessing
splats["means"] = inverse_log_transform(splats["means"])
return splats
def _crop_n_splats(splats: Dict[str, Tensor], n_crop: int) -> Dict[str, Tensor]:
opacities = splats["opacities"]
keep_indices = torch.argsort(opacities, descending=True)[:-n_crop]
for k, v in splats.items():
splats[k] = v[keep_indices]
return splats
def _compress_png(
compress_dir: str, param_name: str, params: Tensor, n_sidelen: int, **kwargs
) -> Dict[str, Any]:
"""Compress parameters with 8-bit quantization and lossless PNG compression.
Args:
compress_dir (str): compression directory
param_name (str): parameter field name
params (Tensor): parameters
n_sidelen (int): image side length
Returns:
Dict[str, Any]: metadata
"""
import imageio.v2 as imageio
if torch.numel == 0:
meta = {
"shape": list(params.shape),
"dtype": str(params.dtype).split(".")[1],
}
return meta
grid = params.reshape((n_sidelen, n_sidelen, -1))
mins = torch.amin(grid, dim=(0, 1))
maxs = torch.amax(grid, dim=(0, 1))
grid_norm = (grid - mins) / (maxs - mins)
img_norm = grid_norm.detach().cpu().numpy()
img = (img_norm * (2**8 - 1)).round().astype(np.uint8)
img = img.squeeze()
imageio.imwrite(os.path.join(compress_dir, f"{param_name}.png"), img)
meta = {
"shape": list(params.shape),
"dtype": str(params.dtype).split(".")[1],
"mins": mins.tolist(),
"maxs": maxs.tolist(),
}
return meta
def _decompress_png(compress_dir: str, param_name: str, meta: Dict[str, Any]) -> Tensor:
"""Decompress parameters from PNG file.
Args:
compress_dir (str): compression directory
param_name (str): parameter field name
meta (Dict[str, Any]): metadata
Returns:
Tensor: parameters
"""
import imageio.v2 as imageio
if not np.all(meta["shape"]):
params = torch.zeros(meta["shape"], dtype=getattr(torch, meta["dtype"]))
return meta
img = imageio.imread(os.path.join(compress_dir, f"{param_name}.png"))
img_norm = img / (2**8 - 1)
grid_norm = torch.tensor(img_norm)
mins = torch.tensor(meta["mins"])
maxs = torch.tensor(meta["maxs"])
grid = grid_norm * (maxs - mins) + mins
params = grid.reshape(meta["shape"])
params = params.to(dtype=getattr(torch, meta["dtype"]))
return params
def _compress_png_16bit(
compress_dir: str, param_name: str, params: Tensor, n_sidelen: int, **kwargs
) -> Dict[str, Any]:
"""Compress parameters with 16-bit quantization and PNG compression.
Args:
compress_dir (str): compression directory
param_name (str): parameter field name
params (Tensor): parameters
n_sidelen (int): image side length
Returns:
Dict[str, Any]: metadata
"""
import imageio.v2 as imageio
if torch.numel == 0:
meta = {
"shape": list(params.shape),
"dtype": str(params.dtype).split(".")[1],
}
return meta
grid = params.reshape((n_sidelen, n_sidelen, -1))
mins = torch.amin(grid, dim=(0, 1))
maxs = torch.amax(grid, dim=(0, 1))
grid_norm = (grid - mins) / (maxs - mins)
img_norm = grid_norm.detach().cpu().numpy()
img = (img_norm * (2**16 - 1)).round().astype(np.uint16)
img_l = img & 0xFF
img_u = (img >> 8) & 0xFF
imageio.imwrite(
os.path.join(compress_dir, f"{param_name}_l.png"), img_l.astype(np.uint8)
)
imageio.imwrite(
os.path.join(compress_dir, f"{param_name}_u.png"), img_u.astype(np.uint8)
)
meta = {
"shape": list(params.shape),
"dtype": str(params.dtype).split(".")[1],
"mins": mins.tolist(),
"maxs": maxs.tolist(),
}
return meta
def _decompress_png_16bit(
compress_dir: str, param_name: str, meta: Dict[str, Any]
) -> Tensor:
"""Decompress parameters from PNG files.
Args:
compress_dir (str): compression directory
param_name (str): parameter field name
meta (Dict[str, Any]): metadata
Returns:
Tensor: parameters
"""
import imageio.v2 as imageio
if not np.all(meta["shape"]):
params = torch.zeros(meta["shape"], dtype=getattr(torch, meta["dtype"]))
return meta
img_l = imageio.imread(os.path.join(compress_dir, f"{param_name}_l.png"))
img_u = imageio.imread(os.path.join(compress_dir, f"{param_name}_u.png"))
img_u = img_u.astype(np.uint16)
img = (img_u << 8) + img_l
img_norm = img / (2**16 - 1)
grid_norm = torch.tensor(img_norm)
mins = torch.tensor(meta["mins"])
maxs = torch.tensor(meta["maxs"])
grid = grid_norm * (maxs - mins) + mins
params = grid.reshape(meta["shape"])
params = params.to(dtype=getattr(torch, meta["dtype"]))
return params
def _compress_npz(
compress_dir: str, param_name: str, params: Tensor, **kwargs
) -> Dict[str, Any]:
"""Compress parameters with numpy's NPZ compression."""
npz_dict = {"arr": params.detach().cpu().numpy()}
save_fp = os.path.join(compress_dir, f"{param_name}.npz")
os.makedirs(os.path.dirname(save_fp), exist_ok=True)
np.savez_compressed(save_fp, **npz_dict)
meta = {
"shape": params.shape,
"dtype": str(params.dtype).split(".")[1],
}
return meta
def _decompress_npz(compress_dir: str, param_name: str, meta: Dict[str, Any]) -> Tensor:
"""Decompress parameters with numpy's NPZ compression."""
arr = np.load(os.path.join(compress_dir, f"{param_name}.npz"))["arr"]
params = torch.tensor(arr)
params = params.reshape(meta["shape"])
params = params.to(dtype=getattr(torch, meta["dtype"]))
return params
def _compress_kmeans(
compress_dir: str,
param_name: str,
params: Tensor,
n_clusters: int = 65536,
quantization: int = 6,
verbose: bool = True,
**kwargs,
) -> Dict[str, Any]:
"""Run K-means clustering on parameters and save centroids and labels to a npz file.
.. warning::
TorchPQ must installed to use K-means clustering.
Args:
compress_dir (str): compression directory
param_name (str): parameter field name
params (Tensor): parameters to compress
n_clusters (int): number of K-means clusters
quantization (int): number of bits in quantization
verbose (bool, optional): Whether to print verbose information. Default to True.
Returns:
Dict[str, Any]: metadata
"""
try:
from torchpq.clustering import KMeans
except:
raise ImportError(
"Please install torchpq with 'pip install torchpq' to use K-means clustering"
)
if torch.numel == 0:
meta = {
"shape": list(params.shape),
"dtype": str(params.dtype).split(".")[1],
}
return meta
x = params.reshape(params.shape[0], -1).permute(1, 0).contiguous()
#n_clusters = min(n_clusters, x.shape[1])
kmeans = KMeans(n_clusters=n_clusters, distance="manhattan", verbose=verbose)
labels = kmeans.fit(x)
labels = labels.detach().cpu().numpy()
centroids = kmeans.centroids.permute(1, 0)
mins = torch.min(centroids)
maxs = torch.max(centroids)
centroids_norm = (centroids - mins) / (maxs - mins)
centroids_norm = centroids_norm.detach().cpu().numpy()
centroids_quant = (
(centroids_norm * (2**quantization - 1)).round().astype(np.uint8)
)
labels = labels.astype(np.uint16)
npz_dict = {
"centroids": centroids_quant,
"labels": labels,
}
np.savez_compressed(os.path.join(compress_dir, f"{param_name}.npz"), **npz_dict)
meta = {
"shape": list(params.shape),
"dtype": str(params.dtype).split(".")[1],
"mins": mins.tolist(),
"maxs": maxs.tolist(),
"quantization": quantization,
}
return meta
def _decompress_kmeans(
compress_dir: str, param_name: str, meta: Dict[str, Any], **kwargs
) -> Tensor:
"""Decompress parameters from K-means compression.
Args:
compress_dir (str): compression directory
param_name (str): parameter field name
meta (Dict[str, Any]): metadata
Returns:
Tensor: parameters
"""
if not np.all(meta["shape"]):
params = torch.zeros(meta["shape"], dtype=getattr(torch, meta["dtype"]))
return meta
npz_dict = np.load(os.path.join(compress_dir, f"{param_name}.npz"))
centroids_quant = npz_dict["centroids"]
labels = npz_dict["labels"]
centroids_norm = centroids_quant / (2 ** meta["quantization"] - 1)
centroids_norm = torch.tensor(centroids_norm)
mins = torch.tensor(meta["mins"])
maxs = torch.tensor(meta["maxs"])
centroids = centroids_norm * (maxs - mins) + mins
labels = torch.tensor(labels.astype(np.int64), dtype=torch.int64) # Convert labels to a PyTorch tensor
params = centroids[labels]
params = params.reshape(meta["shape"])
params = params.to(dtype=getattr(torch, meta["dtype"]))
return params