CDTuner for ComfyUI
Installation
Copy the code in to a .py
file in your ComfyUI/custom_nodes
directory, then restart ComfyUI and refresh the ComfyUI page in your browser.
Details
- SaturationTuner modifies the VAE to increase or decrease latent decoding/encoding color saturation. Higher values = more saturation.
- DetailTuner modifies the model to increase or decrease details and contrast in gens. Play around with the values to get a feel for their effect.
- ColorTuner modifies the intermediate latent noise predictions during sampling to adjust their color and contrast.
- LatentColorTuner modifies latent images to adjust their color and contrast.
- Masked versions of the Color and LatentColor tuner nodes allow you to mask off areas to be affected.
Changelog
- 2024-01-11 #1 - Implemented the detail tuning portion of CDTuner (the part that was already implemented by the existing CDTuner node). Reversed the directions of the ColorTuner values so that they make more sense. Added early exit to ColorTuner so that it only gets applied if there is a non-zero value.
- 2024-01-11 #2 - Implemented masked Color and LatentColor tuner nodes. Moved a few calculations around to more sensible positions.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | import torch
from comfy.sd import VAE
class SaturationTuner:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"vae": ("VAE",),
"saturation": ("FLOAT", {"default": 0.0, "min": -20.0, "max": 20.0, "step": 0.1}),
}}
RETURN_TYPES = ("VAE",)
FUNCTION = "patch"
CATEGORY = "custom_node_experiments"
VAEKEYS = [
"decoder.up.1.upsample.conv.weight",
"decoder.up.0.block.0.nin_shortcut.weight",
]
def patch(self, vae, saturation):
sd = vae.get_sd()
for key in self.VAEKEYS:
sd[key] = sd[key].clone() * (1 + saturation * 0.075)
new_vae = VAE(sd=sd)
return (new_vae, )
class DetailTuner:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"model": ("MODEL",),
"detail_1": ("FLOAT", {"default": 0.0, "min": -10.0, "max": 10.0, "step": 0.1}),
"detail_2": ("FLOAT", {"default": 0.0, "min": -10.0, "max": 10.0, "step": 0.1}),
"contrast_1": ("FLOAT", {"default": 0.0, "min": -10.0, "max": 10.0, "step": 0.1}),
}}
RETURN_TYPES = ("MODEL", )
FUNCTION = "patch"
CATEGORY = "custom_node_experiments"
TO_SCALE = [
"diffusion_model.input_blocks.0.0.weight",
"diffusion_model.input_blocks.0.0.bias",
"diffusion_model.out.0.weight",
"diffusion_model.out.0.bias",
]
SCALE_COEF = [
-0.01,
0.02,
-0.01,
0.02,
]
TO_OFFSET = [
"diffusion_model.out.2.bias",
]
OFFSET_COEF = [
[0.02, 0, 0, 0]
]
def patch(self, model, detail_1, detail_2, contrast_1):
if not any((detail_1, detail_2, contrast_1)):
return (model, )
scale_strengths = [detail_1, detail_1, detail_2, detail_2]
offset_strengths = [contrast_1]
m = model.clone()
m.patch_model()
sd = m.model.state_dict()
patches = {}
for i, key in enumerate(self.TO_SCALE):
if scale_strengths[i] != 0:
patches[key] = (sd[key] * self.SCALE_COEF[i] * scale_strengths[i], )
for i, key in enumerate(self.TO_OFFSET):
if offset_strengths[i] != 0:
patches[key] = (torch.tensor(self.OFFSET_COEF[i]) * offset_strengths[i], )
m.unpatch_model()
m.add_patches(patches)
return (m, )
class ColorTuner:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"model": ("MODEL",),
"contrast_2": ("FLOAT", {"default": 0.0, "min": -20.0, "max": 20.0, "step": 0.1}),
"brightness": ("FLOAT", {"default": 0.0, "min": -20.0, "max": 20.0, "step": 0.1}),
"cyan_red": ("FLOAT", {"default": 0.0, "min": -20.0, "max": 20.0, "step": 0.1}),
"magenta_green": ("FLOAT", {"default": 0.0, "min": -20.0, "max": 20.0, "step": 0.1}),
"yellow_blue": ("FLOAT", {"default": 0.0, "min": -20.0, "max": 20.0, "step": 0.1}),
}}
RETURN_TYPES = ("MODEL", )
FUNCTION = "patch"
CATEGORY = "custom_node_experiments"
COLORS = [[-1,1/3,2/3],[1,1,0],[0,-1,-1],[1,0,1]]
@staticmethod
def default_cfg(args):
return args["uncond"] + (args["cond"] - args["uncond"]) * args["cond_scale"]
def patch(self, model, contrast_2, brightness, cyan_red, magenta_green, yellow_blue):
# color shifting values
ddratios = [contrast_2, brightness, cyan_red, magenta_green, yellow_blue]
# bypass
if not any(ddratios):
return (model, )
ratios = [x * 20/3 for x in [ddratios[0] * 0.02] + ColorTuner.COLOR_CALC(ddratios[1:])]
m = model.clone()
# we need to have some CFG function to wrap, so we set a default if there isn't one
cfg_func = None
if "sampler_cfg_function" in m.model_options:
cfg_func = m.model_options["sampler_cfg_function"]
else:
cfg_func = ColorTuner.default_cfg
def wrapper(args):
for i, x in enumerate(ratios):
args["cond"][:,i,:,:] = args["cond"][:,i,:,:] + x
args["uncond"][:,i,:,:] = args["uncond"][:,i,:,:] + x
return cfg_func(args)
# set the wrapper
m.set_model_sampler_cfg_function(wrapper)
return (m, )
@classmethod
def COLOR_CALC(cls, colors):
outs = [[y * colors[i] * 0.02 for y in x] for i, x in enumerate(cls.COLORS)]
return [sum(x) for x in zip(*outs)]
class ColorTunerMasked:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"model": ("MODEL",),
"contrast_2": ("FLOAT", {"default": 0.0, "min": -20.0, "max": 20.0, "step": 0.1}),
"brightness": ("FLOAT", {"default": 0.0, "min": -20.0, "max": 20.0, "step": 0.1}),
"cyan_red": ("FLOAT", {"default": 0.0, "min": -20.0, "max": 20.0, "step": 0.1}),
"magenta_green": ("FLOAT", {"default": 0.0, "min": -20.0, "max": 20.0, "step": 0.1}),
"yellow_blue": ("FLOAT", {"default": 0.0, "min": -20.0, "max": 20.0, "step": 0.1}),
"mask": ("MASK", ),
}}
RETURN_TYPES = ("MODEL", )
FUNCTION = "patch"
CATEGORY = "custom_node_experiments"
COLORS = [[-1,1/3,2/3],[1,1,0],[0,-1,-1],[1,0,1]]
@staticmethod
def default_cfg(args):
return args["uncond"] + (args["cond"] - args["uncond"]) * args["cond_scale"]
def patch(self, model, contrast_2, brightness, cyan_red, magenta_green, yellow_blue, mask):
# color shifting values
ddratios = [contrast_2, brightness, cyan_red, magenta_green, yellow_blue]
# bypass
if not any(ddratios):
return (model, )
ratios = [x * 20/3 for x in [ddratios[0] * 0.02] + ColorTuner.COLOR_CALC(ddratios[1:])]
m = model.clone()
# we need to have some CFG function to wrap, so we set a default if there isn't one
cfg_func = None
if "sampler_cfg_function" in m.model_options:
cfg_func = m.model_options["sampler_cfg_function"]
else:
cfg_func = ColorTuner.default_cfg
mask = mask.reshape((mask.shape[0], 1, mask.shape[1], mask.shape[2]))
def wrapper(args):
# masking has to be done in the wrapper as we need the cond/uncond shape
l_mask = torch.nn.functional.interpolate(mask, size=args["cond"].shape[2:], mode="bilinear")
l_mask = l_mask.to(device=args["cond"].device)
for i, x in enumerate((x * l_mask for x in ratios)):
args["cond"][:,i,:,:] = args["cond"][:,i,:,:] + x
args["uncond"][:,i,:,:] = args["uncond"][:,i,:,:] + x
return cfg_func(args)
# set the wrapper
m.set_model_sampler_cfg_function(wrapper)
return (m, )
class LatentColorTuner:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"latent": ("LATENT",),
"contrast": ("FLOAT", {"default": 0.0, "min": -1000.0, "max": 1000.0, "step": 0.1}),
"brightness": ("FLOAT", {"default": 0.0, "min": -1000.0, "max": 1000.0, "step": 0.1}),
"cyan_red": ("FLOAT", {"default": 0.0, "min": -1000.0, "max": 1000.0, "step": 0.1}),
"magenta_green": ("FLOAT", {"default": 0.0, "min": -1000.0, "max": 1000.0, "step": 0.1}),
"yellow_blue": ("FLOAT", {"default": 0.0, "min": -1000.0, "max": 1000.0, "step": 0.1}),
}}
RETURN_TYPES = ("LATENT", )
FUNCTION = "patch"
CATEGORY = "custom_node_experiments"
def patch(self, latent, contrast, brightness, cyan_red, magenta_green, yellow_blue):
# color shifting values
ddratios = [contrast, brightness, cyan_red, magenta_green, yellow_blue]
if not any(ddratios):
return (latent, )
ratios = [x * 20/3 for x in [ddratios[0] * 0.02] + ColorTuner.COLOR_CALC(ddratios[1:])]
l = latent.copy()
l["samples"] = l["samples"].clone()
for i, x in enumerate(ratios):
l["samples"][:,i,:,:] = l["samples"][:,i,:,:] - x
return (l, )
class LatentColorTunerMasked:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"latent": ("LATENT",),
"contrast": ("FLOAT", {"default": 0.0, "min": -1000.0, "max": 1000.0, "step": 0.1}),
"brightness": ("FLOAT", {"default": 0.0, "min": -1000.0, "max": 1000.0, "step": 0.1}),
"cyan_red": ("FLOAT", {"default": 0.0, "min": -1000.0, "max": 1000.0, "step": 0.1}),
"magenta_green": ("FLOAT", {"default": 0.0, "min": -1000.0, "max": 1000.0, "step": 0.1}),
"yellow_blue": ("FLOAT", {"default": 0.0, "min": -1000.0, "max": 1000.0, "step": 0.1}),
"mask": ("MASK", ),
},
}
RETURN_TYPES = ("LATENT", )
FUNCTION = "patch"
CATEGORY = "custom_node_experiments"
def patch(self, latent, contrast, brightness, cyan_red, magenta_green, yellow_blue, mask):
# color shifting values
ddratios = [contrast, brightness, cyan_red, magenta_green, yellow_blue]
if not any(ddratios):
return (latent, )
l = latent.copy()
l["samples"] = l["samples"].clone()
mask = mask.reshape((mask.shape[0], 1, mask.shape[1], mask.shape[2]))
mask = torch.nn.functional.interpolate(mask, size=l["samples"].shape[2:], mode="bilinear")
mask = mask.to(device=l["samples"].device)
ratios = [x * 20/3 * mask for x in [ddratios[0] * 0.02] + ColorTuner.COLOR_CALC(ddratios[1:])]
for i, x in enumerate(ratios):
l["samples"][:,i,:,:] = l["samples"][:,i,:,:] - x
return (l, )
NODE_CLASS_MAPPINGS = {
"SaturationTuner": SaturationTuner,
"ColorTuner": ColorTuner,
"ColorTunerMasked": ColorTunerMasked,
"DetailTuner": DetailTuner,
"LatentColorTuner": LatentColorTuner,
"LatentColorTunerMasked": LatentColorTunerMasked,
}
|