Sub topic 10
DeepSORT: Motion Plus Appearance
Assume you know object detection, bounding boxes, NumPy arrays, and basic probability. You should also understand that a Kalman filter predicts an object's next position. This chapter shows how DeepSORT combines that motion prediction with visual appearance.
Why motion alone is not enough
SORT matches a predicted track to a detection mainly by bounding-box overlap. This works when objects move smoothly and remain separated. It fails when two people cross, when one person disappears behind another, or when the camera moves.
DeepSORT adds an appearance embedding. An embedding is a numerical vector produced by a neural network from an object's image crop. Similar-looking crops have nearby vectors. The tracker compares a new detection's embedding with embeddings stored for each track.
DeepSORT therefore uses two signals:
| Signal | Question it answers | Typical failure |
|---|---|---|
| Motion | Could this detection be where the track moved? | Fast or irregular motion |
| Appearance | Does this detection look like the same object? | Similar-looking objects or poor image quality |
The association process
For each video frame, DeepSORT follows this sequence:
- The Kalman filter predicts each track's new position.
- The detector produces bounding boxes and confidence scores.
- A motion gate rejects detections that are statistically too far from a prediction. The gate commonly uses Mahalanobis distance, which accounts for the filter's uncertainty.
- For remaining pairs, the tracker compares appearance embeddings with cosine distance.
- A matching algorithm assigns detections to tracks, usually giving recently updated tracks priority in a matching cascade.
- Matched tracks are updated. Unmatched detections may create tracks, and unmatched tracks enter a temporary missing state.
The motion gate is important. Appearance similarity should not be allowed to match a detection across an impossible distance. Conversely, appearance helps when bounding boxes overlap only slightly because of rapid movement or a missed frame.
A small association example
The following code illustrates appearance costs with a motion gate. A real implementation also runs a Kalman filter and solves the final assignment problem.
import numpy as np
def cosine_cost(track_embeddings, detection_embeddings):
tracks = np.asarray(track_embeddings, dtype=float)
detections = np.asarray(detection_embeddings, dtype=float)
tracks /= np.maximum(np.linalg.norm(tracks, axis=1, keepdims=True), 1e-12)
detections /= np.maximum(
np.linalg.norm(detections, axis=1, keepdims=True), 1e-12
)
return 1.0 - tracks @ detections.T
def gated_appearance_cost(track_embeddings, detection_embeddings,
motion_distance, gate=9.49):
cost = cosine_cost(track_embeddings, detection_embeddings)
motion_distance = np.asarray(motion_distance, dtype=float)
return np.where(motion_distance <= gate, cost, np.inf)
tracks = [[1, 0, 0], [0, 1, 0]]
detections = [[0.9, 0.1, 0], [0.1, 0.9, 0]]
motion = [[2.0, 20.0], [18.0, 1.5]]
print(gated_appearance_cost(tracks, detections, motion))A lower cosine distance means greater visual similarity. The value 9.49 is an example gate, not a universal setting. The correct threshold depends on the measurement model and the desired false-match rate.
Common errors and trade-offs
- Using appearance without motion: Similar uniforms can cause identity swaps.
- Using motion without uncertainty: A fixed pixel-distance threshold behaves poorly at different speeds and image scales.
- Storing only one embedding: A recent crop may be blurred or occluded. Keeping a gallery of recent embeddings makes matching more stable.
- Ignoring camera motion: A moving camera makes every object appear to move. Compensation, such as estimating global image motion, can improve the Kalman prediction.
- Overly strict thresholds: Tracks fragment when valid matches are rejected.
- Overly loose thresholds: One object's identity can transfer to another.
DeepSORT costs more than SORT because it runs an appearance model and stores feature vectors. Use it when identity consistency matters more than minimum latency.
Exercise
Two people cross paths. Their predicted boxes overlap, but their appearance embeddings remain distinct. Which signal should resolve the assignment, and why?
Show answer
Appearance should resolve it after the motion gate. Both detections are spatially plausible, so visual distance provides the identity information that overlap cannot provide.