Nodes/ComfyDL/Assign Anchor→BBox
ComfyUI Node

Assign Anchor→BBox

The anchor-matching step behind object detection, exposed as a node

By Cynthia-lxx·Created 2 months ago·Updated 2 days ago· 6
Assign Anchor→BBox
  • ground_truth
  • anchors
  • anchors_bbox_map
iou_threshold0.50

CdlAssignAnchorToBbox is the "which anchor belongs to which object" step that every anchor-based detector - think SSD, YOLO, Faster R-CNN - has to solve before it can train, but that you almost never see because it's buried inside a training script. ComfyDL digs it out and gives it a node. If you're learning object detection the "Dive into Deep Learning" way (which is exactly what this pack is), this is the moment the theory turns into a tensor you can poke at.

What it's solving

Anchor boxes are a big grid of pre-placed boxes of different sizes and aspect ratios scattered over the image. The model predicts, for each anchor, whether there's an object in it and how to adjust it. But before you can train, you need to know the answer: which ground-truth box, if any, is each anchor responsible for? That's what this node computes.

It works in two passes, straight out of d2l's assign_anchor_to_bbox:

  1. It builds the IoU matrix between every anchor and every ground-truth box (IoU = intersection-over-union, the standard "how much do these boxes overlap" measure). Any anchor whose best IoU is at or above iou_threshold gets assigned to that ground-truth box.
  2. Then it greedily makes sure every ground-truth box still gets at least one anchor - even if the IoU fell below threshold - by locking in each ground-truth box's single best anchor. That's what stops a small object with weak overlaps from having no anchor assigned to it at all.

Inputs and output

  • ground_truth - a cdlTensor of [N, 4] boxes in (x1, y1, x2, y2) corner format. If yours are in center-width-height form, run them through CdlBoxCenterToCorner first.
  • anchors - the cdlTensor of anchor boxes, also [N, 4] corner format.
  • iou_threshold - default 0.5. Lower it and more anchors get assigned (looser matching); raise it and assignments tighten.

The single output, anchors_bbox_map, is a cdlTensor with one entry per anchor: the index of the ground-truth box it was assigned to, or -1 if it matched nothing. That -1 population is the background class - most anchors in real images are background, which is why training data has that huge negative-class imbalance everyone warns you about.

Where it fits

This node doesn't stand alone; it's the middle step of the detection-labeling pipeline: generate anchors with CdlMultiboxPrior, assign them with this node, then build the actual training labels with CdlMultiboxTarget. The README frames the whole pack as educational, and this is squarely one of those "now I understand why the detector works" nodes.

Installing it

It's part of the ComfyDL pack - one install, 106 nodes:

cd ComfyUI/custom_nodes
git clone https://github.com/Cynthia-lxx/ComfyDL
pip install -r ./ComfyDL/requirements.txt

Restart ComfyUI, or install "ComfyDL" through ComfyUI Manager. Only matplotlib is added beyond what ComfyUI already has.

Gotchas

  • This is the textbook single-scale version of anchor assignment. Real detectors use multi-scale anchor grids and more elaborate matching (like ignoring ambiguous anchors). Don't expect this node to plug into a production YOLO pipeline - it's for understanding the concept, and that's its whole job.
  • The iou_threshold interacts with the greedy step: a low threshold can give an anchor multiple candidate boxes, and the greedy pass keeps the strongest assignment. Read the output as "best assignment," not "every match."
  • cdlTensor slots only connect to other ComfyDL nodes - you can't feed it a regular image or a list of boxes from elsewhere. ComfyDL's cdlBbox type keeps everything in [N, 4] corner format for exactly this reason.
Categoryd2l/ObjectDetection

Inputs (3)

NameTypeDefaultDescription
ground_truthTENSOR
anchorsTENSOR
iou_thresholdFLOAT0.500–1

Outputs (1)

NameTypeDescription
anchors_bbox_mapTENSOR