Exemple #1
0
def get_partial(
    cfg: dict,
    history_num_frames: int,
    history_step_size: int,
    history_step_time: float,
    future_num_frames: int,
    future_step_size: int,
    future_step_time: float,
) -> Callable:
    rast_params = cfg["raster_params"]

    render_context = RenderContext(
        raster_size_px=np.array(cfg["raster_params"]["raster_size"]),
        pixel_size_m=np.array(cfg["raster_params"]["pixel_size"]),
        center_in_raster_ratio=np.array(cfg["raster_params"]["ego_center"]),
    )

    rasterizer = StubRasterizer(render_context, rast_params["filter_agents_threshold"],)
    return functools.partial(
        generate_agent_sample,
        render_context=render_context,
        history_num_frames=history_num_frames,
        history_step_size=history_step_size,
        history_step_time=history_step_time,
        future_num_frames=future_num_frames,
        future_step_size=future_step_size,
        future_step_time=future_step_time,
        filter_agents_threshold=rast_params["filter_agents_threshold"],
        rasterizer=rasterizer,
    )
Exemple #2
0
def test_same_displacement(
    cfg: dict,
    zarr_dataset: ChunkedDataset,
    base_displacement: np.ndarray,
    raster_size: tuple,
    ego_center: tuple,
    pixel_size: tuple,
) -> None:
    cfg["raster_params"]["raster_size"] = raster_size
    cfg["raster_params"]["pixel_size"] = np.asarray(pixel_size)
    cfg["raster_params"]["ego_center"] = np.asarray(ego_center)

    render_context = RenderContext(
        np.asarray(raster_size),
        np.asarray(pixel_size),
        np.asarray(ego_center),
        set_origin_to_bottom=cfg["raster_params"]["set_origin_to_bottom"],
    )
    dataset = EgoDataset(
        cfg,
        zarr_dataset,
        StubRasterizer(render_context),
    )
    data = dataset[0]
    assert np.allclose(data["target_positions"], base_displacement)
Exemple #3
0
def test_scene_index_interval(dataset_cls: Callable, scene_idx: int,
                              zarr_dataset: ChunkedDataset, cfg: dict) -> None:
    rasterizer = StubRasterizer((100, 100), np.asarray((0.25, 0.25)),
                                np.asarray((0.5, 0.5)), 0)
    dataset = dataset_cls(cfg, zarr_dataset, rasterizer, None)
    indices = dataset.get_scene_indices(scene_idx)
    subdata = Subset(dataset, indices)
    for _ in subdata:  # type: ignore
        pass
Exemple #4
0
    def __init__(self, *args, **kwargs):  # type: ignore
        super(TestDeepPredictionSampling, self).__init__(*args, **kwargs)
        self.dataset = ChunkedStateDataset(path="./l5kit/tests/data/single_scene.zarr")
        self.dataset.open()

        self.raster_size = (100, 100)
        self.pixel_size = np.array([1.0, 1.0])
        self.ego_center = np.array([0.5, 0.25])
        self.filter_agents_threshold = 0.5
        self.rast = StubRasterizer(self.raster_size, self.pixel_size, self.ego_center, self.filter_agents_threshold)
Exemple #5
0
def test_scene_index_interval(dataset_cls: Callable, scene_idx: int,
                              zarr_dataset: ChunkedStateDataset) -> None:
    cfg = load_config_data("./l5kit/tests/artefacts/config.yaml")
    rasterizer = StubRasterizer((100, 100), np.asarray((0.25, 0.25)),
                                np.asarray((0.5, 0.5)), 0)
    dataset = dataset_cls(cfg, zarr_dataset, rasterizer, None)
    indices = dataset.get_scene_indices(scene_idx)
    subdata = Subset(dataset, indices)
    for _ in subdata:  # type: ignore
        pass
