File size: 4,430 Bytes
569e7bc
 
 
 
 
 
 
 
c93abf7
569e7bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c93abf7
 
 
 
 
569e7bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""Run Robometer on one episode and publish only derived artifacts.

This script is intended for a bounded Hugging Face Job. The source dataset is
never opened for writing; all uploads target the duplicated dataset repository.
"""

from __future__ import annotations

import logging
import os
import subprocess
import sys
from pathlib import Path

from huggingface_hub import HfApi

import lerobot.rewards.robometer.compute_rabc_weights as robometer_compute


DATASET_REPO_ID = os.getenv(
    "ROBOMETER_DATASET_REPO_ID",
    "justintiensmith/Ordering_Constrained_Black_Bin_Sequencing_Robometer",
)
REWARD_MODEL_ID = os.getenv("ROBOMETER_MODEL_ID", "lerobot/Robometer-4B")
EPISODE = int(os.getenv("ROBOMETER_EPISODE", "0"))
CAMERA_KEY = os.getenv("ROBOMETER_CAMERA_KEY", "observation.images.middle")
BATCH_SIZE = int(os.getenv("ROBOMETER_BATCH_SIZE", "4"))
OUTPUT_ROOT = Path(
    os.getenv(
        "ROBOMETER_OUTPUT_DIR",
        "/vol/dissolve/justin/outputs/robometer_black_bin_episode_000",
    )
)
PROGRESS_PATH = OUTPUT_ROOT / "robometer_progress.parquet"
VIDEO_DIR = OUTPUT_ROOT / "videos"


def main() -> None:
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s %(levelname)s %(message)s",
    )

    if EPISODE != 0:
        raise ValueError(
            "This optimized runner currently supports episode 0 only because the in-tree "
            "Robometer helper uses global dataset indices."
        )

    OUTPUT_ROOT.mkdir(parents=True, exist_ok=True)
    VIDEO_DIR.mkdir(parents=True, exist_ok=True)

    # The in-tree helper normally constructs LeRobotDataset without an episode
    # filter. Restricting it here prevents the job from downloading unrelated
    # videos. Episode 0 starts at dataset index 0, so its local/global indices
    # are identical after filtering.
    original_dataset_class = robometer_compute.LeRobotDataset

    def episode_dataset(repo_id: str, download_videos: bool = True):
        return original_dataset_class(
            repo_id,
            episodes=[EPISODE],
            download_videos=download_videos,
        )

    robometer_compute.LeRobotDataset = episode_dataset
    try:
        progress_path = robometer_compute.compute_robometer_progress(
            dataset_repo_id=DATASET_REPO_ID,
            reward_model_path=REWARD_MODEL_ID,
            output_path=str(PROGRESS_PATH),
            device="cuda",
            batch_size=BATCH_SIZE,
            num_subsampled_frames=4,
            episodes=[EPISODE],
            image_key=CAMERA_KEY,
        )
    finally:
        robometer_compute.LeRobotDataset = original_dataset_class

    api = HfApi()
    api.upload_file(
        path_or_fileobj=str(progress_path),
        path_in_repo="robometer/episode_000/progress.parquet",
        repo_id=DATASET_REPO_ID,
        repo_type="dataset",
        commit_message="Add Robometer progress for episode 0",
    )

    lerobot_repo = Path(
        os.getenv(
            "LEROBOT_REPO",
            str(Path(robometer_compute.__file__).resolve().parents[4]),
        )
    )
    overlay_script = lerobot_repo / "examples/dataset/create_progress_videos.py"
    if not overlay_script.exists():
        raise FileNotFoundError(
            f"Could not find the LeRobot overlay script at {overlay_script}. "
            "Set LEROBOT_REPO to the LeRobot source checkout."
        )

    subprocess.run(
        [
            sys.executable,
            str(overlay_script),
            "--repo-id",
            DATASET_REPO_ID,
            "--episode",
            str(EPISODE),
            "--camera-key",
            CAMERA_KEY,
            "--progress-file",
            str(progress_path),
            "--output-dir",
            str(VIDEO_DIR),
        ],
        check=True,
    )

    video_path = VIDEO_DIR / (
        "justintiensmith_Ordering_Constrained_Black_Bin_Sequencing_Robometer_"
        "ep0_progress.mp4"
    )
    if not video_path.exists():
        raise FileNotFoundError(f"Expected rendered video was not created: {video_path}")

    api.upload_file(
        path_or_fileobj=str(video_path),
        path_in_repo="robometer/episode_000/middle_progress.mp4",
        repo_id=DATASET_REPO_ID,
        repo_type="dataset",
        commit_message="Add Robometer visualization for episode 0",
    )
    print(f"PROGRESS_ARTIFACT={progress_path}")
    print(f"VIDEO_ARTIFACT={video_path}")


if __name__ == "__main__":
    main()