-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtxt2img2img2img.py
135 lines (126 loc) · 7 KB
/
txt2img2img2img.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
from modules.shared import opts, cmd_opts, state
from modules.processing import Processed, StableDiffusionProcessingImg2Img, process_images, images
from PIL import Image
import gradio as gr
import modules.scripts as scripts
from modules import sd_samplers
from random import randint
from skimage.util import random_noise
import numpy as np
class Script(scripts.Script):
def title(self):
return "Txt2img2img2img"
def ui(self, is_img2img):
if is_img2img: return
img2img_samplers_names = [s.name for s in sd_samplers.samplers_for_img2img]
t2iii_reprocess = gr.Slider(minimum=1, maximum=128, step=1, label='Number of img2img ', value=1)
t2iii_steps = gr.Slider(minimum=1, maximum=120, step=1, label='img2img steps ', value=6)
t2iii_cfg_scale = gr.Slider(minimum=1, maximum=30, step=0.1, label='img2img cfg scale ', value=7.6)
t2iii_seed_shift = gr.Slider(minimum=-1, maximum=1000000, step=1, label='img2img new seed+ (-1 for random)', value=-1)
t2iii_denoising_strength = gr.Slider(minimum=0, maximum=1, step=0.01, label='img2img denoising strength ', value=0.4)
with gr.Row():
t2iii_save_first = gr.Checkbox(label='Save first image', value=False)
t2iii_only_last = gr.Checkbox(label='Only save last img2img', value=True)
t2iii_face_correction = gr.Checkbox(label='Face correction on all', value=False)
t2iii_face_correction_last = gr.Checkbox(label='Face correction on last', value=True)
t2iii_sampler = gr.Dropdown(label="Sampler", choices=img2img_samplers_names, value="DDIM")
t2iii_clip = gr.Slider(minimum=0, maximum=12, step=1, label='change clip for img2img (0 = disabled)', value=0)
t2iii_noise = gr.Slider(minimum=0, maximum=0.05, step=0.001, label='Add noise before img2img ', value=0)
t2iii_upscale_x = gr.Slider(minimum=64, maximum=2048, step=64, label='img2img width (64 = no rescale)', value=64)
t2iii_upscale_y = gr.Slider(minimum=64, maximum=2048, step=64, label='img2img height (64 = no rescale)', value=64)
return [t2iii_reprocess,t2iii_steps,t2iii_cfg_scale,t2iii_seed_shift,t2iii_denoising_strength,t2iii_save_first,t2iii_only_last,t2iii_face_correction,t2iii_face_correction_last,t2iii_sampler,t2iii_clip,t2iii_noise,t2iii_upscale_x,t2iii_upscale_y]
def run(self,p,t2iii_reprocess,t2iii_steps,t2iii_cfg_scale,t2iii_seed_shift,t2iii_denoising_strength,t2iii_save_first,t2iii_only_last,t2iii_face_correction,t2iii_face_correction_last,t2iii_sampler,t2iii_clip,t2iii_noise,t2iii_upscale_x,t2iii_upscale_y):
def add_noise_to_image(img,seed,t2iii_noise):
img = np.array(img)
img = random_noise(img, mode='gaussian', seed=proc.seed, clip=True, var=t2iii_noise)
img = np.array(255*img, dtype = 'uint8')
img = Image.fromarray(np.array(img))
return img
img2img_samplers_names = [s.name for s in sd_samplers.samplers_for_img2img]
img2img_sampler_index = [i for i in range(len(img2img_samplers_names)) if img2img_samplers_names[i] == t2iii_sampler][0]
if p.seed == -1: p.seed = randint(0,1000000000)
initial_CLIP = opts.data["CLIP_stop_at_last_layers"]
p.do_not_save_samples = not t2iii_save_first
if t2iii_upscale_x > 64:
upscale_x = t2iii_upscale_x
else:
upscale_x = p.width
if t2iii_upscale_y > 64:
upscale_y = t2iii_upscale_y
else:
upscale_y = p.height
n_iter=p.n_iter
for j in range(n_iter):
p.n_iter=1
if t2iii_clip > 0:
opts.data["CLIP_stop_at_last_layers"] = initial_CLIP
proc = process_images(p)
basename = ""
extra_gen_parms = {
'Initial steps':p.steps,
'Initial CFG scale':p.cfg_scale,
"Initial seed": p.seed,
'Initial sampler': p.sampler_name,
'Reprocess amount':t2iii_reprocess
}
for i in range(t2iii_reprocess):
if t2iii_seed_shift == -1:
reprocess_seed = randint(0,999999999)
else:
reprocess_seed = p.seed+t2iii_seed_shift*(i+1)
if t2iii_clip > 0:
opts.data["CLIP_stop_at_last_layers"] = t2iii_clip
if state.interrupted:
if t2iii_clip > 0:
opts.data["CLIP_stop_at_last_layers"] = initial_CLIP
break
if i == 0:
proc_temp = proc
else:
proc_temp = proc2
if t2iii_noise > 0 :
proc_temp.images[0] = add_noise_to_image(proc_temp.images[0],p.seed,t2iii_noise)
img2img_processing = StableDiffusionProcessingImg2Img(
init_images=proc_temp.images,
resize_mode=0,
denoising_strength=t2iii_denoising_strength,
mask=None,
mask_blur=4,
inpainting_fill=0,
inpaint_full_res=True,
inpaint_full_res_padding=0,
inpainting_mask_invert=0,
sd_model=p.sd_model,
outpath_samples=p.outpath_samples,
outpath_grids=p.outpath_grids,
prompt=proc.info.split("\nNegative prompt")[0],
styles=p.styles,
seed=reprocess_seed,
subseed=proc_temp.subseed,
subseed_strength=p.subseed_strength,
seed_resize_from_h=p.seed_resize_from_h,
seed_resize_from_w=p.seed_resize_from_w,
#seed_enable_extras=p.seed_enable_extras,
sampler_name=t2iii_sampler,
# sampler_index=img2img_sampler_index,
batch_size=p.batch_size,
n_iter=p.n_iter,
steps=t2iii_steps,
cfg_scale=t2iii_cfg_scale,
width=upscale_x,
height=upscale_y,
restore_faces=(t2iii_face_correction or (t2iii_face_correction_last and t2iii_reprocess-1 == i)) and not (t2iii_reprocess-1 == i and not t2iii_face_correction_last),
tiling=p.tiling,
do_not_save_samples=not ((t2iii_only_last and t2iii_reprocess-1 == i) or not t2iii_only_last),
do_not_save_grid=p.do_not_save_grid,
extra_generation_params=extra_gen_parms,
overlay_images=p.overlay_images,
negative_prompt=p.negative_prompt,
eta=p.eta
)
proc2 = process_images(img2img_processing)
p.subseed = p.subseed + 1 if p.subseed_strength > 0 else p.subseed
p.seed = p.seed + 1 if p.subseed_strength == 0 else p.seed
if t2iii_clip > 0:
opts.data["CLIP_stop_at_last_layers"] = initial_CLIP
return proc