LLFF / change_lineage.py
SlekLi's picture
Upload change_lineage.py
2d2f9ed verified
import json
import pickle
import numpy as np
import os
def convert_lineage_to_split_genealogy(
lineage_json_path: str,
save_pkl_path: str,
max_children: int = 8, # merge后每个粗点平均对应4个细点,留余量
):
"""
正确的转换方向:
lineage["L1"][i] = [a, b, c, d]
含义:L1第i个粗点 由 L0中的a,b,c,d细点 merge而来
反转:L1第i个粗点 → 子节点是 L0中的 a,b,c,d
genealogy[1]['children_ids'] shape (N_L1粗, max_children)
含义:L1第i个粗点,在L0中对应的细节点索引
使用时:
父节点 = L3/L2/L1 的粗粒度高斯点
子节点 = 更细粒度的高斯点(向原始方向展开)
序列:L3(粗)→L2(细)→L1(更细)→L0(原始)
"""
print(f"[convert] 加载 {lineage_json_path}")
with open(lineage_json_path, 'r') as f:
lineage = json.load(f)
genealogy = {}
# 注意方向:粗→细,所以 key 含义变了
# genealogy[1]: L1粗节点 → L0细节点
# genealogy[2]: L2粗节点 → L1细节点
# genealogy[3]: L3粗节点 → L2细节点
level_map = {
"L1": 1,
"L2": 2,
"L3": 3,
}
for level_name, gen_key in level_map.items():
if level_name not in lineage:
continue
# merge_list[粗点索引] = [细点索引, ...]
merge_list = lineage[level_name]
n_coarse = len(merge_list)
# 统计每个粗点有多少个细点
child_counts = [len(parents) for parents in merge_list]
max_actual = max(child_counts)
avg_actual = np.mean(child_counts)
print(f"[convert] {level_name}(粗→细方向):")
print(f" 粗节点数={n_coarse}")
print(f" 每个粗节点平均对应细节点数={avg_actual:.2f}")
print(f" 最多子节点数={max_actual}")
# 直接用 merge_list 构建 children_ids
# merge_list[i] 就是 L(k-1)_粗节点i 对应的所有 L(k-2)_细节点索引
actual_max = min(max_actual, max_children)
children_ids = np.full((n_coarse, actual_max), fill_value=-1, dtype=np.int32)
truncated = 0
for coarse_idx, fine_indices in enumerate(merge_list):
n = min(len(fine_indices), actual_max)
children_ids[coarse_idx, :n] = fine_indices[:n]
if len(fine_indices) > actual_max:
truncated += 1
if truncated:
print(f" ⚠️ {truncated} 个粗节点子节点数超过{actual_max}被截断")
genealogy[gen_key] = {
'children_ids': children_ids, # (N_coarse, max_children)
}
with open(save_pkl_path, 'wb') as f:
pickle.dump(genealogy, f, protocol=4)
size_mb = os.path.getsize(save_pkl_path) / 1024 / 1024
print(f"\n[convert] 已保存 → {save_pkl_path} ({size_mb:.2f} MB)")
return genealogy
def verify(lineage_json_path, genealogy_pkl_path, n_sample=5):
with open(lineage_json_path, 'r') as f:
lineage = json.load(f)
with open(genealogy_pkl_path, 'rb') as f:
genealogy = pickle.load(f)
print("\n[verify] 抽查:粗节点的子节点应与 lineage 一致")
for level_name, gen_key in [("L1", 1), ("L2", 2), ("L3", 3)]:
if level_name not in lineage or gen_key not in genealogy:
continue
merge_list = lineage[level_name]
children_ids = genealogy[gen_key]['children_ids']
sample_idx = np.random.choice(len(merge_list), min(n_sample, len(merge_list)), replace=False)
errors = 0
for i in sample_idx:
expected = set(merge_list[i])
actual = set(int(x) for x in children_ids[i] if x >= 0)
if not expected.issubset(actual):
print(f" ❌ {level_name}[{i}]: expected={expected}, actual={actual}")
errors += 1
if errors == 0:
print(f" ✅ {level_name}{len(sample_idx)} 个粗节点验证通过")
if __name__ == '__main__':
genealogy = convert_lineage_to_split_genealogy(
lineage_json_path="outputs/lineage.json",
save_pkl_path="outputs/genealogy.pkl",
max_children=4,
)
verify("outputs/lineage.json", "outputs/genealogy.pkl", n_sample=10)