From 1fae41ddcf918168cfc20862ce4755eeeabf925a Mon Sep 17 00:00:00 2001 From: Sean Sube Date: Sat, 11 Nov 2023 20:17:52 -0600 Subject: [PATCH] feat(api): add edge options to region feathering --- api/onnx_web/chain/tile.py | 3 ++- api/onnx_web/diffusers/pipelines/panorama.py | 5 +++-- .../diffusers/pipelines/panorama_xl.py | 5 +++-- api/onnx_web/diffusers/utils.py | 18 ++++++++++++++---- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/api/onnx_web/chain/tile.py b/api/onnx_web/chain/tile.py index e3505984..5850b23d 100644 --- a/api/onnx_web/chain/tile.py +++ b/api/onnx_web/chain/tile.py @@ -95,6 +95,7 @@ def make_tile_mask( shape: Any, tile: Tuple[int, int], overlap: float, + edges: Tuple[bool, bool, bool, bool], ) -> np.ndarray: mask = np.ones(shape) @@ -113,7 +114,7 @@ def make_tile_mask( points_w = [0, min(p1_w, p2_w), max(p1_w, p2_w), tile_w] # build gradients - grad_x, grad_y = [0, 1, 1, 0], [0, 1, 1, 0] + grad_x, grad_y = [int(edges[0]), 1, 1, int(edges[2])], [int(edges[1]), 1, 1, int(edges[3])] logger.debug("tile gradients: %s, %s, %s, %s", points_w, points_h, grad_x, grad_y) mult_x = [np.interp(i, points_w, grad_x) for i in range(tile_w)] diff --git a/api/onnx_web/diffusers/pipelines/panorama.py b/api/onnx_web/diffusers/pipelines/panorama.py index 14d1143a..d90a5def 100644 --- a/api/onnx_web/diffusers/pipelines/panorama.py +++ b/api/onnx_web/diffusers/pipelines/panorama.py @@ -671,11 +671,12 @@ class OnnxStableDiffusionPanoramaPipeline(DiffusionPipeline): ) latents_region_denoised = scheduler_output.prev_sample.numpy() - if feather > 0.0: + if feather[0] > 0.0: mask = make_tile_mask( (h_end - h_start, w_end - w_start), (h_end - h_start, w_end - w_start), - feather, + feather[0], + feather[1], ) mask = np.expand_dims(mask, axis=0) mask = np.repeat(mask, 4, axis=0) diff --git a/api/onnx_web/diffusers/pipelines/panorama_xl.py b/api/onnx_web/diffusers/pipelines/panorama_xl.py index e64abe3f..24147160 100644 --- a/api/onnx_web/diffusers/pipelines/panorama_xl.py +++ b/api/onnx_web/diffusers/pipelines/panorama_xl.py @@ -520,11 +520,12 @@ class StableDiffusionXLPanoramaPipelineMixin(StableDiffusionXLImg2ImgPipelineMix ) latents_region_denoised = scheduler_output.prev_sample.numpy() - if feather > 0.0: + if feather[0] > 0.0: mask = make_tile_mask( (h_end - h_start, w_end - w_start), (h_end - h_start, w_end - w_start), - feather, + feather[0], + feather[1], ) mask = np.expand_dims(mask, axis=0) mask = np.repeat(mask, 4, axis=0) diff --git a/api/onnx_web/diffusers/utils.py b/api/onnx_web/diffusers/utils.py index 6655e2bc..4a0bbd31 100644 --- a/api/onnx_web/diffusers/utils.py +++ b/api/onnx_web/diffusers/utils.py @@ -22,7 +22,7 @@ INVERSION_TOKEN = compile(r"\]+):(-?[\.|\d]+)\>") LORA_TOKEN = compile(r"\]+):(-?[\.|\d]+)\>") WILDCARD_TOKEN = compile(r"__([-/\\\w]+)__") REGION_TOKEN = compile( - r"\]+)\>" + r"\]+)\>" ) RESEED_TOKEN = compile(r"\") @@ -455,18 +455,28 @@ def slice_prompt(prompt: str, slice: int) -> str: return prompt -Region = Tuple[int, int, int, int, float, str] +Region = Tuple[int, int, int, int, float, Tuple[float, Tuple[bool, bool, bool, bool]], str] -def parse_region_group(group) -> Region: +def parse_region_group(group: Tuple[str, ...]) -> Region: top, left, bottom, right, weight, feather, prompt = group + + # break down the feather section + feather_radius, *feather_edges = feather.split("_") + feather_edges = "".join(feather_edges) + return ( int(top), int(left), int(bottom), int(right), float(weight), - float(feather), + (float(feather_radius), ( + "T" in feather_edges, + "L" in feather_edges, + "B" in feather_edges, + "R" in feather_edges, + )), prompt, )