Offset Inverse
Turning a detector's offset predictions back into real boxes
- anchors
- offset_preds
- predicted_bbox
During training, Offset Boxes encodes ground truth into the offset form a model can predict. At inference you've got the opposite problem: the model hands you offset predictions, and you need actual rectangles you can draw on an image. CdlOffsetInverse is that decoder - it applies the inverse transformation to convert (dx, dy, dw, dh) predictions, anchored to a fixed anchor grid, back into corner-format boxes.
It's the offset_inverse function from the Dive into Deep Learning object-detection chapter, and it's the node you reach for the moment your detector's forward pass finishes. In the textbook pipeline, this is the exact step between "model output" and "boxes you can look at."
How the decode works
Given an anchor box and the model's predicted offsets, the inverse of the d2l encoding is:
- predicted center = anchor center + (predicted
dx× anchor size / 10) - predicted size = exp(predicted
dw/ 5) × anchor size
So the 10× and 5× scalings you saw on the encode side come back off here, and the log-ratio becomes an exponential. The result is converted from center-format back to (x1, y1, x2, y2) corners. It's a pure element-wise, per-anchor operation - no cross-anchor logic, no global context - which is exactly why a detector trained on local offset targets can afford to predict all boxes in parallel.
The inputs
anchors-[N, 4]corner-format anchors(x1, y1, x2, y2). Must match the anchor grid the offsets were predicted against, row for row.offset_preds-[N, 4]predicted offsets(dx, dy, dw, dh), typically the raw regression output of your model after a forward pass.
Output: predicted_bbox, a [N, 4] tensor of corner-format predicted boxes.
Installing ComfyDL
Same story as every node in this pack - it's light and self-contained:
cd ComfyUI/custom_nodes
git clone https://github.com/Cynthia-lxx/ComfyDL
cd ComfyDL && pip install -r requirements.txt
Restart ComfyUI, find it under ComfyDL → ObjectDetection, or search "ComfyDL" in ComfyUI Manager. The only dependency is matplotlib; nothing to download model-wise.
Where people trip up
Anchor alignment is everything. The decode is per-row, so anchor row i and offset row i must describe the same anchor. If your model reshaped or reordered anything between prediction and decode, your "reconstructed" boxes will be silently scrambled - shapes still match, positions nonsense. Keep anchors as the single source of truth and thread them through unchanged.
Second, this node decodes every anchor, background ones included. In the textbook pipeline you'd typically follow with thresholding and NMS (or use Multibox Detection, which bundles decode + NMS + thresholding into one node) to drop the noise. If you wire Offset Inverse straight to Show Bboxes, expect a wall of boxes - that's normal, not a bug.
A nice way to sanity-check the round trip: feed the offsets produced by Offset Boxes from real ground truth back through Offset Inverse, and you should recover the original boxes almost exactly. If that round trip is broken, your problem is upstream, not here.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| anchors | TENSOR | — | |
| offset_preds | TENSOR | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| predicted_bbox | TENSOR | — |