File size: 20,786 Bytes
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cf591
 
 
 
 
 
 
 
 
 
 
 
 
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cf591
 
 
 
 
 
0c5ee34
 
74cf591
0c5ee34
74cf591
 
 
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cf591
 
0c5ee34
 
 
 
 
74cf591
0c5ee34
 
 
 
 
 
 
 
74cf591
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cf591
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cf591
 
0c5ee34
74cf591
 
 
 
 
 
 
 
 
 
 
 
 
 
0c5ee34
74cf591
 
 
0c5ee34
 
74cf591
0c5ee34
 
 
74cf591
 
 
 
 
 
 
 
 
 
 
0c5ee34
74cf591
0c5ee34
74cf591
 
 
0c5ee34
 
 
74cf591
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cf591
 
 
 
 
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
74cf591
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cf591
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cf591
 
 
 
 
 
 
 
 
 
 
 
 
 
0c5ee34
74cf591
0c5ee34
 
 
 
 
74cf591
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cf591
0c5ee34
 
 
74cf591
 
0c5ee34
 
74cf591
0c5ee34
 
 
 
74cf591
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cf591
 
 
 
 
 
 
 
 
 
 
 
 
 
0c5ee34
74cf591
0c5ee34
 
 
74cf591
 
0c5ee34
 
 
 
74cf591
 
