File size: 8,610 Bytes
7accb91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://raw.githubusercontent.com/autonomousvision/navsim/main/assets/navsim_transparent.png\" alt=\"drawing\" width=\"800\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# NAVSIM Visualization Tutorial\n",
    "\n",
    "This notebook will introduce some basic plots to visualize the driving scenes in NAVSIM. All plots are created with `matplotlib` and are easy to customize for your application.\n",
    "\n",
    "## Table of Contents\n",
    "1. [Config](#config)\n",
    "2. [Birds-Eye-View](#bev)\n",
    "3. [Cameras](#camera)\n",
    "4. [Creating custom plots](#custom)\n",
    "5. [Creating GIFs](#gifs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Config <a name=\"config\"></a>\n",
    "\n",
    "NAVSIM offers two types of plots: \n",
    "- Birds-Eye-View (BEV) plots or \n",
    "- Camera plots. \n",
    "\n",
    "The LiDAR sensor can be visualized either in BEV or in camera images. All plots have a global configuration in [`navsim/visualization/config.py`](https://github.com/autonomousvision/navsim/blob/main/navsim/navsim/visualization/config.py). In this Python file, you can configure all colors or dimensions. The LiDAR point cloud can be colored in any colormap, showing the distance to the ego vehicle or the height of each point. In this tutorial, we first instantiate a `SceneFilter` and `SceneLoader` from the mini split."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from pathlib import Path\n",
    "\n",
    "import hydra\n",
    "from hydra.utils import instantiate\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from navsim.common.dataloader import SceneLoader\n",
    "from navsim.common.dataclasses import SceneFilter, SensorConfig\n",
    "\n",
    "SPLIT = \"mini\"  # [\"mini\", \"test\", \"trainval\"]\n",
    "FILTER = \"all_scenes\"\n",
    "\n",
    "hydra.initialize(config_path=\"../navsim/planning/script/config/common/scene_filter\")\n",
    "cfg = hydra.compose(config_name=FILTER)\n",
    "scene_filter: SceneFilter = instantiate(cfg)\n",
    "openscene_data_root = Path(os.getenv(\"OPENSCENE_DATA_ROOT\"))\n",
    "\n",
    "scene_loader = SceneLoader(\n",
    "    openscene_data_root / f\"navsim_logs/{SPLIT}\",\n",
    "    openscene_data_root / f\"sensor_blobs/{SPLIT}\",\n",
    "    scene_filter,\n",
    "    sensor_config=SensorConfig.build_all_sensors(),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Birds-Eye-View <a name=\"bev\"></a>\n",
    "\n",
    "The Birds-Eye-View (BEV) visualization in NAVSIM is useful for overviewing the map, bounding-box annotations, or the LiDAR point cloud. In standard setting, the BEV plot includes a 64m $\\times$ 64m frame centered at the rear axle of the ego vehicle (excluding LiDAR for simplicity). First, we take a random token and load a scene to visualize."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "token = np.random.choice(scene_loader.tokens)\n",
    "scene = scene_loader.get_scene_from_token(token)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The function `plot_bev_frame` takes a `Scene` and index of the step to visualize (history or future). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from navsim.visualization.plots import plot_bev_frame\n",
    "\n",
    "frame_idx = scene.scene_metadata.num_history_frames - 1 # current frame\n",
    "fig, ax = plot_bev_frame(scene, frame_idx)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The function `plot_bev_with_agent` visualizes the trajectory of an <span style=\"color:#DE7061\">agent</span> in comparison to the <span style=\"color:#59a14f\">human vehicle operator</span> at the current frame. This notebook shows an example of the naive `ConstantVelocityAgent`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from navsim.visualization.plots import plot_bev_with_agent\n",
    "from navsim.agents.constant_velocity_agent import ConstantVelocityAgent\n",
    "\n",
    "agent = ConstantVelocityAgent()\n",
    "fig, ax = plot_bev_with_agent(scene, agent)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cameras <a name=\"bev\"></a>\n",
    "\n",
    "The agents in NAVSIM have access to eight cameras surrounding the vehicle. The function `plot_cameras_frame` shows the cameras in a 3 $\\times$ 3 grid with cameras in each direction of the ego-vehicle and the BEV plot in the center. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from navsim.visualization.plots import plot_cameras_frame\n",
    "\n",
    "fig, ax = plot_cameras_frame(scene, frame_idx)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With `plot_cameras_frame_with_annotations`, you can visualize the bounding-box annotations in the camera images."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from navsim.visualization.plots import plot_cameras_frame_with_annotations\n",
    "\n",
    "fig, ax = plot_cameras_frame_with_annotations(scene, frame_idx)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With `plot_cameras_frame_with_lidar`, you can visualize the LiDAR point cloud in the camera images."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from navsim.visualization.plots import plot_cameras_frame_with_lidar\n",
    "\n",
    "fig, ax = plot_cameras_frame_with_lidar(scene, frame_idx)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating custom plots <a name=\"custom\"></a>\n",
    "\n",
    "The plots in NAVSIM use `matplotlib` and either add elements to a `plt.Axes` object or return the full `plt.Figure`. Functions in [`navsim/visualization/`](https://github.com/autonomousvision/navsim/blob/main/navsim/navsim/visualization) can be re-used to create custom plots. In this example, we create a plot for the bounding-box annotations and the LiDAR point cloud."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from navsim.visualization.plots import configure_bev_ax\n",
    "from navsim.visualization.bev import add_annotations_to_bev_ax, add_lidar_to_bev_ax\n",
    "\n",
    "\n",
    "fig, ax = plt.subplots(1, 1, figsize=(6, 6))\n",
    "\n",
    "ax.set_title(\"Custom plot\")\n",
    "\n",
    "add_annotations_to_bev_ax(ax, scene.frames[frame_idx].annotations)\n",
    "add_lidar_to_bev_ax(ax, scene.frames[frame_idx].lidar)\n",
    "\n",
    "# configures frame to BEV view\n",
    "configure_bev_ax(ax)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating GIFs <a name=\"gifs\"></a>\n",
    "\n",
    "You can transform frame-wise plots into short animated GIFs. Give any function to `frame_plot_to_gif`, which takes a `Scene` and `frame_idx` as input (ie. `plot_cameras_frame_with_annotations`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from navsim.visualization.plots import frame_plot_to_gif\n",
    "\n",
    "frame_indices = [idx for idx in range(len(scene.frames))]  # all frames in scene\n",
    "file_name = f\"./{token}.gif\"\n",
    "images = frame_plot_to_gif(file_name, plot_cameras_frame_with_annotations, scene, frame_indices)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "navsim",
   "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.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}