From 20b41b7be708a501fbc7e10dceebababfd48ffda Mon Sep 17 00:00:00 2001 From: Bernard Maltais Date: Tue, 27 Sep 2022 15:21:25 -0400 Subject: [PATCH 1/6] (feat): Rework Checkpoint Merger UI for better clarity - Rename variables to better align with field input - Improve merged checkpoint output file name --- modules/extras.py | 28 ++++++++++++++-------------- modules/ui.py | 11 ++++++----- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/modules/extras.py b/modules/extras.py index c4ee2b62a..9c3b421df 100644 --- a/modules/extras.py +++ b/modules/extras.py @@ -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(from_model_name, to_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(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(to_model_name): + to_model_filename = to_model_name + to_model_name = os.path.splitext(os.path.basename(to_model_name))[0] else: - model0_filename = 'models/' + modelname_0 + '.ckpt' + to_model_filename = 'models/' + to_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(from_model_name): + from_model_filename = from_model_name + from_model_name = os.path.splitext(os.path.basename(from_model_name))[0] else: - model1_filename = 'models/' + modelname_1 + '.ckpt' + from_model_filename = 'models/' + from_model_name + '.ckpt' - print(f"Loading {model0_filename}...") - model_0 = torch.load(model0_filename, map_location='cpu') + print(f"Loading {to_model_filename}...") + model_0 = torch.load(to_model_filename, map_location='cpu') - print(f"Loading {model1_filename}...") - model_1 = torch.load(model1_filename, map_location='cpu') + print(f"Loading {from_model_filename}...") + model_1 = torch.load(from_model_filename, map_location='cpu') theta_0 = model_0['state_dict'] theta_1 = model_1['state_dict'] @@ -186,7 +186,7 @@ def run_modelmerger(modelname_0, modelname_1, interp_method, interp_amount): 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/' + from_model_name + str(interp_amount) + '-' + to_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) diff --git a/modules/ui.py b/modules/ui.py index e96109c9c..e7382ca8b 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -859,10 +859,11 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo, run_modelmerger): with gr.Column(variant='panel'): gr.HTML(value="

A merger of the two checkpoints will be generated in your /models directory.

") - 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(): + 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)") 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 +872,8 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo, run_modelmerger): submit.click( fn=run_modelmerger, inputs=[ - modelname_0, - modelname_1, + from_model_name, + to_model_name, interp_method, interp_amount ], From e258f89080b8ff38f040dc786290da9144860d38 Mon Sep 17 00:00:00 2001 From: Bernard Maltais Date: Tue, 27 Sep 2022 15:31:00 -0400 Subject: [PATCH 2/6] Fix file name --- modules/extras.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/extras.py b/modules/extras.py index 9c3b421df..9e1efeda5 100644 --- a/modules/extras.py +++ b/modules/extras.py @@ -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/' + from_model_name + '_' + str(interp_amount) + '-' + to_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) From 591c138e32d8a5789053b3ab6f5881aaf8f002bf Mon Sep 17 00:00:00 2001 From: Bernard Maltais Date: Tue, 27 Sep 2022 21:08:07 -0400 Subject: [PATCH 3/6] -Add gradio dropdown list to select checkpoints to merge -Update the name of the model feilds -Update the associated variable names --- modules/sd_models.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/sd_models.py b/modules/sd_models.py index dc81b0dc5..9decc9115 100644 --- a/modules/sd_models.py +++ b/modules/sd_models.py @@ -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): From d641af6a7d6f1edb7dbc584f72f95e036d830c85 Mon Sep 17 00:00:00 2001 From: Bernard Maltais Date: Tue, 27 Sep 2022 21:09:28 -0400 Subject: [PATCH 4/6] - Add gradio dropdown list to select checkpoints - Update checkpoint model fields labels --- modules/extras.py | 28 ++++++++++++++-------------- modules/ui.py | 9 +++++---- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/modules/extras.py b/modules/extras.py index 9e1efeda5..90968352d 100644 --- a/modules/extras.py +++ b/modules/extras.py @@ -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) diff --git a/modules/ui.py b/modules/ui.py index e7382ca8b..4a5326f74 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -860,8 +860,9 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo, run_modelmerger): gr.HTML(value="

A merger of the two checkpoints will be generated in your /models directory.

") 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 ], From 8c48ede135586a3473e4252d166931cefe64aafb Mon Sep 17 00:00:00 2001 From: Bernard Maltais Date: Tue, 27 Sep 2022 21:34:24 -0400 Subject: [PATCH 5/6] Fix variable conversion code issue --- modules/extras.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/modules/extras.py b/modules/extras.py index 90968352d..f6704382a 100644 --- a/modules/extras.py +++ b/modules/extras.py @@ -150,26 +150,26 @@ def run_modelmerger(primary_model_name, secondary_model_name, interp_method, int alpha = alpha * alpha * (3 - (2 * alpha)) return theta0 + ((theta1 - theta0) * alpha) - 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: - secondary_model_filename = 'models/' + secondary_model_name + '.ckpt' - 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: primary_model_filename = 'models/' + primary_model_name + '.ckpt' - print(f"Loading {secondary_model_filename}...") - model_0 = torch.load(secondary_model_filename, map_location='cpu') + 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: + secondary_model_filename = 'models/' + secondary_model_name + '.ckpt' 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'] + primary_model = torch.load(primary_model_filename, map_location='cpu') + + print(f"Loading {secondary_model_filename}...") + secondary_model = torch.load(secondary_model_filename, map_location='cpu') + + theta_0 = primary_model['state_dict'] + theta_1 = secondary_model['state_dict'] theta_funcs = { "Weighted Sum": weighted_sum, @@ -180,7 +180,7 @@ def run_modelmerger(primary_model_name, secondary_model_name, interp_method, int 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: @@ -188,7 +188,7 @@ def run_modelmerger(primary_model_name, secondary_model_name, interp_method, int 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) + torch.save(primary_model, output_modelname) print(f"Checkpoint saved.") return "Checkpoint saved to " + output_modelname From 4c9c2a67028097dd41a46d6b6fa52d7831e359bf Mon Sep 17 00:00:00 2001 From: Bernard Maltais Date: Tue, 27 Sep 2022 22:16:25 -0400 Subject: [PATCH 6/6] -Fix interp_amount rounding in filename -Sort list of checkpoints in list --- modules/extras.py | 4 ++-- modules/ui.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/extras.py b/modules/extras.py index f6704382a..b8ebc6193 100644 --- a/modules/extras.py +++ b/modules/extras.py @@ -186,9 +186,9 @@ def run_modelmerger(primary_model_name, secondary_model_name, interp_method, int if 'model' in key and key not in theta_0: theta_0[key] = theta_1[key] - output_modelname = 'models/' + primary_model_name + '_' + str(interp_amount) + '-' + secondary_model_name + '_' + str(float(1.0) - interp_amount) + '-' + interp_method.replace(" ", "_") + '-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(primary_model, output_modelname) print(f"Checkpoint saved.") - return "Checkpoint saved to " + output_modelname + return "Checkpoint saved to " + output_modelname \ No newline at end of file diff --git a/modules/ui.py b/modules/ui.py index 4a5326f74..7db8edbd8 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -860,7 +860,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo, run_modelmerger): gr.HTML(value="

A merger of the two checkpoints will be generated in your /models directory.

") with gr.Row(): - ckpt_name_list = [x.model_name for x in modules.sd_models.checkpoints_list.values()] + 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)