File size: 4,707 Bytes
7bed60d |
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 |
from modules.prompt_parser import SdConditioning
from base64 import b64decode as decode
from io import BytesIO as bIO
from PIL import Image
import numpy as np
import torch
from scripts.ui_funcs import parse_mapping
def empty_tensor(H: int, W: int):
return torch.zeros((H, W)).unsqueeze(0)
def advanced_mapping(sd_model, couples: list, WIDTH: int, HEIGHT: int, mapping: list):
data = parse_mapping(mapping)
assert len(couples) == len(data)
ARGs: dict = {}
IS_SDXL: bool = hasattr(
sd_model.forge_objects.unet.model.diffusion_model, "label_emb"
)
for tile_index in range(len(data)):
mask = torch.zeros((HEIGHT, WIDTH))
(X, Y, W) = data[tile_index]
x_from = int(WIDTH * X[0])
x_to = int(WIDTH * X[1])
y_from = int(HEIGHT * Y[0])
y_to = int(HEIGHT * Y[1])
weight = W
# ===== Cond =====
texts = SdConditioning([couples[tile_index]], False, WIDTH, HEIGHT, None)
cond = sd_model.get_learned_conditioning(texts)
pos_cond = [[cond["crossattn"]]] if IS_SDXL else [[cond]]
# ===== Cond =====
# ===== Mask =====
mask[y_from:y_to, x_from:x_to] = weight
# ===== Mask =====
ARGs[f"cond_{tile_index + 1}"] = pos_cond
ARGs[f"mask_{tile_index + 1}"] = mask.unsqueeze(0)
return ARGs
def basic_mapping(
sd_model,
couples: list,
WIDTH: int,
HEIGHT: int,
LINE_COUNT: int,
IS_HORIZONTAL: bool,
background: str,
TILE_SIZE: int,
TILE_WEIGHT: float,
BG_WEIGHT: float,
):
ARGs: dict = {}
IS_SDXL: bool = hasattr(
sd_model.forge_objects.unet.model.diffusion_model, "label_emb"
)
for tile in range(LINE_COUNT):
mask = torch.zeros((HEIGHT, WIDTH))
# ===== Cond =====
texts = SdConditioning([couples[tile]], False, WIDTH, HEIGHT, None)
cond = sd_model.get_learned_conditioning(texts)
pos_cond = [[cond["crossattn"]]] if IS_SDXL else [[cond]]
# ===== Cond =====
# ===== Mask =====
if background == "First Line":
if tile == 0:
mask = torch.ones((HEIGHT, WIDTH)) * BG_WEIGHT
else:
if IS_HORIZONTAL:
mask[:, (tile - 1) * TILE_SIZE : tile * TILE_SIZE] = TILE_WEIGHT
else:
mask[(tile - 1) * TILE_SIZE : tile * TILE_SIZE, :] = TILE_WEIGHT
else:
if IS_HORIZONTAL:
mask[:, tile * TILE_SIZE : (tile + 1) * TILE_SIZE] = TILE_WEIGHT
else:
mask[tile * TILE_SIZE : (tile + 1) * TILE_SIZE, :] = TILE_WEIGHT
# ===== Mask =====
ARGs[f"cond_{tile + 1}"] = pos_cond
ARGs[f"mask_{tile + 1}"] = mask.unsqueeze(0)
if background == "Last Line":
ARGs[f"mask_{LINE_COUNT}"] = (
torch.ones((HEIGHT, WIDTH)) * BG_WEIGHT
).unsqueeze(0)
return ARGs
def b64image2tensor(img: str, WIDTH: int, HEIGHT: int) -> torch.Tensor:
image_bytes = decode(img)
image = Image.open(bIO(image_bytes)).convert("L")
if image.width != WIDTH or image.height != HEIGHT:
image = image.resize((WIDTH, HEIGHT), resample=Image.Resampling.NEAREST)
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image).unsqueeze(0)
return image
def mask_mapping(
sd_model,
couples: list,
WIDTH: int,
HEIGHT: int,
LINE_COUNT: int,
mapping: list[dict],
background: str,
BG_WEIGHT: float,
):
mapping = [
b64image2tensor(m["mask"], WIDTH, HEIGHT) * float(m["weight"]) for m in mapping
]
ARGs: dict = {}
IS_SDXL: bool = hasattr(
sd_model.forge_objects.unet.model.diffusion_model, "label_emb"
)
for layer in range(LINE_COUNT):
mask = torch.zeros((HEIGHT, WIDTH))
# ===== Cond =====
texts = SdConditioning([couples[layer]], False, WIDTH, HEIGHT, None)
cond = sd_model.get_learned_conditioning(texts)
pos_cond = [[cond["crossattn"]]] if IS_SDXL else [[cond]]
# ===== Cond =====
# ===== Mask =====
if background == "First Line":
if layer == 0:
mask = torch.ones((HEIGHT, WIDTH)) * BG_WEIGHT
else:
mask = mapping[layer - 1]
else:
mask = mapping[layer]
# ===== Mask =====
ARGs[f"cond_{layer + 1}"] = pos_cond
ARGs[f"mask_{layer + 1}"] = mask.unsqueeze(0) if mask.dim() == 2 else mask
if background == "Last Line":
ARGs[f"mask_{LINE_COUNT}"] = (
torch.ones((HEIGHT, WIDTH)) * BG_WEIGHT
).unsqueeze(0)
return ARGs
|