LLFF / build_sequence.py
SlekLi's picture
Upload 3 files
98a682c verified
"""
build_sequences.py
==================
ไปŽๆ—่ฐฑ๏ผˆgenealogy.pkl๏ผ‰ๅ’Œๅคšๅฐบๅบฆ 3DGS ้‡ๅŒ–็ดขๅผ•ๆž„ๅปบ split ๅบๅˆ—ใ€‚
ๅบๅˆ—็ป“ๆž„๏ผˆๅ›บๅฎš้•ฟๅบฆ MAX_SEQ_LEN = 38๏ผ‰๏ผš
[parent(1)] [uncleร—โ‰ค4] [childร—โ‰ค32] [EOS(1)] [PADร—...]
ๆฏไธช token ๅญ—ๆฎต๏ผš
dx, dy, dz float32 ๅๆ ‡ๅ็งป๏ผˆparent=0,0,0๏ผ›ๅ…ถไป–=็›ธๅฏนparent๏ผ‰
scale_idx int32 scale codebook ็ดขๅผ•
rot_idx int32 rotation codebook ็ดขๅผ•
dc_idx int32 DC codebook ็ดขๅผ•
sh_idx int32 SH codebook ็ดขๅผ•
opacity float32 ไธ้€ๆ˜ŽๅบฆๅŽŸๅ€ผ๏ผˆไธ้‡ๅŒ–๏ผ‰
role uint8 ่บซไปฝๆ ‡่ฏ†๏ผš
0 = parent๏ผˆ็ˆถ่Š‚็‚น๏ผŒๅๆ ‡ๅŽŸ็‚น๏ผ‰
1 = uncle ๏ผˆๅ”ไผฏ่Š‚็‚น๏ผŒ็›ธๅฏนๅๆ ‡๏ผ‰
2 = child ๏ผˆๅญ่Š‚็‚น๏ผŒ็›ธๅฏนๅๆ ‡๏ผ‰
3 = EOS ๏ผˆๅบๅˆ—็ป“ๆŸ็ฌฆ๏ผ‰
4 = PAD ๏ผˆ่กฅ้ฝ๏ผŒไธๅ‚ไธŽ่ฎก็ฎ—๏ผ‰
ๅฑ‚็บงๆ–นๅ‘๏ผˆ็ฒ—โ†’็ป†๏ผ‰๏ผš
quant_paths ๆŒ‰ L3, L2, L1, L0 ้กบๅบไผ ๅ…ฅ
L3 ๆœ€็ฒ—๏ผˆ็‚นๆœ€ๅฐ‘๏ผ‰ไฝœไธบ parent๏ผŒ้€็บงๅ‘็ป†ๅฑ•ๅผ€
"""
import os
import argparse
import pickle
import numpy as np
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# ๅธธ้‡
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
ROLE_PARENT = np.uint8(0)
ROLE_UNCLE = np.uint8(1)
ROLE_CHILD = np.uint8(2)
ROLE_EOS = np.uint8(3)
ROLE_PAD = np.uint8(4)
MAX_CHILDREN = 32
MAX_UNCLES = 4
# ๅ›บๅฎšๅบๅˆ—้•ฟๅบฆ๏ผšparent(1) + uncle(4) + child(32) + EOS(1)
MAX_SEQ_LEN = 1 + MAX_UNCLES + MAX_CHILDREN + 1 # = 38
TOKEN_DTYPE = np.dtype([
('dx', np.float32),
('dy', np.float32),
('dz', np.float32),
('scale_idx', np.int32),
('rot_idx', np.int32),
('dc_idx', np.int32),
('sh_idx', np.int32),
('opacity', np.float32),
('role', np.uint8),
])
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 1. ๅŠ ่ฝฝ้‡ๅŒ–็ดขๅผ•
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def load_quantized(npz_path: str) -> dict:
npz = np.load(npz_path)
return {
'scale_indices': npz['scale_indices'],
'rotation_indices': npz['rotation_indices'],
'dc_indices': npz['dc_indices'],
'sh_indices': npz['sh_indices'],
'positions': npz['positions'],
'opacities': npz['opacities'].squeeze(),
}
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 2. ๅŠ ่ฝฝๆ—่ฐฑ
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def load_genealogy(genealogy_path: str) -> dict:
with open(genealogy_path, 'rb') as f:
return pickle.load(f)
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 3. Token ๆž„้€ ๅทฅๅ…ท
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def make_token(gauss_idx: int, quant: dict,
parent_pos: np.ndarray, role: np.uint8) -> np.ndarray:
"""ๆž„้€ ๅ•ไธช็‰นๅพ token๏ผŒๅๆ ‡ไธบ็›ธๅฏน parent_pos ็š„ๅ็งปใ€‚"""
pos = quant['positions'][gauss_idx]
delta = pos - parent_pos
token = np.zeros(1, dtype=TOKEN_DTYPE)
token['dx'] = delta[0]
token['dy'] = delta[1]
token['dz'] = delta[2]
token['scale_idx'] = quant['scale_indices'][gauss_idx]
token['rot_idx'] = quant['rotation_indices'][gauss_idx]
token['dc_idx'] = quant['dc_indices'][gauss_idx]
token['sh_idx'] = quant['sh_indices'][gauss_idx]
token['opacity'] = quant['opacities'][gauss_idx]
token['role'] = role
return token[0]
def make_eos_token() -> np.ndarray:
"""็ป“ๆŸ็ฌฆ๏ผš็‰นๅพๅ…จ 0๏ผŒrole=3ใ€‚"""
token = np.zeros(1, dtype=TOKEN_DTYPE)
token['role'] = ROLE_EOS
return token[0]
def make_pad_token() -> np.ndarray:
"""่กฅ้ฝ็ฌฆ๏ผš็‰นๅพๅ…จ 0๏ผŒrole=4๏ผŒไธๅ‚ไธŽไปปไฝ• loss/attentionใ€‚"""
token = np.zeros(1, dtype=TOKEN_DTYPE)
token['role'] = ROLE_PAD
return token[0]
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 4. ๆž„ๅปบๅ•ๅฑ‚ๆ‰€ๆœ‰ split ๅบๅˆ—
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def build_level_sequences(
parent_quant: dict,
child_quant: dict,
children_ids: np.ndarray, # (N_coarse, MAX_CHILDREN)๏ผŒ-1 ไธบ็ฉบไฝ
max_uncles: int = MAX_UNCLES,
min_children: int = 1,
fixed_len: int = MAX_SEQ_LEN,
) -> list:
"""
้ๅކๆฏไธช็ฒ—่Š‚็‚น๏ผŒๆž„้€ ไธ€ๆกๅ›บๅฎš้•ฟๅบฆ็š„ split ๅบๅˆ—ใ€‚
ๅบๅˆ— token ้กบๅบ๏ผš
[parent(1)] [uncleร—โ‰คmax_uncles] [childร—N_valid] [EOS(1)] [PADร—...]
role ็ผ–็ ๏ผš
0 = parent ๅๆ ‡ๅ›บๅฎšไธบ (0,0,0)๏ผŒ็‰นๅพๆฅ่‡ช่‡ช่บซ
1 = uncle ๅๆ ‡็›ธๅฏน parent๏ผŒ็‰นๅพๆฅ่‡ช่‡ช่บซ
2 = child ๅๆ ‡็›ธๅฏน parent๏ผŒ็‰นๅพๆฅ่‡ช็ป†ๅฐบๅบฆ
3 = EOS ็ป“ๆŸ็ฌฆ๏ผŒๆ ‡ๅฟ—็œŸๅฎžๅญ่Š‚็‚นๅทฒๅ…จ้ƒจ่พ“ๅ‡บ
4 = PAD ่กฅ้ฝ๏ผŒattention mask ไธญ่ขซๅฑ่”ฝ
่ฟ”ๅ›ž๏ผšlist of np.ndarray๏ผŒๆฏไธชๅ…ƒ็ด  shape (fixed_len,) TOKEN_DTYPE
"""
N_parents = children_ids.shape[0]
sequences = []
for p_idx in range(N_parents):
child_row = children_ids[p_idx]
valid_children = child_row[child_row >= 0]
if len(valid_children) < min_children:
continue
parent_pos = parent_quant['positions'][p_idx]
tokens = []
# โ”€โ”€ parent๏ผˆๅๆ ‡็ฝฎ้›ถ๏ผ‰โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
t = make_token(p_idx, parent_quant, parent_pos, ROLE_PARENT)
t['dx'] = t['dy'] = t['dz'] = 0.0
tokens.append(t)
# โ”€โ”€ uncle๏ผˆๅ‰ๅŽๅ„ half ไธช็ฉบ้—ด้‚ปๅฑ…๏ผ‰โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
half = max_uncles // 2
added_uncles = 0
for offset in list(range(-half, 0)) + list(range(1, half + 1)):
u_idx = p_idx + offset
if 0 <= u_idx < N_parents and added_uncles < max_uncles:
tokens.append(
make_token(u_idx, parent_quant, parent_pos, ROLE_UNCLE)
)
added_uncles += 1
# โ”€โ”€ child๏ผˆๆ‰€ๆœ‰ๆœ‰ๆ•ˆๅญ่Š‚็‚น๏ผ‰โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
for c_idx in valid_children:
tokens.append(
make_token(int(c_idx), child_quant, parent_pos, ROLE_CHILD)
)
# โ”€โ”€ EOS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
tokens.append(make_eos_token())
# โ”€โ”€ PAD ่กฅ้ฝๅˆฐ fixed_len โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
while len(tokens) < fixed_len:
tokens.append(make_pad_token())
# ่ถ…้•ฟๆˆชๆ–ญ๏ผˆๆž็ซฏๆƒ…ๅ†ต๏ผšuncle+child ่ถ…ๅ‡บ fixed_len-2๏ผ‰
if len(tokens) > fixed_len:
tokens = tokens[:fixed_len - 1]
tokens.append(make_eos_token())
sequences.append(np.array(tokens, dtype=TOKEN_DTYPE))
return sequences
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 5. ๅคšๅฑ‚ๅบๅˆ—ๆž„ๅปบไธปๅ‡ฝๆ•ฐ
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def build_all_sequences(
quant_paths: list, # ๆŒ‰็ฒ—โ†’็ป†้กบๅบ๏ผš[L3.npz, L2.npz, L1.npz, L0.npz]
genealogy_path: str,
save_dir: str,
max_uncles: int = MAX_UNCLES,
min_children: int = 1,
) -> None:
"""
quant_paths ๆŒ‰็ฒ—โ†’็ป†้กบๅบไผ ๅ…ฅ๏ผŒไพ‹ๅฆ‚๏ผš
[L3_quantized.npz, L2_quantized.npz, L1_quantized.npz, L0_quantized.npz]
genealogy.pkl ๆ ผๅผ๏ผš
genealogy[3]['children_ids'] shape (N_L3, MAX_CHILDREN)
L3็ฒ—่Š‚็‚น โ†’ L2็ป†่Š‚็‚น็ดขๅผ•
genealogy[2]['children_ids'] shape (N_L2, MAX_CHILDREN)
L2็ฒ—่Š‚็‚น โ†’ L1็ป†่Š‚็‚น็ดขๅผ•
genealogy[1]['children_ids'] shape (N_L1, MAX_CHILDREN)
L1็ฒ—่Š‚็‚น โ†’ L0็ป†่Š‚็‚น็ดขๅผ•
่พ“ๅ‡บ๏ผˆๆŒ‰็ฒ—โ†’็ป†ๅ‘ฝๅ๏ผ‰๏ผš
save_dir/sequences_L3_to_L2.pkl
save_dir/sequences_L2_to_L1.pkl
save_dir/sequences_L1_to_L0.pkl
"""
os.makedirs(save_dir, exist_ok=True)
print(f"[build] ๅŠ ่ฝฝๆ—่ฐฑ๏ผš{genealogy_path}")
genealogy = load_genealogy(genealogy_path)
print(f"[build] ๅŠ ่ฝฝ้‡ๅŒ–ๆ•ฐๆฎ๏ผˆๅ…ฑ {len(quant_paths)} ไธชๅฐบๅบฆ๏ผŒ็ฒ—โ†’็ป†้กบๅบ๏ผ‰...")
quants = []
for path in quant_paths:
print(f" {os.path.basename(path)}")
quants.append(load_quantized(path))
# quants[0]=ๆœ€็ฒ—(L3), quants[1]=L2, ..., quants[-1]=ๆœ€็ป†(L0)
n_levels = len(quants)
# genealogy key: quants[0]โ†’quants[1] ๅฏนๅบ” key=n_levels-1๏ผˆๅณ3๏ผ‰
# quants[1]โ†’quants[2] ๅฏนๅบ” key=n_levels-2๏ผˆๅณ2๏ผ‰
# ...
for i in range(n_levels - 1):
coarse_level = n_levels - 1 - i # 3, 2, 1
fine_level = coarse_level - 1 # 2, 1, 0
gen_key = coarse_level # genealogy key ไธŽ็ฒ—ๅฐบๅบฆ็ผ–ๅทไธ€่‡ด
coarse_name = f"L{coarse_level}"
fine_name = f"L{fine_level}"
if gen_key not in genealogy:
print(f"[build] ่ญฆๅ‘Š๏ผšๆ—่ฐฑไธญๆ—  key={gen_key}๏ผŒ่ทณ่ฟ‡ {coarse_name}โ†’{fine_name}")
continue
parent_quant = quants[i]
child_quant = quants[i + 1]
children_ids = genealogy[gen_key]['children_ids'] # (N_coarse, 32)
print(f"\n[build] ๆž„ๅปบ {coarse_name}โ†’{fine_name} ๅบๅˆ—")
print(f" ็ˆถ่Š‚็‚นๆ•ฐ={children_ids.shape[0]}, "
f"children_ids.shape={children_ids.shape}")
sequences = build_level_sequences(
parent_quant, child_quant, children_ids,
max_uncles=max_uncles,
min_children=min_children,
fixed_len=MAX_SEQ_LEN,
)
if len(sequences) == 0:
print(" [่ญฆๅ‘Š] ๆฒกๆœ‰ๆœ‰ๆ•ˆๅบๅˆ—๏ผŒ่ฏทๆฃ€ๆŸฅๆ—่ฐฑไธŽ้‡ๅŒ–ๆ•ฐๆฎ")
continue
# ็ปŸ่ฎกๅญ่Š‚็‚นๆ•ฐๅˆ†ๅธƒ
child_counts = np.array([
int((s['role'] == ROLE_CHILD).sum()) for s in sequences
])
print(f" ็”Ÿๆˆๅบๅˆ—ๆ•ฐ๏ผš{len(sequences)}")
print(f" ๅญ่Š‚็‚นๆ•ฐ๏ผšmin={child_counts.min()}, "
f"max={child_counts.max()}, mean={child_counts.mean():.2f}")
# role ๅˆ†ๅธƒ็ปŸ่ฎก
all_roles = np.concatenate([s['role'] for s in sequences])
for r, name in [(0,'parent'),(1,'uncle'),(2,'child'),(3,'EOS'),(4,'PAD')]:
cnt = (all_roles == r).sum()
print(f" role={r}({name:6s})๏ผš{cnt:,} tokens")
out_path = os.path.join(save_dir,
f"sequences_{coarse_name}_to_{fine_name}.pkl")
with open(out_path, 'wb') as f:
pickle.dump(sequences, f, protocol=4)
size_mb = os.path.getsize(out_path) / 1024 / 1024
print(f" ไฟๅญ˜ โ†’ {out_path} ({size_mb:.2f} MB)")
print(f"\n[build] ๅ…จ้ƒจๅบๅˆ—ๆž„ๅปบๅฎŒๆˆ๏ผ")
print(f" ๅ›บๅฎšๅบๅˆ—้•ฟๅบฆ MAX_SEQ_LEN={MAX_SEQ_LEN} "
f"(parent=1, uncleโ‰ค{MAX_UNCLES}, childโ‰ค{MAX_CHILDREN}, EOS=1)")
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# 6. CLI
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def parse_args():
parser = argparse.ArgumentParser(description="ๆž„ๅปบ 3DGS split ๅบๅˆ—")
parser.add_argument('--quant_paths', nargs='+', required=True,
help='้‡ๅŒ– .npz ่ทฏๅพ„๏ผŒๆŒ‰็ฒ—โ†’็ป†้กบๅบ๏ผšL3 L2 L1 L0')
parser.add_argument('--genealogy', required=True,
help='ๆ—่ฐฑ genealogy.pkl ่ทฏๅพ„')
parser.add_argument('--save_dir', default='./sequences',
help='ๅบๅˆ—่พ“ๅ‡บ็›ฎๅฝ•๏ผˆ้ป˜่ฎค ./sequences๏ผ‰')
parser.add_argument('--max_uncles', type=int, default=MAX_UNCLES)
parser.add_argument('--min_children', type=int, default=1)
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
build_all_sequences(
quant_paths=args.quant_paths,
genealogy_path=args.genealogy,
save_dir=args.save_dir,
max_uncles=args.max_uncles,
min_children=args.min_children,
)