Spaces:
Runtime error
Runtime error
File size: 5,010 Bytes
c19ca42 |
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 |
# From https://github.com/cubiq/ComfyUI_IPAdapter_plus
from ..enums import StableDiffusionVersion, TransformerIDResult
def calc_weights(
weight_type: str,
weight: float,
sd_version: StableDiffusionVersion,
weight_composition: float = 0.0,
):
layers = sd_version.transformer_block_num
return [
_calc_weight(
weight_type,
weight,
sd_version,
t_idx,
weight_composition,
)
for t_idx in range(layers)
]
def _calc_weight(
weight_type: str,
weight: float,
sd_version: StableDiffusionVersion,
t_idx: int,
weight_composition: float = 0.0,
):
is_sdxl = sd_version == StableDiffusionVersion.SDXL
layers = sd_version.transformer_block_num
ids: TransformerIDResult = sd_version.transformer_ids
block_type = [i for i in ids.to_list() if i.transformer_index == t_idx][
0
].block_type.value
if weight_type == "normal":
return weight
elif weight_type == "ease in":
weight = weight * (0.05 + 0.95 * (1 - t_idx / layers))
elif weight_type == "ease out":
weight = weight * (0.05 + 0.95 * (t_idx / layers))
elif weight_type == "ease in-out":
weight = weight * (0.05 + 0.95 * (1 - abs(t_idx - (layers / 2)) / (layers / 2)))
elif weight_type == "reverse in-out":
weight = weight * (0.05 + 0.95 * (abs(t_idx - (layers / 2)) / (layers / 2)))
elif weight_type == "weak input":
weight = weight * 0.2 if block_type == "input" else weight
elif weight_type == "weak middle":
weight = weight * 0.2 if block_type == "middle" else weight
elif weight_type == "weak output":
weight = weight * 0.2 if block_type == "output" else weight
elif weight_type == "strong middle":
weight = (
weight * 0.2
if (block_type == "input" or block_type == "output")
else weight
)
elif weight_type.startswith("style transfer"):
weight = (
{6: weight}
if is_sdxl
else {
0: weight,
1: weight,
2: weight,
3: weight,
9: weight,
10: weight,
11: weight,
12: weight,
13: weight,
14: weight,
15: weight,
}
)
elif weight_type.startswith("composition"):
weight = {3: weight} if is_sdxl else {4: weight * 0.25, 5: weight}
elif weight_type == "strong style transfer":
if is_sdxl:
weight = {
0: weight,
1: weight,
2: weight,
4: weight,
5: weight,
6: weight,
7: weight,
8: weight,
9: weight,
10: weight,
}
else:
weight = {
0: weight,
1: weight,
2: weight,
3: weight,
6: weight,
7: weight,
8: weight,
9: weight,
10: weight,
11: weight,
12: weight,
13: weight,
14: weight,
15: weight,
}
elif weight_type == "style and composition":
if is_sdxl:
weight = {3: weight_composition, 6: weight}
else:
weight = {
0: weight,
1: weight,
2: weight,
3: weight,
4: weight_composition * 0.25,
5: weight_composition,
9: weight,
10: weight,
11: weight,
12: weight,
13: weight,
14: weight,
15: weight,
}
elif weight_type == "strong style and composition":
if is_sdxl:
weight = {
0: weight,
1: weight,
2: weight,
3: weight_composition,
4: weight,
5: weight,
6: weight,
7: weight,
8: weight,
9: weight,
10: weight,
}
else:
weight = {
0: weight,
1: weight,
2: weight,
3: weight,
4: weight_composition,
5: weight_composition,
6: weight,
7: weight,
8: weight,
9: weight,
10: weight,
11: weight,
12: weight,
13: weight,
14: weight,
15: weight,
}
else:
raise Exception("Unrecognized weight type!")
if isinstance(weight, dict):
return weight.get(t_idx, 0.0)
else:
return weight
|