-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathworker.py
1578 lines (1377 loc) · 80.7 KB
/
worker.py
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Worker, modify from https://github.com/lllyasviel/Fooocus/blob/main/modules/async_worker.py
"""
import re
import threading
import uuid
import logging
import time
from typing import List
import torch
import ldm_patched.modules.model_management
import modules.patch
import modules.config
from extras.inpaint_mask import SAMOptions, generate_mask_from_image
from fooocusapi.models.common.image_meta import image_parse
from fooocusapi.models.common.task import (
GenerationFinishReason,
ImageGenerationResult, TaskType
)
from fooocusapi.parameters import ImageGenerationParams
from fooocusapi.task_queue import (
QueueTask,
TaskOutputs,
TaskQueue
)
from fooocusapi.utils.file_utils import save_output_file
from fooocusapi.utils.logger import logger
from modules.flags import Performance
from modules.patch import PatchSettings, patch_all, patch_settings
from modules.private_logger import log
from modules.sdxl_styles import fooocus_expansion
from modules.util import erode_or_dilate
import fooocus_version
patch_all()
class AsyncTask:
def __init__(self, args: ImageGenerationParams):
from modules.flags import Performance, MetadataScheme, ip_list, disabled
from modules.util import get_enabled_loras
from modules.config import default_max_lora_number
import args_manager
adp = args.advanced_params
self.yields = []
self.results = []
self.last_stop = False
self.processing = False
self.performance_loras = []
self.generate_image_grid = False
self.prompt = args.prompt
self.negative_prompt = args.negative_prompt
self.style_selections = args.style_selections
self.performance_selection = Performance(args.performance_selection)
self.steps = self.performance_selection.steps()
self.original_steps = self.steps
self.aspect_ratios_selection = args.aspect_ratios_selection
self.image_number = args.image_number
self.output_format = args.save_extension
self.seed = int(args.image_seed)
self.read_wildcards_in_order = args.read_wildcards_in_order
self.sharpness = args.sharpness
self.cfg_scale = args.guidance_scale
self.base_model_name = args.base_model_name
self.refiner_model_name = args.refiner_model_name
self.refiner_switch = args.refiner_switch
self.loras = get_enabled_loras(args.loras)
self.input_image_checkbox = True
self.current_tab = args.current_tab
self.uov_method = args.uov_method
self.uov_input_image = args.uov_input_image
self.upscale_value = args.upscale_value
self.outpaint_selections = args.outpaint_selections
self.inpaint_input_image = args.inpaint_input_image
self.inpaint_additional_prompt = args.inpaint_additional_prompt
self.inpaint_mask_image_upload = args.inpaint_input_image['mask']
self.disable_preview = adp.disable_preview
self.disable_intermediate_results = adp.disable_intermediate_results
self.disable_seed_increment = adp.disable_seed_increment
self.black_out_nsfw = adp.black_out_nsfw
self.adm_scaler_positive = adp.adm_scaler_positive
self.adm_scaler_negative = adp.adm_scaler_negative
self.adm_scaler_end = adp.adm_scaler_end
self.adaptive_cfg = adp.adaptive_cfg
self.clip_skip = adp.clip_skip
self.sampler_name = adp.sampler_name
self.scheduler_name = adp.scheduler_name
self.vae_name = adp.vae_name
self.overwrite_step = adp.overwrite_step
self.overwrite_switch = adp.overwrite_switch
self.overwrite_width = adp.overwrite_width
self.overwrite_height = adp.overwrite_height
self.overwrite_vary_strength = adp.overwrite_vary_strength
self.overwrite_upscale_strength = adp.overwrite_upscale_strength
self.mixing_image_prompt_and_vary_upscale = adp.mixing_image_prompt_and_vary_upscale
self.mixing_image_prompt_and_inpaint = adp.mixing_image_prompt_and_inpaint
self.debugging_cn_preprocessor = adp.debugging_cn_preprocessor
self.skipping_cn_preprocessor = adp.skipping_cn_preprocessor
self.canny_low_threshold = adp.canny_low_threshold
self.canny_high_threshold = adp.canny_high_threshold
self.refiner_swap_method = adp.refiner_swap_method
self.controlnet_softness = adp.controlnet_softness
self.freeu_enabled = adp.freeu_enabled
self.freeu_b1 = adp.freeu_b1
self.freeu_b2 = adp.freeu_b2
self.freeu_s1 = adp.freeu_s1
self.freeu_s2 = adp.freeu_s2
self.debugging_inpaint_preprocessor = adp.debugging_inpaint_preprocessor
self.inpaint_disable_initial_latent = adp.inpaint_disable_initial_latent
self.inpaint_engine = adp.inpaint_engine
self.inpaint_strength = adp.inpaint_strength
self.inpaint_respective_field = adp.inpaint_respective_field
self.inpaint_advanced_masking_checkbox = adp.inpaint_advanced_masking_checkbox
self.invert_mask_checkbox = adp.invert_mask_checkbox
self.inpaint_erode_or_dilate = adp.inpaint_erode_or_dilate
self.save_final_enhanced_image_only = args.save_final_enhanced_image_only if not args_manager.args.disable_image_log else False
self.save_metadata_to_images = args.save_meta
self.metadata_scheme = MetadataScheme(
args.meta_scheme) if not args_manager.args.disable_metadata else MetadataScheme.FOOOCUS
self.cn_tasks = {x: [] for x in ip_list}
for image_prompt in args.image_prompts:
cn_img = image_prompt[0]
cn_stop = image_prompt[1]
cn_weight = image_prompt[2]
cn_type = image_prompt[3]
if cn_img is not None:
self.cn_tasks[cn_type].append([cn_img, cn_stop, cn_weight])
self.debugging_dino = adp.debugging_dino
self.dino_erode_or_dilate = adp.dino_erode_or_dilate
self.debugging_enhance_masks_checkbox = adp.debugging_enhance_masks_checkbox
self.enhance_input_image = args.enhance_input_image
self.enhance_checkbox = args.enhance_checkbox
self.enhance_uov_method = args.enhance_uov_method
self.enhance_uov_processing_order = args.enhance_uov_processing_order
self.enhance_uov_prompt_type = args.enhance_uov_prompt_type
self.enhance_ctrls = []
for enhance in args.enhance_ctrlnets:
enhance_enabled = enhance.enhance_enabled
enhance_mask_dino_prompt_text = enhance.enhance_mask_dino_prompt
enhance_prompt = enhance.enhance_prompt
enhance_negative_prompt = enhance.enhance_negative_prompt
enhance_mask_model = enhance.enhance_mask_model
enhance_mask_cloth_category = enhance.enhance_mask_cloth_category
enhance_mask_sam_model = enhance.enhance_mask_sam_model
enhance_mask_text_threshold = enhance.enhance_mask_text_threshold
enhance_mask_box_threshold = enhance.enhance_mask_box_threshold
enhance_mask_sam_max_detections = enhance.enhance_mask_sam_max_detections
enhance_inpaint_disable_initial_latent = enhance.enhance_inpaint_disable_initial_latent
enhance_inpaint_engine = enhance.enhance_inpaint_engine
enhance_inpaint_strength = enhance.enhance_inpaint_strength
enhance_inpaint_respective_field = enhance.enhance_inpaint_respective_field
enhance_inpaint_erode_or_dilate = enhance.enhance_inpaint_erode_or_dilate
enhance_mask_invert = enhance.enhance_mask_invert
if enhance_enabled:
self.enhance_ctrls.append([
enhance_mask_dino_prompt_text,
enhance_prompt,
enhance_negative_prompt,
enhance_mask_model,
enhance_mask_cloth_category,
enhance_mask_sam_model,
enhance_mask_text_threshold,
enhance_mask_box_threshold,
enhance_mask_sam_max_detections,
enhance_inpaint_disable_initial_latent,
enhance_inpaint_engine,
enhance_inpaint_strength,
enhance_inpaint_respective_field,
enhance_inpaint_erode_or_dilate,
enhance_mask_invert
])
self.should_enhance = self.enhance_checkbox and (self.enhance_uov_method.casefold() != disabled.casefold() or len(self.enhance_ctrls) > 0)
self.images_to_enhance_count = 0
self.enhance_stats = {}
try:
self.outpaint_distance = [
args.outpaint_distance_left,
args.outpaint_distance_top,
args.outpaint_distance_right,
args.outpaint_distance_bottom
]
except IndexError:
self.outpaint_distance = [0, 0, 0, 0]
async_tasks = []
class EarlyReturnException(BaseException):
pass
worker_queue: TaskQueue | None = None
last_model_name = None
def process_stop():
"""Stop process"""
import ldm_patched.modules.model_management
ldm_patched.modules.model_management.interrupt_current_processing()
def task_schedule_loop():
"""Task schedule loop"""
while True:
if len(worker_queue.queue) == 0:
time.sleep(0.05)
continue
current_task = worker_queue.queue[0]
if current_task.start_mills == 0:
process_generate(current_task)
def blocking_get_task_result(job_id: str) -> List[ImageGenerationResult]:
"""
Get task result, when async_task is false
:param job_id:
:return:
"""
waiting_sleep_steps: int = 0
waiting_start_time = time.perf_counter()
while not worker_queue.is_task_finished(job_id):
if waiting_sleep_steps == 0:
logger.std_info(f"[Task Queue] Waiting for task finished, job_id={job_id}")
delay = 0.05
time.sleep(delay)
waiting_sleep_steps += 1
if waiting_sleep_steps % int(10 / delay) == 0:
waiting_time = time.perf_counter() - waiting_start_time
logger.std_info(f"[Task Queue] Already waiting for {round(waiting_time, 1)} seconds, job_id={job_id}")
task = worker_queue.get_task(job_id, True)
return task.task_result
@torch.no_grad()
@torch.inference_mode()
def process_generate(async_job: QueueTask):
"""Generate image"""
try:
import modules.default_pipeline as pipeline
except Exception as e:
logger.std_error(f'[Task Queue] Import default pipeline error: {e}')
if not async_job.is_finished:
worker_queue.finish_task(async_job.job_id)
async_job.set_result(task_result=[], finish_with_error=True, error_message=str(e))
logger.std_error(f"[Task Queue] Finish task with error, seq={async_job.job_id}")
return []
async_task = AsyncTask(async_job.req_param)
global async_tasks
import os
import traceback
import math
import numpy as np
import time
import random
import copy
import modules.core as core
import modules.flags as flags
import modules.patch
import ldm_patched.modules.model_management
import extras.preprocessors as preprocessors
import modules.inpaint_worker as inpaint_worker
import modules.constants as constants
import extras.ip_adapter as ip_adapter
import extras.face_crop
import fooocus_version
from extras.censor import default_censor
from modules.sdxl_styles import apply_style, get_random_style, fooocus_expansion, apply_arrays, random_style_name
from modules.private_logger import log
from extras.expansion import safe_str
from modules.util import (remove_empty_str, HWC3, resize_image, get_image_shape_ceil, set_image_shape_ceil,
get_shape_ceil, resample_image, erode_or_dilate, parse_lora_references_from_prompt,
apply_wildcards)
from modules.upscaler import perform_upscale
from modules.flags import Performance
from modules.meta_parser import get_metadata_parser
pid = os.getpid()
logger.std_info(f"[Task Queue] Start task, job_id={async_job.job_id}, pid={pid}")
outputs = TaskOutputs(async_job)
results = []
async_task = AsyncTask(async_job.req_param)
def progressbar(_, number, text):
"""progress bar"""
logger.std_info(f'[Fooocus] {text}')
outputs.append(['preview', (number, text, None)])
def yield_result(async_task, imgs, progressbar_index, black_out_nsfw, censor=True, do_not_show_finished_images=False):
if not isinstance(imgs, list):
imgs = [imgs]
if censor and (modules.config.default_black_out_nsfw or black_out_nsfw):
progressbar(async_task, progressbar_index, 'Checking for NSFW content ...')
imgs = default_censor(imgs)
async_task.results = async_task.results + imgs
if do_not_show_finished_images:
return
async_task.yields.append(['results', async_task.results])
return
def return_result(_, tasks):
"""
Yield result
:param _: async task object
:param tasks: the image was generated one by one, when image number is not one, it will be a task list
:return:
"""
extension = async_job.req_param.save_extension
for ind, im in enumerate(async_task.results):
if modules.config.temp_path in im:
continue
if async_job.req_param.save_name == '':
image_name = f"{async_job.job_id}-{str(ind)}"
else:
image_name = f"{async_job.req_param.save_name}-{str(ind)}"
try:
img_seed = tasks[ind]['task_seed']
except Exception:
img_seed = async_task.seed
img_filename = save_output_file(
img=im,
image_name=image_name,
extension=extension)
results.append(ImageGenerationResult(
im=img_filename,
seed=str(img_seed),
finish_reason=GenerationFinishReason.success))
async_job.set_result(results, False)
worker_queue.finish_task(async_job.job_id)
logger.std_info(f"[Task Queue] Finish task, job_id={async_job.job_id}")
outputs.append(['results', async_task.results])
pipeline.prepare_text_encoder(async_call=True)
def process_task(all_steps, async_task, callback, controlnet_canny_path, controlnet_cpds_path, current_task_id,
denoising_strength, final_scheduler_name, goals, initial_latent, steps, switch, positive_cond,
negative_cond, task, loras, tiled, use_expansion, width, height, base_progress, preparation_steps,
total_count, show_intermediate_results, persist_image=True):
if async_task.last_stop is not False:
ldm_patched.modules.model_management.interrupt_current_processing()
if 'cn' in goals:
for cn_flag, cn_path in [
(flags.cn_canny, controlnet_canny_path),
(flags.cn_cpds, controlnet_cpds_path)
]:
for cn_img, cn_stop, cn_weight in async_task.cn_tasks[cn_flag]:
positive_cond, negative_cond = core.apply_controlnet(
positive_cond, negative_cond,
pipeline.loaded_ControlNets[cn_path], cn_img, cn_weight, 0, cn_stop)
imgs = pipeline.process_diffusion(
positive_cond=positive_cond,
negative_cond=negative_cond,
steps=steps,
switch=switch,
width=width,
height=height,
image_seed=task['task_seed'],
callback=callback,
sampler_name=async_task.sampler_name,
scheduler_name=final_scheduler_name,
latent=initial_latent,
denoise=denoising_strength,
tiled=tiled,
cfg_scale=async_task.cfg_scale,
refiner_swap_method=async_task.refiner_swap_method,
disable_preview=async_task.disable_preview
)
del positive_cond, negative_cond # Save memory
if inpaint_worker.current_task is not None:
imgs = [inpaint_worker.current_task.post_process(x) for x in imgs]
current_progress = int(base_progress + (100 - preparation_steps) / float(all_steps) * steps)
if modules.config.default_black_out_nsfw or async_task.black_out_nsfw:
progressbar(async_task, current_progress, 'Checking for NSFW content ...')
imgs = default_censor(imgs)
progressbar(async_task, current_progress, f'Saving image {current_task_id + 1}/{total_count} to system ...')
img_paths = save_and_log(async_task, height, imgs, task, use_expansion, width, loras, persist_image)
yield_result(async_task, img_paths, current_progress, async_task.black_out_nsfw, False,
do_not_show_finished_images=not show_intermediate_results or async_task.disable_intermediate_results)
return imgs, img_paths, current_progress
def apply_patch_settings(async_task):
patch_settings[pid] = PatchSettings(
async_task.sharpness,
async_task.adm_scaler_end,
async_task.adm_scaler_positive,
async_task.adm_scaler_negative,
async_task.controlnet_softness,
async_task.adaptive_cfg
)
def save_and_log(async_task, height, imgs, task, use_expansion, width, loras, persist_image=True) -> list:
img_paths = []
for x in imgs:
d = [('Prompt', 'prompt', task['log_positive_prompt']),
('Negative Prompt', 'negative_prompt', task['log_negative_prompt']),
('Fooocus V2 Expansion', 'prompt_expansion', task['expansion']),
('Styles', 'styles',
str(task['styles'] if not use_expansion else [fooocus_expansion] + task['styles'])),
('Performance', 'performance', async_task.performance_selection.value),
('Steps', 'steps', async_task.steps),
('Resolution', 'resolution', str((width, height))),
('Guidance Scale', 'guidance_scale', async_task.cfg_scale),
('Sharpness', 'sharpness', async_task.sharpness),
('ADM Guidance', 'adm_guidance', str((
modules.patch.patch_settings[pid].positive_adm_scale,
modules.patch.patch_settings[pid].negative_adm_scale,
modules.patch.patch_settings[pid].adm_scaler_end))),
('Base Model', 'base_model', async_task.base_model_name),
('Refiner Model', 'refiner_model', async_task.refiner_model_name),
('Refiner Switch', 'refiner_switch', async_task.refiner_switch)]
if async_task.refiner_model_name != 'None':
if async_task.overwrite_switch > 0:
d.append(('Overwrite Switch', 'overwrite_switch', async_task.overwrite_switch))
if async_task.refiner_swap_method != flags.refiner_swap_method:
d.append(('Refiner Swap Method', 'refiner_swap_method', async_task.refiner_swap_method))
if modules.patch.patch_settings[pid].adaptive_cfg != modules.config.default_cfg_tsnr:
d.append(
('CFG Mimicking from TSNR', 'adaptive_cfg', modules.patch.patch_settings[pid].adaptive_cfg))
if async_task.clip_skip > 1:
d.append(('CLIP Skip', 'clip_skip', async_task.clip_skip))
d.append(('Sampler', 'sampler', async_task.sampler_name))
d.append(('Scheduler', 'scheduler', async_task.scheduler_name))
d.append(('VAE', 'vae', async_task.vae_name))
d.append(('Seed', 'seed', str(task['task_seed'])))
if async_task.freeu_enabled:
d.append(('FreeU', 'freeu',
str((async_task.freeu_b1, async_task.freeu_b2, async_task.freeu_s1, async_task.freeu_s2))))
for li, (n, w) in enumerate(loras):
if n != 'None':
d.append((f'LoRA {li + 1}', f'lora_combined_{li + 1}', f'{n} : {w}'))
metadata_parser = None
if async_task.save_metadata_to_images:
metadata_parser = modules.meta_parser.get_metadata_parser(async_task.metadata_scheme)
metadata_parser.set_data(task['log_positive_prompt'], task['positive'],
task['log_negative_prompt'], task['negative'],
async_task.steps, async_task.base_model_name, async_task.refiner_model_name,
loras, async_task.vae_name)
d.append(('Metadata Scheme', 'metadata_scheme',
async_task.metadata_scheme.value if async_task.save_metadata_to_images else async_task.save_metadata_to_images))
d.append(('Version', 'version', 'Fooocus v' + fooocus_version.version))
img_paths.append(log(x, d, metadata_parser, async_task.output_format, task, persist_image))
return img_paths
def apply_control_nets(async_task, height, ip_adapter_face_path, ip_adapter_path, width, current_progress):
for task in async_task.cn_tasks[flags.cn_canny]:
cn_img, cn_stop, cn_weight = task
cn_img = resize_image(HWC3(cn_img), width=width, height=height)
if not async_task.skipping_cn_preprocessor:
cn_img = preprocessors.canny_pyramid(cn_img, async_task.canny_low_threshold,
async_task.canny_high_threshold)
cn_img = HWC3(cn_img)
task[0] = core.numpy_to_pytorch(cn_img)
if async_task.debugging_cn_preprocessor:
yield_result(async_task, cn_img, current_progress, async_task.black_out_nsfw, do_not_show_finished_images=True)
for task in async_task.cn_tasks[flags.cn_cpds]:
cn_img, cn_stop, cn_weight = task
cn_img = resize_image(HWC3(cn_img), width=width, height=height)
if not async_task.skipping_cn_preprocessor:
cn_img = preprocessors.cpds(cn_img)
cn_img = HWC3(cn_img)
task[0] = core.numpy_to_pytorch(cn_img)
if async_task.debugging_cn_preprocessor:
yield_result(async_task, cn_img, current_progress, async_task.black_out_nsfw, do_not_show_finished_images=True)
for task in async_task.cn_tasks[flags.cn_ip]:
cn_img, cn_stop, cn_weight = task
cn_img = HWC3(cn_img)
# https://github.com/tencent-ailab/IP-Adapter/blob/d580c50a291566bbf9fc7ac0f760506607297e6d/README.md?plain=1#L75
cn_img = resize_image(cn_img, width=224, height=224, resize_mode=0)
task[0] = ip_adapter.preprocess(cn_img, ip_adapter_path=ip_adapter_path)
if async_task.debugging_cn_preprocessor:
yield_result(async_task, cn_img, current_progress, async_task.black_out_nsfw, do_not_show_finished_images=True)
for task in async_task.cn_tasks[flags.cn_ip_face]:
cn_img, cn_stop, cn_weight = task
cn_img = HWC3(cn_img)
if not async_task.skipping_cn_preprocessor:
cn_img = extras.face_crop.crop_image(cn_img)
# https://github.com/tencent-ailab/IP-Adapter/blob/d580c50a291566bbf9fc7ac0f760506607297e6d/README.md?plain=1#L75
cn_img = resize_image(cn_img, width=224, height=224, resize_mode=0)
task[0] = ip_adapter.preprocess(cn_img, ip_adapter_path=ip_adapter_face_path)
if async_task.debugging_cn_preprocessor:
yield_result(async_task, cn_img, current_progress, async_task.black_out_nsfw, do_not_show_finished_images=True)
all_ip_tasks = async_task.cn_tasks[flags.cn_ip] + async_task.cn_tasks[flags.cn_ip_face]
if len(all_ip_tasks) > 0:
pipeline.final_unet = ip_adapter.patch_model(pipeline.final_unet, all_ip_tasks)
def apply_vary(async_task, uov_method, denoising_strength, uov_input_image, switch, current_progress, advance_progress=False):
if 'subtle' in uov_method:
denoising_strength = 0.5
if 'strong' in uov_method:
denoising_strength = 0.85
if async_task.overwrite_vary_strength > 0:
denoising_strength = async_task.overwrite_vary_strength
shape_ceil = get_image_shape_ceil(uov_input_image)
if shape_ceil < 1024:
print(f'[Vary] Image is resized because it is too small.')
shape_ceil = 1024
elif shape_ceil > 2048:
print(f'[Vary] Image is resized because it is too big.')
shape_ceil = 2048
uov_input_image = set_image_shape_ceil(uov_input_image, shape_ceil)
initial_pixels = core.numpy_to_pytorch(uov_input_image)
if advance_progress:
current_progress += 1
progressbar(async_task, current_progress, 'VAE encoding ...')
candidate_vae, _ = pipeline.get_candidate_vae(
steps=async_task.steps,
switch=switch,
denoise=denoising_strength,
refiner_swap_method=async_task.refiner_swap_method
)
initial_latent = core.encode_vae(vae=candidate_vae, pixels=initial_pixels)
B, C, H, W = initial_latent['samples'].shape
width = W * 8
height = H * 8
print(f'Final resolution is {str((width, height))}.')
return uov_input_image, denoising_strength, initial_latent, width, height, current_progress
def apply_inpaint(async_task, initial_latent, inpaint_head_model_path, inpaint_image,
inpaint_mask, inpaint_parameterized, denoising_strength, inpaint_respective_field, switch,
inpaint_disable_initial_latent, current_progress, skip_apply_outpaint=False,
advance_progress=False):
if not skip_apply_outpaint:
inpaint_image, inpaint_mask = apply_outpaint(async_task, inpaint_image, inpaint_mask)
inpaint_worker.current_task = inpaint_worker.InpaintWorker(
image=inpaint_image,
mask=inpaint_mask,
use_fill=denoising_strength > 0.99,
k=inpaint_respective_field
)
if async_task.debugging_inpaint_preprocessor:
yield_result(async_task, inpaint_worker.current_task.visualize_mask_processing(), 100,
async_task.black_out_nsfw, do_not_show_finished_images=True)
raise EarlyReturnException
if advance_progress:
current_progress += 1
progressbar(async_task, current_progress, 'VAE Inpaint encoding ...')
inpaint_pixel_fill = core.numpy_to_pytorch(inpaint_worker.current_task.interested_fill)
inpaint_pixel_image = core.numpy_to_pytorch(inpaint_worker.current_task.interested_image)
inpaint_pixel_mask = core.numpy_to_pytorch(inpaint_worker.current_task.interested_mask)
candidate_vae, candidate_vae_swap = pipeline.get_candidate_vae(
steps=async_task.steps,
switch=switch,
denoise=denoising_strength,
refiner_swap_method=async_task.refiner_swap_method
)
latent_inpaint, latent_mask = core.encode_vae_inpaint(
mask=inpaint_pixel_mask,
vae=candidate_vae,
pixels=inpaint_pixel_image)
latent_swap = None
if candidate_vae_swap is not None:
if advance_progress:
current_progress += 1
progressbar(async_task, current_progress, 'VAE SD15 encoding ...')
latent_swap = core.encode_vae(
vae=candidate_vae_swap,
pixels=inpaint_pixel_fill)['samples']
if advance_progress:
current_progress += 1
progressbar(async_task, current_progress, 'VAE encoding ...')
latent_fill = core.encode_vae(
vae=candidate_vae,
pixels=inpaint_pixel_fill)['samples']
inpaint_worker.current_task.load_latent(
latent_fill=latent_fill, latent_mask=latent_mask, latent_swap=latent_swap)
if inpaint_parameterized:
pipeline.final_unet = inpaint_worker.current_task.patch(
inpaint_head_model_path=inpaint_head_model_path,
inpaint_latent=latent_inpaint,
inpaint_latent_mask=latent_mask,
model=pipeline.final_unet
)
if not inpaint_disable_initial_latent:
initial_latent = {'samples': latent_fill}
B, C, H, W = latent_fill.shape
height, width = H * 8, W * 8
final_height, final_width = inpaint_worker.current_task.image.shape[:2]
print(f'Final resolution is {str((final_width, final_height))}, latent is {str((width, height))}.')
return denoising_strength, initial_latent, width, height, current_progress
def apply_outpaint(async_task, inpaint_image, inpaint_mask):
try:
dt_left, dt_top, dt_right, dt_bottom = async_task.outpaint_distance[:4]
except Exception:
dt_left, dt_top, dt_right, dt_bottom = 0, 0, 0, 0
if len(async_task.outpaint_selections) > 0:
H, W, C = inpaint_image.shape
if 'top' in async_task.outpaint_selections:
distance_top = int(H * 0.3)
if dt_top > 0:
distance_top = dt_top
inpaint_image = np.pad(inpaint_image, [[distance_top, 0], [0, 0], [0, 0]], mode='edge')
inpaint_mask = np.pad(inpaint_mask, [[distance_top, 0], [0, 0]], mode='constant',
constant_values=255)
if 'bottom' in async_task.outpaint_selections:
distance_bottom = int(H * 0.3)
if dt_bottom > 0:
distance_bottom = dt_bottom
inpaint_image = np.pad(inpaint_image, [[0, distance_bottom], [0, 0], [0, 0]], mode='edge')
inpaint_mask = np.pad(inpaint_mask, [[0, distance_bottom], [0, 0]], mode='constant',
constant_values=255)
H, W, C = inpaint_image.shape
if 'left' in async_task.outpaint_selections:
distance_left = int(W * 0.3)
if dt_left > 0:
distance_left = dt_left
inpaint_image = np.pad(inpaint_image, [[0, 0], [distance_left, 0], [0, 0]], mode='edge')
inpaint_mask = np.pad(inpaint_mask, [[0, 0], [distance_left, 0]], mode='constant',
constant_values=255)
if 'right' in async_task.outpaint_selections:
distance_right = int(W * 0.3)
if dt_right > 0:
distance_right = dt_right
inpaint_image = np.pad(inpaint_image, [[0, 0], [0, distance_right], [0, 0]], mode='edge')
inpaint_mask = np.pad(inpaint_mask, [[0, 0], [0, distance_right]], mode='constant',
constant_values=255)
inpaint_image = np.ascontiguousarray(inpaint_image.copy())
inpaint_mask = np.ascontiguousarray(inpaint_mask.copy())
async_task.inpaint_strength = 1.0
async_task.inpaint_respective_field = 1.0
return inpaint_image, inpaint_mask
def apply_upscale(async_task, uov_input_image, uov_method, switch, current_progress, advance_progress=False):
H, W, C = uov_input_image.shape
if advance_progress:
current_progress += 1
progressbar(async_task, current_progress, f'Upscaling image from {str((W, H))} ...')
uov_input_image = perform_upscale(uov_input_image)
print(f'Image upscaled.')
if '1.5x' in uov_method:
f = 1.5
elif '2x' in uov_method:
f = 2.0
elif uov_method == 'Upscale (Custom)'.lower() and async_task.upscale_value > 0 and async_task.upscale_value < 5:
f = async_task.upscale_value
else:
f = 2.0
shape_ceil = get_shape_ceil(H * f, W * f)
if shape_ceil < 1024:
print(f'[Upscale] Image is resized because it is too small.')
uov_input_image = set_image_shape_ceil(uov_input_image, 1024)
shape_ceil = 1024
else:
uov_input_image = resample_image(uov_input_image, width=W * f, height=H * f)
image_is_super_large = shape_ceil > 2800
if 'fast' in uov_method:
direct_return = True
elif image_is_super_large:
print('Image is too large. Directly returned the SR image. '
'Usually directly return SR image at 4K resolution '
'yields better results than SDXL diffusion.')
direct_return = True
else:
direct_return = False
if direct_return:
return direct_return, uov_input_image, None, None, None, None, None, current_progress
tiled = True
denoising_strength = 0.382
if async_task.overwrite_upscale_strength > 0:
denoising_strength = async_task.overwrite_upscale_strength
initial_pixels = core.numpy_to_pytorch(uov_input_image)
if advance_progress:
current_progress += 1
progressbar(async_task, current_progress, 'VAE encoding ...')
candidate_vae, _ = pipeline.get_candidate_vae(
steps=async_task.steps,
switch=switch,
denoise=denoising_strength,
refiner_swap_method=async_task.refiner_swap_method
)
initial_latent = core.encode_vae(
vae=candidate_vae,
pixels=initial_pixels, tiled=True)
B, C, H, W = initial_latent['samples'].shape
width = W * 8
height = H * 8
print(f'Final resolution is {str((width, height))}.')
return direct_return, uov_input_image, denoising_strength, initial_latent, tiled, width, height, current_progress
def apply_overrides(async_task, steps, height, width):
if async_task.overwrite_step > 0:
steps = async_task.overwrite_step
switch = int(round(async_task.steps * async_task.refiner_switch))
if async_task.overwrite_switch > 0:
switch = async_task.overwrite_switch
if async_task.overwrite_width > 0:
width = async_task.overwrite_width
if async_task.overwrite_height > 0:
height = async_task.overwrite_height
return steps, switch, width, height
def process_prompt(async_task, prompt, negative_prompt, base_model_additional_loras, image_number, disable_seed_increment, use_expansion, use_style,
use_synthetic_refiner, current_progress, advance_progress=False):
prompts = remove_empty_str([safe_str(p) for p in prompt.splitlines()], default='')
negative_prompts = remove_empty_str([safe_str(p) for p in negative_prompt.splitlines()], default='')
prompt = prompts[0]
negative_prompt = negative_prompts[0]
if prompt == '':
# disable expansion when empty since it is not meaningful and influences image prompt
use_expansion = False
extra_positive_prompts = prompts[1:] if len(prompts) > 1 else []
extra_negative_prompts = negative_prompts[1:] if len(negative_prompts) > 1 else []
if advance_progress:
current_progress += 1
progressbar(async_task, current_progress, 'Loading models ...')
lora_filenames = modules.util.remove_performance_lora(modules.config.lora_filenames,
async_task.performance_selection)
loras, prompt = parse_lora_references_from_prompt(prompt, async_task.loras,
modules.config.default_max_lora_number,
lora_filenames=lora_filenames)
loras += async_task.performance_loras
pipeline.refresh_everything(refiner_model_name=async_task.refiner_model_name,
base_model_name=async_task.base_model_name,
loras=loras, base_model_additional_loras=base_model_additional_loras,
use_synthetic_refiner=use_synthetic_refiner, vae_name=async_task.vae_name)
pipeline.set_clip_skip(async_task.clip_skip)
if advance_progress:
current_progress += 1
progressbar(async_task, current_progress, 'Processing prompts ...')
tasks = []
for i in range(image_number):
if disable_seed_increment:
task_seed = async_task.seed % (constants.MAX_SEED + 1)
else:
task_seed = (async_task.seed + i) % (constants.MAX_SEED + 1) # randint is inclusive, % is not
task_rng = random.Random(task_seed) # may bind to inpaint noise in the future
task_prompt = apply_wildcards(prompt, task_rng, i, async_task.read_wildcards_in_order)
task_prompt = apply_arrays(task_prompt, i)
task_negative_prompt = apply_wildcards(negative_prompt, task_rng, i, async_task.read_wildcards_in_order)
task_extra_positive_prompts = [apply_wildcards(pmt, task_rng, i, async_task.read_wildcards_in_order) for pmt
in
extra_positive_prompts]
task_extra_negative_prompts = [apply_wildcards(pmt, task_rng, i, async_task.read_wildcards_in_order) for pmt
in
extra_negative_prompts]
positive_basic_workloads = []
negative_basic_workloads = []
task_styles = async_task.style_selections.copy()
if use_style:
placeholder_replaced = False
for j, s in enumerate(task_styles):
if s == random_style_name:
s = get_random_style(task_rng)
task_styles[j] = s
p, n, style_has_placeholder = apply_style(s, positive=task_prompt)
if style_has_placeholder:
placeholder_replaced = True
positive_basic_workloads = positive_basic_workloads + p
negative_basic_workloads = negative_basic_workloads + n
if not placeholder_replaced:
positive_basic_workloads = [task_prompt] + positive_basic_workloads
else:
positive_basic_workloads.append(task_prompt)
negative_basic_workloads.append(task_negative_prompt) # Always use independent workload for negative.
positive_basic_workloads = positive_basic_workloads + task_extra_positive_prompts
negative_basic_workloads = negative_basic_workloads + task_extra_negative_prompts
positive_basic_workloads = remove_empty_str(positive_basic_workloads, default=task_prompt)
negative_basic_workloads = remove_empty_str(negative_basic_workloads, default=task_negative_prompt)
tasks.append(dict(
task_seed=task_seed,
task_prompt=task_prompt,
task_negative_prompt=task_negative_prompt,
positive=positive_basic_workloads,
negative=negative_basic_workloads,
expansion='',
c=None,
uc=None,
positive_top_k=len(positive_basic_workloads),
negative_top_k=len(negative_basic_workloads),
log_positive_prompt='\n'.join([task_prompt] + task_extra_positive_prompts),
log_negative_prompt='\n'.join([task_negative_prompt] + task_extra_negative_prompts),
styles=task_styles
))
if use_expansion:
if advance_progress:
current_progress += 1
for i, t in enumerate(tasks):
progressbar(async_task, current_progress, f'Preparing Fooocus text #{i + 1} ...')
expansion = pipeline.final_expansion(t['task_prompt'], t['task_seed'])
print(f'[Prompt Expansion] {expansion}')
t['expansion'] = expansion
t['positive'] = copy.deepcopy(t['positive']) + [expansion] # Deep copy.
if advance_progress:
current_progress += 1
for i, t in enumerate(tasks):
progressbar(async_task, current_progress, f'Encoding positive #{i + 1} ...')
t['c'] = pipeline.clip_encode(texts=t['positive'], pool_top_k=t['positive_top_k'])
if advance_progress:
current_progress += 1
for i, t in enumerate(tasks):
if abs(float(async_task.cfg_scale) - 1.0) < 1e-4:
t['uc'] = pipeline.clone_cond(t['c'])
else:
progressbar(async_task, current_progress, f'Encoding negative #{i + 1} ...')
t['uc'] = pipeline.clip_encode(texts=t['negative'], pool_top_k=t['negative_top_k'])
return tasks, use_expansion, loras, current_progress
def apply_freeu(async_task):
print(f'FreeU is enabled!')
pipeline.final_unet = core.apply_freeu(
pipeline.final_unet,
async_task.freeu_b1,
async_task.freeu_b2,
async_task.freeu_s1,
async_task.freeu_s2
)
def patch_discrete(unet, scheduler_name):
return core.opModelSamplingDiscrete.patch(unet, scheduler_name, False)[0]
def patch_edm(unet, scheduler_name):
return core.opModelSamplingContinuousEDM.patch(unet, scheduler_name, 120.0, 0.002)[0]
def patch_samplers(async_task):
final_scheduler_name = async_task.scheduler_name
if async_task.scheduler_name in ['lcm', 'tcd']:
final_scheduler_name = 'sgm_uniform'
if pipeline.final_unet is not None:
pipeline.final_unet = patch_discrete(pipeline.final_unet, async_task.scheduler_name)
if pipeline.final_refiner_unet is not None:
pipeline.final_refiner_unet = patch_discrete(pipeline.final_refiner_unet, async_task.scheduler_name)
elif async_task.scheduler_name == 'edm_playground_v2.5':
final_scheduler_name = 'karras'
if pipeline.final_unet is not None:
pipeline.final_unet = patch_edm(pipeline.final_unet, async_task.scheduler_name)
if pipeline.final_refiner_unet is not None:
pipeline.final_refiner_unet = patch_edm(pipeline.final_refiner_unet, async_task.scheduler_name)
return final_scheduler_name
def set_hyper_sd_defaults(async_task, current_progress, advance_progress=False):
print('Enter Hyper-SD mode.')
if advance_progress:
current_progress += 1
progressbar(async_task, current_progress, 'Downloading Hyper-SD components ...')
async_task.performance_loras += [(modules.config.downloading_sdxl_hyper_sd_lora(), 0.8)]
if async_task.refiner_model_name != 'None':
print(f'Refiner disabled in Hyper-SD mode.')
async_task.refiner_model_name = 'None'
async_task.sampler_name = 'dpmpp_sde_gpu'
async_task.scheduler_name = 'karras'
async_task.sharpness = 0.0
async_task.cfg_scale = 1.0
async_task.adaptive_cfg = 1.0
async_task.refiner_switch = 1.0
async_task.adm_scaler_positive = 1.0
async_task.adm_scaler_negative = 1.0
async_task.adm_scaler_end = 0.0
return current_progress
def set_lightning_defaults(async_task, current_progress, advance_progress=False):
print('Enter Lightning mode.')
if advance_progress:
current_progress += 1
progressbar(async_task, 1, 'Downloading Lightning components ...')
async_task.performance_loras += [(modules.config.downloading_sdxl_lightning_lora(), 1.0)]
if async_task.refiner_model_name != 'None':
print(f'Refiner disabled in Lightning mode.')
async_task.refiner_model_name = 'None'
async_task.sampler_name = 'euler'
async_task.scheduler_name = 'sgm_uniform'
async_task.sharpness = 0.0
async_task.cfg_scale = 1.0
async_task.adaptive_cfg = 1.0
async_task.refiner_switch = 1.0
async_task.adm_scaler_positive = 1.0
async_task.adm_scaler_negative = 1.0
async_task.adm_scaler_end = 0.0
return current_progress
def set_lcm_defaults(async_task, current_progress, advance_progress=False):
print('Enter LCM mode.')
if advance_progress:
current_progress += 1
progressbar(async_task, 1, 'Downloading LCM components ...')
async_task.performance_loras += [(modules.config.downloading_sdxl_lcm_lora(), 1.0)]
if async_task.refiner_model_name != 'None':
print(f'Refiner disabled in LCM mode.')
async_task.refiner_model_name = 'None'
async_task.sampler_name = 'lcm'
async_task.scheduler_name = 'lcm'
async_task.sharpness = 0.0
async_task.cfg_scale = 1.0
async_task.adaptive_cfg = 1.0
async_task.refiner_switch = 1.0
async_task.adm_scaler_positive = 1.0
async_task.adm_scaler_negative = 1.0
async_task.adm_scaler_end = 0.0
return current_progress
def apply_image_input(async_task, base_model_additional_loras, clip_vision_path, controlnet_canny_path,
controlnet_cpds_path, goals, inpaint_head_model_path, inpaint_image, inpaint_mask,
inpaint_parameterized, ip_adapter_face_path, ip_adapter_path, ip_negative_path,
skip_prompt_processing, use_synthetic_refiner):
if (async_task.current_tab == 'uov' or (
async_task.current_tab == 'ip' and async_task.mixing_image_prompt_and_vary_upscale)) \
and async_task.uov_method != flags.disabled.casefold() and async_task.uov_input_image is not None:
async_task.uov_input_image, skip_prompt_processing, async_task.steps = prepare_upscale(
async_task, goals, async_task.uov_input_image, async_task.uov_method, async_task.performance_selection,
async_task.steps, 1, skip_prompt_processing=skip_prompt_processing)
if (async_task.current_tab == 'inpaint' or (
async_task.current_tab == 'ip' and async_task.mixing_image_prompt_and_inpaint)) \
and isinstance(async_task.inpaint_input_image, dict):
inpaint_image = async_task.inpaint_input_image['image']
inpaint_mask = async_task.inpaint_input_image['mask'][:, :, 0]
if async_task.inpaint_advanced_masking_checkbox:
if isinstance(async_task.inpaint_mask_image_upload, dict):
if (isinstance(async_task.inpaint_mask_image_upload['image'], np.ndarray)
and isinstance(async_task.inpaint_mask_image_upload['mask'], np.ndarray)
and async_task.inpaint_mask_image_upload['image'].ndim == 3):
async_task.inpaint_mask_image_upload = np.maximum(
async_task.inpaint_mask_image_upload['image'],
async_task.inpaint_mask_image_upload['mask'])
if isinstance(async_task.inpaint_mask_image_upload,
np.ndarray) and async_task.inpaint_mask_image_upload.ndim == 3:
H, W, C = inpaint_image.shape
async_task.inpaint_mask_image_upload = resample_image(async_task.inpaint_mask_image_upload,
width=W, height=H)
async_task.inpaint_mask_image_upload = np.mean(async_task.inpaint_mask_image_upload, axis=2)
async_task.inpaint_mask_image_upload = (async_task.inpaint_mask_image_upload > 127).astype(
np.uint8) * 255
inpaint_mask = np.maximum(inpaint_mask, async_task.inpaint_mask_image_upload)
if int(async_task.inpaint_erode_or_dilate) != 0:
inpaint_mask = erode_or_dilate(inpaint_mask, async_task.inpaint_erode_or_dilate)
if async_task.invert_mask_checkbox:
inpaint_mask = 255 - inpaint_mask
inpaint_image = HWC3(inpaint_image)
if isinstance(inpaint_image, np.ndarray) and isinstance(inpaint_mask, np.ndarray) \
and (np.any(inpaint_mask > 127) or len(async_task.outpaint_selections) > 0):