Skip to content

09 Lane assignment: hidden Markov model (Step 5, lanes.assignment_method = "hmm")

Entry function:

from trajlab.processing.lanes import LaneAssignmentParams, assign_lanes

result = assign_lanes(tracks, params=LaneAssignmentParams(method="hmm"), lane_centers=centers)
tracks, events = result.tracks, result.events

Purpose

The heuristic (08) decides each segment on its own. The HMM treats the lane as a hidden state and decodes the whole track at once, combines the Step 4 geometry with the detector intervals, gives posterior probabilities per sample, re-estimates the geometry (EM) and resolves conflicts between vehicles by re-decoding. Implementation: processing/lanes/hmm.py.

Interface

Pipeline step Requires columns Produces columns Further output columns
lane_correction id, datum, pos_lon, pos_lat, lane, speed lane_fixed is_changing_lane, is_lateral_excursion, lane_conflict_resolved, lane_fixed_unresolved, lane_confidence, lane_posterior_margin, suspected_duplicate_track, lane_conflict_unresolved

Besides lane_fixed and the flags of 08, the HMM writes lane_confidence (posterior of the decoded lane), lane_posterior_margin (difference to the second most probable lane), suspected_duplicate_track and lane_conflict_unresolved; events carry detected_by. The final geometry is the context artifact lane_geometry.

Parameter Default Description
lanes.hmm.rate_in_event 2 Rate (1/s) of a switch towards the detected side inside a detector interval.
lanes.hmm.rate_background 0.001 Rate (1/s) of any other switch.
lanes.hmm.sigma_floor_m 0.4 Minimum lateral spread of a lane (m). Defaults to 0.4 (0.2 in the original specification).
lanes.hmm.epsilon 0.001 Weight of the outlier floor in the emission.
lanes.hmm.emission_floor 'relative' "relative" (floor relative to the best lane, default) or "uniform" (uniform over the lateral range, as originally specified); see processing.lanes.hmm.emission_logprob.
lanes.hmm.initial_geometry 'labels' Where the lane centers of the first decoding come from: "labels" (default; medians of the Step 4 labels per em_bin_m bin outside detector intervals, smoothed with em_smooth_sigma_m) or "tracker" (the rolling GMM tracker centers of Step 4, interpolated along s).
lanes.hmm.zone_margin_m 10 Lane 0 is valid up to this distance (m) beyond the ends of an auxiliary-lane zone.
lanes.hmm.merge_gap_s 1 Detector intervals of one vehicle less than this far apart (s) form one manoeuvre for the A -> B -> A collapse (excursion split into "out" and "back").
lanes.hmm.max_dwell_s 2 A->B->A inside one joined manoeuvre window is collapsed to A only if the time in B is at most this (s; a longer stay in B counts as two real lane changes).
lanes.hmm.aux_lane_fallback True If zones are configured but Step 4 gave lane 0 no points, lane 0 still exists with center c_1 minus a lane width (e.g. a short deceleration lane at the end of the section).
lanes.hmm.conflict_margin_m 1 Feasibility: two vehicles in one lane conflict if their centers are closer than (L1 + L2) / 2 plus this margin (m).
lanes.hmm.conflict_penalty -9.21034 Log-penalty added to the emission of the less confident vehicle for the occupied lane on conflict frames. Defaults to log(1e-4).
lanes.hmm.max_conflict_iterations 10 Re-decoding rounds.
lanes.hmm.duplicate_min_overlap_sec 1 Conflicts longer than this (s) between two confident vehicles less than duplicate_max_lateral_m apart are flagged as suspected duplicate tracks instead of re-assigned.
lanes.hmm.duplicate_min_confidence 0.9 Duplicate flag: the lane confidence (0-1) of both vehicles must exceed this (see duplicate_min_overlap_sec).
lanes.hmm.duplicate_max_lateral_m 0.5 Duplicate flag: the vehicles must be laterally closer than this [m] (see duplicate_min_overlap_sec).
lanes.hmm.max_em_iterations 3 Alternating estimation of lane centers and labels.
lanes.hmm.em_tolerance 0.001 Stop when less than this share of labels changes. Defaults to 0.001 (0.1 %).
lanes.hmm.em_bin_m 10 Bin length of the center re-estimation (m).
lanes.hmm.em_smooth_sigma_m 30 Gaussian smoothing of the center profile (m).
lanes.hmm.min_points_per_bin 10 Minimum points of a lane in a bin (about one vehicle per 10 m bin at 30 Hz).
lanes.hmm.conflict_damping True Damped conflict resolution: only conflict episodes of a pair lasting at least conflict_min_duration_sec are resolved, the penalty window of the re-assigned vehicle covers at least conflict_hold_sec (not into its detected lane changes), and every conflict episode of a pair is resolved at most once. Defaults to True since 1.0 (False: every conflict frame is resolved, the behavior before 1.0).
lanes.hmm.conflict_min_duration_sec 1 t_min of the damping (s).
lanes.hmm.conflict_hold_sec 2 t_hold of the damping (s).
lanes.hmm.min_aux_zone_m 50 The HMM ignores auxiliary-lane zones shorter than this (m): their lane center cannot be estimated from the 50 m bins, the auxiliary state then pulls lane-1 vehicles in and must release them at the zone edges. The zones stay in the site configuration (alignment, heuristic). Defaults to 50.0 since 1.0, the bin length of the auxiliary-lane center estimate (0.0: off).

