---
title: 'CEMFormer: Cross-View Episodic Memory Transformer'
url: https://www.emergentmind.com/topics/cemformer
type: topic
---

# CEMFormer: Cross-View Episodic Memory Transformer

CEMFormer (Cross-View Episodic Memory Transformer) is a unified, recurrent transformer-based framework for driver intention prediction that integrates multi-camera video streams—including both in-cabin and forward-facing (external) views—with a compact episodic memory mechanism and a novel context-consistency loss. CEMFormer is designed to anticipate maneuvers such as lane changes or turns by jointly processing visual input and historical scene information, and by explicitly enforcing logical consistency with the driving context. This architecture achieves state-of-the-art performance on benchmark datasets for intention prediction while maintaining computational efficiency and leveraging fewer parameters than previous multi-modal approaches [2305.07840].

## 1. Model Architecture

CEMFormer employs a spatial-temporal encoder based on the Vision Transformer (ViT-B/16) backbone, extended to support: (a) multi-view (cross-camera) fusion, (b) recurrent episodic memory integration, and (c) joint representation learning across time and views.

**Input Representation**  
Each time step $t$ receives $M$ synchronized RGB frames, $x_{t,1}, \ldots, x_{t,M}$, where typically $M = 2$ (in-cabin and external). Each frame, with dimensions $C \times H \times W$ ($C = 3$, $H = W = 224$), is partitioned into $N_m = (H \cdot W)/P^2$ non-overlapping patches ($P = 16$), flattened and projected to $D$-dimensional tokens ($D = 768$ for ViT-B/16), incorporating view-specific learned positional embeddings:
$$
x_m^{vit} = [x_m^1 E_m, \dots, x_m^{N_m} E_m] + E_m^{pos},
$$
where $E_m \in \mathbb{R}^{(P^2 C) \times D}$ and $E_m^{pos} \in \mathbb{R}^{N_m \times D}$.

All view tokens are concatenated:
$$
z_t^0 = [x_{t,1}^{vit}; x_{t,2}^{vit}; \ldots; x_{t,M}^{vit}].
$$

**Recurrent Episodic Memory**  
To incorporate historical context, $K$ learnable memory tokens, $E_t^{mem} \in \mathbb{R}^{K \times D}$, are prepended:
$$
\bar{z}_t^0 = [E_t^{mem}; z_t^0],
$$
where $K=4$ is chosen empirically.

A stack of $L$ transformer encoder layers (e.g., $L=12$) processes $\bar{z}_t^0$. Each layer uses standard multi-head self-attention (MHSA) and MLP blocks:
$$
Q_i = \bar{z}_t^{\ell-1} W_i^Q, \quad
K_i = \bar{z}_t^{\ell-1} W_i^K, \quad
V_i = \bar{z}_t^{\ell-1} W_i^V, \\
\text{head}_i = \mathrm{softmax}\left(\frac{Q_i K_i^T}{\sqrt{d_k}}\right)V_i, \\
\text{MHSA}(\bar{z}_t^{\ell-1}) = [\text{head}_1, \dots, \text{head}_H] W^O,
$$
with $H=12$ and $d_k=D/H$.

After the final layer, the first $K$ positions are sliced as updated memory for the next time step:
$$
E_{t+1}^{mem} = z_{t,1:K}^L, \quad \bar{z}_{t+1}^0 = [E_{t+1}^{mem}; z_{t+1}^0].
$$

This mechanism realizes a trainable, recirculating memory bank, enabling compression and propagation of salient historical context.

**Cross-View Fusion**  
Self-attention is performed jointly over memory tokens and all input view tokens, enabling unified context modeling across modalities (in-cabin, external) and time.

## 2. Context-Consistency Loss

CEMFormer incorporates a loss function that augments standard cross-entropy with a context-consistency term designed to enforce compatibility between predicted intentions and the current traffic context.

- **Cross-Entropy Loss** ($\ell^{ce}$) at each time step:
$$
\ell^{ce} = -\sum_{c \in Y} y_c \log p_c,
$$
where $y$ is a one-hot ground-truth label and $p$ is the softmax output over intention classes.

- **Context-Consistency Loss** ($\ell^{cc}$):  
To penalize predictions that contradict known driving constraints, the following auxiliary loss is introduced:
$$
\ell^{cc} = -\sum_{(r, A) \in \mathcal{S}} \mathds{1}_{[c \in A]} \log (1 - p_r),
$$
where $\mathcal{S}$ is the set of incompatible (intention, context) pairs, $c \in C$ represents the current driving context (e.g., leftmost, rightmost, near intersection), and $p_r$ is the predicted probability for intention $r$.

