Spaces:
Runtime error
Runtime error
File size: 11,463 Bytes
c19ca42 |
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 |
import math
from PIL import Image, ImageChops, ImageDraw
from modules import shared, errors, images
FONT_SIZE=48
def test_processors(image):
from modules.control import processors
if image is None:
shared.log.error('Image not loaded')
return None, None, None
res = []
for processor_id in processors.list_models():
if shared.state.interrupted:
continue
shared.log.info(f'Testing processor: {processor_id}')
processor = processors.Processor(processor_id)
output = image
if processor is None:
shared.log.error(f'Processor load failed: id="{processor_id}"')
processor_id = f'{processor_id} error'
else:
output = processor(image)
if shared.opts.control_unload_processor:
processor.reset()
if output.size != image.size:
output = output.resize(image.size, Image.Resampling.LANCZOS)
if output.mode != image.mode:
output = output.convert(image.mode)
shared.log.debug(f'Testing processor: input={image} mode={image.mode} output={output} mode={output.mode}')
diff = ImageChops.difference(image, output)
if not diff.getbbox():
processor_id = f'{processor_id} null'
draw = ImageDraw.Draw(output)
font = images.get_font(FONT_SIZE)
draw.text((10, 10), processor_id, (0,0,0), font=font)
draw.text((8, 8), processor_id, (255,255,255), font=font)
res.append(output)
yield output, None, None, res
rows = round(math.sqrt(len(res)))
cols = math.ceil(len(res) / rows)
w, h = 256, 256
size = (cols * w + cols, rows * h + rows)
grid = Image.new('RGB', size=size, color='black')
shared.log.info(f'Test processors: images={len(res)} grid={grid}')
for i, image in enumerate(res):
x = (i % cols * w) + (i % cols)
y = (i // cols * h) + (i // cols)
thumb = image.copy().convert('RGB')
thumb.thumbnail((w, h), Image.Resampling.HAMMING)
grid.paste(thumb, box=(x, y))
yield None, grid, None, res
return None, grid, None, res # preview_process, output_image, output_video, output_gallery
def test_controlnets(prompt, negative, image):
from modules import devices, sd_models
from modules.control.units import controlnet
if image is None:
shared.log.error('Image not loaded')
return None, None, None
res = []
for model_id in controlnet.list_models():
if model_id is None:
model_id = 'None'
if shared.state.interrupted:
continue
output = image
if model_id != 'None':
controlnet = controlnet.ControlNet(model_id=model_id, device=devices.device, dtype=devices.dtype)
if controlnet is None:
shared.log.error(f'ControlNet load failed: id="{model_id}"')
continue
shared.log.info(f'Testing ControlNet: {model_id}')
pipe = controlnet.ControlNetPipeline(controlnet=controlnet.model, pipeline=shared.sd_model)
pipe.pipeline.to(device=devices.device, dtype=devices.dtype)
sd_models.set_diffuser_options(pipe)
try:
output = pipe.pipeline(prompt=prompt, negative_prompt=negative, image=image, num_inference_steps=10, output_type='pil')
output = output.images[0]
except Exception as e:
errors.display(e, f'ControlNet {model_id} inference')
model_id = f'{model_id} error'
pipe.restore()
draw = ImageDraw.Draw(output)
font = images.get_font(FONT_SIZE)
draw.text((10, 10), model_id, (0,0,0), font=font)
draw.text((8, 8), model_id, (255,255,255), font=font)
res.append(output)
yield output, None, None, res
rows = round(math.sqrt(len(res)))
cols = math.ceil(len(res) / rows)
w, h = 256, 256
size = (cols * w + cols, rows * h + rows)
grid = Image.new('RGB', size=size, color='black')
shared.log.info(f'Test ControlNets: images={len(res)} grid={grid}')
for i, image in enumerate(res):
x = (i % cols * w) + (i % cols)
y = (i // cols * h) + (i // cols)
thumb = image.copy().convert('RGB')
thumb.thumbnail((w, h), Image.Resampling.HAMMING)
grid.paste(thumb, box=(x, y))
yield None, grid, None, res
return None, grid, None, res # preview_process, output_image, output_video, output_gallery
def test_adapters(prompt, negative, image):
from modules import devices, sd_models
from modules.control.units import t2iadapter
if image is None:
shared.log.error('Image not loaded')
return None, None, None
res = []
for model_id in t2iadapter.list_models():
if model_id is None:
model_id = 'None'
if shared.state.interrupted:
continue
output = image.copy()
if model_id != 'None':
adapter = t2iadapter.Adapter(model_id=model_id, device=devices.device, dtype=devices.dtype)
if adapter is None:
shared.log.error(f'Adapter load failed: id="{model_id}"')
continue
shared.log.info(f'Testing Adapter: {model_id}')
pipe = t2iadapter.AdapterPipeline(adapter=adapter.model, pipeline=shared.sd_model)
pipe.pipeline.to(device=devices.device, dtype=devices.dtype)
sd_models.set_diffuser_options(pipe)
image = image.convert('L') if 'Canny' in model_id or 'Sketch' in model_id else image.convert('RGB')
try:
output = pipe.pipeline(prompt=prompt, negative_prompt=negative, image=image, num_inference_steps=10, output_type='pil')
output = output.images[0]
except Exception as e:
errors.display(e, f'Adapter {model_id} inference')
model_id = f'{model_id} error'
pipe.restore()
draw = ImageDraw.Draw(output)
font = images.get_font(FONT_SIZE)
draw.text((10, 10), model_id, (0,0,0), font=font)
draw.text((8, 8), model_id, (255,255,255), font=font)
res.append(output)
yield output, None, None, res
rows = round(math.sqrt(len(res)))
cols = math.ceil(len(res) / rows)
w, h = 256, 256
size = (cols * w + cols, rows * h + rows)
grid = Image.new('RGB', size=size, color='black')
shared.log.info(f'Test Adapters: images={len(res)} grid={grid}')
for i, image in enumerate(res):
x = (i % cols * w) + (i % cols)
y = (i // cols * h) + (i // cols)
thumb = image.copy().convert('RGB')
thumb.thumbnail((w, h), Image.Resampling.HAMMING)
grid.paste(thumb, box=(x, y))
yield None, grid, None, res
return None, grid, None, res # preview_process, output_image, output_video, output_gallery
def test_xs(prompt, negative, image):
from modules import devices, sd_models
from modules.control.units import xs
if image is None:
shared.log.error('Image not loaded')
return None, None, None
res = []
for model_id in xs.list_models():
if model_id is None:
model_id = 'None'
if shared.state.interrupted:
continue
output = image
if model_id != 'None':
xs = xs.ControlNetXS(model_id=model_id, device=devices.device, dtype=devices.dtype)
if xs is None:
shared.log.error(f'ControlNet-XS load failed: id="{model_id}"')
continue
shared.log.info(f'Testing ControlNet-XS: {model_id}')
pipe = xs.ControlNetXSPipeline(controlnet=xs.model, pipeline=shared.sd_model)
pipe.pipeline.to(device=devices.device, dtype=devices.dtype)
sd_models.set_diffuser_options(pipe)
try:
output = pipe.pipeline(prompt=prompt, negative_prompt=negative, image=image, num_inference_steps=10, output_type='pil')
output = output.images[0]
except Exception as e:
errors.display(e, f'ControlNet-XS {model_id} inference')
model_id = f'{model_id} error'
pipe.restore()
draw = ImageDraw.Draw(output)
font = images.get_font(FONT_SIZE)
draw.text((10, 10), model_id, (0,0,0), font=font)
draw.text((8, 8), model_id, (255,255,255), font=font)
res.append(output)
yield output, None, None, res
rows = round(math.sqrt(len(res)))
cols = math.ceil(len(res) / rows)
w, h = 256, 256
size = (cols * w + cols, rows * h + rows)
grid = Image.new('RGB', size=size, color='black')
shared.log.info(f'Test ControlNet-XS: images={len(res)} grid={grid}')
for i, image in enumerate(res):
x = (i % cols * w) + (i % cols)
y = (i // cols * h) + (i // cols)
thumb = image.copy().convert('RGB')
thumb.thumbnail((w, h), Image.Resampling.HAMMING)
grid.paste(thumb, box=(x, y))
yield None, grid, None, res
return None, grid, None, res # preview_process, output_image, output_video, output_gallery
def test_lite(prompt, negative, image):
from modules import devices, sd_models
from modules.control.units import lite
if image is None:
shared.log.error('Image not loaded')
return None, None, None
res = []
for model_id in lite.list_models():
if model_id is None:
model_id = 'None'
if shared.state.interrupted:
continue
output = image
if model_id != 'None':
lite = lite.ControlLLLite(model_id=model_id, device=devices.device, dtype=devices.dtype)
if lite is None:
shared.log.error(f'Control-LLite load failed: id="{model_id}"')
continue
shared.log.info(f'Testing ControlNet-XS: {model_id}')
pipe = lite.ControlLLitePipeline(pipeline=shared.sd_model)
pipe.apply(controlnet=lite.model, image=image, conditioning=1.0)
pipe.pipeline.to(device=devices.device, dtype=devices.dtype)
sd_models.set_diffuser_options(pipe)
try:
output = pipe.pipeline(prompt=prompt, negative_prompt=negative, image=image, num_inference_steps=10, output_type='pil')
output = output.images[0]
except Exception as e:
errors.display(e, f'ControlNet-XS {model_id} inference')
model_id = f'{model_id} error'
pipe.restore()
draw = ImageDraw.Draw(output)
font = images.get_font(FONT_SIZE)
draw.text((10, 10), model_id, (0,0,0), font=font)
draw.text((8, 8), model_id, (255,255,255), font=font)
res.append(output)
yield output, None, None, res
rows = round(math.sqrt(len(res)))
cols = math.ceil(len(res) / rows)
w, h = 256, 256
size = (cols * w + cols, rows * h + rows)
grid = Image.new('RGB', size=size, color='black')
shared.log.info(f'Test ControlNet-XS: images={len(res)} grid={grid}')
for i, image in enumerate(res):
x = (i % cols * w) + (i % cols)
y = (i // cols * h) + (i // cols)
thumb = image.copy().convert('RGB')
thumb.thumbnail((w, h), Image.Resampling.HAMMING)
grid.paste(thumb, box=(x, y))
yield None, grid, None, res
return None, grid, None, res # preview_process, output_image, output_video, output_gallery
|