Merge pull request #1185 from bmaltais/checkpoint-merger-ui-improvement

(feat): Rework Checkpoint Merger UI for better clarity and usability
This commit is contained in:
AUTOMATIC1111 2022-09-28 08:42:26 +03:00 committed by GitHub
commit 15f333a266
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 28 deletions

View File

@ -140,7 +140,7 @@ def run_pnginfo(image):
return '', geninfo, info
def run_modelmerger(modelname_0, modelname_1, 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,26 +150,26 @@ def run_modelmerger(modelname_0, modelname_1, interp_method, interp_amount):
alpha = alpha * alpha * (3 - (2 * alpha))
return theta0 + ((theta1 - theta0) * alpha)
if os.path.exists(modelname_0):
model0_filename = modelname_0
modelname_0 = os.path.splitext(os.path.basename(modelname_0))[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:
model0_filename = 'models/' + modelname_0 + '.ckpt'
primary_model_filename = 'models/' + primary_model_name + '.ckpt'
if os.path.exists(modelname_1):
model1_filename = modelname_1
modelname_1 = os.path.splitext(os.path.basename(modelname_1))[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:
model1_filename = 'models/' + modelname_1 + '.ckpt'
secondary_model_filename = 'models/' + secondary_model_name + '.ckpt'
print(f"Loading {model0_filename}...")
model_0 = torch.load(model0_filename, map_location='cpu')
print(f"Loading {primary_model_filename}...")
primary_model = torch.load(primary_model_filename, map_location='cpu')
print(f"Loading {model1_filename}...")
model_1 = torch.load(model1_filename, map_location='cpu')
print(f"Loading {secondary_model_filename}...")
secondary_model = torch.load(secondary_model_filename, map_location='cpu')
theta_0 = model_0['state_dict']
theta_1 = model_1['state_dict']
theta_0 = primary_model['state_dict']
theta_1 = secondary_model['state_dict']
theta_funcs = {
"Weighted Sum": weighted_sum,
@ -180,15 +180,15 @@ def run_modelmerger(modelname_0, modelname_1, interp_method, interp_amount):
print(f"Merging...")
for key in tqdm.tqdm(theta_0.keys()):
if 'model' in key and key in theta_1:
theta_0[key] = theta_func(theta_0[key], theta_1[key], interp_amount)
theta_0[key] = theta_func(theta_0[key], theta_1[key], (float(1.0) - interp_amount)) # Need to reverse the interp_amount to match the desired mix ration in the merged checkpoint
for key in theta_1.keys():
if 'model' in key and key not in theta_0:
theta_0[key] = theta_1[key]
output_modelname = 'models/' + modelname_0 + '-' + modelname_1 + '-' + interp_method.replace(" ", "_") + '-' + str(interp_amount) + '-merged.ckpt'
output_modelname = 'models/' + primary_model_name + '_' + str(round(interp_amount,2)) + '-' + secondary_model_name + '_' + str(round((float(1.0) - interp_amount),2)) + '-' + interp_method.replace(" ", "_") + '-merged.ckpt'
print(f"Saving to {output_modelname}...")
torch.save(model_0, output_modelname)
torch.save(primary_model, output_modelname)
print(f"Checkpoint saved.")
return "Checkpoint saved to " + output_modelname

View File

@ -10,7 +10,7 @@ from ldm.util import instantiate_from_config
from modules import shared
CheckpointInfo = namedtuple("CheckpointInfo", ['filename', 'title', 'hash'])
CheckpointInfo = namedtuple("CheckpointInfo", ['filename', 'title', 'hash', 'model_name'])
checkpoints_list = {}
try:
@ -45,7 +45,8 @@ def list_models():
if os.path.exists(cmd_ckpt):
h = model_hash(cmd_ckpt)
title = modeltitle(cmd_ckpt, h)
checkpoints_list[title] = CheckpointInfo(cmd_ckpt, title, h)
model_name = title.rsplit(".",1)[0] # remove extension if present
checkpoints_list[title] = CheckpointInfo(cmd_ckpt, title, h, model_name)
elif cmd_ckpt is not None and cmd_ckpt != shared.default_sd_model_file:
print(f"Checkpoint in --ckpt argument not found: {cmd_ckpt}", file=sys.stderr)
@ -53,7 +54,8 @@ def list_models():
for filename in glob.glob(model_dir + '/**/*.ckpt', recursive=True):
h = model_hash(filename)
title = modeltitle(filename, h)
checkpoints_list[title] = CheckpointInfo(filename, title, h)
model_name = title.rsplit(".",1)[0] # remove extension if present
checkpoints_list[title] = CheckpointInfo(filename, title, h, model_name)
def model_hash(filename):

View File

@ -859,10 +859,12 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo, run_modelmerger):
with gr.Column(variant='panel'):
gr.HTML(value="<p>A merger of the two checkpoints will be generated in your <b>/models</b> directory.</p>")
modelname_0 = gr.Textbox(elem_id="modelmerger_modelname_0", label="Model Name (to)")
modelname_1 = gr.Textbox(elem_id="modelmerger_modelname_1", label="Model Name (from)")
interp_method = gr.Radio(choices=["Weighted Sum", "Sigmoid"], value="Weighted Sum", label="Interpolation Method")
with gr.Row():
ckpt_name_list = sorted([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')
with gr.Column(variant='panel'):
@ -871,8 +873,8 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo, run_modelmerger):
submit.click(
fn=run_modelmerger,
inputs=[
modelname_0,
modelname_1,
primary_model_name,
secondary_model_name,
interp_method,
interp_amount
],