Merge pull request #41 from GeorgLegato/Upscalers_SettingImprov

[Feature] Upscale feature - [Bugfix] prompt JSON loading error - [Feature] txt2image and inpainting model separation
pull/46/head
vahid khroasani 2023-04-20 06:07:29 +04:00 committed by GitHub
commit 0dafabe52f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 234 additions and 54 deletions

View File

@ -1,9 +1,9 @@
import sys
import os
import time
import json
from jsonschema import validate
basedir = os.getcwd()
sys.path.extend(basedir + "/extensions/infinite-zoom-automatic1111-webui/")
import numpy as np
import gradio as gr
from PIL import Image
@ -12,7 +12,7 @@ import json
from iz_helpers import shrink_and_paste_on_blank, write_video
from webui import wrap_gradio_gpu_call
from modules import script_callbacks
from modules import script_callbacks, scripts
import modules.shared as shared
from modules.processing import (
process_images,
@ -20,31 +20,23 @@ from modules.processing import (
StableDiffusionProcessingImg2Img,
)
import scripts.postprocessing_upscale
from modules.ui import create_output_panel, plaintext_to_html
import modules.sd_models
import modules.sd_samplers
available_samplers = [
"DDIM",
"Euler a",
"Euler",
"LMS",
"Heun",
"DPM2",
"DPM2 a",
"DPM++ 2S a",
"DPM++ 2M",
"DPM++ SDE",
"DPM fast",
"DPM adaptive",
"LMS Karras",
"DPM2 Karras",
"DPM2 a Karras",
"DPM++ 2S a Karras",
"DPM++ 2M Karras",
"DPM++ SDE Karras",
]
default_prompt = "A psychedelic jungle with trees that have glowing, fractal-like patterns, Simon stalenhag poster 1920s style, street level view, hyper futuristic, 8k resolution, hyper realistic"
default_negative_prompt = "frames, borderline, text, character, duplicate, error, out of frame, watermark, low quality, ugly, deformed, blur"
from modules import scripts
usefulDirs = scripts.basedir().split(os.sep)[-2:] # contains install and our extension foldername
jsonprompt_schemafile = usefulDirs[0]+"/"+usefulDirs[1]+"/scripts/promptschema.json"
available_samplers = [s.name for s in modules.sd_samplers.samplers]
default_prompt = '{"prompts":{"data":[[0,"Cat"],["1","Dog"],["2","Happy Pets"]],"headers":["outpaint steps","prompt"]},"negPrompt":"ugly"}'
empty_prompt = '{"prompts":{"data":[],"headers":["outpaint steps","prompt"]},"negPrompt":""}'
#must be python dict
invalid_prompt ={"prompts":{"data":[[0,"Your prompt-json is invalid, please check Settings"]],"headers":["outpaint steps","prompt"]},"negPrompt":"Invalid prompt-json"}
def closest_upper_divisible_by_eight(num):
if num % 8 == 0:
@ -52,6 +44,13 @@ def closest_upper_divisible_by_eight(num):
else:
return math.ceil(num / 8) * 8
def do_upscaleImg(curImg,upscale_do, upscaler_name,upscale_by):
if (not upscale_do): return curImg
pp= scripts.postprocessing_upscale.scripts_postprocessing.PostprocessedImage(curImg)
ups = scripts.postprocessing_upscale.ScriptPostprocessingUpscale()
ups.process(pp, upscale_mode=2, upscale_by=upscale_by, upscale_to_width=None, upscale_to_height=None, upscale_crop=False, upscaler_1_name=upscaler_name, upscaler_2_name=None, upscaler_2_visibility=0.0)
return pp.image
def renderTxt2Img(prompt, negative_prompt, sampler, steps, cfg_scale, width, height):
processed = None
@ -72,7 +71,6 @@ def renderTxt2Img(prompt, negative_prompt, sampler, steps, cfg_scale, width, hei
processed = process_images(p)
return processed
def renderImg2Img(
prompt,
negative_prompt,
@ -90,6 +88,7 @@ def renderImg2Img(
inpainting_padding,
):
processed = None
p = StableDiffusionProcessingImg2Img(
sd_model=shared.sd_model,
outpath_samples=shared.opts.outdir_img2img_samples,
@ -133,6 +132,7 @@ def create_zoom(
guidance_scale,
num_inference_steps,
custom_init_image,
custom_exit_image,
video_frame_rate,
video_zoom_mode,
video_start_frame_dupe_amount,
@ -147,6 +147,9 @@ def create_zoom(
outputsizeH,
batchcount,
sampler,
upscale_do,
upscaler_name,
upscale_by,
progress=None,
):
for i in range(batchcount):
@ -158,6 +161,7 @@ def create_zoom(
guidance_scale,
num_inference_steps,
custom_init_image,
custom_exit_image,
video_frame_rate,
video_zoom_mode,
video_start_frame_dupe_amount,
@ -171,7 +175,11 @@ def create_zoom(
outputsizeW,
outputsizeH,
sampler,
upscale_do,
upscaler_name,
upscale_by,
progress,
)
return result
@ -183,6 +191,7 @@ def create_zoom_single(
guidance_scale,
num_inference_steps,
custom_init_image,
custom_exit_image,
video_frame_rate,
video_zoom_mode,
video_start_frame_dupe_amount,
@ -196,6 +205,9 @@ def create_zoom_single(
outputsizeW,
outputsizeH,
sampler,
upscale_do,
upscaler_name,
upscale_by,
progress=None,
):
# try:
@ -229,6 +241,15 @@ def create_zoom_single(
(width, height), resample=Image.LANCZOS
)
else:
modelname = shared.opts.data.get("infzoom_txt2img_model")
if (modelname):
# switch to txt2img model
checkinfo = modules.sd_models.checkpoint_alisases[modelname]
if (not checkinfo):
raise NameError("Checklist not found in registry")
if progress: progress(0, desc="Loading Model for txt2img: " + checkinfo.name)
modules.sd_models.load_model(checkinfo)
processed = renderTxt2Img(
prompts[min(k for k in prompts.keys() if k >= 0)],
negative_prompt,
@ -240,25 +261,36 @@ def create_zoom_single(
)
current_image = processed.images[0]
mask_width = math.trunc(width / 4) # was initially 512px => 128px
mask_height = math.trunc(height / 4) # was initially 512px => 128px
num_interpol_frames = round(video_frame_rate * zoom_speed)
all_frames = []
all_frames.append(current_image)
if upscale_do and progress:
progress(0, desc="upscaling inital image")
all_frames.append(do_upscaleImg(current_image,upscale_do, upscaler_name,upscale_by) if upscale_do else current_image)
inmodelname = shared.opts.data.get("infzoom_inpainting_model")
if (inmodelname):
# switch to inpaint model now
checkinfo = modules.sd_models.checkpoint_alisases[inmodelname]
if (not checkinfo):
raise NameError("Checklist not found in registry")
if progress: progress(0, desc="Loading Model for inpainting/img2img: " + checkinfo.name)
modules.sd_models.load_model(checkinfo)
for i in range(num_outpainting_steps):
print_out = "Outpaint step: " + str(i + 1) + " / " + str(num_outpainting_steps)
print(print_out)
# if progress is not None:
# progress(
# ((i + 1) / num_outpainting_steps),
# desc=print_out,
# )
if progress: progress( ((i + 1) / num_outpainting_steps), desc=print_out)
prev_image_fix = current_image
prev_image = shrink_and_paste_on_blank(current_image, mask_width, mask_height)
current_image = prev_image
# create mask (black image with white mask_width width edges)
@ -267,6 +299,7 @@ def create_zoom_single(
# inpainting step
current_image = current_image.convert("RGB")
processed = renderImg2Img(
prompts[max(k for k in prompts.keys() if k <= i)],
negative_prompt,
@ -341,8 +374,21 @@ def create_zoom_single(
interpol_image.paste(prev_image_fix_crop, mask=prev_image_fix_crop)
all_frames.append(interpol_image)
all_frames.append(current_image)
if upscale_do and progress:
progress(
((i + 1) / num_outpainting_steps),
desc="upscaling interpol"
)
all_frames.append(do_upscaleImg(interpol_image, upscale_do, upscaler_name,upscale_by) if upscale_do else interpol_image)
if upscale_do and progress:
progress(
((i + 1) / num_outpainting_steps),
desc="upscaling current"
)
all_frames.append(do_upscaleImg(current_image,upscale_do, upscaler_name,upscale_by) if upscale_do else current_image)
video_file_name = "infinite_zoom_" + str(int(time.time())) + ".mp4"
output_path = shared.opts.data.get(
@ -372,17 +418,26 @@ def create_zoom_single(
)
def exportPrompts(p, np):
print("prompts:" + str(p) + "\n" + str(np))
def validatePromptJson_throws(data):
with open(jsonprompt_schemafile, "r") as s: schema = json.load(s)
validate(instance=data, schema=schema)
def putPrompts(files):
file_paths = [file.name for file in files]
with open(files.name, "r") as f:
file_contents = f.read()
data = json.loads(file_contents)
print(data)
return [gr.DataFrame.update(data["prompts"]), gr.Textbox.update(data["negPrompt"])]
try:
with open(files.name, 'r') as f:
file_contents = f.read()
data = json.loads(file_contents)
validatePromptJson_throws(data)
return [gr.DataFrame.update(data["prompts"]), gr.Textbox.update(data["negPrompt"])]
except Exception:
gr.Error("loading your prompt failed. It seems to be invalid. Your prompt table is preserved.")
print("[InfiniteZoom:] Loading your prompt failed. It seems to be invalid. Your prompt table is preserved.")
return [gr.DataFrame.update(), gr.Textbox.update()]
def clearPrompts():
return [gr.DataFrame.update(value=[[0,"Infinite Zoom. Start over"]]), gr.Textbox.update("")]
def on_ui_tabs():
@ -396,10 +451,12 @@ def on_ui_tabs():
"""
)
generate_btn = gr.Button(value="Generate video", variant="primary")
interrupt = gr.Button(value="Interrupt", elem_id="interrupt_training")
with gr.Row():
generate_btn = gr.Button(value="Generate video", variant="primary")
interrupt = gr.Button(value="Interrupt", elem_id="interrupt_training")
with gr.Row():
with gr.Column(scale=1, variant="panel"):
with gr.Tab("Main"):
main_outpaint_steps = gr.Slider(
minimum=2,
@ -409,18 +466,29 @@ def on_ui_tabs():
label="Total Outpaint Steps",
info="The more it is, the longer your videos will be",
)
# safe reading json prompt
pr = shared.opts.data.get("infzoom_defPrompt",default_prompt)
if (not pr): pr = empty_prompt
try:
jpr = json.loads(pr)
validatePromptJson_throws(jpr)
except Exception:
jpr = invalid_prompt
main_prompts = gr.Dataframe(
type="array",
headers=["outpaint step", "prompt"],
datatype=["number", "str"],
row_count=1,
col_count=(2, "fixed"),
value=[[0, default_prompt]],
value=jpr["prompts"],
wrap=True,
)
main_negative_prompt = gr.Textbox(
value=default_negative_prompt, label="Negative Prompt"
value=jpr["negPrompt"], label="Negative Prompt"
)
# these button will be moved using JS unde the dataframe view as small ones
@ -447,6 +515,10 @@ def on_ui_tabs():
outputs=[main_prompts, main_negative_prompt],
inputs=[importPrompts_button],
)
clearPrompts_button= gr.Button(value="Clear prompts",variant="secondary",elem_classes="sm infzoom_tab_butt", elem_id="infzoom_clP_butt")
clearPrompts_button.click(fn=clearPrompts,inputs=[],outputs=[main_prompts,main_negative_prompt])
main_sampler = gr.Dropdown(
label="Sampler",
choices=available_samplers,
@ -483,7 +555,10 @@ def on_ui_tabs():
value=50,
label="Sampling Steps for each outpaint",
)
init_image = gr.Image(type="pil", label="custom initial image")
with gr.Row():
init_image = gr.Image(type="pil", label="custom initial image")
exit_image = gr.Image(type="pil", label="custom exit image", visible=False) #TODO: implement exit-image rendering
batchcount_slider = gr.Slider(
minimum=1,
maximum=25,
@ -545,6 +620,19 @@ def on_ui_tabs():
label="masked padding", minimum=0, maximum=256, value=0
)
with gr.Tab("Post proccess"):
upscale_do = gr.Checkbox(False, label="Enable Upscale")
upscaler_name = gr.Dropdown(label='Upscaler', elem_id="infZ_upscaler", choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name)
upscale_by = gr.Slider(
label="Upscale by factor", minimum=1, maximum=8, value=1
)
with gr.Accordion("Help",open=False):
gr.Markdown("""# Performance critical
Depending on amount of frames and which upscaler you choose it might took a long time to render.
Our best experience and trade-off is the R-ERSGAn4x upscaler.
""")
with gr.Column(scale=1, variant="compact"):
output_video = gr.Video(label="Output").style(width=512, height=512)
(
@ -553,7 +641,7 @@ def on_ui_tabs():
html_info,
html_log,
) = create_output_panel(
"infinit-zoom", shared.opts.outdir_img2img_samples
"infinite-zoom", shared.opts.outdir_img2img_samples
)
generate_btn.click(
fn=wrap_gradio_gpu_call(create_zoom, extra_outputs=[None, '', '']),
@ -564,6 +652,7 @@ def on_ui_tabs():
main_guidance_scale,
sampling_step,
init_image,
exit_image,
video_frame_rate,
video_zoom_mode,
video_start_frame_dupe_amount,
@ -578,6 +667,10 @@ def on_ui_tabs():
main_height,
batchcount_slider,
main_sampler,
upscale_do,
upscaler_name,
upscale_by
],
outputs=[output_video, out_image, generation_info, html_info, html_log],
)
@ -640,10 +733,42 @@ def on_ui_settings():
"Writing videos has dependency to an existing FFPROBE executable on your machine. D/L here (https://github.com/BtbN/FFmpeg-Builds/releases) your OS variant and point to your installation path",
gr.Textbox,
{"interactive": True},
section=section,
),
section=section
)
)
shared.opts.add_option(
"infzoom_txt2img_model",
shared.OptionInfo(
shared.list_checkpoint_tiles[0],
"Name of your desired model to render keyframes (txt2img)",
gr.Dropdown,
lambda: {"choices": shared.list_checkpoint_tiles()},
section=section
)
)
shared.opts.add_option(
"infzoom_inpainting_model",
shared.OptionInfo(
"sd-v1-5-inpainting.ckpt",
"Name of your desired inpaint model (img2img-inpaint). Default is vanilla sd-v1-5-inpainting.ckpt ",
gr.Dropdown,
lambda: {"choices": shared.list_checkpoint_tiles()},
section=section
)
)
shared.opts.add_option(
"infzoom_defPrompt",
shared.OptionInfo(
default_prompt,
"Default prompt-setup to start with'",
gr.Code,
{"interactive": True, "language":"json"},
section=section
)
)
script_callbacks.on_ui_tabs(on_ui_tabs)
script_callbacks.on_ui_settings(on_ui_settings)

55
scripts/promptschema.json Normal file
View File

@ -0,0 +1,55 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"prompts": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "array",
"items": [
{
"oneOf": [
{
"type": "integer",
"minimum": 0
},
{
"type": "string"
}
]
},
{
"type": "string"
}
],
"minItems": 2,
"maxItems": 2,
"uniqueItems": true
},
"minItems": 1
},
"headers": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 2
}
},
"required": [
"data",
"headers"
]
},
"negPrompt": {
"type": "string"
}
},
"required": [
"prompts",
"negPrompt"
]
}