From c6acb9f7d4ad9c89f5e7f312819bf9ca88f503a8 Mon Sep 17 00:00:00 2001 From: rui40000 Date: Thu, 16 Jul 2026 11:26:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20RGBA=20=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E5=AF=BC=E8=87=B4=E7=9A=84=E9=80=9A=E9=81=93=E6=95=B0?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 抠图类节点(BiRefNet Ultra V2、RMBG 等)的 image 输出为 4 通道 RGBA, 此前节点直接把它喂给 VAE 编码器,触发: Given groups=1, weight of size [128, 3, 3, 3], expected input[1, 4, 1024, 1024] to have 3 channels, but got 4 channels instead 现按 ComfyUI 惯例收敛输入通道:RGBA 丢弃 alpha、灰度扩成三通道、 其余通道数给出可读报错而非让 VAE 抛形状错误。 BiRefNetUltraV2 的 image 输出由 RGB2RGBA(原图, mask) 合成,RGB 三通道 未被 premultiply,因此丢弃 alpha 后即还原原图 —— 实测 RGBA 与 RGB 输入 的输出逐像素完全一致(最大差异 0.00000000),该接法无需调整工作流。 --- sdmatte_node.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sdmatte_node.py b/sdmatte_node.py index e0d323b..2d3626d 100644 --- a/sdmatte_node.py +++ b/sdmatte_node.py @@ -258,7 +258,17 @@ class RuiSDMatte: dtype = next(model.unet.parameters()).dtype size = int(inference_size) - B, H, W, _ = image.shape + B, H, W, C = image.shape + + # ComfyUI 的 IMAGE 约定是 RGB,但抠图类节点(BiRefNet / RMBG 等)常输出 RGBA。 + # SDMatte 的 VAE 编码器只收 3 通道,多出的 alpha 必须丢掉而不能当颜色喂进去。 + if C == 4: + image = image[..., :3] + elif C == 1: + image = image.repeat(1, 1, 1, 3) + elif C != 3: + raise ValueError(f"image 需要 1 / 3 / 4 通道,实际收到 {C} 通道") + image = image.contiguous() # 掩码可能与图像批次数不一致,按 ComfyUI 惯例广播 if mask.dim() == 2: