-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreflectance_compress.py
149 lines (106 loc) · 5.52 KB
/
reflectance_compress.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
import os
import time
import argparse
import numpy as np
from glob import glob
from tqdm import tqdm
import torch
import torchac
from pytorch3d.ops.knn import _KNN, knn_gather, knn_points
import kit
from net import Network
torch.cuda.manual_seed(1)
torch.manual_seed(1)
np.random.seed(1)
parser = argparse.ArgumentParser(
prog='reflectance_compress.py',
description='Compress Point Cloud Reflectance Attributes.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('--ckpt', required=True, help='Trained ckeckpoint file.')
parser.add_argument('--input_glob', required=True, help='Point clouds glob pattern to be compressed.')
parser.add_argument('--compressed_path', required=True, help='Compressed file saving directory.')
parser.add_argument('--local_region', type=int, help='Neighbooring scope for context windows (i.e., K).', default=8)
parser.add_argument('--granularity', type=int, help='Upper limit for each group (i.e., s*).', default=2**14)
parser.add_argument('--init_ratio', type=int, help='The ratio for size of the very first group (i.e., alpha).', default=128)
parser.add_argument('--expand_ratio', type=int, help='Expand ratio (i.e., r)', default=2)
parser.add_argument('--prg_seed', type=int, help='Pseudorandom seed for PRG.', default=2147483647)
args = parser.parse_args()
if not os.path.exists(args.compressed_path):
os.makedirs(args.compressed_path)
files = np.array(glob(args.input_glob, recursive=True))
np.random.shuffle(files)
files = files[:]
net = Network(local_region=args.local_region, granularity=args.granularity, init_ratio=args.init_ratio, expand_ratio=args.expand_ratio)
net.load_state_dict(torch.load(args.ckpt))
net = torch.compile(net, mode='max-autotune')
net.cuda().eval()
# warm up our model
# since the very first step of network is extremely slow...
_ = net.mu_sigma_pred(net.pt(torch.rand((1, 32, 8, 3)).cuda(), torch.rand((1, 32, 8, 3)).cuda()))
enc_times = []
fnames, bpps = [], []
with torch.no_grad():
for f in tqdm(files):
fname = os.path.split(f)[-1]
pc = kit.read_point_cloud_reflactance(f)
batch_x = torch.tensor(pc).unsqueeze(0)
batch_x = batch_x.cuda()
B, N, _ = batch_x.shape
torch.cuda.synchronize()
TIME_STAMP = time.time()
g_cpu = torch.Generator()
g_cpu.manual_seed(args.prg_seed)
batch_x = batch_x[:, torch.randperm(batch_x.size()[1], generator=g_cpu), :]
_, N, _ = batch_x.shape
base_size = min(N//args.init_ratio, args.granularity)
window_size = base_size
context_ls, target_ls = [], []
cursor = base_size
while cursor<N:
window_size = min(window_size*args.expand_ratio, args.granularity)
context_ls.append(batch_x[:, :cursor, :])
target_ls.append(batch_x[:, cursor:cursor+window_size, :])
cursor += window_size
total_ac_time = 0
total_bits = 0
for i in range(len(target_ls)):
target_geo, target_attr = target_ls[i][:, :, :3].clone(), target_ls[i][:, :, 3:].clone()
context_geo, context_attr = context_ls[i][:, :, :3].clone(), context_ls[i][:, :, 3:].clone()
target_attr, context_attr = target_attr.repeat((1, 1, 3)), context_attr.repeat((1, 1, 3))
context_attr = context_attr / 100
_, idx, context_grouped_geo = knn_points(target_geo, context_geo, K=net.local_region, return_nn=True)
context_grouped_attr = knn_gather(context_attr, idx)
context_grouped_geo = context_grouped_geo - target_geo.view(B, -1, 1, 3)
context_grouped_geo = kit.n_scale_ball(context_grouped_geo)
feature = net.pt(context_grouped_geo, context_grouped_attr)
mu_sigma = net.mu_sigma_pred(feature)
mu, sigma = mu_sigma[:, :, :3]+0.5, torch.exp(mu_sigma[:, :, 3:])
cdf = kit.get_cdf_reflactance(mu[0]*100, sigma[0]*32)
target_feature = (target_attr[0]).to(torch.int16)
cdf = cdf[:, 0, :]
target_feature = target_feature[:, 0]
# byte_stream = torchac.encode_float_cdf(cdf.cpu(), target_feature.cpu(), check_input_bounds=True)
byte_stream = torchac.encode_int16_normalized_cdf(
kit._convert_to_int_and_normalize(cdf, True).cpu(),
target_feature.cpu())
comp_f = os.path.join(args.compressed_path, fname+f'.{i}.bin')
with open(comp_f, 'wb') as fout:
fout.write(byte_stream)
total_bits += kit.get_file_size_in_bits(comp_f)
comp_base_f = os.path.join(args.compressed_path, fname+'.c.bin')
context_base = context_ls[0][0, :, 3:].detach().cpu().numpy()
torch.cuda.synchronize()
enc_times.append(time.time() - TIME_STAMP)
context_base.astype(np.uint8).tofile(comp_base_f)
total_bits += kit.get_file_size_in_bits(comp_base_f)
geo_f = os.path.join(args.compressed_path, fname+'.geo.bin')
batch_x[:, :, :3].detach().cpu().numpy().astype(np.float32).tofile(geo_f)
fnames.append(fname)
bpps.append(np.round(total_bits/N, 3))
print('Memory:', round(torch.cuda.max_memory_allocated()/1024/1024, 3), 'MB')
print(f'Done! Total {len(fnames)} \
| reflectance bpp: {round(np.array(bpps).mean(), 3)}\
| ave enc time: {round(np.array(enc_times).mean(), 3)} s')
print('Params:', sum(p.numel() for p in net.parameters()),
'Trainable params:', sum(p.numel() for p in net.parameters() if p.requires_grad))