10.3.3 EDA数据探索
(1)下面的代码首先从配置文件中获取训练数据集的路径 zarr_loc,然后使用 LocalDataManager 创建一个本地数据管理器对象 dm。接着,通过 ChunkedDataset 类加载并打开了训练数据集 train_zarr。最后,使用 print(train_zarr) 打印训练数据集的信息,以查看其中一个对象的结构和内容。这有助于了解数据集的组织和特征,以便进行后续的数据处理和分析。
zarr_loc = cfg["train_data_loader"]["key"]
dm = LocalDataManager()
train_zarr = ChunkedDataset(dm.require(zarr_loc)).open()
#let's see what one of the objects looks like
print(train_zarr)
执行后会输出:
+------------+------------+------------+-----------------+----------------------+----------------------+----------------------+---------------------+
| Num Scenes | Num Frames | Num Agents | Total Time (hr) | Avg Frames per Scene | Avg Agents per Frame | Avg Scene Time (sec) | Avg Frame frequency |
+------------+------------+------------+-----------------+----------------------+----------------------+----------------------+---------------------+
| 16265 | 4039527 | 320124624 | 112.19 | 248.36 | 79.25 | 24.83 | 10.00 |
+------------+------------+------------+-----------------+----------------------+----------------------+----------------------+---------------------+
(2)在下面的代码中,首先使用配置文件 cfg 和本地数据管理器 dm 构建了一个栅格化器 ras,栅格化是渲染3D模型的典型技术之一。接着,利用构建的栅格化器 rast、配置文件 cfg 和训练数据集 train_zarr 创建了一个智能驾驶汽车数据集对象 dataset。这个数据集将用于训练模型,其中包含了智能驾驶汽车的信息以及相关的场景数据,为进一步的模型训练和预测提供了必要的输入。
rast = build_rasterizer(cfg, dm)
dataset = EgoDataset(cfg, train_zarr , rast)
(3)使用配置文件 cfg 和之前创建的栅格化器 rasterizer,实例化了一个智能驾驶汽车数据集对象 AV_ds。该数据集将用于处理自动驾驶车辆(AV)的相关数据,为模型训练提供输入。
from l5kit.dataset import EgoDataset
AV_ds = EgoDataset(cfg, train_zarr, rasterizer)
(4)下面的代码从 AV_ds 数据集中获取索引为 50 的样本数据 sample,然后将其图像信息提取并转换为 RGB 格式。接着,使用 transform_points 将目标位置从世界坐标系转换为图像坐标系,通过 draw_trajectory 在图像上绘制了目标轨迹。最后,使用 Matplotlib 在一个大小为 (7, 7) 的子图上显示了包含目标轨迹的图像,如图10-4所示。这有助于可视化模型输入数据,以更好地理解车辆的运动轨迹。
from l5kit.visualization import draw_trajectory, TARGET_POINTS_COLOR
from l5kit.geometry import transform_points
sample = AV_ds[50]
im = sample["image"].transpose(1, 2, 0)
im = AV_ds.rasterizer.to_rgb(im)
target_positions_pixels = transform_points(sample["target_positions"] + sample["centroid"][:2], sample["world_to_image"])
draw_trajectory(im, target_positions_pixels, sample["target_yaws"], TARGET_POINTS_COLOR)
_, ax = plt.subplots(figsize = (7, 7))
plt.imshow(im[::-1])
plt.show()
图10-4 栅格化自动驾驶汽车的运动轨迹
在本项目中,目标轨迹的图像的颜色是由变量TARGET_POINTS_COLOR定义的。在上述代码中,使用了 draw_trajectory 函数,并将目标点的颜色设置为 TARGET_POINTS_COLOR。通常,TARGET_POINTS_COLOR 的默认值是蓝色。如果在代码中没有特别指定,那么轨迹上的目标点应该是蓝色的。
(5)在下面的代码中,通过修改配置文件 cfg 中的 map_type 参数为 "py_satellite",将地图类型切换为卫星图像。接着,使用修改后的配置文件构建了新的栅格化器 rasterizer,并更新了智能驾驶汽车数据集 AV_ds。随后,从更新后的数据集中获取了索引为 50 的样本数据 sample。最后,通过 draw_trajectory 函数在卫星图像上绘制了目标轨迹,并使用 Matplotlib 显示了包含目标轨迹的图像,如图10-5所示。这使得可视化更加逼真,基于卫星图像的场景信息。
cfg["raster_params"]["map_type"] = "py_satellite"
rasterizer = build_rasterizer(cfg, dm)
AV_ds = EgoDataset(cfg, train_zarr, rasterizer)
sample = AV_ds[50]
im = sample["image"].transpose(1, 2, 0)
im = AV_ds.rasterizer.to_rgb(im)
target_positions_pixels = transform_points(sample["target_positions"] + sample["centroid"][:2], sample["world_to_image"])
draw_trajectory(im, target_positions_pixels, sample["target_yaws"], TARGET_POINTS_COLOR)
_, ax = plt.subplots(figsize = (7, 7))
plt.imshow(im[::-1])
plt.show()
图10-5 卫星轨迹展示
(6)在下面的代码中,通过将配置文件 cfg 中的 map_type 参数更改为 "py_semantic",将地图类型切换为语义地图。然后,使用更新后的配置文件构建了新的栅格化器 rasterizer。接着,使用 AgentDataset 类创建了代理(Agent)数据集对象 A_ds,并从中获取了索引为 50 的样本数据 sample。最后,通过 draw_trajectory 函数在语义地图上绘制了目标轨迹,并使用 Matplotlib 显示了包含目标轨迹的图像,如图10-6所示。这使得可视化更具有语义信息,基于语义地图的场景呈现。
from l5kit.dataset import AgentDataset
cfg["raster_params"]["map_type"] = "py_semantic"
A_ds = AgentDataset(cfg, train_zarr, rasterizer)
sample = A_ds[50]
im = sample["image"].transpose(1, 2, 0)
im = A_ds.rasterizer.to_rgb(im)
target_positions_pixels = transform_points(sample["target_positions"] + sample["centroid"][:2], sample["world_to_image"])
draw_trajectory(im, target_positions_pixels, sample["target_yaws"], TARGET_POINTS_COLOR)
_, ax = plt.subplots(figsize = (7, 7))
plt.imshow(im[::-1])
plt.show()
图10-6 语义地图车辆轨迹
(7)通过将配置文件中的地图类型设为 "py_semantic",使用相关数据集创建了一系列语义地图中车辆运动轨迹的图像,并通过动画展示这些图像,提供了对车辆行为的可视化。
def animate_solution(images):
def animate(i):
im.set_data(images[i])
fig, ax = plt.subplots()
im = ax.imshow(images[0])
return animation.FuncAnimation(fig, animate, frames = len(images), interval = 60)
cfg["raster_params"]["map_type"] = "py_semantic"
rasterizer = build_rasterizer(cfg, dm)
A_ds = EgoDataset(cfg, train_zarr, rasterizer)
scene_idx = 34
indexes = AV_ds.get_scene_indices(scene_idx)
images = []
for idx in indexes:
data = A_ds[idx]
im = data["image"].transpose(1, 2, 0)
im = A_ds.rasterizer.to_rgb(im)
target_positions_pixels = transform_points(data["target_positions"] + data["centroid"][:2], data["world_to_image"])
center_in_pixels = np.asarray(cfg["raster_params"]["ego_center"]) * cfg["raster_params"]["raster_size"]
draw_trajectory(im, target_positions_pixels, data["target_yaws"], TARGET_POINTS_COLOR)
clear_output(wait=True)
images.append(PIL.Image.fromarray(im[::-1]))
HTML(animate_solution(images).to_jshtml())
上述代码的实现流程如下:
- 首先,定义了一个函数 animate_solution,用于创建一个动画,显示一系列图像。在主代码中,首先修改配置文件 cfg 的地图类型为 "py_semantic",然后使用该配置构建了一个语义地图的栅格化器 rasterizer。
- 接着,通过智能驾驶汽车数据集 AV_ds 获取了场景索引为 34 的相关样本索引。然后,通过循环遍历这些样本,将每个样本的图像数据转换为 RGB 格式,并使用 draw_trajectory 函数在图像上绘制目标轨迹。最终,将每一帧的图像作为 PIL 图像对象添加到 images 列表中。
- 最后,使用 animate_solution 函数创建动画,并通过 HTML 模块显示在Jupyter Notebook中。这个动画将显示一系列语义地图中车辆的运动轨迹,如图10-7所示。
图10-7 展示车辆运动轨迹动画