Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Fix ImageUpscaleWithModel crashing on RGBA images (#16500)

Jialong(Bruce) Li557597f5b25f
diff --git a/comfy_extras/nodes_upscale_model.py b/comfy_extras/nodes_upscale_model.py
index d215084e3..c1ac8071a 100644
--- a/comfy_extras/nodes_upscale_model.py
+++ b/comfy_extras/nodes_upscale_model.py
@@ -71,6 +71,11 @@ class ImageUpscaleWithModel(io.ComfyNode):
def execute(cls, upscale_model, image) -> io.NodeOutput:
device = upscale_model.patcher.load_device
+ alpha = None
+ if image.shape[-1] == 4:
+ alpha = image[..., 3:4]
+ image = image[..., :3]
+
memory_required = (512 * 512 * 3) * image.element_size() * max(upscale_model.scale, 1.0) * 384.0 #The 384.0 is an estimate of how much some of these models take, TODO: make it more accurate
memory_required += image.nelement() * image.element_size()
model_management.load_models_gpu([upscale_model.patcher], memory_required=memory_required, force_full_load=True)
@@ -96,6 +101,11 @@ class ImageUpscaleWithModel(io.ComfyNode):
raise e
s = torch.clamp(s.movedim(-3,-1), min=0, max=1.0).to(comfy.model_management.intermediate_dtype())
+
+ if alpha is not None:
+ alpha = comfy.utils.common_upscale(alpha.movedim(-1, -3).to(s), s.shape[2], s.shape[1], "bilinear", "disabled")
+ s = torch.cat((s, alpha.movedim(-3, -1)), dim=-1)
+
return io.NodeOutput(s)
upscale = execute # TODO: remove
diff --git a/tests-unit/comfy_extras_test/upscale_model_alpha_test.py b/tests-unit/comfy_extras_test/upscale_model_alpha_test.py
new file mode 100644
index 000000000..8a10a3208
--- /dev/null
+++ b/tests-unit/comfy_extras_test/upscale_model_alpha_test.py
@@ -0,0 +1,58 @@
+import torch
+
+from comfy.cli_args import args as cli_args
+
+if not torch.cuda.is_available():
+ cli_args.cpu = True
+
+import comfy.model_management # noqa: E402
+from comfy_extras.nodes_upscale_model import ImageUpscaleWithModel # noqa: E402
+
+
+class StubUpscaleModel:
+ """Stands in for a spandrel ImageModelDescriptor that only accepts RGB."""
+
+ scale = 2
+
+ class patcher:
+ load_device = torch.device("cpu")
+
+ def __init__(self):
+ self.seen_channels = None
+
+ def __call__(self, image):
+ self.seen_channels = image.shape[1]
+ assert image.shape[1] == 3, "model was handed a non-RGB tensor"
+ return torch.nn.functional.interpolate(image, scale_factor=self.scale, mode="nearest")
+
+
+def rgba_image():
+ """16x16 RGBA where the top half is transparent and the bottom half opaque."""
+ image = torch.zeros(1, 16, 16, 4)
+ image[..., :3] = 0.5
+ image[0, 8:, :, 3] = 1.0
+ return image
+
+
+def upscale(image, monkeypatch):
+ monkeypatch.setattr(comfy.model_management, "load_models_gpu", lambda *args, **kwargs: None)
+ model = StubUpscaleModel()
+ return model, ImageUpscaleWithModel.execute(model, image)
+
+
+def test_rgba_input_does_not_crash_and_alpha_is_preserved(monkeypatch):
+ model, out = upscale(rgba_image(), monkeypatch)
+ image = out.result[0]
+
+ assert model.seen_channels == 3
+ assert image.shape == (1, 32, 32, 4)
+ assert image[0, :14, :, 3].max() < 0.1
+ assert image[0, 18:, :, 3].min() > 0.9
+
+
+def test_rgb_input_is_untouched(monkeypatch):
+ model, out = upscale(torch.zeros(1, 16, 16, 3), monkeypatch)
+ image = out.result[0]
+
+ assert model.seen_channels == 3
+ assert image.shape == (1, 32, 32, 3)