Fuse Detections from Several Models¶
This guide shows how to combine the detections of several models, or of several test-time augmentation (TTA) passes of one model, into a single result with Detections.fuse. Fusion uses Weighted Box Fusion (WBF): overlapping detections of the same object become one detection whose box is a weighted average, instead of being discarded or unioned.
Choose NMS, NMM or Fusion¶
All three remove duplicate detections of one object. They differ in what happens to the duplicates.
| Method | The duplicates | The fused confidence | Use it when |
|---|---|---|---|
with_nms |
discarded, the best box is kept | the best box's confidence | one model, plain duplicates |
with_nmm |
boxes (and masks) united | a combination of the group's confidences | pieces of one object, such as objects cut by tile borders |
with_fusion / fuse |
averaged into one box | the mean over the models, a model that missed the object counts as zero | several models or TTA passes |
Fusion is the right choice when every source sees the whole object and the sources disagree a little about where it is. Averaging their boxes usually localizes the object better than any single source, and the fused confidence tells you how many sources agreed.
Fuse the Output of Several Models¶
Run each model on the same image and convert each result into its own Detections. Pass them to fuse as separate arguments. Every argument is one source. The example below uses hand-written detections so it runs without any model:
import numpy as np
import supervision as sv
model_a = sv.Detections(
xyxy=np.array([[100, 100, 200, 200], [300, 300, 380, 380]], dtype=float),
confidence=np.array([0.90, 0.60]),
class_id=np.array([0, 0]),
)
model_b = sv.Detections(
xyxy=np.array([[104, 98, 206, 204], [500, 100, 560, 160]], dtype=float),
confidence=np.array([0.80, 0.70]),
class_id=np.array([0, 0]),
)
fused = sv.Detections.fuse(model_a, model_b)
fused.xyxy.round(1).tolist()
# [[101.9, 99.1, 202.8, 201.9], [500.0, 100.0, 560.0, 160.0], [300.0, 300.0, 380.0, 380.0]]
fused.confidence.round(2).tolist()
# [0.85, 0.35, 0.3]
Three things happen here:
- The two boxes around the first object overlap, so they become one box. Its coordinates lie between the two inputs, closer to the more confident one.
- The fused confidence is
(0.90 + 0.80) / 2 = 0.85, the mean over both models. - The other two boxes were seen by one model only, so their confidence is halved:
0.70 / 2 = 0.35and0.60 / 2 = 0.30. A model that missed an object counts as a zero vote.
With a list of Detections, unpack it: sv.Detections.fuse(*detections_list).
The fused confidence is a mean, not a maximum, so it is lower than the best input. A confidence threshold that you tuned for one model or for NMS usually needs to be lowered, or applied before fusion. To drop the boxes that only one of two models found, filter after fusing, for example fused[fused.confidence > 0.5].
Fuse Test-Time Augmentation Passes¶
Test-time augmentation runs one model on several versions of an image (shifted, zoomed, flipped) and combines the results. Map every pass's boxes back to the coordinates of the original image first, then fuse them, one Detections per pass:
import supervision as sv
# `predict_view` stands for your own code: run the model on one view of the image
# and return its detections in the coordinates of the original image
passes = [predict_view(view) for view in views]
fused = sv.Detections.fuse(*passes)
A pass that found nothing still counts as a source. Pass its empty Detections as well, so the fused confidence drops for objects that pass missed. If you leave it out, the confidence of the remaining boxes is too high.
Boxes from one pass never fuse with each other. A low-confidence near-copy cannot add a second vote to a cluster that already contains a detection from that pass, but it can still fuse with detections from other passes into another cluster. With equal source weights, only a near-copy that remains a singleton has its confidence divided by the number of passes.
Tune the Fusion¶
Every option after the detections is a keyword argument:
fused = sv.Detections.fuse(
model_a,
model_b,
threshold=0.6,
class_agnostic=False,
box_weights="uniform",
source_weights=[2.0, 1.0],
)
threshold: how much two boxes must overlap, measured against the current fused box, to join one cluster. Raise it to keep nearby objects apart, lower it to merge sloppier boxes. Defaults to0.5.class_agnostic: withTrue, detections of different classes can fuse andclass_idis ignored. Defaults toFalse, so each class is fused on its own.box_weights:"confidence"(the default, as in the Weighted Box Fusion paper) averages the boxes by confidence."uniform"gives every box of a cluster the same weight, a plain mean. Test-time augmentation passes of one model are usually averaged with"uniform".source_weights: one positive number per input, in the order of the arguments. A source with weight2.0counts twice as much as one with1.0in the box average and in the confidence. Use it to trust a stronger model more. The weights are relative, so[2.0, 1.0]and[1.0, 0.5]give the same result.overlap_metricandoverlap_on: pick IoU or IoS, and whether overlap is measured on boxes or masks. See the with_fusion reference.
Fuse Concatenated Detections¶
If you already have the detections of all sources in one Detections, for example from Detections.merge, use with_fusion and tell it which source produced each row:
import numpy as np
merged = sv.Detections.merge([model_a, model_b])
sources = np.array([0, 0, 1, 1]) # model_a produced rows 0 and 1, model_b rows 2 and 3
fused = merged.with_fusion(num_models=2, sources=sources)
num_models is the number of sources, including those that found nothing, and is required with sources. This is what Detections.fuse does for you. Without sources, a cluster may hold several boxes of one model, and num_models only rescales the confidence.
Combine the Members Yourself¶
fusion_groups returns, for each fused detection, the indices of the input rows that were fused into it. It takes the same arguments as with_fusion and clusters the detections the same way. Use it to fuse something supervision does not fuse for you, such as masks at another resolution or extra fields:
groups[i] belongs to row i of the fused result. The first index of every group is its most confident member, the one whose class_id and data the fused detection takes.
Masks and Oriented Boxes¶
Fusion follows the geometry the detections carry:
- Detections with masks are fused by mask overlap. Each pixel of the fused mask is kept when the members that cover it carry at least half of the cluster's weight.
- Detections with oriented boxes (
data["xyxyxyxy"]) are fused as rotated boxes. - Pass
overlap_on="box"to cluster by box overlap instead of mask overlap. Boxes remain rotated when oriented coordinates are present; otherwisexyxyis used. With masks, this option unions the members' masks instead of applying weighted pixel voting.
One Detections cannot carry both masks and oriented boxes. Detections.fuse keeps only the fields that every nonempty input has, so if one model has no masks, the masks are dropped from the result.
Frequently Asked Questions¶
What is the difference between with_nms, with_nmm and with_fusion?¶
with_nms keeps the best box of each group of overlapping boxes. with_nmm unions the group into one box. with_fusion averages the group, and its confidence counts how many sources agreed.
Should I use with_fusion on the output of one model?¶
Usually no. Duplicates from one model are better removed with with_nms. Fusion helps when several independent views of the same object disagree a little.
Why is the fused confidence lower than my best detection?¶
It is the mean over all sources, and a source that missed the object counts as zero. Lower the confidence threshold you apply after fusion, or filter by confidence before fusing.
How do I fuse a list of Detections?¶
Unpack it: sv.Detections.fuse(*detections_list). Passing the list itself raises a TypeError.
Author¶
- Borda — Open Source Engineer, Roboflow