Multibox Target
The node that turns anchor boxes into something a loss can chew on
- anchors
- labels
- bbox_offset
- bbox_mask
- class_labels
You've generated a grid of anchor boxes with Multibox Prior and drawn ground-truth boxes on an image. Now what? Raw anchors and raw labels can't train a detector - they're in different languages. Multibox Target is the translator. It works out which anchor should chase which ground-truth box, and packages the result into the exact three tensors a single-shot detector loss needs. If you're building an SSD-style detector in ComfyDL, this is the node that makes training possible at all.
This whole pack is a port of the classic Dive into Deep Learning (d2l) textbook code into ComfyUI nodes, and this node is a one-to-one translation of the multibox_target function from the object-detection chapter. It's educational first and practical second - you're not going to train a production YOLO here, but you will finally see how anchor labeling actually works, node by node.
How it works
For each image in your batch it runs the IoU-based assignment: every anchor is paired with the ground-truth box it overlaps most (IoU ≥ 0.5), and every ground-truth box is guaranteed at least one anchor. Anchors with no good match are background. Then it does two things with the matched anchors:
- labels them with the ground-truth class (class IDs are shifted by +1, so background stays 0)
- computes the offset from anchor to assigned box - center shifts scaled by 10×, width/height ratios log-scaled by 5× - and zeroes the offsets for unmatched anchors
The d2l scaling isn't arbitrary; it normalizes the regression targets so the model isn't chasing raw pixel deltas of wildly different magnitudes.
The inputs
anchors-[1, N, 4]corner-format boxes, typically straight out of Multibox Prior. The leading 1 is the batch dim, squeezed internally.labels-[B, M, 5], one row per ground-truth box per image, formatted[class_id, x1, y1, x2, y2].
That's it. No widgets to fiddle with; it's a pure data node.
The outputs
bbox_offset-[B, N*4]regression targets, one set per anchor (zeros for background anchors).bbox_mask-[B, N*4], 1.0 where an anchor matched a ground-truth box, 0 elsewhere. This is what stops background anchors from contributing to the box-loss.class_labels-[B, N]per-anchor class, 0 = background.
The natural wiring: bbox_offset into a regression loss and class_labels into a classification loss. ComfyDL has a Squared Loss node, and you can pair the mask with it to only penalize matched anchors - that's exactly the textbook setup. At inference you don't need this node at all; that's Offset Inverse plus NMS's job.
Installing ComfyDL
ComfyDL ships ~106 nodes in one pack, and the dependency footprint is tiny - its requirements.txt is just matplotlib, everything else (torch) already ships with ComfyUI:
cd ComfyUI/custom_nodes
git clone https://github.com/Cynthia-lxx/ComfyDL
cd ComfyDL && pip install -r requirements.txt
Restart ComfyUI and the nodes appear under the ComfyDL menu. Or use ComfyUI Manager and search "ComfyDL". No model files to download - it's all code and random-initialized weights.
Gotchas
Watch your tensor shapes. anchors needs the leading [1, N, 4] (the squeeze happens inside, but the input type expects 3D), and labels must be [B, M, 5] - a single image still wants a batch dimension. Get them swapped and you'll get a shape error rather than a helpful message. And remember the cdlTensor/cdlModel types only connect to other ComfyDL nodes, so keep this in its own little graph rather than trying to feed it into stock ComfyUI math nodes.
If you're brand new to anchor boxes, the whole ComfyDL detection chain - Prior → Target → Offset Boxes → Offset Inverse → NMS → Show BBoxes - is one of the best visual ways to actually understand a single-shot detector, because each step is a node you can inspect.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| anchors | TENSOR | — | |
| labels | TENSOR | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| bbox_offset | TENSOR | — |
| bbox_mask | TENSOR | — |
| class_labels | TENSOR | — |