From 7b8ced0f686f79cd13c05b6ec62f85c252391d3a Mon Sep 17 00:00:00 2001 From: Sean Sube Date: Sat, 18 Feb 2023 16:27:48 -0600 Subject: [PATCH] feat(api): prefer chain stage parameters over request parameters (#138) --- api/onnx_web/chain/blend_img2img.py | 20 ++++----- api/onnx_web/chain/blend_inpaint.py | 26 ++++++----- api/onnx_web/chain/blend_mask.py | 4 +- api/onnx_web/chain/correct_codeformer.py | 2 + api/onnx_web/chain/correct_gfpgan.py | 2 + api/onnx_web/chain/source_txt2img.py | 10 ++--- .../chain/upscale_stable_diffusion.py | 6 +-- api/onnx_web/diffusion/run.py | 2 +- api/onnx_web/params.py | 45 ++++++++++++++++++- 9 files changed, 82 insertions(+), 35 deletions(-) diff --git a/api/onnx_web/chain/blend_img2img.py b/api/onnx_web/chain/blend_img2img.py index 99591b31..72bad324 100644 --- a/api/onnx_web/chain/blend_img2img.py +++ b/api/onnx_web/chain/blend_img2img.py @@ -19,15 +19,13 @@ def blend_img2img( server: ServerContext, _stage: StageParams, params: ImageParams, - source_image: Image.Image, + source: Image.Image, *, - strength: float, - prompt: Optional[str] = None, callback: ProgressCallback = None, **kwargs, ) -> Image.Image: - prompt = prompt or params.prompt - logger.info("blending image using img2img, %s steps: %s", params.steps, prompt) + params = params.with_args(**kwargs) + logger.info("blending image using img2img, %s steps: %s", params.steps, params.prompt) pipe = load_pipeline( server, @@ -41,25 +39,25 @@ def blend_img2img( logger.debug("using LPW pipeline for img2img") rng = torch.manual_seed(params.seed) result = pipe.img2img( - prompt, + params.prompt, generator=rng, guidance_scale=params.cfg, - image=source_image, + image=source, negative_prompt=params.negative_prompt, num_inference_steps=params.steps, - strength=strength, + strength=params.strength, callback=callback, ) else: rng = np.random.RandomState(params.seed) result = pipe( - prompt, + params.prompt, generator=rng, guidance_scale=params.cfg, - image=source_image, + image=source, negative_prompt=params.negative_prompt, num_inference_steps=params.steps, - strength=strength, + strength=params.strength, callback=callback, ) diff --git a/api/onnx_web/chain/blend_inpaint.py b/api/onnx_web/chain/blend_inpaint.py index 7e517e91..fbe54d9c 100644 --- a/api/onnx_web/chain/blend_inpaint.py +++ b/api/onnx_web/chain/blend_inpaint.py @@ -22,27 +22,29 @@ def blend_inpaint( server: ServerContext, stage: StageParams, params: ImageParams, - source_image: Image.Image, + source: Image.Image, *, expand: Border, - mask_image: Optional[Image.Image] = None, + mask: Optional[Image.Image] = None, fill_color: str = "white", mask_filter: Callable = mask_filter_none, noise_source: Callable = noise_source_histogram, callback: ProgressCallback = None, **kwargs, ) -> Image.Image: + params = params.with_args(**kwargs) + expand = expand.with_args(**kwargs) logger.info( "blending image using inpaint, %s steps: %s", params.steps, params.prompt ) - if mask_image is None: + if mask is None: # if no mask was provided, keep the full source image - mask_image = Image.new("RGB", source_image.size, "black") + mask = Image.new("RGB", source.size, "black") - source_image, mask_image, noise_image, _full_dims = expand_image( - source_image, - mask_image, + source, mask, noise, _full_dims = expand_image( + source, + mask, expand, fill=fill_color, noise_source=noise_source, @@ -50,14 +52,14 @@ def blend_inpaint( ) if is_debug(): - save_image(server, "last-source.png", source_image) - save_image(server, "last-mask.png", mask_image) - save_image(server, "last-noise.png", noise_image) + save_image(server, "last-source.png", source) + save_image(server, "last-mask.png", mask) + save_image(server, "last-noise.png", noise) def outpaint(image: Image.Image, dims: Tuple[int, int, int]): left, top, tile = dims size = Size(*image.size) - mask = mask_image.crop((left, top, left + tile, top + tile)) + mask = mask.crop((left, top, left + tile, top + tile)) if is_debug(): save_image(server, "tile-source.png", image) @@ -108,7 +110,7 @@ def blend_inpaint( return result.images[0] output = process_tile_order( - stage.tile_order, source_image, SizeChart.auto, 1, [outpaint] + stage.tile_order, source, SizeChart.auto, 1, [outpaint] ) logger.info("final output image size", output.size) diff --git a/api/onnx_web/chain/blend_mask.py b/api/onnx_web/chain/blend_mask.py index 9beb2c26..521e5379 100644 --- a/api/onnx_web/chain/blend_mask.py +++ b/api/onnx_web/chain/blend_mask.py @@ -19,7 +19,7 @@ def blend_mask( _stage: StageParams, _params: ImageParams, *, - resized: Optional[List[Image.Image]] = None, + sources: Optional[List[Image.Image]] = None, mask: Optional[Image.Image] = None, _callback: ProgressCallback = None, **kwargs, @@ -36,7 +36,7 @@ def blend_mask( resized = [ valid_image(s, min_dims=mult_mask.size, max_dims=mult_mask.size) - for s in resized + for s in sources ] return Image.composite(resized[0], resized[1], mult_mask) diff --git a/api/onnx_web/chain/correct_codeformer.py b/api/onnx_web/chain/correct_codeformer.py index 0989a3e5..ef157162 100644 --- a/api/onnx_web/chain/correct_codeformer.py +++ b/api/onnx_web/chain/correct_codeformer.py @@ -25,6 +25,8 @@ def correct_codeformer( # must be within the load function for patch to take effect from codeformer import CodeFormer + upscale = upscale.with_args(**kwargs) + device = job.get_device() pipe = CodeFormer(upscale=upscale.face_outscale).to(device.torch_str()) return pipe(stage_source or source) diff --git a/api/onnx_web/chain/correct_gfpgan.py b/api/onnx_web/chain/correct_gfpgan.py index 99afce03..2cb0336f 100644 --- a/api/onnx_web/chain/correct_gfpgan.py +++ b/api/onnx_web/chain/correct_gfpgan.py @@ -55,6 +55,8 @@ def correct_gfpgan( upscale: UpscaleParams, **kwargs, ) -> Image.Image: + upscale = upscale.with_args(**kwargs) + if upscale.correction_model is None: logger.warn("no face model given, skipping") return source_image diff --git a/api/onnx_web/chain/source_txt2img.py b/api/onnx_web/chain/source_txt2img.py index b5d763eb..69b908e3 100644 --- a/api/onnx_web/chain/source_txt2img.py +++ b/api/onnx_web/chain/source_txt2img.py @@ -21,12 +21,12 @@ def source_txt2img( source_image: Image.Image, *, size: Size, - prompt: str = None, callback: ProgressCallback = None, **kwargs, ) -> Image.Image: - prompt = prompt or params.prompt - logger.info("generating image using txt2img, %s steps: %s", params.steps, prompt) + params = params.with_args(**kwargs) + size = size.with_args(**kwargs) + logger.info("generating image using txt2img, %s steps: %s", params.steps, params.prompt) if source_image is not None: logger.warn( @@ -47,7 +47,7 @@ def source_txt2img( logger.debug("using LPW pipeline for txt2img") rng = torch.manual_seed(params.seed) result = pipe.text2img( - prompt, + params.prompt, height=size.height, width=size.width, generator=rng, @@ -60,7 +60,7 @@ def source_txt2img( else: rng = np.random.RandomState(params.seed) result = pipe( - prompt, + params.prompt, height=size.height, width=size.width, generator=rng, diff --git a/api/onnx_web/chain/upscale_stable_diffusion.py b/api/onnx_web/chain/upscale_stable_diffusion.py index ffdb0036..a92bd353 100644 --- a/api/onnx_web/chain/upscale_stable_diffusion.py +++ b/api/onnx_web/chain/upscale_stable_diffusion.py @@ -69,12 +69,12 @@ def upscale_stable_diffusion( source: Image.Image, *, upscale: UpscaleParams, - prompt: str = None, callback: ProgressCallback = None, **kwargs, ) -> Image.Image: - prompt = prompt or params.prompt - logger.info("upscaling with Stable Diffusion, %s steps: %s", params.steps, prompt) + params = params.with_args(**kwargs) + upscale = upscale.with_args(**kwargs) + logger.info("upscaling with Stable Diffusion, %s steps: %s", params.steps, params.prompt) pipeline = load_stable_diffusion(server, upscale, job.get_device()) generator = torch.manual_seed(params.seed) diff --git a/api/onnx_web/diffusion/run.py b/api/onnx_web/diffusion/run.py index d0d3bc29..da58a42b 100644 --- a/api/onnx_web/diffusion/run.py +++ b/api/onnx_web/diffusion/run.py @@ -255,7 +255,7 @@ def run_blend_pipeline( server, stage, params, - resized=sources, + sources=sources, mask=mask, callback=progress, ) diff --git a/api/onnx_web/params.py b/api/onnx_web/params.py index c86a0a8b..f63d5315 100644 --- a/api/onnx_web/params.py +++ b/api/onnx_web/params.py @@ -47,6 +47,14 @@ class Border: "bottom": self.bottom, } + def with_args(self, **kwargs): + return Border( + kwargs.get("left", self.left), + kwargs.get("right", self.right), + kwargs.get("top", self.top), + kwargs.get("bottom", self.bottom), + ) + @classmethod def even(cls, all: int): return Border(all, all, all, all) @@ -75,6 +83,12 @@ class Size: "width": self.width, } + def with_args(self, **kwargs): + return Size( + kwargs.get("height", self.height), + kwargs.get("width", self.width), + ) + class DeviceParams: def __init__( @@ -156,13 +170,25 @@ class ImageParams: "model": self.model, "scheduler": self.scheduler.__name__, "prompt": self.prompt, - "negativePrompt": self.negative_prompt, + "negative_prompt": self.negative_prompt, "cfg": self.cfg, "seed": self.seed, "steps": self.steps, "lpw": self.lpw, } + def with_args(self, **kwargs): + return ImageParams( + kwargs.get("model", self.model), + kwargs.get("scheduler", self.scheduler), + kwargs.get("prompt", self.prompt), + kwargs.get("cfg", self.cfg), + kwargs.get("steps", self.steps), + kwargs.get("seed", self.seed), + kwargs.get("negative_prompt", self.negative_prompt), + kwargs.get("lpw", self.lpw), + ) + class StageParams: """ @@ -259,3 +285,20 @@ class UpscaleParams: "tile_pad": self.tile_pad, "upscale_order": self.upscale_order, } + + def with_args(self, **kwargs): + return ImageParams( + kwargs.get("upscale_model", self.upscale_model), + kwargs.get("correction_model", self.correction_model), + kwargs.get("denoise", self.denoise), + kwargs.get("faces", self.faces), + kwargs.get("face_outscale", self.face_outscale), + kwargs.get("face_strength", self.face_strength), + kwargs.get("format", self.format), + kwargs.get("half", self.half), + kwargs.get("outscale", self.outscale), + kwargs.get("pre_pad", self.pre_pad), + kwargs.get("scale", self.scale), + kwargs.get("tile_pad", self.tile_pad), + kwargs.get("upscale_order", self.upscale_order), + )