added progressbar

added an option to disable progressbar
added interrupt support to DDIM/PLMS
This commit is contained in:
AUTOMATIC 2022-09-06 02:09:01 +03:00
parent b6763fb884
commit a243bc7859
11 changed files with 170 additions and 9 deletions

View File

@ -55,7 +55,10 @@ def img2img(prompt: str, init_img, init_img_with_mask, steps: int, sampler_index
initial_seed = None initial_seed = None
initial_info = None initial_info = None
state.job_count = n_iter
for i in range(n_iter): for i in range(n_iter):
p.n_iter = 1 p.n_iter = 1
p.batch_size = 1 p.batch_size = 1
p.do_not_save_grid = True p.do_not_save_grid = True
@ -72,6 +75,8 @@ def img2img(prompt: str, init_img, init_img_with_mask, steps: int, sampler_index
p.denoising_strength = max(p.denoising_strength * 0.95, 0.1) p.denoising_strength = max(p.denoising_strength * 0.95, 0.1)
history.append(processed.images[0]) history.append(processed.images[0])
state.nextjob()
grid = images.image_grid(history, batch_size, rows=1) grid = images.image_grid(history, batch_size, rows=1)
images.save_image(grid, p.outpath_grids, "grid", initial_seed, prompt, opts.grid_format, info=info, short_filename=not opts.grid_extended_filename) images.save_image(grid, p.outpath_grids, "grid", initial_seed, prompt, opts.grid_format, info=info, short_filename=not opts.grid_extended_filename)
@ -103,6 +108,8 @@ def img2img(prompt: str, init_img, init_img_with_mask, steps: int, sampler_index
batch_count = math.ceil(len(work) / p.batch_size) batch_count = math.ceil(len(work) / p.batch_size)
print(f"SD upscaling will process a total of {len(work)} images tiled as {len(grid.tiles[0][2])}x{len(grid.tiles)} in a total of {batch_count} batches.") print(f"SD upscaling will process a total of {len(work)} images tiled as {len(grid.tiles[0][2])}x{len(grid.tiles)} in a total of {batch_count} batches.")
state.job_count = batch_count
for i in range(batch_count): for i in range(batch_count):
p.init_images = work[i*p.batch_size:(i+1)*p.batch_size] p.init_images = work[i*p.batch_size:(i+1)*p.batch_size]
@ -116,6 +123,8 @@ def img2img(prompt: str, init_img, init_img_with_mask, steps: int, sampler_index
p.seed = processed.seed + 1 p.seed = processed.seed + 1
work_results += processed.images work_results += processed.images
state.nextjob()
image_index = 0 image_index = 0
for y, h, row in grid.tiles: for y, h, row in grid.tiles:
for tiledata in row: for tiledata in row:

View File

@ -153,6 +153,8 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
with torch.no_grad(), precision_scope("cuda"), ema_scope(): with torch.no_grad(), precision_scope("cuda"), ema_scope():
p.init() p.init()
state.job_count = p.n_iter
for n in range(p.n_iter): for n in range(p.n_iter):
if state.interrupted: if state.interrupted:
break break
@ -207,6 +209,8 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
output_images.append(image) output_images.append(image)
state.nextjob()
unwanted_grid_because_of_img_count = len(output_images) < 2 and opts.grid_only_if_multiple unwanted_grid_because_of_img_count = len(output_images) < 2 and opts.grid_only_if_multiple
if not p.do_not_save_grid and not unwanted_grid_because_of_img_count: if not p.do_not_save_grid and not unwanted_grid_because_of_img_count:
return_grid = opts.return_grid return_grid = opts.return_grid

View File

@ -1,10 +1,12 @@
from collections import namedtuple from collections import namedtuple
import ldm.models.diffusion.ddim
import torch import torch
import tqdm import tqdm
import k_diffusion.sampling import k_diffusion.sampling
from ldm.models.diffusion.ddim import DDIMSampler import ldm.models.diffusion.ddim
from ldm.models.diffusion.plms import PLMSSampler import ldm.models.diffusion.plms
from modules.shared import opts, cmd_opts, state from modules.shared import opts, cmd_opts, state
import modules.shared as shared import modules.shared as shared
@ -29,8 +31,8 @@ samplers_data_k_diffusion = [
samplers = [ samplers = [
*samplers_data_k_diffusion, *samplers_data_k_diffusion,
SamplerData('DDIM', lambda model: VanillaStableDiffusionSampler(DDIMSampler, model), []), SamplerData('DDIM', lambda model: VanillaStableDiffusionSampler(ldm.models.diffusion.ddim.DDIMSampler, model), []),
SamplerData('PLMS', lambda model: VanillaStableDiffusionSampler(PLMSSampler, model), []), SamplerData('PLMS', lambda model: VanillaStableDiffusionSampler(ldm.models.diffusion.plms.PLMSSampler, model), []),
] ]
samplers_for_img2img = [x for x in samplers if x.name != 'PLMS'] samplers_for_img2img = [x for x in samplers if x.name != 'PLMS']
@ -43,6 +45,23 @@ def p_sample_ddim_hook(sampler_wrapper, x_dec, cond, ts, *args, **kwargs):
return sampler_wrapper.orig_p_sample_ddim(x_dec, cond, ts, *args, **kwargs) return sampler_wrapper.orig_p_sample_ddim(x_dec, cond, ts, *args, **kwargs)
def extended_tdqm(sequence, *args, desc=None, **kwargs):
state.sampling_steps = len(sequence)
state.sampling_step = 0
for x in tqdm.tqdm(sequence, *args, desc=state.job, **kwargs):
if state.interrupted:
break
yield x
state.sampling_step += 1
ldm.models.diffusion.ddim.tqdm = lambda *args, desc=None, **kwargs: extended_tdqm(*args, desc=desc, **kwargs)
ldm.models.diffusion.plms.tqdm = lambda *args, desc=None, **kwargs: extended_tdqm(*args, desc=desc, **kwargs)
class VanillaStableDiffusionSampler: class VanillaStableDiffusionSampler:
def __init__(self, constructor, sd_model): def __init__(self, constructor, sd_model):
self.sampler = constructor(sd_model) self.sampler = constructor(sd_model)
@ -102,13 +121,18 @@ class CFGDenoiser(torch.nn.Module):
return denoised return denoised
def extended_trange(*args, **kwargs): def extended_trange(count, *args, **kwargs):
for x in tqdm.trange(*args, desc=state.job, **kwargs): state.sampling_steps = count
state.sampling_step = 0
for x in tqdm.trange(count, *args, desc=state.job, **kwargs):
if state.interrupted: if state.interrupted:
break break
yield x yield x
state.sampling_step += 1
class KDiffusionSampler: class KDiffusionSampler:
def __init__(self, funcname, sd_model): def __init__(self, funcname, sd_model):

View File

@ -42,10 +42,18 @@ batch_cond_uncond = cmd_opts.always_batch_cond_uncond or not (cmd_opts.lowvram o
class State: class State:
interrupted = False interrupted = False
job = "" job = ""
job_no = 0
job_count = 0
sampling_step = 0
sampling_steps = 0
def interrupt(self): def interrupt(self):
self.interrupted = True self.interrupted = True
def nextjob(self):
self.job_no += 1
self.sampling_step = 0
state = State() state = State()
artist_db = modules.artists.ArtistsDatabase(os.path.join(script_path, 'artists.csv')) artist_db = modules.artists.ArtistsDatabase(os.path.join(script_path, 'artists.csv'))
@ -89,6 +97,7 @@ class Options:
"ESRGAN_tile_overlap": OptionInfo(8, "Tile overlap, in pixels for ESRGAN upscaling. Low values = visible seam.", gr.Slider, {"minimum": 0, "maximum": 48, "step": 1}), "ESRGAN_tile_overlap": OptionInfo(8, "Tile overlap, in pixels for ESRGAN upscaling. Low values = visible seam.", gr.Slider, {"minimum": 0, "maximum": 48, "step": 1}),
"random_artist_categories": OptionInfo([], "Allowed categories for random artists selection when using the Roll button", gr.CheckboxGroup, {"choices": artist_db.categories()}), "random_artist_categories": OptionInfo([], "Allowed categories for random artists selection when using the Roll button", gr.CheckboxGroup, {"choices": artist_db.categories()}),
"upscale_at_full_resolution_padding": OptionInfo(16, "Inpainting at full resolution: padding, in pixels, for the masked region.", gr.Slider, {"minimum": 0, "maximum": 128, "step": 4}), "upscale_at_full_resolution_padding": OptionInfo(16, "Inpainting at full resolution: padding, in pixels, for the masked region.", gr.Slider, {"minimum": 0, "maximum": 128, "step": 4}),
"show_progressbar": OptionInfo(True, "Show progressbar"),
} }
def __init__(self): def __init__(self):

View File

@ -48,7 +48,6 @@ css_hide_progressbar = """
.meta-text { display:none!important; } .meta-text { display:none!important; }
""" """
def plaintext_to_html(text): def plaintext_to_html(text):
text = "".join([f"<p>{html.escape(x)}</p>\n" for x in text.split('\n')]) text = "".join([f"<p>{html.escape(x)}</p>\n" for x in text.split('\n')])
return text return text
@ -134,6 +133,24 @@ def wrap_gradio_call(func):
return f return f
def check_progress_call():
if not opts.show_progressbar:
return ""
if shared.state.job_count == 0:
return ""
progress = shared.state.job_no / shared.state.job_count
if shared.state.sampling_steps > 0:
progress += 1 / shared.state.job_count * shared.state.sampling_step / shared.state.sampling_steps
progress = min(progress, 1)
progressbar = f"""<div class='progressDiv'><div class='progress' style="width:{progress * 100}%">{str(int(progress*100))+"%" if progress > 0.01 else ""}</div></div>"""
return f"<span style='display: none'>{time.time()}</span><p>{progressbar}</p>"
def roll_artist(prompt): def roll_artist(prompt):
allowed_cats = set([x for x in shared.artist_db.categories() if len(opts.random_artist_categories)==0 or x in opts.random_artist_categories]) allowed_cats = set([x for x in shared.artist_db.categories() if len(opts.random_artist_categories)==0 or x in opts.random_artist_categories])
artist = random.choice([x for x in shared.artist_db.artists if x.category in allowed_cats]) artist = random.choice([x for x in shared.artist_db.artists if x.category in allowed_cats])
@ -156,6 +173,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
negative_prompt = gr.Textbox(label="Negative prompt", elem_id="txt2img_negative_prompt", show_label=False, placeholder="Negative prompt", lines=1, visible=False) negative_prompt = gr.Textbox(label="Negative prompt", elem_id="txt2img_negative_prompt", show_label=False, placeholder="Negative prompt", lines=1, visible=False)
roll = gr.Button('Roll', elem_id="txt2img_roll", visible=len(shared.artist_db.artists) > 0) roll = gr.Button('Roll', elem_id="txt2img_roll", visible=len(shared.artist_db.artists) > 0)
submit = gr.Button('Generate', elem_id="txt2img_generate", variant='primary') submit = gr.Button('Generate', elem_id="txt2img_generate", variant='primary')
check_progress = gr.Button('Check progress', elem_id="check_progress", visible=False)
with gr.Row().style(equal_height=False): with gr.Row().style(equal_height=False):
with gr.Column(variant='panel'): with gr.Column(variant='panel'):
@ -185,6 +203,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
with gr.Group(): with gr.Group():
txt2img_gallery = gr.Gallery(label='Output', elem_id='txt2img_gallery') txt2img_gallery = gr.Gallery(label='Output', elem_id='txt2img_gallery')
with gr.Group(): with gr.Group():
with gr.Row(): with gr.Row():
save = gr.Button('Save') save = gr.Button('Save')
@ -193,12 +212,16 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
send_to_extras = gr.Button('Send to extras') send_to_extras = gr.Button('Send to extras')
interrupt = gr.Button('Interrupt') interrupt = gr.Button('Interrupt')
progressbar = gr.HTML(elem_id="progressbar")
with gr.Group(): with gr.Group():
html_info = gr.HTML() html_info = gr.HTML()
generation_info = gr.Textbox(visible=False) generation_info = gr.Textbox(visible=False)
txt2img_args = dict( txt2img_args = dict(
fn=txt2img, fn=txt2img,
_js="submit",
inputs=[ inputs=[
prompt, prompt,
negative_prompt, negative_prompt,
@ -223,6 +246,13 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
prompt.submit(**txt2img_args) prompt.submit(**txt2img_args)
submit.click(**txt2img_args) submit.click(**txt2img_args)
check_progress.click(
fn=check_progress_call,
inputs=[],
outputs=[progressbar],
)
interrupt.click( interrupt.click(
fn=lambda: shared.state.interrupt(), fn=lambda: shared.state.interrupt(),
inputs=[], inputs=[],
@ -252,10 +282,12 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
] ]
) )
with gr.Blocks(analytics_enabled=False) as img2img_interface: with gr.Blocks(analytics_enabled=False) as img2img_interface:
with gr.Row(): with gr.Row():
prompt = gr.Textbox(label="Prompt", elem_id="img2img_prompt", show_label=False, placeholder="Prompt", lines=1) prompt = gr.Textbox(label="Prompt", elem_id="img2img_prompt", show_label=False, placeholder="Prompt", lines=1)
submit = gr.Button('Generate', elem_id="img2img_generate", variant='primary') submit = gr.Button('Generate', elem_id="img2img_generate", variant='primary')
check_progress = gr.Button('Check progress', elem_id="check_progress", visible=False)
with gr.Row().style(equal_height=False): with gr.Row().style(equal_height=False):
@ -310,6 +342,8 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
save = gr.Button('Save') save = gr.Button('Save')
img2img_send_to_extras = gr.Button('Send to extras') img2img_send_to_extras = gr.Button('Send to extras')
progressbar = gr.HTML(elem_id="progressbar")
with gr.Group(): with gr.Group():
html_info = gr.HTML() html_info = gr.HTML()
generation_info = gr.Textbox(visible=False) generation_info = gr.Textbox(visible=False)
@ -352,6 +386,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
img2img_args = dict( img2img_args = dict(
fn=img2img, fn=img2img,
_js="submit",
inputs=[ inputs=[
prompt, prompt,
init_img, init_img,
@ -386,6 +421,12 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
prompt.submit(**img2img_args) prompt.submit(**img2img_args)
submit.click(**img2img_args) submit.click(**img2img_args)
check_progress.click(
fn=check_progress_call,
inputs=[],
outputs=[progressbar],
)
interrupt.click( interrupt.click(
fn=lambda: shared.state.interrupt(), fn=lambda: shared.state.interrupt(),
inputs=[], inputs=[],

View File

@ -51,6 +51,8 @@ function gradioApp(){
return document.getElementsByTagName('gradio-app')[0].shadowRoot; return document.getElementsByTagName('gradio-app')[0].shadowRoot;
} }
global_progressbar = null
function addTitles(root){ function addTitles(root){
root.querySelectorAll('span, button, select').forEach(function(span){ root.querySelectorAll('span, button, select').forEach(function(span){
tooltip = titles[span.textContent]; tooltip = titles[span.textContent];
@ -71,6 +73,17 @@ function addTitles(root){
select.title = titles[select.value] || ""; select.title = titles[select.value] || "";
} }
}) })
progressbar = root.getElementById('progressbar')
if(progressbar!= null && progressbar != global_progressbar){
global_progressbar = progressbar
var mutationObserver = new MutationObserver(function(m){
window.setTimeout(requestProgress, 500)
});
mutationObserver.observe( progressbar, { childList:true, subtree:true })
}
} }
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
@ -78,7 +91,6 @@ document.addEventListener("DOMContentLoaded", function() {
addTitles(gradioApp()); addTitles(gradioApp());
}); });
mutationObserver.observe( gradioApp(), { childList:true, subtree:true }) mutationObserver.observe( gradioApp(), { childList:true, subtree:true })
}); });
function selected_gallery_index(){ function selected_gallery_index(){
@ -105,3 +117,22 @@ function extract_image_from_gallery(gallery){
return gallery[index]; return gallery[index];
} }
function requestProgress(){
btn = gradioApp().getElementById("check_progress");
if(btn==null) return;
btn.click();
}
function submit(){
window.setTimeout(requestProgress, 500)
res = []
for(var i=0;i<arguments.length;i++){
res.push(arguments[i])
}
console.log(res)
return res
}

View File

@ -78,6 +78,8 @@ class Script(scripts.Script):
batch_count = len(work) batch_count = len(work)
print(f"Poor man's outpainting will process a total of {len(work)} images tiled as {len(grid.tiles[0][2])}x{len(grid.tiles)}.") print(f"Poor man's outpainting will process a total of {len(work)} images tiled as {len(grid.tiles[0][2])}x{len(grid.tiles)}.")
state.job_count = batch_count
for i in range(batch_count): for i in range(batch_count):
p.init_images = [work[i]] p.init_images = [work[i]]
p.image_mask = work_mask[i] p.image_mask = work_mask[i]
@ -93,6 +95,8 @@ class Script(scripts.Script):
p.seed = processed.seed + 1 p.seed = processed.seed + 1
work_results += processed.images work_results += processed.images
state.nextjob()
image_index = 0 image_index = 0
for y, h, row in grid.tiles: for y, h, row in grid.tiles:
for tiledata in row: for tiledata in row:

View File

@ -20,6 +20,8 @@ def draw_xy_grid(xs, ys, x_label, y_label, cell):
first_pocessed = None first_pocessed = None
state.job_count = len(xs) * len(ys)
for iy, y in enumerate(ys): for iy, y in enumerate(ys):
for ix, x in enumerate(xs): for ix, x in enumerate(xs):
state.job = f"{ix + iy * len(xs) + 1} out of {len(xs) * len(ys)}" state.job = f"{ix + iy * len(xs) + 1} out of {len(xs) * len(ys)}"
@ -29,6 +31,7 @@ def draw_xy_grid(xs, ys, x_label, y_label, cell):
first_pocessed = processed first_pocessed = processed
res.append(processed.images[0]) res.append(processed.images[0])
state.nextjob()
grid = images.image_grid(res, rows=len(ys)) grid = images.image_grid(res, rows=len(ys))
grid = images.draw_grid_annotations(grid, res[0].width, res[0].height, hor_texts, ver_texts) grid = images.draw_grid_annotations(grid, res[0].width, res[0].height, hor_texts, ver_texts)

View File

@ -67,6 +67,8 @@ def draw_xy_grid(xs, ys, x_label, y_label, cell):
first_pocessed = None first_pocessed = None
state.job_count = len(xs) * len(ys)
for iy, y in enumerate(ys): for iy, y in enumerate(ys):
for ix, x in enumerate(xs): for ix, x in enumerate(xs):
state.job = f"{ix + iy * len(xs) + 1} out of {len(xs) * len(ys)}" state.job = f"{ix + iy * len(xs) + 1} out of {len(xs) * len(ys)}"
@ -77,6 +79,8 @@ def draw_xy_grid(xs, ys, x_label, y_label, cell):
res.append(processed.images[0]) res.append(processed.images[0])
state.nextjob()
grid = images.image_grid(res, rows=len(ys)) grid = images.image_grid(res, rows=len(ys))
grid = images.draw_grid_annotations(grid, res[0].width, res[0].height, hor_texts, ver_texts) grid = images.draw_grid_annotations(grid, res[0].width, res[0].height, hor_texts, ver_texts)

View File

@ -71,3 +71,28 @@ input[type="range"]{
padding-left: 0.6em; padding-left: 0.6em;
padding-right: 0.6em; padding-right: 0.6em;
} }
.progressDiv{
width: 100%;
height: 30px;
background: #b4c0cc;
border-radius: 8px;
}
.dark .progressDiv{
background: #424c5b;
}
.progressDiv .progress{
width: 0%;
height: 30px;
background: #0060df;
color: white;
font-weight: bold;
line-height: 30px;
padding: 0 8px 0 0;
text-align: right;
border-radius: 8px;
}

View File

@ -53,6 +53,7 @@ def load_model_from_config(config, ckpt, verbose=False):
cached_images = {} cached_images = {}
def run_extras(image, gfpgan_strength, upscaling_resize, extras_upscaler_1, extras_upscaler_2, extras_upscaler_2_visibility): def run_extras(image, gfpgan_strength, upscaling_resize, extras_upscaler_1, extras_upscaler_2, extras_upscaler_2_visibility):
processing.torch_gc() processing.torch_gc()
@ -121,10 +122,16 @@ queue_lock = threading.Lock()
def wrap_gradio_gpu_call(func): def wrap_gradio_gpu_call(func):
def f(*args, **kwargs): def f(*args, **kwargs):
shared.state.sampling_step = 0
shared.state.job_count = 1
shared.state.job_no = 0
with queue_lock: with queue_lock:
res = func(*args, **kwargs) res = func(*args, **kwargs)
shared.state.job = "" shared.state.job = ""
shared.state.job_count = 0
return res return res