0c5ee34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# OpenTrack - Humanoid Motion Tracking Demo\n",
    "\n",
    "This notebook demonstrates OpenTrack, an open-source humanoid motion tracking system using MuJoCo.\n",
    "\n",
    "**Note**: Training can be resource-intensive. We'll use debug mode for quick testing."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Setup Environment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import subprocess\n",
    "import time\n",
    "import glob\n",
    "from pathlib import Path\n",
    "from IPython.display import Video, display, HTML\n",
    "import threading\n",
    "import queue\n",
    "\n",
    "# Define project directory structure\n",
    "PROJECT_BASE = Path(\"/data/workspaces/opentrack\")\n",
    "DATASETS_DIR = PROJECT_BASE / \"datasets\"\n",
    "MODELS_DIR = PROJECT_BASE / \"models\"\n",
    "VIDEOS_DIR = PROJECT_BASE / \"videos\"\n",
    "REPO_DIR = PROJECT_BASE / \"OpenTrack\"\n",
    "\n",
    "# Create directories\n",
    "for dir_path in [PROJECT_BASE, DATASETS_DIR, MODELS_DIR, VIDEOS_DIR]:\n",
    "    dir_path.mkdir(parents=True, exist_ok=True)\n",
    "    print(f\"βœ“ {dir_path}\")\n",
    "\n",
    "print(\"\\nβœ“ Environment setup complete!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Clone OpenTrack Repository"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Clone the repository if not already cloned\n",
    "if not REPO_DIR.exists():\n",
    "    print(f\"Cloning OpenTrack to {REPO_DIR}...\")\n",
    "    subprocess.run(\n",
    "        ['git', 'clone', 'https://github.com/GalaxyGeneralRobotics/OpenTrack.git', str(REPO_DIR)],\n",
    "        check=True\n",
    "    )\n",
    "    print(\"βœ“ Repository cloned successfully\")\n",
    "else:\n",
    "    print(f\"βœ“ Repository already exists at {REPO_DIR}\")\n",
    "\n",
    "# Change to repository directory\n",
    "os.chdir(REPO_DIR)\n",
    "print(f\"\\nβœ“ Working directory: {os.getcwd()}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Install Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install PyTorch (CPU version for compatibility)\n",
    "!pip install -q torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cpu\n",
    "\n",
    "# Install OpenTrack requirements\n",
    "!pip install -q -r requirements.txt\n",
    "\n",
    "# Install additional packages for video handling\n",
    "!pip install -q imageio imageio-ffmpeg\n",
    "\n",
    "print(\"βœ“ All dependencies installed\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Download Motion Capture Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from huggingface_hub import snapshot_download\n",
    "\n",
    "# Define mocap directory in our datasets folder\n",
    "mocap_dir = DATASETS_DIR / \"lafan1\" / \"UnitreeG1\"\n",
    "mocap_dir.mkdir(parents=True, exist_ok=True)\n",
    "\n",
    "repo_id = \"robfiras/loco-mujoco-datasets\"\n",
    "\n",
    "print(\"Downloading all mocap data from Lafan1/mocap/UnitreeG1...\")\n",
    "print(f\"Target directory: {mocap_dir}\")\n",
    "print(\"This will download all .npz files concurrently.\\n\")\n",
    "\n",
    "try:\n",
    "    # Use snapshot_download with allow_patterns to download only the files we need\n",
    "    snapshot_path = snapshot_download(\n",
    "        repo_id=repo_id,\n",
    "        repo_type=\"dataset\",\n",
    "        allow_patterns=\"Lafan1/mocap/UnitreeG1/*.npz\",\n",
    "        local_dir=str(DATASETS_DIR),\n",
    "        local_dir_use_symlinks=False\n",
    "    )\n",
    "    \n",
    "    print(f\"\\nβœ“ Download complete! Files saved to: {snapshot_path}\")\n",
    "    \n",
    "    # Verify files\n",
    "    npz_files = list(mocap_dir.glob(\"*.npz\"))\n",
    "    print(f\"βœ“ Found {len(npz_files)} .npz files in {mocap_dir}\")\n",
    "    \n",
    "    if npz_files:\n",
    "        print(\"\\nSample files:\")\n",
    "        for f in sorted(npz_files)[:10]:  # Show first 10 files\n",
    "            print(f\"  - {f.name}\")\n",
    "        if len(npz_files) > 10:\n",
    "            print(f\"  ... and {len(npz_files) - 10} more files\")\n",
    "    \n",
    "    # Create symlink from OpenTrack's expected data directory to our datasets\n",
    "    opentrack_data_dir = REPO_DIR / \"data\" / \"mocap\"\n",
    "    opentrack_data_dir.parent.mkdir(parents=True, exist_ok=True)\n",
    "    \n",
    "    # Remove old symlink/directory if it exists\n",
    "    if opentrack_data_dir.exists() or opentrack_data_dir.is_symlink():\n",
    "        if opentrack_data_dir.is_symlink():\n",
    "            opentrack_data_dir.unlink()\n",
    "        else:\n",
    "            import shutil\n",
    "            shutil.rmtree(opentrack_data_dir)\n",
    "    \n",
    "    # Create symlink\n",
    "    opentrack_data_dir.symlink_to(DATASETS_DIR, target_is_directory=True)\n",
    "    print(f\"\\nβœ“ Created symlink: {opentrack_data_dir} -> {DATASETS_DIR}\")\n",
    "    \n",
    "except Exception as e:\n",
    "    print(f\"⚠ Error downloading mocap data: {e}\")\n",
    "    print(\"\\nYou may need to download manually from:\")\n",
    "    print(\"https://huggingface.co/datasets/robfiras/loco-mujoco-datasets/tree/main/Lafan1/mocap/UnitreeG1\")\n",
    "    print(\"\\nOr check if you need to authenticate:\")\n",
    "    print(\"  huggingface-cli login\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Helper Functions for Background Process Management"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_command_with_output(cmd, description=\"Running command\"):\n",
    "    \"\"\"\n",
    "    Run a command and display output in real-time\n",
    "    \"\"\"\n",
    "    print(f\"\\n{'='*60}\")\n",
    "    print(f\"{description}\")\n",
    "    print(f\"Command: {' '.join(cmd)}\")\n",
    "    print(f\"{'='*60}\\n\")\n",
    "    \n",
    "    process = subprocess.Popen(\n",
    "        cmd,\n",
    "        stdout=subprocess.PIPE,\n",
    "        stderr=subprocess.STDOUT,\n",
    "        text=True,\n",
    "        bufsize=1,\n",
    "        universal_newlines=True\n",
    "    )\n",
    "    \n",
    "    # Read output line by line\n",
    "    for line in process.stdout:\n",
    "        print(line, end='')\n",
    "    \n",
    "    process.wait()\n",
    "    \n",
    "    if process.returncode == 0:\n",
    "        print(f\"\\nβœ“ {description} completed successfully\")\n",
    "    else:\n",
    "        print(f\"\\n⚠ {description} exited with code {process.returncode}\")\n",
    "    \n",
    "    return process.returncode\n",
    "\n",
    "\n",
    "def find_latest_experiment(exp_name_pattern):\n",
    "    \"\"\"\n",
    "    Find the latest experiment folder matching the pattern\n",
    "    \"\"\"\n",
    "    # Check both MODELS_DIR and REPO_DIR/logs\n",
    "    search_dirs = [MODELS_DIR, REPO_DIR / \"logs\"]\n",
    "    \n",
    "    all_experiments = []\n",
    "    for logs_dir in search_dirs:\n",
    "        if not logs_dir.exists():\n",
    "            continue\n",
    "        \n",
    "        # Find all matching experiments\n",
    "        experiments = [\n",
    "            d for d in logs_dir.iterdir() \n",
    "            if d.is_dir() and exp_name_pattern in d.name\n",
    "        ]\n",
    "        all_experiments.extend(experiments)\n",
    "    \n",
    "    if not all_experiments:\n",
    "        return None\n",
    "    \n",
    "    # Sort by modification time and return latest\n",
    "    latest = sorted(all_experiments, key=lambda x: x.stat().st_mtime, reverse=True)[0]\n",
    "    return latest.name\n",
    "\n",
    "\n",
    "def find_generated_videos(output_dir=None):\n",
    "    \"\"\"\n",
    "    Find all generated video files\n",
    "    \"\"\"\n",
    "    if output_dir is None:\n",
    "        output_dir = VIDEOS_DIR\n",
    "    else:\n",
    "        output_dir = Path(output_dir)\n",
    "    \n",
    "    videos = []\n",
    "    \n",
    "    if output_dir.exists():\n",
    "        videos = list(output_dir.glob(\"*.mp4\")) + list(output_dir.glob(\"*.gif\"))\n",
    "    \n",
    "    if not videos:\n",
    "        # Try alternative locations\n",
    "        alternative_dirs = [REPO_DIR, REPO_DIR / \"logs\", MODELS_DIR]\n",
    "        for alt_dir in alternative_dirs:\n",
    "            if alt_dir.exists():\n",
    "                found = list(alt_dir.glob(\"**/*.mp4\")) + list(alt_dir.glob(\"**/*.gif\"))\n",
    "                videos.extend(found)\n",
    "    \n",
    "    return sorted(videos, key=lambda x: x.stat().st_mtime, reverse=True)\n",
    "\n",
    "\n",
    "def setup_model_output_symlink():\n",
    "    \"\"\"\n",
    "    Create symlink from OpenTrack's logs directory to our models directory\n",
    "    \"\"\"\n",
    "    opentrack_logs_dir = REPO_DIR / \"logs\"\n",
    "    \n",
    "    # Remove old symlink/directory if it exists\n",
    "    if opentrack_logs_dir.exists() or opentrack_logs_dir.is_symlink():\n",
    "        if opentrack_logs_dir.is_symlink():\n",
    "            opentrack_logs_dir.unlink()\n",
    "        else:\n",
    "            import shutil\n",
    "            # Move existing logs to MODELS_DIR first\n",
    "            if opentrack_logs_dir.is_dir():\n",
    "                for item in opentrack_logs_dir.iterdir():\n",
    "                    shutil.move(str(item), str(MODELS_DIR))\n",
    "            shutil.rmtree(opentrack_logs_dir)\n",
    "    \n",
    "    # Create symlink\n",
    "    opentrack_logs_dir.symlink_to(MODELS_DIR, target_is_directory=True)\n",
    "    print(f\"βœ“ Created symlink: {opentrack_logs_dir} -> {MODELS_DIR}\")\n",
    "\n",
    "print(\"βœ“ Helper functions loaded\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Quick Training (Debug Mode)\n",
    "\n",
    "We'll run a quick training session in debug mode. This won't produce a well-trained model but will verify everything works."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup symlink for model outputs\n",
    "setup_model_output_symlink()\n",
    "\n",
    "print(f\"\\nModels will be saved to: {MODELS_DIR}\\n\")\n",
    "\n",
    "# Run quick training\n",
    "cmd = [\n",
    "    'python', 'train_policy.py',\n",
    "    '--exp_name', 'debug',\n",
    "    '--terrain_type', 'flat_terrain'\n",
    "]\n",
    "\n",
    "return_code = run_command_with_output(cmd, \"Training OpenTrack (debug mode)\")\n",
    "\n",
    "# Find the experiment folder\n",
    "exp_folder = find_latest_experiment('debug')\n",
    "if exp_folder:\n",
    "    print(f\"\\nβœ“ Training completed! Experiment: {exp_folder}\")\n",
    "    print(f\"βœ“ Model saved in: {MODELS_DIR / exp_folder}\")\n",
    "else:\n",
    "    print(\"\\n⚠ Could not find experiment folder\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Convert Checkpoint (Brax β†’ PyTorch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the latest experiment name\n",
    "exp_folder = find_latest_experiment('debug')\n",
    "\n",
    "if exp_folder:\n",
    "    print(f\"Converting checkpoint for: {exp_folder}\")\n",
    "    \n",
    "    cmd = [\n",
    "        'python', 'brax2torch.py',\n",
    "        '--exp_name', exp_folder\n",
    "    ]\n",
    "    \n",
    "    return_code = run_command_with_output(cmd, \"Converting Brax checkpoint to PyTorch\")\n",
    "else:\n",
    "    print(\"⚠ No experiment found. Run training first.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. Generate Videos (Headless Rendering)\n",
    "\n",
    "This will run the policy and generate videos using MuJoCo's headless renderer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the latest experiment name\n",
    "exp_folder = find_latest_experiment('debug')\n",
    "\n",
    "if exp_folder:\n",
    "    print(f\"Generating videos for: {exp_folder}\")\n",
    "    print(f\"Videos will be saved to: {VIDEOS_DIR}\\n\")\n",
    "    \n",
    "    # Use --use_renderer for headless video generation (NOT --use_viewer)\n",
    "    cmd = [\n",
    "        'python', 'play_policy.py',\n",
    "        '--exp_name', exp_folder,\n",
    "        '--use_renderer',\n",
    "        # '--play_ref_motion',  # Uncomment to also show reference motion\n",
    "    ]\n",
    "    \n",
    "    return_code = run_command_with_output(cmd, \"Generating videos with MuJoCo renderer\")\n",
    "    \n",
    "    print(\"\\n\" + \"=\"*60)\n",
    "    print(\"Searching for generated videos...\")\n",
    "    print(\"=\"*60)\n",
    "    \n",
    "    # Wait a moment for files to be written\n",
    "    time.sleep(2)\n",
    "    \n",
    "    # Find generated videos\n",
    "    videos = find_generated_videos()\n",
    "    \n",
    "    # Move videos to VIDEOS_DIR if they're not already there\n",
    "    if videos:\n",
    "        import shutil\n",
    "        moved_videos = []\n",
    "        for video in videos:\n",
    "            if not video.is_relative_to(VIDEOS_DIR):\n",
    "                dest = VIDEOS_DIR / video.name\n",
    "                shutil.copy2(video, dest)\n",
    "                moved_videos.append(dest)\n",
    "                print(f\"Moved: {video.name} -> {dest}\")\n",
    "            else:\n",
    "                moved_videos.append(video)\n",
    "        videos = moved_videos\n",
    "    \n",
    "    if videos:\n",
    "        print(f\"\\nβœ“ Found {len(videos)} video(s) in {VIDEOS_DIR}:\")\n",
    "        for video in videos:\n",
    "            print(f\"  - {video}\")\n",
    "    else:\n",
    "        print(\"\\n⚠ No videos found. Checking alternative locations...\")\n",
    "        # Search more broadly\n",
    "        all_videos = list(REPO_DIR.rglob(\"*.mp4\")) + list(REPO_DIR.rglob(\"*.gif\"))\n",
    "        if all_videos:\n",
    "            print(f\"Found {len(all_videos)} video(s) in project:\")\n",
    "            for video in all_videos[:10]:  # Show first 10\n",
    "                print(f\"  - {video}\")\n",
    "else:\n",
    "    print(\"⚠ No experiment found. Run training first.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9. Display Generated Videos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Find and display all generated videos from our videos directory\n",
    "videos = find_generated_videos()\n",
    "\n",
    "if not videos:\n",
    "    # Try alternative search in the whole project\n",
    "    videos = list(REPO_DIR.rglob(\"*.mp4\")) + list(REPO_DIR.rglob(\"*.gif\"))\n",
    "\n",
    "if videos:\n",
    "    print(f\"Displaying {len(videos)} video(s) from {VIDEOS_DIR}:\\n\")\n",
    "    \n",
    "    for i, video_path in enumerate(videos[:5]):  # Display first 5 videos\n",
    "        print(f\"\\n{'='*60}\")\n",
    "        print(f\"Video {i+1}: {video_path.name}\")\n",
    "        print(f\"Location: {video_path}\")\n",
    "        print(f\"{'='*60}\")\n",
    "        \n",
    "        try:\n",
    "            if video_path.suffix == '.mp4':\n",
    "                display(Video(str(video_path), width=800, embed=True))\n",
    "            elif video_path.suffix == '.gif':\n",
    "                display(HTML(f'<img src=\"{video_path}\" width=\"800\">'))\n",
    "        except Exception as e:\n",
    "            print(f\"⚠ Error displaying video: {e}\")\n",
    "            print(f\"You can download it from: {video_path}\")\n",
    "else:\n",
    "    print(\"⚠ No videos found.\")\n",
    "    print(\"\\nPossible reasons:\")\n",
    "    print(\"1. Training didn't complete successfully\")\n",
    "    print(\"2. Checkpoint conversion failed\")\n",
    "    print(\"3. Video generation failed\")\n",
    "    print(\"\\nCheck the output of previous cells for errors.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10. Optional: Generate Rough Terrain\n",
    "\n",
    "If you want to test on rough terrain, run this cell first to generate terrain data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate rough terrain using Perlin noise\n",
    "cmd = ['python', 'generate_terrain.py']\n",
    "return_code = run_command_with_output(cmd, \"Generating rough terrain\")\n",
    "\n",
    "print(\"\\nβœ“ Terrain generated! You can now train with --terrain_type rough_terrain\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 11. Optional: Full Training (Takes Much Longer)\n",
    "\n",
    "⚠️ Warning: This will take significant time and resources. Only run if you have GPU access and time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Full training (uncomment to run)\n",
    "# cmd = [\n",
    "#     'python', 'train_policy.py',\n",
    "#     '--exp_name', 'flat_terrain_full',\n",
    "#     '--terrain_type', 'flat_terrain'\n",
    "# ]\n",
    "# \n",
    "# return_code = run_command_with_output(cmd, \"Full training on flat terrain\")\n",
    "# \n",
    "# # Then convert and play\n",
    "# exp_folder = find_latest_experiment('flat_terrain_full')\n",
    "# if exp_folder:\n",
    "#     !python brax2torch.py --exp_name {exp_folder}\n",
    "#     !python play_policy.py --exp_name {exp_folder} --use_renderer\n",
    "\n",
    "print(\"Full training cell ready (currently commented out)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "This notebook demonstrates:\n",
    "1. βœ… Setting up OpenTrack in a Jupyter environment\n",
    "2. βœ… Running training (debug mode for quick testing)\n",
    "3. βœ… Converting Brax checkpoints to PyTorch\n",
    "4. βœ… Generating videos using headless MuJoCo renderer\n",
    "5. βœ… Displaying videos in the notebook\n",
    "\n",
    "**Project Structure:**\n",
    "```\n",
    "/data/workspaces/opentrack/\n",
    "β”œβ”€β”€ datasets/          # Motion capture data (.npz files)\n",
    "β”‚   └── lafan1/\n",
    "β”‚       └── UnitreeG1/\n",
    "β”œβ”€β”€ models/            # Trained models and checkpoints\n",
    "β”‚   └── <timestamp>_<exp_name>/\n",
    "β”œβ”€β”€ videos/            # Generated videos (.mp4, .gif)\n",
    "└── OpenTrack/         # Cloned repository\n",
    "    β”œβ”€β”€ data/mocap -> ../datasets/  (symlink)\n",
    "    └── logs -> ../models/          (symlink)\n",
    "```\n",
    "\n",
    "**Next Steps:**\n",
    "- All mocap data is in `/data/workspaces/opentrack/datasets/`\n",
    "- Run full training with GPU support\n",
    "- Test on rough terrain\n",
    "- Experiment with reference motion playback\n",
    "- Trained models are saved in `/data/workspaces/opentrack/models/`\n",
    "- Generated videos are in `/data/workspaces/opentrack/videos/`\n",
    "\n",
    "**Troubleshooting:**\n",
    "- If videos aren't generated, check that `--use_renderer` flag is used (not `--use_viewer`)\n",
    "- Ensure MuJoCo can run headless (may need `xvfb` on some systems)\n",
    "- Check `/data/workspaces/opentrack/models/` directory for experiment outputs\n",
    "- All data persists in `/data/workspaces/opentrack/` across notebook sessions"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 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.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}