IOPaint/lama_cleaner/tests/test_controlnet.py

118 lines
3.3 KiB
Python
Raw Normal View History

2023-04-01 03:23:37 +02:00
import os
2023-05-11 15:51:58 +02:00
from lama_cleaner.const import SD_CONTROLNET_CHOICES
2023-12-28 03:48:52 +01:00
from lama_cleaner.tests.utils import current_dir, check_device, get_config, assert_equal
2023-05-11 15:51:58 +02:00
2023-04-01 03:23:37 +02:00
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
2023-12-28 03:48:52 +01:00
2023-12-22 07:00:30 +01:00
model_name = "runwayml/stable-diffusion-inpainting"
2023-04-01 03:23:37 +02:00
2023-12-28 03:48:52 +01:00
def convert_controlnet_method_name(name):
return name.replace("/", "--")
2023-05-18 06:29:45 +02:00
2023-04-01 03:23:37 +02:00
2023-12-28 03:48:52 +01:00
@pytest.mark.parametrize("device", ["cuda", "mps", "cpu"])
@pytest.mark.parametrize("controlnet_method", [SD_CONTROLNET_CHOICES[0]])
def test_runway_sd_1_5(device, controlnet_method):
sd_steps = check_device(device)
2023-04-01 03:23:37 +02:00
model = ModelManager(
2023-12-22 07:00:30 +01:00
name=model_name,
2023-12-28 03:48:52 +01:00
device=torch.device(device),
2023-04-01 03:23:37 +02:00
disable_nsfw=True,
2023-12-28 03:48:52 +01:00
sd_cpu_textencoder=True,
enable_controlnet=True,
controlnet_method=controlnet_method,
2023-05-11 15:51:58 +02:00
)
2023-12-28 03:48:52 +01:00
2023-05-11 15:51:58 +02:00
cfg = get_config(
prompt="a fox sitting on a bench",
sd_steps=sd_steps,
2023-12-28 03:48:52 +01:00
enable_controlnet=True,
controlnet_conditioning_scale=0.5,
controlnet_method=controlnet_method,
2023-05-11 15:51:58 +02:00
)
2023-12-28 03:48:52 +01:00
name = f"device_{device}"
2023-05-11 15:51:58 +02:00
assert_equal(
model,
cfg,
2023-12-28 03:48:52 +01:00
f"sd_controlnet_{convert_controlnet_method_name(controlnet_method)}_{name}.png",
2023-05-11 15:51:58 +02:00
img_p=current_dir / "overture-creations-5sI6fQgYIuo.png",
mask_p=current_dir / "overture-creations-5sI6fQgYIuo_mask.png",
)
2023-12-28 03:48:52 +01:00
@pytest.mark.parametrize("device", ["cuda", "mps", "cpu"])
def test_controlnet_switch(device):
sd_steps = check_device(device)
2023-05-11 15:51:58 +02:00
model = ModelManager(
2023-12-22 07:00:30 +01:00
name=model_name,
2023-12-28 03:48:52 +01:00
device=torch.device(device),
2023-05-11 15:51:58 +02:00
disable_nsfw=True,
sd_cpu_textencoder=False,
cpu_offload=True,
2023-12-28 03:48:52 +01:00
enable_controlnet=True,
controlnet_method="lllyasviel/control_v11p_sd15_canny",
2023-04-01 03:23:37 +02:00
)
cfg = get_config(
prompt="a fox sitting on a bench",
sd_steps=sd_steps,
2023-12-28 03:48:52 +01:00
enable_controlnet=True,
controlnet_method="lllyasviel/control_v11f1p_sd15_depth",
2023-04-01 03:23:37 +02:00
)
assert_equal(
model,
cfg,
2023-12-28 03:48:52 +01:00
f"controlnet_switch_canny_to_depth_device_{device}.png",
2023-04-01 03:23:37 +02:00
img_p=current_dir / "overture-creations-5sI6fQgYIuo.png",
mask_p=current_dir / "overture-creations-5sI6fQgYIuo_mask.png",
)
2023-05-13 07:45:27 +02:00
2023-12-28 03:48:52 +01:00
@pytest.mark.parametrize("device", ["cuda", "mps", "cpu"])
@pytest.mark.parametrize(
"local_file", ["sd-v1-5-inpainting.ckpt", "v1-5-pruned-emaonly.safetensors"]
)
def test_local_file_path(device, local_file):
sd_steps = check_device(device)
controlnet_kwargs = dict(
enable_controlnet=True,
controlnet_method=SD_CONTROLNET_CHOICES[0],
)
2023-05-13 07:45:27 +02:00
model = ModelManager(
2023-12-28 03:48:52 +01:00
name=local_file,
device=torch.device(device),
2023-05-13 07:45:27 +02:00
disable_nsfw=True,
sd_cpu_textencoder=False,
cpu_offload=True,
2023-12-28 03:48:52 +01:00
**controlnet_kwargs,
2023-05-13 07:45:27 +02:00
)
cfg = get_config(
prompt="a fox sitting on a bench",
sd_steps=sd_steps,
2023-12-28 03:48:52 +01:00
**controlnet_kwargs,
2023-05-13 07:45:27 +02:00
)
2023-12-28 03:48:52 +01:00
name = f"device_{device}"
2023-05-13 07:45:27 +02:00
assert_equal(
model,
cfg,
2023-12-28 03:48:52 +01:00
f"{controlnet_kwargs['controlnet_method']}_local_model_{name}.png",
2023-05-13 07:45:27 +02:00
img_p=current_dir / "overture-creations-5sI6fQgYIuo.png",
mask_p=current_dir / "overture-creations-5sI6fQgYIuo_mask.png",
)