Exemple #6
0
def test_frame_index_interval(dataset_cls: Callable, frame_idx: int,
                              zarr_dataset: ChunkedDataset, cfg: dict) -> None:
    render_context = RenderContext(np.asarray((100, 100)),
                                   np.asarray((0.25, 0.25)),
                                   np.asarray((0.5, 0.5)))
    rasterizer = StubRasterizer(render_context, 0)
    dataset = dataset_cls(cfg, zarr_dataset, rasterizer, None)
    indices = dataset.get_frame_indices(frame_idx)
    subdata = Subset(dataset, indices)
    for _ in subdata:  # type: ignore
        pass
Exemple #7
0
def test_scene_index_interval(dataset_cls: Callable, scene_idx: int,
                              zarr_dataset: ChunkedStateDataset) -> None:
    cfg = load_config_data("./l5kit/configs/default.yaml")
    # replace th for agents for AgentDataset test
    cfg["raster_params"]["filter_agents_threshold"] = 0.5
    rasterizer = StubRasterizer((100, 100), np.asarray((0.25, 0.25)),
                                np.asarray((0.5, 0.5)), 0)
    dataset = dataset_cls(cfg, zarr_dataset, rasterizer, None)
    indices = dataset.get_scene_indices(scene_idx)
    subdata = Subset(dataset, indices)
    for _ in subdata:  # type: ignore
        pass
Exemple #8
0
def test_compute_mse_error(tmp_path: Path, zarr_dataset: ChunkedDataset, cfg: dict) -> None:
    render_context = RenderContext(
        np.asarray((10, 10)),
        np.asarray((0.25, 0.25)),
        np.asarray((0.5, 0.5)),
        set_origin_to_bottom=cfg["raster_params"]["set_origin_to_bottom"],
    )
    rast = StubRasterizer(render_context)
    dataset = AgentDataset(cfg, zarr_dataset, rast)

    gt_coords = []
    gt_avails = []
    timestamps = []
    track_ids = []

    for idx, el in enumerate(dataset):  # type: ignore
        gt_coords.append(el["target_positions"])
        gt_avails.append(el["target_availabilities"])
        timestamps.append(el["timestamp"])
        track_ids.append(el["track_id"])
        if idx == 100:
            break  # speed up test

    gt_coords = np.asarray(gt_coords)
    gt_avails = np.asarray(gt_avails)
    timestamps = np.asarray(timestamps)
    track_ids = np.asarray(track_ids)

    # test same values error
    write_gt_csv(str(tmp_path / "gt1.csv"), timestamps, track_ids, gt_coords, gt_avails)
    write_pred_csv(str(tmp_path / "pred1.csv"), timestamps, track_ids, gt_coords, confs=None)

    metrics = compute_metrics_csv(str(tmp_path / "gt1.csv"), str(tmp_path / "pred1.csv"), [neg_multi_log_likelihood])
    for metric_value in metrics.values():
        assert np.all(metric_value == 0.0)

    # test different values error
    pred_coords = gt_coords.copy()
    pred_coords += np.random.randn(*pred_coords.shape)
    write_pred_csv(str(tmp_path / "pred3.csv"), timestamps, track_ids, pred_coords, confs=None)

    metrics = compute_metrics_csv(str(tmp_path / "gt1.csv"), str(tmp_path / "pred3.csv"), [neg_multi_log_likelihood])
    for metric_value in metrics.values():
        assert np.any(metric_value > 0.0)

    # test invalid conf by removing lines in gt1
    with open(str(tmp_path / "pred4.csv"), "w") as fp:
        lines = open(str(tmp_path / "pred1.csv")).readlines()
        fp.writelines(lines[:-10])

    with pytest.raises(ValueError):
        compute_metrics_csv(str(tmp_path / "gt1.csv"), str(tmp_path / "pred4.csv"), [neg_multi_log_likelihood])
def test_scene_index_interval(dataset_cls: Callable, scene_idx: int,
                              zarr_dataset: ChunkedDataset, cfg: dict) -> None:
    render_context = RenderContext(
        np.asarray((100, 100)),
        np.asarray((0.25, 0.25)),
        np.asarray((0.5, 0.5)),
        set_origin_to_bottom=cfg["raster_params"]["set_origin_to_bottom"],
    )
    rasterizer = StubRasterizer(render_context)
    dataset = dataset_cls(cfg, zarr_dataset, rasterizer, None)
    indices = dataset.get_scene_indices(scene_idx)
    subdata = Subset(dataset, indices)
    for _ in subdata:  # type: ignore
        pass
