Last active
August 21, 2024 01:37
-
-
Save james-s-tayler/8a9ec782a6f917ae220af3b2b50366a2 to your computer and use it in GitHub Desktop.
Flux Tonemap - no blurry background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import importlib | |
import os | |
node_list = [ #Add list of .py files containing nodes here | |
"sampler_tonemap" | |
] | |
NODE_CLASS_MAPPINGS = {} | |
NODE_DISPLAY_NAME_MAPPINGS = {} | |
for module_name in node_list: | |
imported_module = importlib.import_module(".{}".format(module_name), __name__) | |
NODE_CLASS_MAPPINGS = {**NODE_CLASS_MAPPINGS, **imported_module.NODE_CLASS_MAPPINGS} | |
if hasattr(imported_module, "NODE_DISPLAY_NAME_MAPPINGS"): | |
NODE_DISPLAY_NAME_MAPPINGS = {**NODE_DISPLAY_NAME_MAPPINGS, **imported_module.NODE_DISPLAY_NAME_MAPPINGS} | |
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from math import isclose | |
class ModelSamplerTonemapNoiseTest: | |
@classmethod | |
def INPUT_TYPES(s): | |
return {"required": { | |
"model": ("MODEL",), | |
"mode": (["Fixed Multiplier", "CFG-Multiplier Mapping"],), | |
"fixed_multiplier": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0, "step": 0.01}), | |
}, | |
"optional": { | |
"cfg_multiplier_map": ("STRING", { | |
"multiline": True, | |
"default": "12:0.11, 11:0.12, 10:0.14, 9:0.15, 8:0.17, 7:0.19, 6:0.23, 5:0.24, 4:0.29, 3:0.42, 2:0.73" | |
}), | |
}} | |
RETURN_TYPES = ("MODEL",) | |
FUNCTION = "patch" | |
CATEGORY = "custom_node_experiments" | |
def patch(self, model, mode, fixed_multiplier, cfg_multiplier_map=""): | |
cfg_multiplier_dict = {} | |
if mode == "CFG-Multiplier Mapping": | |
for pair in cfg_multiplier_map.split(','): | |
cfg, multiplier = map(float, pair.strip().split(':')) | |
cfg_multiplier_dict[cfg] = multiplier | |
def get_multiplier(cond_scale): | |
if mode == "Fixed Multiplier": | |
return fixed_multiplier | |
else: | |
for cfg, multiplier in cfg_multiplier_dict.items(): | |
if isclose(cfg, cond_scale, rel_tol=1e-9, abs_tol=1e-9): | |
return multiplier | |
return fixed_multiplier # Use fixed_multiplier as default | |
def sampler_tonemap_reinhard(args): | |
cond = args["cond"] | |
uncond = args["uncond"] | |
cond_scale = args["cond_scale"] | |
if cond_scale == 1.0: | |
return cond | |
multiplier = get_multiplier(cond_scale) | |
noise_pred = (cond - uncond) | |
noise_pred_vector_magnitude = (torch.linalg.vector_norm(noise_pred, dim=(1)) + 0.0000000001)[:,None] | |
noise_pred /= noise_pred_vector_magnitude | |
mean = torch.mean(noise_pred_vector_magnitude, dim=(1,2,3), keepdim=True) | |
std = torch.std(noise_pred_vector_magnitude, dim=(1,2,3), keepdim=True) | |
top = (std * 3 + mean) * multiplier | |
#reinhard | |
noise_pred_vector_magnitude *= (1.0 / top) | |
new_magnitude = noise_pred_vector_magnitude / (noise_pred_vector_magnitude + 1.0) | |
new_magnitude *= top | |
return uncond + noise_pred * new_magnitude * cond_scale | |
m = model.clone() | |
m.set_model_sampler_cfg_function(sampler_tonemap_reinhard) | |
return (m, ) | |
NODE_CLASS_MAPPINGS = { | |
"ModelSamplerTonemapNoiseTest": ModelSamplerTonemapNoiseTest, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Nodes: | |
- ClipTextEncodeFlux (name it GuidancePositive) | |
- input clip from clip loader | |
- input string to t5xxl and clip_l from positive prompt | |
- output conditioning to AdaptiveGuider.positive | |
- guidance 3.5 | |
- ClipTextEncodeFlux (name it GuidanceNegative) | |
- input clip from clip loader | |
- input string to t5xxl and clip_l from negative prompt | |
- output conditioning to AdaptiveGuider.negative | |
- guidance 7 | |
- AdaptiveGuidance | |
- input model from ModelSamplerTonemapNoiseTest | |
- output guider to SamplerCustomAdvanced.guider | |
- threshold 0.990 | |
- CFG 6.0 | |
- ModelSamplerTonemapNoiseTest | |
- input model from GGUF loader | |
- mode CFG-Multiplier Mapping | |
- fixed_multiplier 1.00 | |
12:0.11, 11:0.12, 10:0.14, 9:0.15, 8:0.17, 7:0.19, 6:0.23, 5:0.24, 4:0.29, 3:0.42, 2:0.73 | |
Notes: | |
Recommended values Tonemap multiplier: | |
CFG = 12 -> multiplier = 0.11 | |
CFG = 11 -> multiplier = 0.12 | |
CFG = 10 -> multiplier = 0.14 | |
CFG = 9 -> multiplier = 0.15 | |
CFG = 8 -> multiplier = 0.17 | |
CFG = 7 -> multiplier = 0.19 | |
CFG = 6 -> multiplier = 0.23 | |
(CFG < 6 doesn't seem to remove the blur) | |
CFG = 5 -> multiplier = 0.24 | |
CFG = 4 -> multiplier = 0.29 | |
CFG = 3 -> multiplier = 0.42 | |
CFG = 2 -> multiplier = 0.73 | |
CFG = 1 -> :^) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment