| 1 | import os |
| 2 | import argparse |
| 3 | import json |
| 4 | from glob import glob |
| 5 | |
| 6 | import pandas as pd |
| 7 | from tqdm import tqdm |
| 8 | |
| 9 | from spatiallm.tuner.data import ( |
| 10 | LAYOUT_S_PLACEHOLDER, |
| 11 | LAYOUT_E_PLACEHOLDER, |
| 12 | POINT_CLOUD_PLACEHOLDER, |
| 13 | ) |
| 14 | from spatiallm.layout.layout import Layout |
| 15 | |
| 16 | if __name__ == "__main__": |
| 17 | parser = argparse.ArgumentParser() |
| 18 | parser.add_argument( |
| 19 | "-d", |
| 20 | "--dataset_dir", |
| 21 | type=str, |
| 22 | required=True, |
| 23 | help="Path to the input point cloud directory", |
| 24 | ) |
| 25 | parser.add_argument( |
| 26 | "-s", |
| 27 | "--split_csv", |
| 28 | type=str, |
| 29 | required=True, |
| 30 | help="Path to the split csv file", |
| 31 | ) |
| 32 | parser.add_argument( |
| 33 | "--code_template_file", |
| 34 | type=str, |
| 35 | default="code_template.txt", |
| 36 | ) |
| 37 | parser.add_argument( |
| 38 | "-n", |
| 39 | "--dataset_name", |
| 40 | type=str, |
| 41 | required=True, |
| 42 | help="Name of the dataset", |
| 43 | ) |
| 44 | args = parser.parse_args() |
| 45 | |
| 46 | pcd_dir = os.path.join(args.dataset_dir, "pcd") |
| 47 | layout_dir = os.path.join(args.dataset_dir, "layout") |
| 48 | |
| 49 | pcd_files = glob(os.path.join(pcd_dir, "*.ply")) |
| 50 | pcd_scene_ids = [os.path.basename(pcd_file).split(".")[0] for pcd_file in pcd_files] |
| 51 | layout_files = glob(os.path.join(layout_dir, "*.txt")) |
| 52 | layout_scene_ids = [ |
| 53 | os.path.basename(layout_file).split(".")[0] for layout_file in layout_files |
| 54 | ] |
| 55 | |
| 56 | # find the common filenames between pcd_filenames and layout_filenames |
| 57 | scene_ids = set(pcd_scene_ids) & set(layout_scene_ids) |
| 58 | scene_ids = list(scene_ids) |
| 59 | |
| 60 | df = pd.read_csv(args.split_csv, dtype=str) |
| 61 | df.set_index("id", inplace=True) |
| 62 | |
| 63 | print(f"Creating dataset with {len(scene_ids)} scenes...") |
| 64 | |
| 65 | with open(args.code_template_file, "r") as f: |
| 66 | code_template = f.read() |
| 67 | |
| 68 | dataset = { |
| 69 | "train": [], |
| 70 | "val": [], |
| 71 | } |
| 72 | for si, scene_id in enumerate(tqdm(scene_ids)): |
| 73 | try: |
| 74 | if scene_id not in df.index: |
| 75 | continue |
| 76 | split = df.loc[scene_id, "split"] |
| 77 | |
| 78 | # load txt file |
| 79 | with open(os.path.join(layout_dir, f"{scene_id}.txt"), "r") as f: |
| 80 | layout_content = f.read() |
| 81 | |
| 82 | layout = Layout(layout_content) |
| 83 | language_string = layout.to_language_string() |
| 84 | |
| 85 | conversation_data = { |
| 86 | "conversations": [ |
| 87 | { |
| 88 | "from": "human", |
| 89 | "value": f"{POINT_CLOUD_PLACEHOLDER}Detect boxes. The reference code is as followed: {code_template}", |
| 90 | }, |
| 91 | { |
| 92 | "from": "gpt", |
| 93 | "value": f"{LAYOUT_S_PLACEHOLDER}{language_string}{LAYOUT_E_PLACEHOLDER}", |
| 94 | }, |
| 95 | ], |
| 96 | "point_clouds": [ |
| 97 | os.path.join(os.path.basename(pcd_dir), f"{scene_id}.ply"), |
| 98 | ], |
| 99 | } |
| 100 | dataset[split].append(conversation_data) |
| 101 | except Exception as e: |
| 102 | print(f"Error processing scene {scene_id}: {e}") |
| 103 | continue |
| 104 | |
| 105 | # save train set to train.json |
| 106 | print(f"Saving train set with {len(dataset['train'])} samples...") |
| 107 | with open( |
| 108 | os.path.join(args.dataset_dir, f"{args.dataset_name}_train.json"), "w" |
| 109 | ) as f: |
| 110 | json.dump(dataset["train"], f, indent=2) |
| 111 | |
| 112 | # save val set to val.json |
| 113 | print(f"Saving val set with {len(dataset['val'])} samples...") |
| 114 | with open( |
| 115 | os.path.join(args.dataset_dir, f"{args.dataset_name}_val.json"), "w" |
| 116 | ) as f: |
| 117 | json.dump(dataset["val"], f, indent=2) |
| 118 | |
| 119 | # update dataset_info.json |
| 120 | dataset_info = { |
| 121 | f"{args.dataset_name}_train": { |
| 122 | "file_name": f"{args.dataset_name}_train.json", |
| 123 | "formatting": "sharegpt", |
| 124 | "columns": { |
| 125 | "messages": "conversations", |
| 126 | "point_clouds": "point_clouds", |
| 127 | }, |
| 128 | }, |
| 129 | f"{args.dataset_name}_val": { |
| 130 | "file_name": f"{args.dataset_name}_val.json", |
| 131 | "formatting": "sharegpt", |
| 132 | "columns": { |
| 133 | "messages": "conversations", |
| 134 | "point_clouds": "point_clouds", |
| 135 | }, |
| 136 | }, |
| 137 | } |
| 138 | |
| 139 | if not os.path.exists(os.path.join(args.dataset_dir, "dataset_info.json")): |
| 140 | with open(os.path.join(args.dataset_dir, "dataset_info.json"), "w") as f: |
| 141 | json.dump(dataset_info, f, indent=2) |
| 142 | else: |
| 143 | with open(os.path.join(args.dataset_dir, "dataset_info.json"), "r") as f: |
| 144 | original_dataset_info = json.load(f) |
| 145 | original_dataset_info.update(dataset_info) |
| 146 | with open(os.path.join(args.dataset_dir, "dataset_info.json"), "w") as f: |
| 147 | json.dump(original_dataset_info, f, indent=2) |