File size: 2,082 Bytes
6ef1166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
"""
预计算 3D-FUTURE 模型的尺寸缓存

运行一次后会生成 _size_cache.json 文件,后续渲染时直接加载缓存,速度快100倍
"""
import os
import sys
import json
import pickle
from tqdm import tqdm

# 添加 ATISS 路径
ATISS_PATH = os.path.join(os.path.dirname(__file__), 'ATISS')
sys.path.insert(0, ATISS_PATH)

def precompute_size_cache(pkl_path, output_path=None):
    """预计算模型尺寸缓存"""
    if output_path is None:
        output_path = pkl_path.replace('.pkl', '_size_cache.json')
    
    print(f"加载数据集: {pkl_path}")
    with open(pkl_path, 'rb') as f:
        dataset = pickle.load(f)
    
    if not hasattr(dataset, 'objects'):
        print("错误: 数据集没有 objects 属性")
        return
    
    objects = dataset.objects
    print(f"共 {len(objects)} 个模型")
    
    size_cache = {}
    failed = []
    
    for obj in tqdm(objects, desc="计算模型尺寸"):
        if not hasattr(obj, 'model_uid'):
            continue
        
        try:
            # 访问 size 属性会触发计算
            size = obj.size
            size_cache[obj.model_uid] = [float(s) for s in size]
        except Exception as e:
            failed.append((obj.model_uid, str(e)))
    
    print(f"\n成功: {len(size_cache)}, 失败: {len(failed)}")
    
    # 保存缓存
    print(f"保存缓存到: {output_path}")
    with open(output_path, 'w') as f:
        json.dump(size_cache, f)
    
    if failed:
        print(f"\n失败的模型 (前10个):")
        for uid, err in failed[:10]:
            print(f"  {uid}: {err}")
    
    return size_cache


def main():
    import argparse
    parser = argparse.ArgumentParser(description="预计算 3D-FUTURE 模型尺寸缓存")
    parser.add_argument('--pkl', type=str, required=True, help='Pickle 数据集路径')
    parser.add_argument('--output', type=str, default=None, help='输出缓存文件路径')
    args = parser.parse_args()
    
    precompute_size_cache(args.pkl, args.output)


if __name__ == '__main__':
    main()