File size: 5,805 Bytes
9897e20 | 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 | {
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import dataclasses\n",
"import mediapy\n",
"from huggingface_hub import PyTorchModelHubMixin\n",
"from huggingface_hub import ModelCard\n",
"from gpudrive.networks.late_fusion import NeuralNet\n",
"from gpudrive.env.config import EnvConfig\n",
"from gpudrive.env.env_torch import GPUDriveTorchEnv\n",
"from gpudrive.env.dataset import SceneDataLoader\n",
"from gpudrive.utils.config import load_config \n",
"import sys\n",
"import imageio\n",
"import numpy as np\n",
"import os\n",
"from gpudrive.utils.multi_policy_rollout import multi_policy_rollout\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def create_policy_masks(env, num_sim_agents=2, num_worlds=10):\n",
" policy_mask = torch.zeros_like(env.cont_agent_mask, dtype=torch.int)\n",
" agent_indices = env.cont_agent_mask.nonzero(as_tuple=True)\n",
"\n",
" for i, (world_idx, agent_idx) in enumerate(zip(*agent_indices)):\n",
" policy_mask[world_idx, agent_idx] = (i % num_sim_agents) + 1\n",
"\n",
" policy_masks = {f'pi_{int(policy.item())}': torch.zeros_like(env.cont_agent_mask, dtype=torch.bool,device=device) \n",
" for policy in policy_mask.unique() if policy.item() != 0}\n",
"\n",
" for p in range(1, num_sim_agents + 1):\n",
" policy_masks[f'pi_{p}'] = (policy_mask == p).reshape(num_worlds, -1)\n",
"\n",
" return policy_masks\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Configs model has been trained with\n",
"config = load_config(\"../../examples/experimental/config/reliable_agents_params\")\n",
"max_agents = config.max_controlled_agents\n",
"NUM_ENVS = 2\n",
"device = \"cpu\" # cpu just because we're in a notebook\n",
"NUM_SIM_AGENTS = 2\n",
"FPS = 5\n",
"\n",
"sim_agent1 = NeuralNet.from_pretrained(\"daphne-cornelisse/policy_S10_000_02_27\")\n",
"sim_agent2 = NeuralNet.from_pretrained(\"daphne-cornelisse/policy_S1000_02_27\")\n",
"\n",
"# Some other info\n",
"card = ModelCard.load(\"daphne-cornelisse/policy_S10_000_02_27\")\n",
"\n",
"\n",
"\n",
"\n",
"sim_agent1 = NeuralNet.from_pretrained(\"daphne-cornelisse/policy_S10_000_02_27\")\n",
"sim_agent2 = NeuralNet.from_pretrained(\"daphne-cornelisse/policy_S1000_02_27\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"train_loader = SceneDataLoader(\n",
" root='../../data/processed/examples',\n",
" batch_size=NUM_ENVS,\n",
" dataset_size=100,\n",
" sample_with_replacement=False,\n",
")\n",
"\n",
"# Set params\n",
"env_config = dataclasses.replace(\n",
" EnvConfig(),\n",
" ego_state=config.ego_state,\n",
" road_map_obs=config.road_map_obs,\n",
" partner_obs=config.partner_obs,\n",
" reward_type=config.reward_type,\n",
" norm_obs=config.norm_obs,\n",
" dynamics_model=config.dynamics_model,\n",
" collision_behavior=config.collision_behavior,\n",
" dist_to_goal_threshold=config.dist_to_goal_threshold,\n",
" polyline_reduction_threshold=config.polyline_reduction_threshold,\n",
" remove_non_vehicles=config.remove_non_vehicles,\n",
" lidar_obs=config.lidar_obs,\n",
" disable_classic_obs=config.lidar_obs,\n",
" obs_radius=config.obs_radius,\n",
" steer_actions = torch.round(\n",
" torch.linspace(-torch.pi, torch.pi, config.action_space_steer_disc), decimals=3 \n",
" ),\n",
" accel_actions = torch.round(\n",
" torch.linspace(-4.0, 4.0, config.action_space_accel_disc), decimals=3\n",
" ),\n",
")\n",
"\n",
"\n",
"\n",
"env = GPUDriveTorchEnv(\n",
" config=env_config,\n",
" data_loader=train_loader,\n",
" max_cont_agents=max_agents,\n",
" device=device,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"next_obs = env.reset()\n",
"\n",
"\n",
"control_mask = env.cont_agent_mask\n",
"\n",
"policy_mask = create_policy_masks(env, 2,NUM_ENVS)\n",
"\n",
"policies_set = {'pi_1': (sim_agent1,policy_mask['pi_1']),\n",
" 'pi_2': (sim_agent2, policy_mask['pi_2'])\n",
" } \n",
" \n",
"\n",
"\n",
"metrics,frames=multi_policy_rollout(\n",
"env,\n",
"policies_set, \n",
"device,\n",
"deterministic=False,\n",
"render_sim_state = True,\n",
"render_every_n_steps= 5\n",
")\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"env.close()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"mediapy.show_videos(frames, fps=15, width=500, height=500, columns=2, codec='gif')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "gpudriveenv",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|