Nodes/ComfyDL/Multibox Prior
ComfyUI Node

Multibox Prior

Generate every anchor box your detector will ever need

By Cynthia-lxx·Created 2 months ago·Updated 2 days ago· 6
Multibox Prior
  • data
  • anchors
sizes0.75,0.5,0.25
ratios1,2,0.5

CdlMultiboxPrior generates the anchor boxes for a single-shot object detection setup - the "proposal" shapes a detector learns from. It's the direct port of the d2l textbook's multibox_prior function, and its job is to stamp a grid of boxes of different sizes and aspect ratios centered on every pixel of your image. Later, a detector model scores those boxes and predicts how to nudge them onto actual objects; this node is the scaffold that makes the whole thing possible.

If that sounds like a lot of boxes, you're right - and that's the point. The node computes len(sizes) + len(ratios) - 1 anchors per pixel. With the defaults (sizes=0.75,0.5,0.25 and ratios=1,2,0.5), that's 3 + 3 - 1 = 5 boxes per pixel, so a modest 561×728 input yields over two million candidate boxes. Anchor-based detection lives or dies by this precomputed set, which is why the d2l pipeline treats it as a standalone, reusable step.

How it works

It parses your comma-separated sizes and ratios, then for every pixel center computes boxes at each scale/aspect-ratio combination, expressed in normalized coordinates (top-left and bottom-right corners, 0–1). The output shape is [1, H*W*boxes_per_pixel, 4] - one leading batch dim, one row per anchor, four corner values. The spatial dimensions come from the optional data tensor's last two dims; if you leave data unwired it defaults to 561×728, which is the d2l demo image size. Feed it the actual image tensor and the anchors match your real resolution.

Inputs and output

  • sizes - comma-separated string of box scales, default 0.75,0.5,0.25.
  • ratios - comma-separated string of aspect ratios, default 1,2,0.5.
  • data - optional cdlTensor; used only to infer the image height/width. Omit for the 561×728 default.

The single output is anchors, a cdlTensor of shape [1, H*W*bpp, 4] in normalized corner coordinates.

Installing ComfyDL

It ships with the pack:

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

Restart ComfyUI. The only extra dependency is matplotlib; torch comes with ComfyUI. ComfyUI Manager users: search "ComfyDL", and if it's not in the built-in list (the pack isn't published to the official Comfy Registry yet), use Install via Git URL with the repo link.

Common issues

The gotcha is scale expectations. The sizes are relative to the image (0.75 = 75% of the smaller dimension), so the same settings produce physically different boxes on different resolutions - wire in data when you want anchors for a real image instead of the default shape. And the output volume is easy to underestimate: add one more ratio and you add a million-plus rows on a large image, which makes downstream steps slower. Keep the lists short until you know you need more. Finally, the output is in normalized coordinates - don't feed it straight to a pixel-space visualizer without scaling.

Categoryd2l/ObjectDetection

Inputs (3)

NameTypeDefaultDescription
sizesSTRING0.75,0.5,0.25
ratiosSTRING1,2,0.5
dataoptTENSOR

Outputs (1)

NameTypeDescription
anchorsTENSOR