- Add gradio dropdown list to select checkpoints

- Update checkpoint model fields labels
This commit is contained in:
Bernard Maltais 2022-09-27 21:09:28 -04:00
parent 591c138e32
commit d641af6a7d
2 changed files with 19 additions and 18 deletions

View file

@ -140,7 +140,7 @@ def run_pnginfo(image):
return '', geninfo, info
def run_modelmerger(from_model_name, to_model_name, interp_method, interp_amount):
def run_modelmerger(primary_model_name, secondary_model_name, interp_method, interp_amount):
# Linear interpolation (https://en.wikipedia.org/wiki/Linear_interpolation)
def weighted_sum(theta0, theta1, alpha):
return ((1 - alpha) * theta0) + (alpha * theta1)
@ -150,23 +150,23 @@ def run_modelmerger(from_model_name, to_model_name, interp_method, interp_amount
alpha = alpha * alpha * (3 - (2 * alpha))
return theta0 + ((theta1 - theta0) * alpha)
if os.path.exists(to_model_name):
to_model_filename = to_model_name
to_model_name = os.path.splitext(os.path.basename(to_model_name))[0]
if os.path.exists(secondary_model_name):
secondary_model_filename = secondary_model_name
secondary_model_name = os.path.splitext(os.path.basename(secondary_model_name))[0]
else:
to_model_filename = 'models/' + to_model_name + '.ckpt'
secondary_model_filename = 'models/' + secondary_model_name + '.ckpt'
if os.path.exists(from_model_name):
from_model_filename = from_model_name
from_model_name = os.path.splitext(os.path.basename(from_model_name))[0]
if os.path.exists(primary_model_name):
primary_model_filename = primary_model_name
primary_model_name = os.path.splitext(os.path.basename(primary_model_name))[0]
else:
from_model_filename = 'models/' + from_model_name + '.ckpt'
primary_model_filename = 'models/' + primary_model_name + '.ckpt'
print(f"Loading {to_model_filename}...")
model_0 = torch.load(to_model_filename, map_location='cpu')
print(f"Loading {secondary_model_filename}...")
model_0 = torch.load(secondary_model_filename, map_location='cpu')
print(f"Loading {from_model_filename}...")
model_1 = torch.load(from_model_filename, map_location='cpu')
print(f"Loading {primary_model_filename}...")
model_1 = torch.load(primary_model_filename, map_location='cpu')
theta_0 = model_0['state_dict']
theta_1 = model_1['state_dict']
@ -186,7 +186,7 @@ def run_modelmerger(from_model_name, to_model_name, interp_method, interp_amount
if 'model' in key and key not in theta_0:
theta_0[key] = theta_1[key]
output_modelname = 'models/' + from_model_name + '_' + str(interp_amount) + '-' + to_model_name + '_' + str(float(1.0) - interp_amount) + '-' + interp_method.replace(" ", "_") + '-merged.ckpt'
output_modelname = 'models/' + primary_model_name + '_' + str(interp_amount) + '-' + secondary_model_name + '_' + str(float(1.0) - interp_amount) + '-' + interp_method.replace(" ", "_") + '-merged.ckpt'
print(f"Saving to {output_modelname}...")
torch.save(model_0, output_modelname)

View file

@ -860,8 +860,9 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo, run_modelmerger):
gr.HTML(value="<p>A merger of the two checkpoints will be generated in your <b>/models</b> directory.</p>")
with gr.Row():
from_model_name = gr.Textbox(elem_id="modelmerger_from_model_name", label="Model Name (from)")
to_model_name = gr.Textbox(elem_id="modelmerger_to_model_name", label="Model Name (to)")
ckpt_name_list = [x.model_name for x in modules.sd_models.checkpoints_list.values()]
primary_model_name = gr.Dropdown(ckpt_name_list, elem_id="modelmerger_primary_model_name", label="Primary Model Name")
secondary_model_name = gr.Dropdown(ckpt_name_list, elem_id="modelmerger_secondary_model_name", label="Secondary Model Name")
interp_amount = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label='Interpolation Amount', value=0.3)
interp_method = gr.Radio(choices=["Weighted Sum", "Sigmoid"], value="Weighted Sum", label="Interpolation Method")
submit = gr.Button(elem_id="modelmerger_merge", label="Merge", variant='primary')
@ -872,8 +873,8 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo, run_modelmerger):
submit.click(
fn=run_modelmerger,
inputs=[
from_model_name,
to_model_name,
primary_model_name,
secondary_model_name,
interp_method,
interp_amount
],