- **Joint Objective**:  
A temporally exponentially weighted sum is used to encourage early and confident prediction:
$$
\mathcal{L}_{\text{joint}} = \sum_{i=1}^N \sum_{t=1}^T e^{-(T-t)}\bigl(\ell_i^{ce}(t) + \ell_i^{cc}(t)\bigr).
$$

## 3. Training Protocol and Dataset

- **Dataset**: All evaluations use the Brain4Cars benchmark comprising 594 video clips with dual-view (in-cabin and forward-facing) data and 5 intention classes: go straight, left lane change, left turn, right lane change, right turn.

- **Preprocessing**:  
  - Frames are resized to $224 \times 224$.
  - Frames are split into $14 \times 14$ patches per view.
  - Clips are partitioned into $T$ equal-duration segments; a single frame per segment is sampled (TSN-inspired).
  - Random horizontal flips and crops act as augmentation.

- **Optimization**:
  - Encoder initialized from DINO-pretrained ViT-B/16 on ImageNet.
  - Only the self-attention layers of the spatial-temporal encoder are fine-tuned.
  - AdamW optimizer, weight decay 0.05, cosine learning rate schedule (initial $5 \times 10^{-5}$).
  - Batch size 10, trained over 200 epochs on NVIDIA RTX 3090 Ti.

- **Validation**:  
  - 5-fold cross-validation, mean and standard deviation of accuracy and F1 reported.

## 4. Experimental Results

CEMFormer demonstrates superior performance across all evaluated settings, as summarized below:

| Configuration      | Accuracy (%) | F1 (%) | Gain vs. SOTA | Parameter Count |
|--------------------|-------------|--------|---------------|-----------------|
| In-cabin only      | 84.5        | 82.7   | +1.4/1.0      | —               |
| External only      | 64.8        | 66.3   | +4–6 acc      | —               |
| Multi-view fusion  | 85.4        | 87.1   | +2.8 F1       | ~87M (~60% less)|

- CEMFormer outperforms Gebert et al. (83.1%/81.7% in-cabin) and prior multi-view methods by 2.8 F1 on Brain4Cars.
- Parameter efficiency: approximately 87 million parameters, which is about 60% fewer than leading competing architectures.

**Ablation Studies**:
- Episodic Memory module raises F1 by ≈4.4%.
- Context-Consistency loss confers a +5.4% F1 improvement.
- Combined EM and CC yield 85.37% accuracy / 87.09% F1.
- Optimal number of memory tokens: $K=4$. Lower or higher $K$ (e.g., 2 or 8) show diminished and more variable gains.
- Inference throughput: 22 FPS (single-view) and ≈15.6 FPS (dual-view) on RTX 3090 Ti.

## 5. Qualitative Analysis and Attention Interpretation

Attention analysis of the final encoder layer provides critical insight into CEMFormer's mechanisms:

- Without episodic memory, attention patterns are frame-local and temporally variable, resulting in inconsistent saliency and degraded intention prediction.
- With episodic memory, the model exhibits temporally stable and spatially coherent focus—e.g., on temporally relevant driver cues such as head pose and steering wheel from early time steps, refining attention as more context accumulates.
- Absence of context-consistency loss permits attention drift to contextually irrelevant regions, such as backgrounds.
- Integration of both context-consistency loss and dual-view input encourages attention towards logically consistent, task-relevant regions, including driver gaze and appropriate lane markers, respecting traffic constraints like lane boundaries.

## 6. Architectural and Methodological Significance

CEMFormer delivers a compact, real-time-capable driver intention prediction system that enhances prior transformer-based and fusion techniques. Its key contributions are:

- Unified memory-augmented spatial-temporal transformer for cross-view and temporal fusion.
- Trainable episodic memory bank enabling efficient history compression and propagation.
- Augmentation of standard intention classification losses with auxiliary context-based supervision, demonstrably improving prediction fidelity and logical coherence with real-world constraints.

By jointly leveraging multi-modal visual input and explicit temporal reasoning, with direct enforcement of logical consistency, CEMFormer sets a new state-of-the-art in compact, accurate driver intention modeling on publicly available benchmarks [2305.07840].

Source: https://www.emergentmind.com/topics/cemformer