File size: 3,688 Bytes
2230fab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-License-Identifier: Apache-2.0
"""The CPU reference reads the weights the way the spec describes them (S:centerpoint:119-148, 205-257): the
Autoware parameters of both variants, and the module structure recovered from the ONNX topology (layer counts,
strides, channels, parameter counts after the BN folds), without onnxruntime or torch.

    TT_VISIBLE_DEVICES=none python -m pytest -q code/tt_centerpoint/tests/test_reference_structure_host.py
"""
from __future__ import annotations

import numpy as np
import pytest

pytest.importorskip("onnx")
pytest.importorskip("yaml")

from tt_centerpoint.reference.config import CenterPointConfig, variant_dir  # noqa: E402
from tt_centerpoint.reference.weights import load_weights  # noqa: E402
from tt_centerpoint.tests import _data  # noqa: E402


@pytest.mark.parametrize("variant,ds", [("base", 1), ("tiny", 2)])
def test_autoware_parameters(variant, ds):
    c = CenterPointConfig.from_weights(_data.weights_dir(), variant)
    assert c.version == "v4.1" and c.class_names == ("CAR", "TRUCK", "BUS", "BICYCLE", "PEDESTRIAN")
    assert (c.grid_x, c.grid_y, c.downsample_factor, c.down_grid_x) == (480, 480, ds, 480 // ds)
    assert c.max_voxel_size == 40000 and c.point_feature_size == 4 and c.encoder_in_feature_size == 9
    assert c.point_cloud_range == (-76.8, -76.8, -4.0, 76.8, 76.8, 6.0) and c.voxel_size == (0.32, 0.32, 10.0)
    assert not c.has_twist and not c.has_variance
    assert c.distance_bin_upper_limits == (50.0, 90.0, 121.0, 200.0)
    assert c.score_thresholds.shape == (4, 5) and np.all(c.score_thresholds == np.float32(0.35))
    assert c.yaw_norm_thresholds == (0.3, 0.3, 0.3, 0.3, 0.0)
    assert c.circle_nms_dist_threshold == 0.5 and c.with_common(circle_nms_dist_threshold=0).circle_nms_dist_threshold == 1.5
    rm = c.remapper
    assert rm.num_labels == 8 and rm.allow[1, 2] and rm.allow[1, 4] and rm.allow[3, 4] and not rm.allow[7].any()
    assert (rm.min_area[1, 2], rm.max_area[1, 2], rm.min_area[2, 4], rm.max_area[2, 4]) == (12.1, 36.0, 36.0, 999.999)
    assert c.head_channels == {"heatmap": 5, "reg": 2, "height": 1, "dim": 3, "rot": 2, "vel": 2}


@pytest.mark.parametrize("variant,first_stride", [("base", 1), ("tiny", 2)])
def test_graph_structure_and_parameter_counts(variant, first_stride):
    w = load_weights(_data.weights_dir(), variant)
    h = w.head
    assert [len(b) for b in h.blocks] == [4, 6, 6]
    assert [b[0].stride for b in h.blocks] == [first_stride, 2, 2]
    assert [(b[0].in_channels, b[-1].out_channels) for b in h.blocks] == [(32, 64), (64, 128), (128, 256)]
    assert [(d.kind, d.in_channels, d.out_channels, d.stride) for d in h.deblocks] == [
        ("conv", 64, 128, 1), ("conv_transpose", 128, 128, 2), ("conv_transpose", 256, 128, 4)]
    assert [d.bn_node is not None for d in h.deblocks] == [False, True, True]
    assert (h.shared.in_channels, h.shared.out_channels, h.shared.kernel) == (384, 64, 3)
    assert h.head_channels == {"heatmap": 5, "reg": 2, "height": 1, "dim": 3, "rot": 2, "vel": 2}
    n = sum(layer.weight.size + layer.bias.size for layer in h.layers())
    assert n == 5_032_975 - 1_024 + 2 * 128      # 5,032,975 ONNX params; the 2 deconv BNs fold into 2 x 128 biases
    e = w.encoder
    assert e.w0.shape == (9, 16) and e.w1.shape == (32, 32)
    assert e.w0.size + e.b0.size + e.w1.size + e.b1.size == 1_216       # 1,360 with the BN parameters (S:centerpoint:212)
    assert e.onnx_taps == {"pfn.layer0": e.onnx_taps["pfn.layer0"], "pfn": "pillar_features"}
    assert set(h.onnx_taps()) >= {"backbone.block0", "neck.deblock2", "head.shared", "head.heatmap"}
    assert variant_dir(_data.weights_dir(), variant).name == variant