import os import json import random from tqdm import tqdm cad_classes = ['building', 'chair', 'fan', 'lamp', 'table', 'tools', 'vehicle'] dataset = [] for cad_class in cad_classes: file_name = f'./dataset/assemblies_15/{cad_class}' file_list = os.listdir(file_name) for file in tqdm(file_list): full_name = os.path.join(file_name, file) if 'train_examples.json' not in os.listdir(full_name): continue else: example_path = os.path.join(full_name, 'train_examples.json') with open(example_path, 'r', encoding='utf-8') as f: data = json.load(f) for d in data: dataset.append(d) dataset_path = './dataset/train_dataset.json' random.shuffle(dataset) print(len(dataset)) with open(dataset_path, 'w', encoding='utf-8') as f: json.dump(dataset, f, ensure_ascii=False, indent=4) test_dataset = [] val_dataset = [] for cad_class in cad_classes: file_name = f'./dataset/assemblies_15/{cad_class}' file_list = os.listdir(file_name) for file in tqdm(file_list): full_name = os.path.join(file_name, file) if 'test_examples.json' not in os.listdir(full_name): continue else: example_path = os.path.join(full_name, 'test_examples.json') with open(example_path, 'r', encoding='utf-8') as f: data = json.load(f) for d in data: p = random.random() if p < 0.01: test_dataset.append(d) elif p < 0.02: val_dataset.append(d) else: continue dataset_path = './dataset/test_dataset.json' print(len(test_dataset)) random.shuffle(test_dataset) with open(dataset_path, 'w', encoding='utf-8') as f: json.dump(test_dataset, f, ensure_ascii=False, indent=4) dataset_path = './dataset/val_dataset.json' print(len(val_dataset)) random.shuffle(val_dataset) with open(dataset_path, 'w', encoding='utf-8') as f: json.dump(val_dataset, f, ensure_ascii=False, indent=4)