Entry function, predict based on the given args. Args: args
(args:argparse.Namespace)
| 590 | |
| 591 | |
| 592 | def predict(args:argparse.Namespace): |
| 593 | """ |
| 594 | Entry function, predict based on the given args. |
| 595 | |
| 596 | Args: |
| 597 | args |
| 598 | """ |
| 599 | args.data_dir, args.split, data_subset = get_dataset_dir(args.data_name) |
| 600 | print(f"Predicting on: {args.data_dir}/{args.split}") |
| 601 | print(f"Data subset: {data_subset}") |
| 602 | |
| 603 | if multiprocessing.get_start_method(allow_none=True) != "spawn": |
| 604 | multiprocessing.set_start_method("spawn", force=True) |
| 605 | |
| 606 | with ProcessPoolExecutor(max_workers=len(DEVICES),initializer=_init_llm,initargs=(args.model_path,)) as poolexec: |
| 607 | tasks = [] |
| 608 | print("Moving model to devices") |
| 609 | futures = [poolexec.submit(move_to, dev) for dev in DEVICES] |
| 610 | for fut in futures: print(fut.result()) |
| 611 | |
| 612 | for dataset in data_subset: |
| 613 | save_dir = os.path.join(args.output_dir, dataset) |
| 614 | if not os.path.exists(save_dir): |
| 615 | os.makedirs(save_dir) |
| 616 | |
| 617 | episode_dir = os.path.join(args.data_dir, args.split, dataset) |
| 618 | output_file = os.path.join(save_dir, "predict.jsonl") |
| 619 | |
| 620 | # Get the list of all episodes files |
| 621 | if os.path.exists(episode_dir): |
| 622 | episodes_files = os.listdir(episode_dir) |
| 623 | else: |
| 624 | continue |
| 625 | |
| 626 | future = [] |
| 627 | all_tasks = [] |
| 628 | |
| 629 | print("Loading episodes") |
| 630 | with ThreadPoolExecutor(max_workers=16) as executor: |
| 631 | for episodes_file in episodes_files: |
| 632 | |
| 633 | episodes_path = os.path.join(episode_dir, episodes_file, f"{episodes_file}.json") |
| 634 | try: |
| 635 | with open(episodes_path, 'r', encoding='utf-8') as f: |
| 636 | episodes = json.load(f) |
| 637 | except Exception as e: |
| 638 | print(f"Failed to load {episodes_path}: {e}") |
| 639 | continue |
| 640 | # Skip this file on error |
| 641 | |
| 642 | for episode in episodes: |
| 643 | episode["category"] = dataset |
| 644 | image_path = os.path.join(episode_dir, episodes_file, f"{episodes_file}_{episode['step_id']}.jpeg") |
| 645 | if not os.path.exists(image_path): |
| 646 | image_path = image_path.replace(".jpeg", ".png") |
| 647 | if not os.path.exists(image_path): |
| 648 | image_path = episode['image_path'] |
| 649 | future.append(executor.submit(process_data, episode, image_path, args)) |
no test coverage detected