Nodes/ComfyDL/VOC Random Crop
ComfyUI Node

VOC Random Crop

Crop the image and its label together, so the segmentation data stays honest

By Cynthia-lxx·Created 2 months ago·Updated 2 days ago· 6
VOC Random Crop
  • feature
  • label
  • cropped_feature
  • cropped_label
height320
width480
seed0

Data augmentation for segmentation has one rule that trips everyone up the first time: you can't crop the image and crop the label independently, or the pixels stop lining up and your ground truth is suddenly wrong. VOC Random Crop is the node that gets it right - it crops a feature image and its label image with the same random crop window, so the aligned pixels stay aligned. It's the d2l voc_rand_crop as a node, and it's your answer to "how do I get more training data without corrupting it."

The mechanism is exactly the "together" promise: it picks a random top-left corner and crop size via torchvision's RandomCrop, applies that identical window to both inputs, and returns the pair. A seed widget (default 0) makes the randomness reproducible. And there's a genuinely thoughtful fallback baked in: if your requested crop size is larger than the input image, torchvision's RandomCrop would just error - this node catches that and does a center_crop instead, so a mismatch gives you a working (if less augmented) result rather than a dead queue. That's the kind of defensive detail an educational pack gets right.

Inputs

  • feature - the image, as a native ComfyUI IMAGE ([B, H, W, C]).
  • label - the segmentation label, also an IMAGE with the same dimensions. Same size is mandatory - the node crops both with the same window and assumes they match.
  • height / width - the crop size in pixels, defaults 320 × 480 (step 32).
  • seed - optional, default 0. Set it and the same crop comes out every run; change it for variety.

Outputs

  • cropped_feature - the cropped image, IMAGE.
  • cropped_label - the cropped label, IMAGE, cut from the identical window.

Both keep the ComfyUI [B, H, W, C] layout, so they can flow straight back into the pack's conversion chain (VOC Label IndicesMASK) or into other image nodes.

Installing it

Part of ComfyDL. ComfyUI Manager, search "ComfyDL". Or:

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

Restart ComfyUI. Pack-wide, the only extra dependency is matplotlib; the torchvision usage here comes from your existing ComfyUI environment, so no separate install.

Gotchas

The size mismatch is the real trap, even with the center-crop safety net: if your label is smaller than your feature, you're silently getting a center crop, which is not random augmentation - it's just resizing by another name, and it won't help your model generalize the way random crops do. Keep feature and label the same dimensions at all times. Also note the node crops only the first batch item of each input - feed one image pair at a time. And one honest caveat: the "random" crop is seeded globally via torch.manual_seed, so if other nodes in the same run also set seeds, ordering can matter; in practice, use the seed widget and don't fight it. Niche pack with no community threads yet, but the aligned-crop logic is straight from the textbook, so the failure modes are all "did I feed it matching inputs," not "is the node broken."

Categoryd2l/Segmentation

Inputs (5)

NameTypeDefaultDescription
featureIMAGE
labelIMAGE
heightINT3201–4096
widthINT4801–4096
seedoptINT00–999999

Outputs (2)

NameTypeDescription
cropped_featureIMAGE
cropped_labelIMAGE