Method

  • States: lanes $k \in {1, \dots, n}$ (∪ {0} within auxiliary-lane zones ± zone_margin_m). With lane-count zones a lane is a valid state only within its observed range ± 25 m (06); otherwise centers are held constant beyond their known range.
  • Emission: $p(d \mid k) = (1-\varepsilon)\,N(d; c_k(s), \sigma_k^2) + \varepsilon\,\max_j N(d; c_j(s), \sigma_j^2)$ ("relative" floor, default); the alternative "uniform" uses $\varepsilon\,U$ (uniform over the lateral range). $\varepsilon$ = 10⁻³, $\sigma_k = \max(\sigma_{floor},\ 1.4826\,\mathrm{MAD}(d - c_k(s)))$ over points labeled $k$ outside detector intervals; $\sigma_{floor}$ = 0.4 m.
  • Transitions (rates per second, rate-invariant): $p_{k \to k\pm1} = 1 - e^{-\lambda\Delta t}$ with $\lambda$ = rate_in_event (2.0 /s) towards the detected side inside a detector interval, otherwise rate_background (10⁻³ /s, so slow drifts the detector misses can still switch); no jumps over more than one lane.
  • Decoding (standard HMM algorithms, Rabiner 1989): Viterbi $\delta_t(k) = \max_{j \in {k, k\pm1}}[\delta_{t-1}(j) + \log A_t(j,k)] + \log e_t(k)$; scaled forward–backward $\alpha_t = (\alpha_{t-1}A_t) \odot e_t$, $\beta_t = A_{t+1}(e_{t+1} \odot \beta_{t+1})$, $\gamma_t \propto \alpha_t \odot \beta_t$ → lane_confidence $= \gamma_t(\hat k_t)$, lane_posterior_margin $= \gamma_{(1)} - \gamma_{(2)}$. Batched over vehicles; $O(TK)$ per vehicle.
  • Events: every change of the decoded lane is a lane change at the first sample in the new lane; inside a detector interval the event takes that interval's start/end (detector+hmm), else the times where |SEWMA| drops below the threshold (hmm). Detector intervals without a change are excursions.
  • Add-on rules (each removed in turn in an ablation, see the design choices):
  • zone margin: lane 0 stays valid zone_margin_m (10 m) beyond a zone (needed: F1 −0.07 without);
  • joined manoeuvre windows: detector intervals less than merge_gap_s (1 s) apart count as one manoeuvre for the A→B→A rule, which collapses a return within one manoeuvre to A (needed: F1 −0.10 without);
  • dwell limit: the collapse requires at most max_dwell_s (2 s) in the other lane;
  • lane-0 fallback: lane 0 exists in zones even without Step 4 support (aux_lane_fallback). The last two show no effect on the synthetic battery but are kept for real cases (see the design choices).
  • Initial geometry: $c_k(s)$ = median of the Step 4 labels per 10 m bin outside detector intervals, Gaussian smoothing 30 m, ≥ min_points_per_bin points; sparse lanes keep a constant median.
  • EM (max_em_iterations = 3): decode → re-estimate $c_k(s)$, $\sigma_k$ from the HMM labels outside lane changes/excursions → decode; stop below em_tolerance (0.1 %) changed labels.
  • Feasibility across vehicles: a conflict is the same lane at the same time with $|s_1 - s_2| < (L_1 + L_2)/2 +$ conflict_margin_m; the vehicle with the lower mean confidence on the conflict frames gets conflict_penalty (log 10⁻⁴) added to the emission of the occupied lane there and is re-decoded (≤ max_conflict_iterations rounds). Long confident overlaps of laterally coincident tracks are flagged suspected_duplicate_track; remaining conflicts lane_conflict_unresolved.

Design choices