Exemple #10
0
def base_displacement(zarr_dataset: ChunkedStateDataset) -> np.ndarray:
    cfg = load_config_data("./l5kit/tests/artefacts/config.yaml")
    cfg["raster_params"]["raster_size"] = (100, 100)
    cfg["raster_params"]["ego_center"] = np.asarray((0.5, 0.5))
    cfg["raster_params"]["pixel_size"] = np.asarray((0.25, 0.25))

    dataset = EgoDataset(
        cfg,
        zarr_dataset,
        StubRasterizer(
            cfg["raster_params"]["raster_size"],
            cfg["raster_params"]["pixel_size"],
            cfg["raster_params"]["ego_center"],
            0.5,
        ),
    )
    data = dataset[0]
    return data["target_positions"]
Exemple #11
0
def test_coordinates_straight_road(zarr_dataset: ChunkedDataset,
                                   cfg: dict) -> None:
    # on a straight road `target_positions` should increase on x only
    render_context = RenderContext(
        np.asarray(cfg["raster_params"]["raster_size"]),
        np.asarray(cfg["raster_params"]["pixel_size"]),
        np.asarray(cfg["raster_params"]["ego_center"]),
    )
    dataset = EgoDataset(
        cfg,
        zarr_dataset,
        StubRasterizer(
            render_context,
            0.5,
        ),
    )

    # get first prediction and first 50 centroids
    centroids = []
    preds = []
    preds_world = []
    for idx in range(50):
        data = dataset[idx]
        if idx == 0:
            preds = data["target_positions"]
            preds_world = transform_points(
                preds, np.linalg.inv(data["agent_from_world"]))

        centroids.append(data["centroid"][:2])
    centroids = np.stack(centroids)

    # compute XY variances for preds and centroids
    var_preds = np.var(preds, 0, ddof=1)
    var_centroids = np.var(centroids, 0, ddof=1)

    assert var_preds[1] / var_preds[
        0] < 0.001  # variance on Y is way lower than on X
    assert var_centroids[1] / var_centroids[
        0] > 0.9  # variance on Y is similar to X

    # check similarity between coordinates
    assert np.allclose(preds_world[:-1], centroids[1:])
def get_partial(cfg: dict, history_num_frames: int, history_step_size: int,
                future_num_frames: int, future_step_size: int) -> Callable:
    rast_params = cfg["raster_params"]

    rasterizer = StubRasterizer(
        rast_params["raster_size"],
        np.asarray(rast_params["pixel_size"]),
        np.asarray(rast_params["ego_center"]),
        rast_params["filter_agents_threshold"],
    )
    return functools.partial(
        generate_agent_sample,
        raster_size=rast_params["raster_size"],
        pixel_size=np.asarray(rast_params["pixel_size"]),
        ego_center=np.asarray(rast_params["ego_center"]),
        history_num_frames=history_num_frames,
        history_step_size=history_step_size,
        future_num_frames=future_num_frames,
        future_step_size=future_step_size,
        filter_agents_threshold=rast_params["filter_agents_threshold"],
        rasterizer=rasterizer,
    )
Exemple #13
0
def test_same_displacement(
    cfg: dict,
    zarr_dataset: ChunkedDataset,
    base_displacement: np.ndarray,
    raster_size: tuple,
    ego_center: tuple,
    pixel_size: tuple,
) -> None:
    cfg["raster_params"]["raster_size"] = raster_size
    cfg["raster_params"]["ego_center"] = np.asarray(ego_center)
    cfg["raster_params"]["pixel_size"] = np.asarray(pixel_size)

    dataset = EgoDataset(
        cfg,
        zarr_dataset,
        StubRasterizer(
            cfg["raster_params"]["raster_size"],
            cfg["raster_params"]["pixel_size"],
            cfg["raster_params"]["ego_center"],
            0.5,
        ),
    )
    data = dataset[0]
    assert np.allclose(data["target_positions"], base_displacement)
