File size: 11,349 Bytes
0d80452
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 🤗 LeRobot Quickstart\n",
    "\n",
    "Calibration → teleoperation → data collection → training → evaluation.\n",
    "\n",
    "Install the required dependencies: `pip install -e .[notebook,dataset,training,viz,hardware]`.\n",
    "\n",
    "**How to use:**\n",
    "1. Edit the **Configuration** cell with your settings.\n",
    "2. Run all cells (`Run All`).\n",
    "3. Each section prints a ready-to-paste terminal command - copy it and run it.\n",
    "\n",
    "Each setup is different, please refer to the [LeRobot documentation](https://huggingface.co/docs/lerobot/il_robots) for more details on each step and available options. <br>\n",
    "Feel free to make this notebook your own and adapt it to your needs!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## Utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _cameras_arg(cameras: dict) -> str:\n",
    "    if not cameras:\n",
    "        return \"\"\n",
    "    entries = [f\"{n}: {{{', '.join(f'{k}: {v}' for k, v in cfg.items())}}}\" for n, cfg in cameras.items()]\n",
    "    return \"{ \" + \", \".join(entries) + \" }\"\n",
    "\n",
    "\n",
    "def print_cmd(*parts: str) -> None:\n",
    "    \"\"\"Print a shell command with line continuations, skipping empty parts.\"\"\"\n",
    "    non_empty = [p for p in parts if p]\n",
    "    print(\" \\\\\\n    \".join(non_empty))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## Configuration\n",
    "\n",
    "Edit this cell, then **Run All** to generate all commands below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Robot (follower) - run `lerobot-find-port` to discover the port\n",
    "ROBOT_TYPE = \"so101_follower\"\n",
    "ROBOT_PORT = \"/dev/ttyACM0\"\n",
    "ROBOT_ID = \"my_follower_arm\"\n",
    "\n",
    "# Teleop (leader) - run `lerobot-find-port` to discover the port\n",
    "TELEOP_TYPE = \"so101_leader\"\n",
    "TELEOP_PORT = \"/dev/ttyACM1\"\n",
    "TELEOP_ID = \"my_leader_arm\"\n",
    "\n",
    "# Cameras - set to {} to disable\n",
    "# Run `lerobot-find-cameras opencv` to list available cameras and their indices\n",
    "CAMERAS = {\n",
    "    \"top\": {\"type\": \"opencv\", \"index_or_path\": 2, \"width\": 640, \"height\": 480, \"fps\": 30},\n",
    "    \"wrist\": {\"type\": \"opencv\", \"index_or_path\": 4, \"width\": 640, \"height\": 480, \"fps\": 30},\n",
    "}\n",
    "\n",
    "# Dataset\n",
    "HF_USER = \"your_hf_username\"  # `hf auth whoami` to find your username\n",
    "DATASET_NAME = \"my_so101_dataset\"\n",
    "TASK_DESCRIPTION = \"pick and place the block\"\n",
    "NUM_EPISODES = 10\n",
    "\n",
    "# Training\n",
    "POLICY_TYPE = \"act\"  # act, diffusion, smolvla, ...\n",
    "POLICY_DEVICE = \"cuda\"  # cuda / cpu / mps\n",
    "TRAIN_STEPS = 10_000\n",
    "SAVE_FREQ = 2_000\n",
    "OUTPUT_DIR = f\"outputs/train/{DATASET_NAME}\"\n",
    "\n",
    "# Inference - Hub repo ID or local checkpoint path\n",
    "# e.g. set to f\"{OUTPUT_DIR}/checkpoints/last\" to use a local checkpoint\n",
    "POLICY_PATH = f\"{HF_USER}/{DATASET_NAME}_{POLICY_TYPE}\"\n",
    "LAST_CHECKPOINT_PATH = f\"{OUTPUT_DIR}/checkpoints/last\"\n",
    "\n",
    "# Derived\n",
    "DATASET_REPO_ID = f\"{HF_USER}/{DATASET_NAME}\"\n",
    "DATASET_ROOT = f\"data/{DATASET_NAME}\"\n",
    "POLICY_REPO_ID = f\"{HF_USER}/{DATASET_NAME}_{POLICY_TYPE}\"\n",
    "EVAL_REPO_ID = f\"{HF_USER}/eval_{DATASET_NAME}\"\n",
    "CAMERAS_ARG = _cameras_arg(CAMERAS)\n",
    "CAMERAS_FLAG = f'--robot.cameras=\"{CAMERAS_ARG}\"' if CAMERAS_ARG else \"\"\n",
    "\n",
    "print(f\"Robot  : {ROBOT_TYPE} @ {ROBOT_PORT}\")\n",
    "print(f\"Teleop : {TELEOP_TYPE} @ {TELEOP_PORT}\")\n",
    "print(f\"Cameras: {list(CAMERAS) or 'none'}\")\n",
    "print(f\"Dataset: {DATASET_REPO_ID} ({NUM_EPISODES} episodes) saved to {DATASET_ROOT}\")\n",
    "print(f\"Policy : {POLICY_TYPE} -> {POLICY_REPO_ID}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 1. Calibration\n",
    "\n",
    "Run once per arm before first use."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Follower\n",
    "print_cmd(\n",
    "    \"lerobot-calibrate\",\n",
    "    f\"--robot.type={ROBOT_TYPE}\",\n",
    "    f\"--robot.port={ROBOT_PORT}\",\n",
    "    f\"--robot.id={ROBOT_ID}\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Leader\n",
    "print_cmd(\n",
    "    \"lerobot-calibrate\",\n",
    "    f\"--teleop.type={TELEOP_TYPE}\",\n",
    "    f\"--teleop.port={TELEOP_PORT}\",\n",
    "    f\"--teleop.id={TELEOP_ID}\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 2. Teleoperation\n",
    "\n",
    "See the [teleoperation docs](https://huggingface.co/docs/lerobot/il_robots#teleoperate) and the [cameras guide](https://huggingface.co/docs/lerobot/cameras) for more options."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print_cmd(\n",
    "    \"lerobot-teleoperate\",\n",
    "    f\"--robot.type={ROBOT_TYPE}\",\n",
    "    f\"--robot.port={ROBOT_PORT}\",\n",
    "    f\"--robot.id={ROBOT_ID}\",\n",
    "    CAMERAS_FLAG,\n",
    "    f\"--teleop.type={TELEOP_TYPE}\",\n",
    "    f\"--teleop.port={TELEOP_PORT}\",\n",
    "    f\"--teleop.id={TELEOP_ID}\",\n",
    "    \"--display_data=true\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 3. Record Dataset\n",
    "\n",
    "See the [recording docs](https://huggingface.co/docs/lerobot/il_robots#record-a-dataset) for tips on gathering good data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print_cmd(\n",
    "    \"lerobot-record\",\n",
    "    f\"--robot.type={ROBOT_TYPE}\",\n",
    "    f\"--robot.port={ROBOT_PORT}\",\n",
    "    f\"--robot.id={ROBOT_ID}\",\n",
    "    CAMERAS_FLAG,\n",
    "    f\"--teleop.type={TELEOP_TYPE}\",\n",
    "    f\"--teleop.port={TELEOP_PORT}\",\n",
    "    f\"--teleop.id={TELEOP_ID}\",\n",
    "    f\"--dataset.repo_id={DATASET_REPO_ID}\",\n",
    "    f\"--dataset.num_episodes={NUM_EPISODES}\",\n",
    "    f'--dataset.single_task=\"{TASK_DESCRIPTION}\"',\n",
    "    \"--dataset.streaming_encoding=true\",\n",
    "    \"--display_data=true\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Resume a previously interrupted recording session\n",
    "print_cmd(\n",
    "    \"lerobot-record\",\n",
    "    f\"--robot.type={ROBOT_TYPE}\",\n",
    "    f\"--robot.port={ROBOT_PORT}\",\n",
    "    f\"--robot.id={ROBOT_ID}\",\n",
    "    CAMERAS_FLAG,\n",
    "    f\"--teleop.type={TELEOP_TYPE}\",\n",
    "    f\"--teleop.port={TELEOP_PORT}\",\n",
    "    f\"--teleop.id={TELEOP_ID}\",\n",
    "    f\"--dataset.repo_id={DATASET_REPO_ID}\",\n",
    "    f\"--dataset.root={DATASET_ROOT}\",\n",
    "    f\"--dataset.num_episodes={NUM_EPISODES}\",\n",
    "    f'--dataset.single_task=\"{TASK_DESCRIPTION}\"',\n",
    "    \"--dataset.streaming_encoding=true\",\n",
    "    \"--display_data=true\",\n",
    "    \"--resume=true\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 4. Train Policy\n",
    "\n",
    "See the [training docs](https://huggingface.co/docs/lerobot/il_robots#train-a-policy) for configuration options and tips."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print_cmd(\n",
    "    \"lerobot-train\",\n",
    "    f\"--dataset.repo_id={DATASET_REPO_ID}\",\n",
    "    f\"--policy.type={POLICY_TYPE}\",\n",
    "    f\"--policy.device={POLICY_DEVICE}\",\n",
    "    f\"--policy.repo_id={POLICY_REPO_ID}\",\n",
    "    f\"--output_dir={OUTPUT_DIR}\",\n",
    "    f\"--steps={TRAIN_STEPS}\",\n",
    "    f\"--save_freq={SAVE_FREQ}\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Resume a previously interrupted training session\n",
    "print_cmd(\n",
    "    \"lerobot-train\",\n",
    "    f\"--config_path={LAST_CHECKPOINT_PATH}/pretrained_model/train_config.json\",\n",
    "    \"--resume=true\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 5. Inference\n",
    "\n",
    "Uses `POLICY_PATH` from the Configuration cell (defaults to the Hub repo ID). You can also put there the `LAST_CHECKPOINT_PATH`.\n",
    "\n",
    "See the [inference docs](https://huggingface.co/docs/lerobot/il_robots#run-inference-and-evaluate-your-policy) for details.\n",
    "\n",
    "Recently ```lerobot-rollout``` was introduced, you can [read more about it here](https://huggingface.co/docs/lerobot/main/en/il_robots?eval=Base+mode+%28no+recording%29#run-inference-and-evaluate-your-policy)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print_cmd(\n",
    "    \"lerobot-rollout\",\n",
    "    \"--strategy.type=base\",\n",
    "    f\"--policy.path={POLICY_PATH}\",\n",
    "    f\"--robot.type={ROBOT_TYPE}\",\n",
    "    f\"--robot.port={ROBOT_PORT}\",\n",
    "    CAMERAS_FLAG,\n",
    "    f'--task=\"{TASK_DESCRIPTION}\"',\n",
    "    \"--duration=60\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "if you are using the V0.5.1 release you should use ```lerobot-record``` instead of rollout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print_cmd(\n",
    "    \"lerobot-record\",\n",
    "    f\"--policy.path={POLICY_PATH}\",\n",
    "    f\"--robot.type={ROBOT_TYPE}\",\n",
    "    f\"--robot.port={ROBOT_PORT}\",\n",
    "    f\"--robot.id={ROBOT_ID}\",\n",
    "    CAMERAS_FLAG,\n",
    "    f\"--teleop.type={TELEOP_TYPE}\",\n",
    "    f\"--teleop.port={TELEOP_PORT}\",\n",
    "    f\"--teleop.id={TELEOP_ID}\",\n",
    "    f\"--dataset.repo_id={EVAL_REPO_ID}\",\n",
    "    f\"--dataset.num_episodes={NUM_EPISODES}\",\n",
    "    f'--dataset.single_task=\"{TASK_DESCRIPTION}\"',\n",
    "    \"--dataset.streaming_encoding=true\",\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "lerobot (3.12.3)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}