Skip to content

Commit

Permalink
Node to make stable cascade image to image easier.
Browse files Browse the repository at this point in the history
  • Loading branch information
comfyanonymous committed Feb 19, 2024
1 parent 88f3004 commit a311524
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions comfy_extras/nodes_stable_cascade.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import torch
import nodes
import comfy.utils


class StableCascade_EmptyLatentImage:
Expand Down Expand Up @@ -47,6 +48,39 @@ def generate(self, width, height, compression, batch_size=1):
"samples": b_latent,
})

class StableCascade_StageC_VAEEncode:
def __init__(self, device="cpu"):
self.device = device

@classmethod
def INPUT_TYPES(s):
return {"required": {
"image": ("IMAGE",),
"vae": ("VAE", ),
"compression": ("INT", {"default": 42, "min": 4, "max": 128, "step": 1}),
}}
RETURN_TYPES = ("LATENT", "LATENT")
RETURN_NAMES = ("stage_c", "stage_b")
FUNCTION = "generate"

CATEGORY = "_for_testing/stable_cascade"

def generate(self, image, vae, compression):
width = image.shape[-2]
height = image.shape[-3]
out_width = (width // compression) * vae.downscale_ratio
out_height = (height // compression) * vae.downscale_ratio

s = comfy.utils.common_upscale(image.movedim(-1,1), out_width, out_height, "bicubic", "center").movedim(1,-1)

c_latent = vae.encode(s[:,:,:,:3])
b_latent = torch.zeros([c_latent.shape[0], 4, height // 4, width // 4])
return ({
"samples": c_latent,
}, {
"samples": b_latent,
})

class StableCascade_StageB_Conditioning:
@classmethod
def INPUT_TYPES(s):
Expand All @@ -71,4 +105,5 @@ def set_prior(self, conditioning, stage_c):
NODE_CLASS_MAPPINGS = {
"StableCascade_EmptyLatentImage": StableCascade_EmptyLatentImage,
"StableCascade_StageB_Conditioning": StableCascade_StageB_Conditioning,
"StableCascade_StageC_VAEEncode": StableCascade_StageC_VAEEncode,
}

0 comments on commit a311524

Please sign in to comment.