Exemple #14
0
def test_same_displacement(
    zarr_dataset: ChunkedStateDataset,
    base_displacement: np.ndarray,
    raster_size: tuple,
    ego_center: tuple,
    pixel_size: tuple,
) -> None:
    cfg = load_config_data("./l5kit/tests/artefacts/config.yaml")
    cfg["raster_params"]["raster_size"] = raster_size
    cfg["raster_params"]["ego_center"] = np.asarray(ego_center)
    cfg["raster_params"]["pixel_size"] = np.asarray(pixel_size)

    dataset = EgoDataset(
        cfg,
        zarr_dataset,
        StubRasterizer(
            cfg["raster_params"]["raster_size"],
            cfg["raster_params"]["pixel_size"],
            cfg["raster_params"]["ego_center"],
            0.5,
        ),
    )
    data = dataset[0]
    assert np.allclose(data["target_positions"], base_displacement)
Exemple #15
0
def export_zarr_to_csv(
    zarr_dataset: ChunkedDataset,
    csv_file_path: str,
    future_num_frames: int,
    filter_agents_threshold: float,
    history_step_size: int = 1,
    future_step_size: int = 1,
    agents_mask: Optional[np.array] = None,
) -> None:
    """Produces a csv file containing the ground truth from a zarr file.

    Arguments:
        zarr_dataset (np.ndarray): The open zarr dataset.
        csv_file_path (str): File path to write a CSV to.
        future_num_frames (int): Amount of future displacements we want.
        filter_agents_threshold (float): Value between 0 and 1 to use as cutoff value for agent filtering
        history_step_size (int): Steps to take between frames, can be used to subsample history frames.
        future_step_size (int): Steps to take between targets into the future.
        agents_mask (Optional[np.array]): a boolean mask of shape (len(zarr_dataset.agents)) which will be used
        instead of computing the agents_mask
    """

    assert future_step_size == history_step_size == 1, "still not handled in select_agents"
    cfg = {
        "raster_params": {
            "pixel_size": np.asarray((0.25, 0.25)),
            "raster_size": (100, 100),
            "filter_agents_threshold": filter_agents_threshold,
            "ego_center": np.asarray((0.5, 0.5)),
        },
        "model_params": {
            "history_num_frames": 0,
            "future_num_frames": future_num_frames,
            "history_step_size": history_step_size,
            "future_step_size": future_step_size,
        },
    }

    rasterizer = StubRasterizer(
        raster_size=cfg["raster_params"]["raster_size"],
        pixel_size=cfg["raster_params"]["pixel_size"],
        ego_center=cfg["raster_params"]["ego_center"],
        filter_agents_threshold=filter_agents_threshold,
    )
    dataset = AgentDataset(cfg=cfg,
                           zarr_dataset=zarr_dataset,
                           rasterizer=rasterizer,
                           agents_mask=agents_mask)

    future_coords_offsets = []
    target_availabilities = []

    timestamps = []
    agent_ids = []

    for el in tqdm(dataset, desc="extracting GT"):  # type: ignore
        future_coords_offsets.append(el["target_positions"])
        timestamps.append(el["timestamp"])
        agent_ids.append(el["track_id"])
        target_availabilities.append(el["target_availabilities"])

    write_gt_csv(
        csv_file_path,
        np.asarray(timestamps),
        np.asarray(agent_ids),
        np.asarray(future_coords_offsets),
        np.asarray(target_availabilities),
    )
