1
0
Fork 0
onnx-web/api/onnx_web/chain/upscale_outpaint.py

141 lines
4.7 KiB
Python
Raw Normal View History

2023-01-28 23:09:19 +00:00
from logging import getLogger
from typing import Callable, Tuple
import numpy as np
2023-02-05 23:55:04 +00:00
import torch
2023-02-05 13:53:26 +00:00
from diffusers import OnnxStableDiffusionInpaintPipeline
from PIL import Image, ImageDraw
from ..diffusion.load import get_latents_from_seed, get_tile_latents, load_pipeline
from ..image import expand_image, mask_filter_none, noise_source_histogram
from ..output import save_image
2023-02-12 00:10:36 +00:00
from ..params import Border, ImageParams, Size, SizeChart, StageParams
from ..server.device_pool import JobContext, ProgressCallback
2023-02-05 13:53:26 +00:00
from ..utils import ServerContext, is_debug
from .utils import process_tile_grid, process_tile_order
2023-01-28 23:09:19 +00:00
logger = getLogger(__name__)
def upscale_outpaint(
2023-02-05 03:23:34 +00:00
job: JobContext,
server: ServerContext,
stage: StageParams,
params: ImageParams,
source_image: Image.Image,
*,
border: Border,
2023-01-29 06:07:12 +00:00
prompt: str = None,
mask_image: Image.Image = None,
2023-02-05 13:53:26 +00:00
fill_color: str = "white",
mask_filter: Callable = mask_filter_none,
noise_source: Callable = noise_source_histogram,
2023-02-12 18:22:11 +00:00
callback: ProgressCallback = None,
**kwargs,
2023-01-28 15:08:59 +00:00
) -> Image.Image:
2023-01-29 06:07:12 +00:00
prompt = prompt or params.prompt
2023-02-05 13:53:26 +00:00
logger.info("upscaling image by expanding borders: %s", border)
margin_x = float(max(border.left, border.right))
margin_y = float(max(border.top, border.bottom))
overlap = min(margin_x / source_image.width, margin_y / source_image.height)
if mask_image is None:
2023-01-28 15:38:25 +00:00
# if no mask was provided, keep the full source image
2023-02-05 13:53:26 +00:00
mask_image = Image.new("RGB", source_image.size, "black")
source_image, mask_image, noise_image, full_dims = expand_image(
source_image,
mask_image,
border,
fill=fill_color,
noise_source=noise_source,
2023-02-05 13:53:26 +00:00
mask_filter=mask_filter,
)
draw_mask = ImageDraw.Draw(mask_image)
full_size = Size(*full_dims)
full_latents = get_latents_from_seed(params.seed, full_size)
if is_debug():
2023-02-05 13:53:26 +00:00
save_image(server, "last-source.png", source_image)
save_image(server, "last-mask.png", mask_image)
save_image(server, "last-noise.png", noise_image)
def outpaint(image: Image.Image, dims: Tuple[int, int, int]):
2023-01-28 16:26:04 +00:00
left, top, tile = dims
2023-01-28 15:08:59 +00:00
size = Size(*image.size)
mask = mask_image.crop((left, top, left + tile, top + tile))
2023-01-28 15:55:47 +00:00
2023-01-28 16:26:04 +00:00
if is_debug():
2023-02-05 13:53:26 +00:00
save_image(server, "tile-source.png", image)
save_image(server, "tile-mask.png", mask)
latents = get_tile_latents(full_latents, dims)
2023-02-05 13:53:26 +00:00
pipe = load_pipeline(
server,
2023-02-05 13:53:26 +00:00
OnnxStableDiffusionInpaintPipeline,
params.model,
params.scheduler,
job.get_device(),
2023-02-05 23:40:10 +00:00
params.lpw,
2023-02-05 13:53:26 +00:00
)
2023-02-05 23:15:37 +00:00
if params.lpw:
2023-02-05 23:55:04 +00:00
logger.debug("using LPW pipeline for inpaint")
rng = torch.manual_seed(params.seed)
result = pipe.inpaint(
image,
mask,
prompt,
generator=rng,
guidance_scale=params.cfg,
height=size.height,
latents=latents,
negative_prompt=params.negative_prompt,
num_inference_steps=params.steps,
width=size.width,
callback=callback,
)
else:
rng = np.random.RandomState(params.seed)
result = pipe(
prompt,
image,
generator=rng,
guidance_scale=params.cfg,
height=size.height,
latents=latents,
mask_image=mask,
negative_prompt=params.negative_prompt,
num_inference_steps=params.steps,
width=size.width,
callback=callback,
)
# once part of the image has been drawn, keep it
2023-02-05 13:53:26 +00:00
draw_mask.rectangle((left, top, left + tile, top + tile), fill="black")
return result.images[0]
if overlap == 0:
logger.debug("outpainting with 0 margin, using grid tiling")
output = process_tile_grid(source_image, SizeChart.auto, 1, [outpaint])
elif border.left == border.right and border.top == border.bottom:
2023-02-07 14:20:02 +00:00
logger.debug(
"outpainting with an even border, using spiral tiling with %s overlap",
overlap,
)
output = process_tile_order(
stage.tile_order,
source_image,
SizeChart.auto,
1,
[outpaint],
overlap=overlap,
2023-02-05 23:55:04 +00:00
)
else:
logger.debug("outpainting with an uneven border, using grid tiling")
output = process_tile_grid(source_image, SizeChart.auto, 1, [outpaint])
2023-02-05 13:53:26 +00:00
logger.info("final output image size: %sx%s", output.width, output.height)
return output