update readme
Browse files- README.md +144 -0
- README_dmcontrol_collect.md +0 -87
- train_sb3_dmcontrol.py → sb3_train.py +0 -0
README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# DynaTraj — Training and Data Collection (SB3 + dm_control)
|
| 2 |
+
|
| 3 |
+
This repo provides two entry-point scripts:
|
| 4 |
+
- `sb3_train.py`: Train an RL policy with Stable-Baselines3 on dm_control tasks and periodically save policy checkpoints
|
| 5 |
+
- `sb3_collect.py`: Load specific checkpoints and collect fixed-length trajectories into `.npz` datasets with minimal metadata
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## Environment & Dependencies
|
| 10 |
+
|
| 11 |
+
- Python 3.10+
|
| 12 |
+
- Required:
|
| 13 |
+
- dm_control + MuJoCo: `pip install dm-control mujoco`
|
| 14 |
+
- Reinforcement learning: `pip install stable-baselines3`
|
| 15 |
+
- Utilities: `pip install numpy tqdm torch`
|
| 16 |
+
- Gym API: prefers `gymnasium`, falls back to `gym` (handled automatically)
|
| 17 |
+
- Optional (only if you need on-screen rendering during collection):
|
| 18 |
+
- OpenCV UI: `pip install opencv-python`
|
| 19 |
+
- or Matplotlib: `pip install matplotlib`
|
| 20 |
+
|
| 21 |
+
Note: On headless Linux/remote environments, rendering might require EGL/OSMesa configuration. If you do not render, you do not need any display backend.
|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
## Output Layout (Conventions)
|
| 26 |
+
|
| 27 |
+
- Training checkpoints: `weights/<domain>/<task>/ckpt-<k>.pt`
|
| 28 |
+
- `k` starts from 1; default save interval is every 10,000 timesteps
|
| 29 |
+
- Examples: `ckpt-1.pt` ≈ 10k steps, `ckpt-2.pt` ≈ 20k steps
|
| 30 |
+
- Full SB3 model: `weights/sb3_<algo>_<domain>-<task>_seed<seed>_<timestamp>.zip`
|
| 31 |
+
- Collected datasets: `dataset/*.npz` with a companion `*_metadata.pkl`
|
| 32 |
+
|
| 33 |
+
Default absolute paths in this repo:
|
| 34 |
+
- Checkpoint root: `/home/lau/sim/DynaTraj/weights`
|
| 35 |
+
- Dataset output: `/home/lau/sim/DynaTraj/dataset`
|
| 36 |
+
|
| 37 |
+
---
|
| 38 |
+
|
| 39 |
+
## Training
|
| 40 |
+
|
| 41 |
+
Script: `sb3_train.py`
|
| 42 |
+
|
| 43 |
+
Supported algorithms (argument is case-sensitive and must be lowercase here):
|
| 44 |
+
- `sac`, `ppo`, `td3`
|
| 45 |
+
|
| 46 |
+
Common args:
|
| 47 |
+
- `--domain`: dm_control domain, e.g., `cheetah`, `quadruped` (default: `cheetah`)
|
| 48 |
+
- `--task`: task name, e.g., `run`, `walk` (default: `run`)
|
| 49 |
+
- `--algo`: `sac|ppo|td3` (default: `sac`)
|
| 50 |
+
- `--total_timesteps`: total training steps (default: `500000`)
|
| 51 |
+
- `--n_envs`: number of parallel envs (default: `1`; uses sub-process vectorization if >1)
|
| 52 |
+
- `--seed`: random seed (default: `0`)
|
| 53 |
+
- `--device`: `cpu|cuda|auto` (default: `auto`)
|
| 54 |
+
- `--out_dir`: where to save models & checkpoints (default: `/home/lau/sim/DynaTraj/weights`)
|
| 55 |
+
|
| 56 |
+
Example:
|
| 57 |
+
```bash
|
| 58 |
+
python /home/lau/sim/DynaTraj/train_sb3_dmcontrol.py \
|
| 59 |
+
--domain cheetah --task run\
|
| 60 |
+
--algo sac \
|
| 61 |
+
--total_timesteps 500000 \
|
| 62 |
+
--out_dir /home/lau/sim/DynaTraj/weights
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
Checkpointing:
|
| 66 |
+
- A policy checkpoint is saved every 10,000 global timesteps as `ckpt-<k>.pt` (1-based counter).
|
| 67 |
+
- A full SB3 model `.zip` is also saved at the end for plain SB3 loading if needed.
|
| 68 |
+
|
| 69 |
+
---
|
| 70 |
+
|
| 71 |
+
## Data Collection
|
| 72 |
+
|
| 73 |
+
Script: `sb3_collect.py`
|
| 74 |
+
|
| 75 |
+
Purpose: Run inference with specific training checkpoints and write fixed-length trajectories to `.npz` files plus metadata.
|
| 76 |
+
|
| 77 |
+
Common args:
|
| 78 |
+
- `--domain` / `--task`: must match training (defaults: `cheetah` / `run`)
|
| 79 |
+
- `--ckpt_root`: root directory of checkpoints (default: `/home/lau/sim/DynaTraj/weights`)
|
| 80 |
+
- `--ckpt_indices`: comma-separated checkpoint indices, e.g., `1,5,10` (note: starts from 1)
|
| 81 |
+
- `--trajectories_per_ckpt`: how many trajectories per checkpoint (default: `5120`)
|
| 82 |
+
- `--steps_per_trajectory`: steps per trajectory (default: `24`)
|
| 83 |
+
- `--out_dir`: dataset output directory (default: `/home/lau/sim/DynaTraj/dataset`)
|
| 84 |
+
- `--device`: inference device (default: `cpu`)
|
| 85 |
+
- `--render`: optional flag to render frames (requires OpenCV or Matplotlib)
|
| 86 |
+
- `--algo`: usually unnecessary. The script reads the real algo name from the checkpoint payload. Only used as a fallback (must be UPPERCASE: `SAC|PPO|TD3`).
|
| 87 |
+
|
| 88 |
+
Example:
|
| 89 |
+
```bash
|
| 90 |
+
|
| 91 |
+
python /home/lau/sim/DynaTraj/sb3_collect.py \
|
| 92 |
+
--domain cheetah --task run \
|
| 93 |
+
--algo SAC \
|
| 94 |
+
--ckpt_root /home/lau/sim/DynaTraj/weights \
|
| 95 |
+
--ckpt_indices 1,10,20,30,40,50 \
|
| 96 |
+
--trajectories_per_ckpt 5120 \
|
| 97 |
+
--steps_per_trajectory 24 \
|
| 98 |
+
--out_dir /home/lau/sim/DynaTraj/dataset \
|
| 99 |
+
--device cpu
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
With rendering (optional):
|
| 103 |
+
```bash
|
| 104 |
+
python /home/lau/sim/DynaTraj/sb3_collect.py \
|
| 105 |
+
--domain cheetah --task run \
|
| 106 |
+
--algo SAC \
|
| 107 |
+
--ckpt_root /home/lau/sim/DynaTraj/weights \
|
| 108 |
+
--ckpt_indices 50 \
|
| 109 |
+
--trajectories_per_ckpt 5120 \
|
| 110 |
+
--steps_per_trajectory 24 \
|
| 111 |
+
--out_dir /home/lau/sim/DynaTraj/dataset \
|
| 112 |
+
--device cpu \
|
| 113 |
+
--render
|
| 114 |
+
```
|
| 115 |
+
|
| 116 |
+
Notes:
|
| 117 |
+
- The script searches checkpoints under `ckpt_root/<domain>/<task>/` as `ckpt-<k>.pt`. If you see an error for `ckpt-0.pt`, switch to 1-based indices.
|
| 118 |
+
- If your environment has no display backend, simply omit `--render`.
|
| 119 |
+
|
| 120 |
+
---
|
| 121 |
+
|
| 122 |
+
## Output Format
|
| 123 |
+
|
| 124 |
+
For each `k`:
|
| 125 |
+
- Dataset file: `<out_dir>/sb3_<domain>_<task>_ckpt<kNNN>_<timestamp>.npz`
|
| 126 |
+
- Metadata file: `<out_dir>/sb3_<domain>_<task>_ckpt<kNNN>_<timestamp>_metadata.pkl`
|
| 127 |
+
|
| 128 |
+
Metadata keys include:
|
| 129 |
+
- `domain`, `task`, `algo`, `seed`
|
| 130 |
+
- `ckpt_index` (the `k` you collected)
|
| 131 |
+
- `trajectories_per_ckpt`, `steps_per_trajectory`
|
| 132 |
+
- `total_trajectories`, `total_steps`
|
| 133 |
+
- `render` (whether rendering was enabled)
|
| 134 |
+
|
| 135 |
+
The exact arrays inside `.npz` are defined by the internal `TrajectoryBuffer` implementation. Typically they include time-aligned observation/state tensors, actions, rewards, and done flags.
|
| 136 |
+
|
| 137 |
+
---
|
| 138 |
+
|
| 139 |
+
## Tips
|
| 140 |
+
|
| 141 |
+
- Prefer absolute paths (all examples above use absolute paths).
|
| 142 |
+
- Training uses `DummyVecEnv`/`SubprocVecEnv`, automatically flattens dm_control observations and clips actions to env bounds.
|
| 143 |
+
- Collection reconstructs an SB3 policy and loads the `policy_state_dict` from each checkpoint; it does not require the full `.zip` model.
|
| 144 |
+
- Algo-arg case: training expects lowercase (`sac|ppo|td3`), collection fallback uses UPPERCASE (`SAC|PPO|TD3`).
|
README_dmcontrol_collect.md
DELETED
|
@@ -1,87 +0,0 @@
|
|
| 1 |
-
## dm_control data collection (dmcontrol_collect.py)
|
| 2 |
-
|
| 3 |
-
### Overview
|
| 4 |
-
This script collects trajectories from DeepMind Control (dm_control) environments using uniformly sampled torque actions in [-1, 1]. Data are saved with `TrajectoryBuffer` as compressed `.npz` along with a metadata `.pkl`.
|
| 5 |
-
|
| 6 |
-
Collected state per step contains (in order):
|
| 7 |
-
- joint angles (radians)
|
| 8 |
-
- joint angular velocities (rad/s)
|
| 9 |
-
- root position (x, y, z)
|
| 10 |
-
- root linear velocity (vx, vy, vz)
|
| 11 |
-
- root rotation quaternion (qx, qy, qz, qw)
|
| 12 |
-
- root angular velocity (wx, wy, wz)
|
| 13 |
-
- last applied torque (action vector)
|
| 14 |
-
|
| 15 |
-
### Requirements
|
| 16 |
-
- Python 3.9+
|
| 17 |
-
- dm_control and MuJoCo installed:
|
| 18 |
-
```bash
|
| 19 |
-
pip install dm-control mujoco
|
| 20 |
-
```
|
| 21 |
-
|
| 22 |
-
### Hyperparameters (CLI)
|
| 23 |
-
|
| 24 |
-
| Name | Type / Default | Description |
|
| 25 |
-
|------|-----------------|-------------|
|
| 26 |
-
| `--domain` | str, default `quadruped` | dm_control domain name, e.g. `quadruped`, `cheetah`. |
|
| 27 |
-
| `--task` | str, default `walk` | dm_control task name, e.g. `walk`, `run`. |
|
| 28 |
-
| `--seed` | int, default `0` | PRNG seed used for env and action sampling. |
|
| 29 |
-
| `--trajectories_per_file` | int, default `512` | Number of trajectories to collect and save in one output file. |
|
| 30 |
-
| `--steps_per_trajectory` | int, default `48` | Number of steps per trajectory segment saved to the dataset. |
|
| 31 |
-
| `--out_dir` | str, default `/home/lau/sim/DynaTraj/dataset` | Directory to store output `.npz` and metadata `.pkl`. |
|
| 32 |
-
| `--render` | flag (bool), default `False` | If set, render frames during collection (tries OpenCV, then matplotlib). |
|
| 33 |
-
|
| 34 |
-
Notes:
|
| 35 |
-
- Actions are sampled i.i.d. uniformly from [-1, 1] each step and treated as torques.
|
| 36 |
-
- If the model uses a free base, the root quaternion is output as `(x, y, z, w)`.
|
| 37 |
-
|
| 38 |
-
### Output format
|
| 39 |
-
- Dataset file: `dmcontrol_{domain}_{task}_seed{seed}_{timestamp}.npz`
|
| 40 |
-
- Metadata file: `dmcontrol_{domain}_{task}_seed{seed}_{timestamp}_metadata.pkl`
|
| 41 |
-
|
| 42 |
-
`npz` keys (all stored by `TrajectoryBuffer`):
|
| 43 |
-
- `obs`: shape `[N, B, T, D_obs]`
|
| 44 |
-
- `ext_obs`: shape `[N, B, T, D_obs]` (same content as `obs` in this script)
|
| 45 |
-
- `action`: shape `[N, B, T, D_act]`
|
| 46 |
-
- `reward`: shape `[N, B, T]`
|
| 47 |
-
- `done`: shape `[N, B, T]`
|
| 48 |
-
|
| 49 |
-
Where:
|
| 50 |
-
- `N` = number of trajectory segments (equals `trajectories_per_file` for `B=1`)
|
| 51 |
-
- `B` = batch size (this script uses `B=1`)
|
| 52 |
-
- `T` = `steps_per_trajectory`
|
| 53 |
-
- `D_obs` = state dimension described above
|
| 54 |
-
- `D_act` = action dimension from the environment action spec
|
| 55 |
-
|
| 56 |
-
The metadata `.pkl` contains: domain, task, seed, counts, action bounds, timestamp, and `render` flag.
|
| 57 |
-
|
| 58 |
-
### Examples
|
| 59 |
-
- Quadruped walk (default):
|
| 60 |
-
```bash
|
| 61 |
-
python /home/lau/sim/DynaTraj/dmcontrol_collect.py
|
| 62 |
-
```
|
| 63 |
-
- Cheetah run (planar cheetah):
|
| 64 |
-
```bash
|
| 65 |
-
python /home/lau/sim/DynaTraj/dmcontrol_collect.py --domain cheetah --task run --seed 1 --trajectories_per_file 512 --steps_per_trajectory 48 --out_dir /home/lau/sim/DynaTraj/dataset
|
| 66 |
-
```
|
| 67 |
-
- With rendering (requires OpenCV or matplotlib):
|
| 68 |
-
```bash
|
| 69 |
-
python /home/lau/sim/DynaTraj/dmcontrol_collect.py --domain quadruped --task walk --render
|
| 70 |
-
```
|
| 71 |
-
|
| 72 |
-
### Tips
|
| 73 |
-
- Rendering slows down collection; disable `--render` when collecting large datasets.
|
| 74 |
-
- If a task terminates early, the script resets automatically and continues until it reaches the requested number of trajectories.
|
| 75 |
-
- Ensure MuJoCo is set up properly in your environment if dm_control fails to import.
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
python /home/lau/sim/DynaTraj/sb3_collect.py \
|
| 79 |
-
--domain cheetah --task run \
|
| 80 |
-
--algo SAC \
|
| 81 |
-
--ckpt_root /home/lau/sim/DynaTraj/weights \
|
| 82 |
-
--ckpt_indices 1,10,20,30,40,50 \
|
| 83 |
-
--trajectories_per_ckpt 5120 \
|
| 84 |
-
--steps_per_trajectory 24 \
|
| 85 |
-
--out_dir /home/lau/sim/DynaTraj/dataset \
|
| 86 |
-
--device cpu \
|
| 87 |
-
--render
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
train_sb3_dmcontrol.py → sb3_train.py
RENAMED
|
File without changes
|