def export_zarr_to_ground_truth_csv(
    zarr_dataset: ChunkedStateDataset,
    csv_file_path: str,
    history_num_frames: int,
    future_num_frames: int,
    filter_agents_threshold: float,
    pixel_size: np.ndarray = np.array([0.25, 0.25]),
    history_step_size: int = 1,
    future_step_size: int = 1,
) -> None:
    """Produces a csv file containing the ground truth from a zarr file.

    Arguments:
        zarr_dataset (np.ndarray): The open zarr dataset.
        csv_file_path (str): File path to write a CSV to.
        history_num_frames (int): Amount of history frames to draw into the rasters.
        future_num_frames (int): Amount of history frames to draw into the rasters.
        filter_agents_threshold (float): Value between 0 and 1 to use as cutoff value for agent filtering
        pixel_size (np.ndarray) -- Size of one pixel in the real world
        history_step_size (int): Steps to take between frames, can be used to subsample history frames.
        future_step_size (int): Steps to take between targets into the future.
        based on their probability of being a relevant agent.
    """

    assert future_step_size == history_step_size == 1, "still not handled in select_agents"
    cfg = {
        "raster_params": {
            "pixel_size": pixel_size,
            "raster_size": (100, 100),
            "filter_agents_threshold": filter_agents_threshold,
            "ego_center": np.asarray((0.5, 0.5)),
        },
        "model_params": {
            "history_num_frames": history_num_frames,
            "future_num_frames": future_num_frames,
            "history_step_size": history_step_size,
            "future_step_size": future_step_size,
        },
    }

    rasterizer = StubRasterizer(
        raster_size=cfg["raster_params"]["raster_size"],
        pixel_size=pixel_size,
        ego_center=cfg["raster_params"]["ego_center"],
        filter_agents_threshold=filter_agents_threshold,
    )
    dataset = AgentDataset(cfg=cfg,
                           zarr_dataset=zarr_dataset,
                           rasterizer=rasterizer)

    future_coords_offsets = []
    timestamps = []
    agent_ids = []

    for el in dataset:  # type: ignore
        future_coords_offsets.append(el["target_positions"])
        timestamps.append(el["timestamp"])
        agent_ids.append(el["track_id"])

    write_coords_as_csv(
        csv_file_path,
        future_num_frames,
        np.asarray(future_coords_offsets),
        np.asarray(timestamps),
        np.asarray(agent_ids),
    )
def export_zarr_to_csv(
    zarr_dataset: ChunkedDataset,
    csv_file_path: str,
    future_num_frames: int,
    filter_agents_threshold: float,
    step_time: float = 0.1,
    agents_mask: Optional[np.array] = None,
) -> None:
    """Produces a csv file containing the ground truth from a zarr file.

    Arguments:
        zarr_dataset (np.ndarray): The open zarr dataset.
        csv_file_path (str): File path to write a CSV to.
        future_num_frames (int): Amount of future displacements we want.
        filter_agents_threshold (float): Value between 0 and 1 to use as cutoff value for agent filtering
        agents_mask (Optional[np.array]): a boolean mask of shape (len(zarr_dataset.agents)) which will be used
        instead of computing the agents_mask
    """

    cfg = {
        "raster_params": {
            "pixel_size": np.asarray((0.25, 0.25)),
            "raster_size": (100, 100),
            "filter_agents_threshold": filter_agents_threshold,
            "disable_traffic_light_faces": True,
            "ego_center": np.asarray((0.5, 0.5)),
            "set_origin_to_bottom": True,
        },
        "model_params": {
            "history_num_frames": 0,
            "future_num_frames": future_num_frames,
            "step_time": step_time
        },
    }

    render_context = RenderContext(
        np.asarray(cfg["raster_params"]["raster_size"]),
        cfg["raster_params"]["pixel_size"],
        cfg["raster_params"]["ego_center"],
        cfg["raster_params"]["set_origin_to_bottom"],
    )
    rasterizer = StubRasterizer(render_context)
    dataset = AgentDataset(cfg=cfg,
                           zarr_dataset=zarr_dataset,
                           rasterizer=rasterizer,
                           agents_mask=agents_mask)

    future_coords_offsets = []
    target_availabilities = []

    timestamps = []
    agent_ids = []

    for el in tqdm(dataset, desc="extracting GT"):  # type: ignore
        # convert agent coordinates to world offsets
        offsets = transform_points(el["target_positions"],
                                   el["world_from_agent"]) - el["centroid"][:2]
        future_coords_offsets.append(offsets)

        timestamps.append(el["timestamp"])
        agent_ids.append(el["track_id"])
        target_availabilities.append(el["target_availabilities"])

    write_gt_csv(
        csv_file_path,
        np.asarray(timestamps),
        np.asarray(agent_ids),
        np.asarray(future_coords_offsets),
        np.asarray(target_availabilities),
    )