IOPaint/lama_cleaner/tests/test_outpainting.py

115 lines
3.5 KiB
Python
Raw Normal View History

2023-08-30 07:28:31 +02:00
import os
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
from pathlib import Path
import pytest
import torch
from lama_cleaner.model_manager import ModelManager
from lama_cleaner.schema import HDStrategy, SDSampler
from lama_cleaner.tests.test_model import get_config, assert_equal
current_dir = Path(__file__).parent.absolute().resolve()
save_dir = current_dir / "result"
save_dir.mkdir(exist_ok=True, parents=True)
device = "cuda" if torch.cuda.is_available() else "cpu"
device = torch.device(device)
2023-12-19 06:16:30 +01:00
@pytest.mark.parametrize("name", ["runwayml/stable-diffusion-inpainting"])
2023-08-30 07:28:31 +02:00
@pytest.mark.parametrize("sd_device", ["mps"])
@pytest.mark.parametrize(
"rect",
[
[0, -100, 512, 512 - 128 + 100],
[0, 128, 512, 512 - 128 + 100],
[128, 0, 512 - 128 + 100, 512],
[-100, 0, 512 - 128 + 100, 512],
[0, 0, 512, 512 + 200],
[0, 0, 512 + 200, 512],
[-100, -100, 512 + 200, 512 + 200],
],
)
2023-08-30 15:30:11 +02:00
def test_outpainting(name, sd_device, rect):
2023-08-30 07:28:31 +02:00
def callback(i, t, latents):
pass
if sd_device == "cuda" and not torch.cuda.is_available():
return
model = ModelManager(
2023-08-30 15:30:11 +02:00
name=name,
2023-08-30 07:28:31 +02:00
device=torch.device(sd_device),
disable_nsfw=True,
sd_cpu_textencoder=False,
callback=callback,
)
cfg = get_config(
HDStrategy.ORIGINAL,
prompt="a dog sitting on a bench in the park",
2023-08-30 15:30:11 +02:00
sd_steps=50,
2023-12-19 06:16:30 +01:00
use_extender=True,
extender_x=rect[0],
extender_y=rect[1],
extender_width=rect[2],
extender_height=rect[3],
2023-11-14 07:19:56 +01:00
sd_guidance_scale=8.0,
2023-08-30 07:28:31 +02:00
sd_sampler=SDSampler.dpm_plus_plus,
)
assert_equal(
model,
cfg,
2023-12-19 06:16:30 +01:00
f"{name.replace('/', '--')}_outpainting_dpm++_{'_'.join(map(str, rect))}.png",
2023-08-30 07:28:31 +02:00
img_p=current_dir / "overture-creations-5sI6fQgYIuo.png",
mask_p=current_dir / "overture-creations-5sI6fQgYIuo_mask.png",
)
2023-08-30 15:30:11 +02:00
2023-12-19 06:16:30 +01:00
@pytest.mark.parametrize("name", ["kandinsky-community/kandinsky-2-2-decoder-inpaint"])
2023-08-30 15:30:11 +02:00
@pytest.mark.parametrize("sd_device", ["mps"])
@pytest.mark.parametrize(
"rect",
[
2023-11-14 07:19:56 +01:00
[-128, -128, 768, 768],
2023-08-30 15:30:11 +02:00
],
)
def test_kandinsky_outpainting(name, sd_device, rect):
def callback(i, t, latents):
pass
if sd_device == "cuda" and not torch.cuda.is_available():
return
model = ModelManager(
2023-12-19 06:16:30 +01:00
name=name,
2023-08-30 15:30:11 +02:00
device=torch.device(sd_device),
disable_nsfw=True,
sd_cpu_textencoder=False,
callback=callback,
)
cfg = get_config(
HDStrategy.ORIGINAL,
prompt="a cat",
negative_prompt="lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature",
sd_steps=50,
2023-12-19 06:16:30 +01:00
use_extender=True,
extender_x=rect[0],
extender_y=rect[1],
extender_width=rect[2],
extender_height=rect[3],
2023-11-14 07:19:56 +01:00
sd_guidance_scale=7,
2023-08-30 15:30:11 +02:00
sd_sampler=SDSampler.dpm_plus_plus,
)
assert_equal(
model,
cfg,
2023-12-19 06:16:30 +01:00
f"{name.replace('/', '--')}_outpainting_dpm++_{'_'.join(map(str, rect))}.png",
2023-08-30 15:30:11 +02:00
img_p=current_dir / "cat.png",
mask_p=current_dir / "overture-creations-5sI6fQgYIuo_mask.png",
2023-11-14 07:19:56 +01:00
fx=1,
fy=1,
2023-08-30 15:30:11 +02:00
)