(args)
| 128 | |
| 129 | |
| 130 | def predict(args): |
| 131 | args.data_dir, args.split, data_subset = get_dataset_dir(args.data_name) |
| 132 | print(f"Predicting on: {args.data_dir}/{args.split}") |
| 133 | print(f"Data subset: {data_subset}") |
| 134 | |
| 135 | if multiprocessing.get_start_method(allow_none=True) != "spawn": |
| 136 | multiprocessing.set_start_method("spawn", force=True) |
| 137 | |
| 138 | with ProcessPoolExecutor(max_workers=len(DEVICES),initializer=_init_llm,initargs=(args.model_path,)) as poolexec: |
| 139 | tasks = [] |
| 140 | print("Moving model to devices") |
| 141 | futures = [poolexec.submit(move_to, dev) for dev in DEVICES] |
| 142 | for fut in futures: |
| 143 | print(fut.result()) |
| 144 | |
| 145 | for dataset in data_subset: |
| 146 | save_dir = os.path.join(args.output_dir, dataset) |
| 147 | if not os.path.exists(save_dir): |
| 148 | os.makedirs(save_dir) |
| 149 | |
| 150 | episode_dir = os.path.join(args.data_dir, args.split, dataset) |
| 151 | output_file = os.path.join(save_dir, "predict.jsonl") |
| 152 | |
| 153 | # Get the list of all episodes files |
| 154 | if os.path.exists(episode_dir): |
| 155 | episodes_files = os.listdir(episode_dir) |
| 156 | else: |
| 157 | continue |
| 158 | |
| 159 | future = [] |
| 160 | all_tasks = [] |
| 161 | print("Loading episodes") |
| 162 | with ThreadPoolExecutor(max_workers=16) as executor: |
| 163 | for episodes_file in episodes_files: |
| 164 | |
| 165 | episodes_path = os.path.join(episode_dir, episodes_file, f"{episodes_file}.json") |
| 166 | try: |
| 167 | with open(episodes_path, 'r', encoding='utf-8') as f: |
| 168 | episodes = json.load(f) |
| 169 | except Exception as e: |
| 170 | print(f"Failed to load {episodes_path}: {e}") |
| 171 | continue |
| 172 | # Skip this file on error |
| 173 | |
| 174 | for episode in episodes: |
| 175 | episode["category"] = dataset |
| 176 | image_path = os.path.join(episode_dir, episodes_file, f"{episodes_file}_{episode['step_id']}.jpeg") |
| 177 | if not os.path.exists(image_path): |
| 178 | image_path = image_path.replace(".jpeg", ".png") |
| 179 | if not os.path.exists(image_path): |
| 180 | image_path = episode['image_path'] |
| 181 | future.append(executor.submit(load_image, episode, image_path, args.data_name)) |
| 182 | |
| 183 | for f in as_completed(future): |
| 184 | all_tasks.append(f.result()) |
| 185 | |
| 186 | with open(output_file, "w", encoding="utf-8") as f_out: |
| 187 | print("Predicting") |
no test coverage detected