1
0
Fork 0
onnx-web/api/onnx_web/diffusion.py

255 lines
6.5 KiB
Python
Raw Normal View History

2023-01-16 00:46:00 +00:00
from diffusers import (
2023-01-16 00:54:20 +00:00
DiffusionPipeline,
# onnx
OnnxStableDiffusionPipeline,
OnnxStableDiffusionImg2ImgPipeline,
OnnxStableDiffusionInpaintPipeline,
2023-01-16 00:46:00 +00:00
)
2023-01-17 05:45:54 +00:00
from PIL import Image, ImageChops
from typing import Any, Optional
2023-01-16 00:46:00 +00:00
import gc
2023-01-16 00:46:00 +00:00
import numpy as np
import torch
2023-01-16 00:46:00 +00:00
2023-01-27 23:11:27 +00:00
from .chain import (
StageParams,
)
2023-01-16 00:54:20 +00:00
from .image import (
expand_image,
)
from .params import (
ImageParams,
Border,
Size,
)
2023-01-16 00:54:20 +00:00
from .upscale import (
run_upscale_correction,
UpscaleParams,
2023-01-16 00:54:20 +00:00
)
from .utils import (
is_debug,
base_join,
ServerContext,
2023-01-16 00:54:20 +00:00
)
2023-01-16 00:46:00 +00:00
last_pipeline_instance = None
last_pipeline_options = (None, None, None)
last_pipeline_scheduler = None
def get_latents_from_seed(seed: int, size: Size) -> np.ndarray:
'''
From https://www.travelneil.com/stable-diffusion-updates.html
'''
2023-01-16 00:46:00 +00:00
# 1 is batch size
latents_shape = (1, 4, size.height // 8, size.width // 8)
2023-01-16 00:46:00 +00:00
# Gotta use numpy instead of torch, because torch's randn() doesn't support DML
rng = np.random.default_rng(seed)
image_latents = rng.standard_normal(latents_shape).astype(np.float32)
return image_latents
def load_pipeline(pipeline: DiffusionPipeline, model: str, provider: str, scheduler: Any, device: Optional[str] = None):
2023-01-16 00:46:00 +00:00
global last_pipeline_instance
global last_pipeline_scheduler
global last_pipeline_options
options = (pipeline, model, provider)
if last_pipeline_instance != None and last_pipeline_options == options:
print('reusing existing pipeline')
pipe = last_pipeline_instance
else:
print('unloading previous pipeline')
last_pipeline_instance = None
last_pipeline_scheduler = None
gc.collect()
torch.cuda.empty_cache()
print('loading new pipeline')
2023-01-16 00:46:00 +00:00
pipe = pipeline.from_pretrained(
model,
provider=provider,
safety_checker=None,
scheduler=scheduler.from_pretrained(model, subfolder='scheduler')
)
if device is not None:
pipe = pipe.to(device)
2023-01-16 00:46:00 +00:00
last_pipeline_instance = pipe
last_pipeline_options = options
last_pipeline_scheduler = scheduler
if last_pipeline_scheduler != scheduler:
print('loading new scheduler')
scheduler = scheduler.from_pretrained(
2023-01-16 00:46:00 +00:00
model, subfolder='scheduler')
if device is not None:
scheduler = scheduler.to(device)
pipe.scheduler = scheduler
2023-01-16 00:46:00 +00:00
last_pipeline_scheduler = scheduler
print('running garbage collection during pipeline change')
gc.collect()
2023-01-16 00:46:00 +00:00
return pipe
2023-01-16 00:54:20 +00:00
def run_txt2img_pipeline(
ctx: ServerContext,
params: ImageParams,
size: Size,
output: str,
upscale: UpscaleParams
):
2023-01-16 00:54:20 +00:00
pipe = load_pipeline(OnnxStableDiffusionPipeline,
params.model, params.provider, params.scheduler)
2023-01-16 00:54:20 +00:00
2023-01-16 01:49:40 +00:00
latents = get_latents_from_seed(params.seed, size)
rng = np.random.RandomState(params.seed)
2023-01-16 00:54:20 +00:00
result = pipe(
params.prompt,
height=size.height,
width=size.width,
2023-01-16 00:54:20 +00:00
generator=rng,
guidance_scale=params.cfg,
2023-01-16 00:54:20 +00:00
latents=latents,
negative_prompt=params.negative_prompt,
num_inference_steps=params.steps,
)
image = result.images[0]
image = run_upscale_correction(
ctx, StageParams(), params, image, upscale=upscale)
2023-01-16 00:54:20 +00:00
dest = base_join(ctx.output_path, output)
image.save(dest)
del image
del result
print('saved txt2img output: %s' % (dest))
2023-01-16 00:54:20 +00:00
def run_img2img_pipeline(
ctx: ServerContext,
params: ImageParams,
output: str,
upscale: UpscaleParams,
source_image: Image,
strength: float,
):
2023-01-16 00:54:20 +00:00
pipe = load_pipeline(OnnxStableDiffusionImg2ImgPipeline,
params.model, params.provider, params.scheduler)
2023-01-16 00:54:20 +00:00
rng = np.random.RandomState(params.seed)
2023-01-16 00:54:20 +00:00
result = pipe(
params.prompt,
2023-01-16 00:54:20 +00:00
generator=rng,
guidance_scale=params.cfg,
image=source_image,
negative_prompt=params.negative_prompt,
num_inference_steps=params.steps,
2023-01-16 00:54:20 +00:00
strength=strength,
)
image = result.images[0]
image = run_upscale_correction(
ctx, StageParams(), params, image, upscale=upscale)
2023-01-16 00:54:20 +00:00
dest = base_join(ctx.output_path, output)
image.save(dest)
del image
del result
print('saved img2img output: %s' % (dest))
2023-01-16 00:54:20 +00:00
def run_inpaint_pipeline(
ctx: ServerContext,
stage: StageParams,
params: ImageParams,
size: Size,
output: str,
upscale: UpscaleParams,
2023-01-16 00:54:20 +00:00
source_image: Image,
mask_image: Image,
expand: Border,
2023-01-16 00:54:20 +00:00
noise_source: Any,
mask_filter: Any,
strength: float,
fill_color: str,
2023-01-16 00:54:20 +00:00
):
pipe = load_pipeline(OnnxStableDiffusionInpaintPipeline,
params.model, params.provider, params.scheduler)
2023-01-16 00:54:20 +00:00
latents = get_latents_from_seed(params.seed, size)
rng = np.random.RandomState(params.seed)
2023-01-16 00:54:20 +00:00
print('applying mask filter and generating noise source')
source_image, mask_image, noise_image, _full_dims = expand_image(
source_image,
mask_image,
expand,
fill=fill_color,
2023-01-16 00:54:20 +00:00
noise_source=noise_source,
mask_filter=mask_filter)
if is_debug():
source_image.save(base_join(ctx.output_path, 'last-source.png'))
mask_image.save(base_join(ctx.output_path, 'last-mask.png'))
noise_image.save(base_join(ctx.output_path, 'last-noise.png'))
2023-01-16 00:54:20 +00:00
result = pipe(
params.prompt,
2023-01-16 00:54:20 +00:00
generator=rng,
guidance_scale=params.cfg,
height=size.height,
2023-01-16 00:54:20 +00:00
image=source_image,
latents=latents,
mask_image=mask_image,
negative_prompt=params.negative_prompt,
num_inference_steps=params.steps,
width=size.width,
)
image = result.images[0]
if image.size == source_image.size:
image = ImageChops.blend(source_image, image, strength)
else:
print('output image size does not match source, skipping post-blend')
2023-01-16 00:54:20 +00:00
image = run_upscale_correction(
ctx, StageParams(), params, image, upscale=upscale)
dest = base_join(ctx.output_path, output)
image.save(dest)
2023-01-16 00:54:20 +00:00
del image
del result
print('saved inpaint output: %s' % (dest))
2023-01-17 05:45:54 +00:00
2023-01-17 05:45:54 +00:00
def run_upscale_pipeline(
ctx: ServerContext,
params: ImageParams,
2023-01-17 05:45:54 +00:00
_size: Size,
output: str,
upscale: UpscaleParams,
source_image: Image
2023-01-17 05:45:54 +00:00
):
image = run_upscale_correction(
ctx, StageParams(), params, source_image, upscale=upscale)
2023-01-17 05:45:54 +00:00
dest = base_join(ctx.output_path, output)
2023-01-17 05:45:54 +00:00
image.save(dest)
del image
2023-01-17 05:45:54 +00:00
print('saved img2img output: %s' % (dest))