Nodes/ComfyUI aichemy nodes/YOLOv8 Segmentaion
ComfyUI Node

YOLOv8 Segmentaion

The node that makes YOLOv8 masks actually line up

By HAL41·Created 3 years ago·Updated 2 years ago· 6
YOLOv8 Segmentaion
  • image
  • segmentation
  • annotation
model_name
class_ind0
combine1
instance_ind0

The name is a lie in the most reassuring way. "aichemy" sounds like it should call some API or need a key, and it does neither. The whole pack is one node with one job: fix the fact that YOLOv8's segmentation mask doesn't line up with your image.

If you've ever run a YOLOv8-seg model in ComfyUI to cut a subject out - for compositing onto a new background, background removal, or feeding an inpaint - you've probably seen the mask come back subtly wrong. A phantom border above the shoulders, a bottom edge that crops early, outlines that don't hug the person you asked for. That's not you doing it wrong. Ultralytics doesn't run the model on your image at its native size. It letterboxes the image down to the model's input resolution (640×640 for the stock models), runs inference there, and hands you a mask at that padded, lower size. Naively resize it and the padding gets baked in - shifted, shrunk, misaligned. The README's comparison image shows exactly this, and it's the entire reason this node exists.

It does the math to strip the padding and resize the mask back to your image's original resolution, so the mask actually hugs the subject. That matters more than it sounds: a bounding-box detector gives you a rectangle that slices through arms and hair, while a segmentation mask follows the outline - the same lesson ADetailer users learned the hard way when seams appeared around fixed faces. If you want to cut a person out and drop them somewhere else, this is the missing step between "YOLO found them" and "the mask aligns."

How it works

The node registers its own model folder at ComfyUI/models/yolov8, loads whatever .pt you pick from the dropdown via ultralytics, and runs inference on the first image in the batch. Then it crops the mask using logic that deliberately replicates ultralytics' own scale_image padding-removal function (the author re-implemented it because importing the original "behaved weirdly", and added a 0.1 tolerance to match it exactly). Finally it resizes back to the original shape. Nothing magic - just the padding math YOLO should have done for you.

The inputs that matter

  • model_name - dropdown of .pt files in ComfyUI/models/yolov8. Drop a yolov8*-seg.pt there (or a fine-tuned one) or the dropdown is empty.
  • class_ind - which class to mask. On the stock COCO models, 0 is person.
  • combine - 1 ORs every detected instance of that class into one mask; 0 picks a single instance via instance_ind (and quietly falls back to combining if the index is out of range).

Outputs: segmentation (MASK) wires into anything that takes a mask - Set Latent Mask, composite nodes, inpainting, background swap. annotation (IMAGE) is the YOLO-drawn picture with boxes and labels, handy for a quick "did it actually find the subject" check.

Installing it

ComfyUI Manager, search "aichemy", or:

cd ComfyUI/custom_nodes
git clone https://github.com/HAL41/ComfyUI-aichemy-nodes

then restart ComfyUI. Watch the URL: the README's own clone path uses underscores (ComfyUI_aichemy_nodes) and that repo is dead - the hyphen one above is live.

Then put a segmentation model in place:

mkdir -p ComfyUI/models/yolov8
# e.g. download yolov8m-seg.pt into that folder

One real trap before you do: the pack's requirements.txt pins torch==2.1.2, torchvision==0.16.2, numpy==1.26.3 and ultralytics==8.1.1. Do not blindly pip install -r requirements.txt over a working ComfyUI - that pin can downgrade your torch and break everything else. Install just ultralytics (and opencv-python if missing) into your existing environment and let it ride your current torch.

Common issues

  • torchvision::nms CUDA backend error - the classic YOLO-in-ComfyUI wall, from a torch/torchvision mismatch (often a non-CUDA torchvision in the venv). Reinstall both from the same PyTorch wheel index.
  • Empty mask - class_ind doesn't exist in your model, or that class simply wasn't detected. Check the annotation output.
  • Only the first image of a batch is processed - don't plan around batch feeding.

One last aside: ultralytics YOLO weights are AGPL. Fine for personal tinkering, but worth knowing before you build a product around it. For a one-trick pack, this is the trick that saves real hair-pulling if YOLO compositing is your workflow. And yes, the display name is spelled "Segmentaion" - nobody's fixing that.

Categoryaichemy

Inputs (5)

NameTypeDefaultDescription
imageIMAGE
model_nameCOMBO0 options:
class_indINT0
combineINT10–1
instance_indINT0

Outputs (2)

NameTypeDescription
segmentationMASK
annotationIMAGE