{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Log-playback (expert) trajectory extraction and usage\n", "\n", "This notebook demonstrates how to extract expert actions using different dynamics models and step through scene with those actions. \n", "\n", "Details on the dynamics models are found in [the docs](https://github.com/Emerge-Lab/gpudrive/tree/main/pygpudrive/env).\n", "\n", "See also the [`LogTrajectory`](https://github.com/Emerge-Lab/gpudrive/blob/main/pygpudrive/datatypes/trajectory.py) datatype and [usage](https://github.com/Emerge-Lab/gpudrive/blob/c2ebb838a6b684c0f579c149bcb74f9cf31dc3ba/pygpudrive/env/env_torch.py#L436)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from pathlib import Path\n", "import mediapy\n", "\n", "# Set working directory to the base directory 'gpudrive'\n", "working_dir = Path.cwd()\n", "while working_dir.name != 'gpudrive':\n", " working_dir = working_dir.parent\n", " if working_dir == Path.home():\n", " raise FileNotFoundError(\"Base directory 'gpudrive' not found\")\n", "os.chdir(working_dir)\n", "\n", "from gpudrive.env.config import EnvConfig, SceneConfig\n", "from gpudrive.env.env_torch import GPUDriveTorchEnv\n", "from gpudrive.visualize.utils import img_from_fig\n", "from gpudrive.env.dataset import SceneDataLoader" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Configurations" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "DYNAMICS_MODEL = \"delta_local\" # \"delta_local\" / \"state\" / \"classic\"\n", "DATA_PATH = \"data/processed/examples\" # Your data path\n", "MAX_NUM_OBJECTS = 64\n", "NUM_ENVS = 3\n", "\n", "# Configs\n", "env_config = EnvConfig(dynamics_model=DYNAMICS_MODEL)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Make environment" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Make dataloader\n", "data_loader = SceneDataLoader(\n", " root=\"data/processed/examples\", # Path to the dataset\n", " batch_size=NUM_ENVS, # Batch size, you want this to be equal to the number of worlds (envs) so that every world receives a different scene\n", " dataset_size=NUM_ENVS, # Total number of different scenes we want to use\n", " sample_with_replacement=False, \n", " seed=42, \n", " shuffle=True, \n", ")\n", "\n", "# Make environment\n", "env = GPUDriveTorchEnv(\n", " config=env_config,\n", " data_loader=data_loader,\n", " max_cont_agents=MAX_NUM_OBJECTS, # Maximum number of agents to control per scenario\n", " device=\"cuda\", \n", " action_type=\"continuous\" # \"continuous\" or \"discrete\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get log-playback (expert) actions\n", "\n", "- Different dynamics models have different action spaces. For details, [see the docs.](https://github.com/Emerge-Lab/gpudrive/tree/main/pygpudrive/env)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "control_mask = env.cont_agent_mask\n", "\n", "obs = env.reset(control_mask)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.Size([3, 64, 91, 3])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Extract full expert trajectory\n", "expert_actions, _, _, _ = env.get_expert_actions()\n", "\n", "expert_actions.shape # Shape: (num_envs, num_steps, num_agents, num_actions)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step through an episode" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Reset environment\n", "obs = env.reset(control_mask)\n", "done_envs = []\n", "\n", "frames = {f\"env_{i}\": [] for i in range(NUM_ENVS)}\n", "\n", "# Step through the scene\n", "for t in range(env.episode_len):\n", "\n", " env.step_dynamics(expert_actions[:, :, t, :])\n", " \n", " dones = env.get_dones()\n", " \n", " # Render the scenes\n", " env_indices = [i for i in range(NUM_ENVS) if i not in done_envs]\n", " figs = env.vis.plot_simulator_state(\n", " env_indices=env_indices,\n", " time_steps=[t]*NUM_ENVS,\n", " zoom_radius=100,\n", " #center_agent_indices=[0]*NUM_ENVS,\n", " )\n", " for i, env_id in enumerate(env_indices):\n", " frames[f\"env_{env_id}\"].append(img_from_fig(figs[i])) \n", " \n", " # Check if done\n", " for env_id in range(NUM_ENVS):\n", " if dones[env_id].all():\n", " done_envs.append(env_id)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Show videos" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n",
" \n",
" env_0 | \n",
" \n",
" env_1 | \n",
" \n",
" env_2 |