The choices below are made for this library unless a paper is named; the evidence for each is given under "Source" (a test, a script of applications/ant2027/ or a measurement named in the text).

  • Relative emission floor instead of a uniform one. With σ at its floor and ε = 10⁻³ both Gaussians fall below a uniform floor in the middle ≈ 1.9 m between two lane centers, so the emission carries no information there and the decoded crossing lands anywhere in that band. The relative floor bounds the influence of one sample in the same way (no lane is worse than the best one by more than log 1/ε) but keeps "the closest lane wins". Synthetic lane changes: crossing-time error 0.6 s → 0.0 s, F1 0.83 → 1.0; in the ablation the uniform floor costs ΔF1 −0.019.
  • Initial lane centers from the Step 4 labels, not from the tracked window centers. With sparse traffic the tracked centers collapse locally (synthetic drift: lane 1 from 2.07 m to 0.66 m over 100 m while the vehicles stay at 2.04 m). At drift speeds of about 0.1 m/s every 0.1 m of center error is 1 s of crossing-time error. The label medians are the same estimator as the EM update; initial_geometry = "tracker" remains available.
  • σ floor 0.4 m instead of 0.2 m. The larger floor is at least as good on the synthetic ablation (ΔF1 +0.001, accuracy +0.03 pp).
  • Add-on rules. Removing a rule in the ablation (40 synthetic scenarios with a realistic error model) shows which are needed: without the zone margin ΔF1 −0.073 (merges: vehicles observed exactly up to a zone end otherwise get forced transitions to and from lane 0); without joined manoeuvre windows ΔF1 −0.100 (the detector splits an excursion into "out" and "back", so a 2 m excursion would become two lane changes). The dwell limit and the lane-0 fallback have no effect on the synthetic battery, which does not contain their cases, and are kept for real ones: the dwell limit keeps a real change with a 5 s stay in the other lane from being collapsed into an excursion, and the fallback handles a short deceleration lane at a section end (exiD: F1 0.685 → 0.963). Both are switchable (max_dwell_s, aux_lane_fallback). Source: tests/processing/lanes/test_lane_hmm.py, tests/processing/lanes/test_lane_hmm_m5.py, analysis/quality/synthetic_battery.py.
  • EM first, feasibility once on the final geometry. Running the conflict loop inside every EM round would multiply the cost without changing the geometry noticeably (conflict rows are a tiny share of the points).
  • min_points_per_bin = 10 and a constant center for sparse lanes. At 30 Hz one vehicle gives about 9 samples per 10 m bin; with 20 a lightly used lane disappeared from the geometry and its vehicles were forced into the neighboring lanes.
  • Damped conflict resolution (default). Undamped re-decoding oscillates in dense traffic with overlapping positions: vehicles are re-assigned back and forth. With damping only conflict episodes of at least conflict_min_duration_sec (1 s) are resolved, the penalty window covers at least conflict_hold_sec (2 s) but never the vehicle's own detected lane changes, and each episode is resolved once. The two values were set beforehand, not tuned on ground truth. Source: tests/processing/lanes/test_conflict_damping.py.
  • Auxiliary-lane zones shorter than min_aux_zone_m (50 m) are ignored by the HMM. The auxiliary-lane center is estimated in 50 m bins; in a shorter zone it ends up almost on lane 1, so lane-1 vehicles enter the auxiliary state and must leave it at the zone edges (flicker). The 50 m are the bin width, a structural value, not tuned. The zones stay in the site configuration for the alignment and the heuristic.
  • The HMM is the library default. With damped conflict resolution and min_aux_zone_m = 50 it reached a sample accuracy of 99.24 % on exiD (13 directions; heuristic 98.98 %, point-wise 99.35 %) and 99.18 % on highD (20 recordings; point-wise 99.23 %), with lane-change F1 0.974 and 0.961 (heuristic on exiD 0.940, point-wise 0.973 and 0.944) and almost no flicker. See Method for the in-lane overtakings on the paper's recordings.

Validation and limitations

  • tests/processing/lanes/test_lane_hmm.py, tests/processing/lanes/test_lane_hmm_m5.py, tests/processing/lanes/test_conflict_damping.py, tests/processing/lanes/test_lane_hmm_interval_bounds.py; synthetic battery (analysis/quality/synthetic_battery.py).
  • Without damped conflict resolution (Sites 1 and 2, restricted GMM, constant HGV correction, stitching) the HMM had more in-lane overtakings than the heuristic: +0.22 per 100 vehicle-km (0.55 vs 0.33, block-bootstrap CI +0.03…+0.40), with fewer lane transitions (−5 %), less flicker (0.060 vs 0.081 per vehicle-km), a smaller lateral residual, and a better synthetic lane-change F1 (0.76 vs 0.71). In congestion the HMM was better (1.7 vs 4.0 per 100 vehicle-km).
  • With the constant HGV shift the HMM gets more in-lane overtakings in one direction of campaign 1b (lanes with opposite offset signs; 02).
  • Runtime: about 1.2 s per 1000 vehicle-minutes at 30 Hz (pure NumPy).