| |
| """ |
| 更新 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() |
| |
| |
| print(f"Loading reference file: {args.reference}") |
| with open(args.reference, 'r') as f: |
| ref_data = json.load(f) |
| |
| |
| 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") |
| |
| |
| 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)}") |
| |
| |
| front3d_train_indices = [] |
| |
| |
| 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] |
| |
| |
| if dataset == 'matterport3d': |
| entry['split'] = 'train' |
| matterport_count += 1 |
| continue |
| |
| |
| 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 |
| |
| |
| 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") |
| |
| |
| 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' |
| |
| |
| 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)}") |
| |
| |
| 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() |
|
|