File size: 5,666 Bytes
4f9eed9 | 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 | #!/usr/bin/env python3
"""
更新 zones_data_mixed.json 的 split 信息:
1. 从 zones_data_coarse.json 中读取已有的 split 映射
2. matterport3d 数据全部设为 train
3. 从 3d-front 中额外随机选择 500 条作为 test
"""
import json
import random
import argparse
from pathlib import Path
from collections import defaultdict
def main():
parser = argparse.ArgumentParser(description="Update split in zones_data_mixed.json")
parser.add_argument("--input", type=str,
default="/home/v-meiszhang/amlt-project/InternScenes/data/zones_data_mixed.json",
help="Input zones_data_mixed.json file")
parser.add_argument("--reference", type=str,
default="/home/v-meiszhang/amlt-project/InternScenes/tools/data_gen/zones_data_coarse.json",
help="Reference file for split mapping")
parser.add_argument("--output", type=str,
default="/home/v-meiszhang/amlt-project/InternScenes/data/zones_data_mixed.json",
help="Output file path")
parser.add_argument("--3dfront-test-size", type=int, default=500,
help="Number of 3d-front samples to mark as test")
parser.add_argument("--seed", type=int, default=42,
help="Random seed")
args = parser.parse_args()
# 读取参考文件获取 split 映射
print(f"Loading reference file: {args.reference}")
with open(args.reference, 'r') as f:
ref_data = json.load(f)
# 构建 scene_path -> split 的映射
split_mapping = {}
for entry in ref_data.get('data', []):
scene_path = entry.get('scene_path', '')
split = entry.get('split', 'train')
if scene_path:
split_mapping[scene_path] = split
print(f"Loaded {len(split_mapping)} split mappings from reference")
# 统计参考文件的 split 分布
ref_split_counts = defaultdict(int)
for split in split_mapping.values():
ref_split_counts[split] += 1
print(f"Reference split distribution: {dict(ref_split_counts)}")
# 读取要更新的文件
print(f"\nLoading input file: {args.input}")
with open(args.input, 'r') as f:
mixed_data = json.load(f)
# 统计数据集分布
dataset_counts = defaultdict(int)
for entry in mixed_data.get('data', []):
scene_path = entry.get('scene_path', '')
if scene_path:
dataset = scene_path.split('/')[0]
dataset_counts[dataset] += 1
print(f"Dataset distribution: {dict(dataset_counts)}")
# 收集 3d-front 的 train 数据索引(用于后续选择 test)
front3d_train_indices = []
# 更新 split
updated_count = 0
matterport_count = 0
for i, entry in enumerate(mixed_data.get('data', [])):
scene_path = entry.get('scene_path', '')
if not scene_path:
continue
dataset = scene_path.split('/')[0]
# matterport3d 全部设为 train
if dataset == 'matterport3d':
entry['split'] = 'train'
matterport_count += 1
continue
# 其他数据集使用参考文件的 split
if scene_path in split_mapping:
old_split = entry.get('split', 'train')
new_split = split_mapping[scene_path]
if old_split != new_split:
entry['split'] = new_split
updated_count += 1
# 收集 3d-front 的 train 索引
if dataset == '3d-front' and entry.get('split') == 'train':
front3d_train_indices.append(i)
print(f"\nUpdated {updated_count} entries based on reference split")
print(f"Set {matterport_count} matterport3d entries to train")
# 从 3d-front 中随机选择额外的 test
random.seed(args.seed)
test_size = getattr(args, '3dfront_test_size', 500)
if len(front3d_train_indices) >= test_size:
selected_test_indices = random.sample(front3d_train_indices, test_size)
for idx in selected_test_indices:
mixed_data['data'][idx]['split'] = 'test'
print(f"Selected {test_size} 3d-front entries as additional test")
else:
print(f"Warning: Only {len(front3d_train_indices)} 3d-front train entries available, selecting all as test")
for idx in front3d_train_indices:
mixed_data['data'][idx]['split'] = 'test'
# 统计最终的 split 分布
final_split_counts = defaultdict(int)
dataset_split_counts = defaultdict(lambda: defaultdict(int))
for entry in mixed_data.get('data', []):
split = entry.get('split', 'train')
scene_path = entry.get('scene_path', '')
dataset = scene_path.split('/')[0] if scene_path else 'unknown'
final_split_counts[split] += 1
dataset_split_counts[dataset][split] += 1
print(f"\nFinal split distribution: {dict(final_split_counts)}")
print("\nPer-dataset split distribution:")
for dataset, splits in sorted(dataset_split_counts.items()):
print(f" {dataset}: {dict(splits)}")
# 更新 metadata
if 'metadata' in mixed_data:
mixed_data['metadata']['split_statistics'] = {
'total': dict(final_split_counts),
'per_dataset': {k: dict(v) for k, v in dataset_split_counts.items()}
}
# 保存结果
print(f"\nSaving to: {args.output}")
with open(args.output, 'w') as f:
json.dump(mixed_data, f, ensure_ascii=False, indent=2)
print("Done!")
if __name__ == "__main__":
main()
|