Spaces:
Runtime error
Runtime error
File size: 10,236 Bytes
c49a0ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
import os
import unittest
import requests
import importlib
utils = importlib.import_module("extensions.sd-webui-controlnet.tests.utils", "utils")
from scripts.enums import StableDiffusionVersion
from modules import shared
class TestAlwaysonTxt2ImgWorking(unittest.TestCase):
def setUp(self):
self.sd_version = StableDiffusionVersion(
int(
os.environ.get(
"CONTROLNET_TEST_SD_VERSION", StableDiffusionVersion.SD1x.value
)
)
)
self.model = utils.get_model("canny", self.sd_version)
controlnet_unit = {
"enabled": True,
"module": "none",
"model": self.model,
"weight": 1.0,
"image": utils.readImage("test/test_files/img2img_basic.png"),
"mask": utils.readImage("test/test_files/img2img_basic.png"),
"resize_mode": 1,
"lowvram": False,
"processor_res": 64,
"threshold_a": 64,
"threshold_b": 64,
"guidance_start": 0.0,
"guidance_end": 1.0,
"control_mode": 0,
"pixel_perfect": False,
}
setup_args = [controlnet_unit] * getattr(self, "units_count", 1)
self.setup_route(setup_args)
def setup_route(self, setup_args):
self.url_txt2img = "http://localhost:7860/sdapi/v1/txt2img"
self.simple_txt2img = {
"enable_hr": False,
"denoising_strength": 0,
"firstphase_width": 0,
"firstphase_height": 0,
"prompt": "example prompt",
"styles": [],
"seed": -1,
"subseed": -1,
"subseed_strength": 0,
"seed_resize_from_h": -1,
"seed_resize_from_w": -1,
"batch_size": 1,
"n_iter": 1,
"steps": 3,
"cfg_scale": 7,
"width": 64,
"height": 64,
"restore_faces": False,
"tiling": False,
"negative_prompt": "",
"eta": 0,
"s_churn": 0,
"s_tmax": 0,
"s_tmin": 0,
"s_noise": 1,
"sampler_index": "Euler a",
"alwayson_scripts": {},
}
self.setup_controlnet_params(setup_args)
def setup_controlnet_params(self, setup_args):
self.simple_txt2img["alwayson_scripts"]["ControlNet"] = {"args": setup_args}
def assert_status_ok(self, msg=None, expected_image_num=None):
msg = ("" if msg is None else msg) + f"\nPayload:\n{self.simple_txt2img}"
resp = requests.post(self.url_txt2img, json=self.simple_txt2img)
self.assertEqual(resp.status_code, 200, msg)
# Note: Exception/error in ControlNet code likely will cause hook failure, which further leads
# to detected map not being appended at the end of response image array.
data = resp.json()
if expected_image_num is None:
expected_image_num = self.simple_txt2img["n_iter"] * self.simple_txt2img[
"batch_size"
] + min(
sum(
[
unit.get("save_detected_map", True)
for unit in self.simple_txt2img["alwayson_scripts"]["ControlNet"][
"args"
]
]
),
shared.opts.data.get("control_net_unit_count", 3),
)
self.assertEqual(len(data["images"]), expected_image_num, msg)
def test_txt2img_simple_performed(self):
self.assert_status_ok()
def test_txt2img_alwayson_scripts_default_units(self):
self.units_count = 0
self.setUp()
self.assert_status_ok()
def test_txt2img_multiple_batches_performed(self):
self.simple_txt2img["n_iter"] = 2
self.assert_status_ok()
def test_txt2img_batch_performed(self):
self.simple_txt2img["batch_size"] = 2
self.assert_status_ok()
def test_txt2img_2_units(self):
self.units_count = 2
self.setUp()
self.assert_status_ok()
def test_txt2img_8_units(self):
self.units_count = 8
self.setUp()
self.assert_status_ok()
def test_txt2img_default_params(self):
self.simple_txt2img["alwayson_scripts"]["ControlNet"]["args"] = [
{
"input_image": utils.readImage("test/test_files/img2img_basic.png"),
"model": self.model,
}
]
self.assert_status_ok()
def test_call_with_preprocessors(self):
available_modules = utils.get_modules()
available_modules_list = available_modules.get("module_list", [])
available_modules_detail = available_modules.get("module_detail", {})
for module in ["depth", "openpose_full"]:
assert module in available_modules_list, f"Failed to find {module}."
assert (
module in available_modules_detail
), f"Failed to find {module}'s detail."
with self.subTest(module=module):
self.simple_txt2img["alwayson_scripts"]["ControlNet"]["args"] = [
{
"input_image": utils.readImage(
"test/test_files/img2img_basic.png"
),
"model": self.model,
"module": module,
}
]
self.assert_status_ok(f"Running preprocessor module: {module}")
def test_call_invalid_params(self):
for param in ("processor_res", "threshold_a", "threshold_b"):
with self.subTest(param=param):
self.simple_txt2img["alwayson_scripts"]["ControlNet"]["args"] = [
{
"input_image": utils.readImage(
"test/test_files/img2img_basic.png"
),
"model": self.model,
param: -1,
}
]
self.assert_status_ok(f"Run with {param} = -1.")
def test_save_detected_map(self):
for save_map in (True, False):
with self.subTest(save_map=save_map):
self.simple_txt2img["alwayson_scripts"]["ControlNet"]["args"] = [
{
"input_image": utils.readImage(
"test/test_files/img2img_basic.png"
),
"model": self.model,
"module": "depth",
"save_detected_map": save_map,
}
]
resp = requests.post(self.url_txt2img, json=self.simple_txt2img).json()
self.assertEqual(2 if save_map else 1, len(resp["images"]))
def run_test_unit(
self, module: str, model: str, sd_version: StableDiffusionVersion
) -> None:
if self.sd_version != sd_version:
return
self.simple_txt2img["alwayson_scripts"]["ControlNet"]["args"] = [
{
"input_image": utils.readImage("test/test_files/img2img_basic.png"),
"model": utils.get_model(model, sd_version),
"module": module,
}
]
self.assert_status_ok()
def test_ip_adapter_face(self):
self.run_test_unit(
"ip-adapter_clip_sdxl_plus_vith",
"ip-adapter-plus-face_sdxl_vit-h",
StableDiffusionVersion.SDXL,
)
self.run_test_unit(
"ip-adapter_clip_sd15",
"ip-adapter-plus-face_sd15",
StableDiffusionVersion.SD1x,
)
def test_ip_adapter_fullface(self):
self.run_test_unit(
"ip-adapter_clip_sd15",
"ip-adapter-full-face_sd15",
StableDiffusionVersion.SD1x,
)
def test_control_lora(self):
self.run_test_unit("canny", "sai_xl_canny_128lora", StableDiffusionVersion.SDXL)
self.run_test_unit("canny", "control_lora_rank128_v11p_sd15_canny", StableDiffusionVersion.SD1x)
def test_control_lllite(self):
self.run_test_unit(
"canny", "kohya_controllllite_xl_canny", StableDiffusionVersion.SDXL
)
def test_diffusers_controlnet(self):
self.run_test_unit(
"canny", "diffusers_xl_canny_small", StableDiffusionVersion.SDXL
)
def test_t2i_adapter(self):
self.run_test_unit(
"canny", "t2iadapter_canny_sd15v2", StableDiffusionVersion.SD1x
)
self.run_test_unit("canny", "t2i-adapter_xl_canny", StableDiffusionVersion.SDXL)
def test_reference(self):
self.run_test_unit("reference_only", "None", StableDiffusionVersion.SD1x)
self.run_test_unit("reference_only", "None", StableDiffusionVersion.SDXL)
def test_unrecognized_param(self):
unit = self.simple_txt2img["alwayson_scripts"]["ControlNet"]["args"][0]
unit["foo"] = True
unit["is_ui"] = False
self.assert_status_ok()
def test_default_model(self):
# Model "None" should be used when model is not specified in the payload.
self.simple_txt2img["alwayson_scripts"]["ControlNet"]["args"] = [
{
"input_image": utils.readImage("test/test_files/img2img_basic.png"),
"module": "reference_only",
}
]
self.assert_status_ok()
def test_advanced_weighting(self):
unit = self.simple_txt2img["alwayson_scripts"]["ControlNet"]["args"][0]
unit["advanced_weighting"] = [0.75] * self.sd_version.controlnet_layer_num()
self.assert_status_ok()
def test_hr_option(self):
# In non-hr run, hr_option should be ignored.
unit = self.simple_txt2img["alwayson_scripts"]["ControlNet"]["args"][0]
unit["hr_option"] = "High res only"
self.assert_status_ok(expected_image_num=2)
# Hr run.
self.simple_txt2img["enable_hr"] = True
self.assert_status_ok(expected_image_num=3)
if __name__ == "__main__":
unittest.main()
|