fix: 修复 RGBA 输入导致的通道数报错

抠图类节点(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),该接法无需调整工作流。
This commit is contained in:
rui40000
2026-07-16 11:26:09 +08:00
parent e1a575fcda
commit c6acb9f7d4
+11 -1
View File
@@ -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: