Skip to content

Commit

Permalink
feat(api): save image params along with image (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Feb 2, 2023
1 parent 066b1a0 commit 6697c2e
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 55 deletions.
7 changes: 5 additions & 2 deletions api/onnx_web/chain/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
ImageParams,
StageParams,
)
from ..output import (
save_image,
)
from ..utils import (
is_debug,
ServerContext,
Expand Down Expand Up @@ -82,7 +85,7 @@ def stage_tile(tile: Image.Image, _dims) -> Image.Image:
**kwargs)

if is_debug():
tile.save(path.join(ctx.output_path, 'last-tile.png'))
save_image(ctx, 'last-tile.png', tile)

return tile

Expand All @@ -97,7 +100,7 @@ def stage_tile(tile: Image.Image, _dims) -> Image.Image:
name, image.width, image.height)

if is_debug():
image.save(path.join(ctx.output_path, 'last-stage.png'))
save_image(ctx, 'last-stage.png', image)

end = monotonic()
duration = timedelta(seconds=(end - start))
Expand Down
14 changes: 8 additions & 6 deletions api/onnx_web/chain/blend_inpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
SizeChart,
StageParams,
)
from ..output import (
save_image,
)
from ..utils import (
base_join,
is_debug,
ServerContext,
)
Expand Down Expand Up @@ -63,18 +65,18 @@ def blend_inpaint(
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'))
save_image(ctx, 'last-source.png', source_image)
save_image(ctx, 'last-mask.png', mask_image)
save_image(ctx, 'last-noise.png', noise_image)

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))

if is_debug():
image.save(base_join(ctx.output_path, 'tile-source.png'))
mask.save(base_join(ctx.output_path, 'tile-mask.png'))
save_image(ctx, 'tile-source.png', image)
save_image(ctx, 'tile-mask.png', mask)

# TODO: must use inpainting model here
model = '../models/stable-diffusion-onnx-v1-inpainting'
Expand Down
7 changes: 4 additions & 3 deletions api/onnx_web/chain/persist_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
ImageParams,
StageParams,
)
from ..output import (
save_image,
)
from ..utils import (
base_join,
ServerContext,
)

Expand All @@ -22,7 +24,6 @@ def persist_disk(
output: str,
**kwargs,
) -> Image.Image:
dest = base_join(ctx.output_path, output)
source_image.save(dest)
dest = save_image(ctx, output, source_image)
logger.info('saved image to %s', dest)
return source_image
13 changes: 8 additions & 5 deletions api/onnx_web/chain/upscale_outpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
SizeChart,
StageParams,
)
from ..output import (
save_image,
)
from ..utils import (
base_join,
is_debug,
Expand Down Expand Up @@ -70,18 +73,18 @@ def upscale_outpaint(
full_latents = get_latents_from_seed(params.seed, full_size)

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'))
save_image(ctx, 'last-source.png', source_image)
save_image(ctx, 'last-mask.png', mask_image)
save_image(ctx, 'last-noise.png', noise_image)

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))

if is_debug():
image.save(base_join(ctx.output_path, 'tile-source.png'))
mask.save(base_join(ctx.output_path, 'tile-mask.png'))
save_image(ctx, 'tile-source.png', image)
save_image(ctx, 'tile-mask.png', mask)

# TODO: must use inpainting model here
model = '../models/stable-diffusion-onnx-v1-inpainting'
Expand Down
1 change: 0 additions & 1 deletion api/onnx_web/diffusion/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
run_gc,
)

import gc
import numpy as np

logger = getLogger(__name__)
Expand Down
30 changes: 17 additions & 13 deletions api/onnx_web/diffusion/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
Size,
StageParams,
)
from ..output import (
save_image,
save_params,
)
from ..upscale import (
run_upscale_correction,
UpscaleParams,
)
from ..utils import (
base_join,
run_gc,
ServerContext,
)
Expand Down Expand Up @@ -61,8 +64,8 @@ def run_txt2img_pipeline(
image = run_upscale_correction(
ctx, StageParams(), params, image, upscale=upscale)

dest = base_join(ctx.output_path, output)
image.save(dest)
dest = save_image(ctx, output, image)
save_params(ctx, output, params, size, upscale=upscale)

del image
del result
Expand Down Expand Up @@ -97,8 +100,9 @@ def run_img2img_pipeline(
image = run_upscale_correction(
ctx, StageParams(), params, image, upscale=upscale)

dest = base_join(ctx.output_path, output)
image.save(dest)
dest = save_image(ctx, output, image)
size = Size(*source_image.size)
save_params(ctx, output, params, size, upscale=upscale)

del image
del result
Expand All @@ -110,12 +114,12 @@ def run_img2img_pipeline(
def run_inpaint_pipeline(
ctx: ServerContext,
params: ImageParams,
_size: Size,
size: Size,
output: str,
upscale: UpscaleParams,
source_image: Image.Image,
mask_image: Image.Image,
expand: Border,
border: Border,
noise_source: Any,
mask_filter: Any,
strength: float,
Expand All @@ -127,7 +131,7 @@ def run_inpaint_pipeline(
stage,
params,
source_image,
border=expand,
border=border,
mask_image=mask_image,
fill_color=fill_color,
mask_filter=mask_filter,
Expand All @@ -144,8 +148,8 @@ def run_inpaint_pipeline(
image = run_upscale_correction(
ctx, stage, params, image, upscale=upscale)

dest = base_join(ctx.output_path, output)
image.save(dest)
dest = save_image(ctx, output, image)
save_params(ctx, output, params, size, upscale=upscale, border=border)

del image
run_gc()
Expand All @@ -156,16 +160,16 @@ def run_inpaint_pipeline(
def run_upscale_pipeline(
ctx: ServerContext,
params: ImageParams,
_size: Size,
size: Size,
output: str,
upscale: UpscaleParams,
source_image: Image.Image,
) -> None:
image = run_upscale_correction(
ctx, StageParams(), params, source_image, upscale=upscale)

dest = base_join(ctx.output_path, output)
image.save(dest)
dest = save_image(ctx, output, image)
save_params(ctx, output, params, size, upscale=upscale)

del image
run_gc()
Expand Down
50 changes: 50 additions & 0 deletions api/onnx_web/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from json import dumps
from PIL import Image
from typing import Any, Optional

from .params import (
Border,
ImageParams,
Size,
UpscaleParams,
)
from .utils import (
base_join,
ServerContext,
)


def json_params(
output: str,
params: ImageParams,
size: Size,
upscale: Optional[UpscaleParams] = None,
border: Optional[Border] = None,
) -> Any:
return {
'border': border.tojson(),
'output': output,
'params': params.tojson(),
'size': upscale.resize(size.add_border(border)).tojson(),
'upscale': upscale.tojson(),
}


def save_image(ctx: ServerContext, output: str, image: Image.Image) -> str:
path = base_join(ctx.output_path, output)
image.save(path)
return path


def save_params(
ctx: ServerContext,
output: str,
params: ImageParams,
size: Size,
upscale: Optional[UpscaleParams] = None,
border: Optional[Border] = None,
) -> str:
path = base_join(ctx.output_path, '%s.json' % (output))
json = json_params(output, params, size, upscale=upscale, border=border)
with open(path, 'w') as f:
f.write(dumps(json))
35 changes: 10 additions & 25 deletions api/onnx_web/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
noise_source_normal,
noise_source_uniform,
)
from .output import (
json_params,
save_image,
save_params,
)
from .params import (
Border,
ImageParams,
Expand Down Expand Up @@ -437,11 +442,7 @@ def img2img():
executor.submit_stored(output, run_img2img_pipeline,
context, params, output, upscale, source_image, strength)

return jsonify({
'output': output,
'params': params.tojson(),
'size': upscale.resize(size).tojson(),
})
return jsonify(json_params(output, params, size, upscale=upscale))


@app.route('/api/txt2img', methods=['POST'])
Expand All @@ -458,11 +459,7 @@ def txt2img():
executor.submit_stored(
output, run_txt2img_pipeline, context, params, size, output, upscale)

return jsonify({
'output': output,
'params': params.tojson(),
'size': upscale.resize(size).tojson(),
})
return jsonify(json_params(output, params, size, upscale=upscale))


@app.route('/api/inpaint', methods=['POST'])
Expand Down Expand Up @@ -529,11 +526,7 @@ def inpaint():
strength,
fill_color)

return jsonify({
'output': output,
'params': params.tojson(),
'size': upscale.resize(size.add_border(expand)).tojson(),
})
return jsonify(json_params(output, params, size, upscale=upscale, border=expand))


@app.route('/api/upscale', methods=['POST'])
Expand All @@ -557,11 +550,7 @@ def upscale():
executor.submit_stored(output, run_upscale_pipeline,
context, params, size, output, upscale, source_image)

return jsonify({
'output': output,
'params': params.tojson(),
'size': upscale.resize(size).tojson(),
})
return jsonify(json_params(output, params, size, upscale=upscale))


@app.route('/api/chain', methods=['POST'])
Expand Down Expand Up @@ -607,11 +596,7 @@ def chain():
executor.submit_stored(output, pipeline, context,
params, fake_source, output=output, size=size)

return jsonify({
'output': output,
'params': params.tojson(),
'size': size.tojson(),
})
return jsonify(json_params(output, params, size))


@app.route('/api/ready')
Expand Down

0 comments on commit 6697c2e

Please sign in to comment.