(feat): Rework Checkpoint Merger UI for better clarity
- Rename variables to better align with field input - Improve merged checkpoint output file name
This commit is contained in:
parent
f2a4a2c3a6
commit
20b41b7be7
|
@ -140,7 +140,7 @@ def run_pnginfo(image):
|
||||||
return '', geninfo, info
|
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)
|
# Linear interpolation (https://en.wikipedia.org/wiki/Linear_interpolation)
|
||||||
def weighted_sum(theta0, theta1, alpha):
|
def weighted_sum(theta0, theta1, alpha):
|
||||||
return ((1 - alpha) * theta0) + (alpha * theta1)
|
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))
|
alpha = alpha * alpha * (3 - (2 * alpha))
|
||||||
return theta0 + ((theta1 - theta0) * alpha)
|
return theta0 + ((theta1 - theta0) * alpha)
|
||||||
|
|
||||||
if os.path.exists(modelname_0):
|
if os.path.exists(to_model_name):
|
||||||
model0_filename = modelname_0
|
to_model_filename = to_model_name
|
||||||
modelname_0 = os.path.splitext(os.path.basename(modelname_0))[0]
|
to_model_name = os.path.splitext(os.path.basename(to_model_name))[0]
|
||||||
else:
|
else:
|
||||||
model0_filename = 'models/' + modelname_0 + '.ckpt'
|
to_model_filename = 'models/' + to_model_name + '.ckpt'
|
||||||
|
|
||||||
if os.path.exists(modelname_1):
|
if os.path.exists(from_model_name):
|
||||||
model1_filename = modelname_1
|
from_model_filename = from_model_name
|
||||||
modelname_1 = os.path.splitext(os.path.basename(modelname_1))[0]
|
from_model_name = os.path.splitext(os.path.basename(from_model_name))[0]
|
||||||
else:
|
else:
|
||||||
model1_filename = 'models/' + modelname_1 + '.ckpt'
|
from_model_filename = 'models/' + from_model_name + '.ckpt'
|
||||||
|
|
||||||
print(f"Loading {model0_filename}...")
|
print(f"Loading {to_model_filename}...")
|
||||||
model_0 = torch.load(model0_filename, map_location='cpu')
|
model_0 = torch.load(to_model_filename, map_location='cpu')
|
||||||
|
|
||||||
print(f"Loading {model1_filename}...")
|
print(f"Loading {from_model_filename}...")
|
||||||
model_1 = torch.load(model1_filename, map_location='cpu')
|
model_1 = torch.load(from_model_filename, map_location='cpu')
|
||||||
|
|
||||||
theta_0 = model_0['state_dict']
|
theta_0 = model_0['state_dict']
|
||||||
theta_1 = model_1['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:
|
if 'model' in key and key not in theta_0:
|
||||||
theta_0[key] = theta_1[key]
|
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}...")
|
print(f"Saving to {output_modelname}...")
|
||||||
torch.save(model_0, output_modelname)
|
torch.save(model_0, output_modelname)
|
||||||
|
|
||||||
|
|
|
@ -859,10 +859,11 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo, run_modelmerger):
|
||||||
with gr.Column(variant='panel'):
|
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>")
|
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)")
|
with gr.Row():
|
||||||
modelname_1 = gr.Textbox(elem_id="modelmerger_modelname_1", label="Model Name (from)")
|
from_model_name = gr.Textbox(elem_id="modelmerger_from_model_name", label="Model Name (from)")
|
||||||
interp_method = gr.Radio(choices=["Weighted Sum", "Sigmoid"], value="Weighted Sum", label="Interpolation Method")
|
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_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')
|
submit = gr.Button(elem_id="modelmerger_merge", label="Merge", variant='primary')
|
||||||
|
|
||||||
with gr.Column(variant='panel'):
|
with gr.Column(variant='panel'):
|
||||||
|
@ -871,8 +872,8 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo, run_modelmerger):
|
||||||
submit.click(
|
submit.click(
|
||||||
fn=run_modelmerger,
|
fn=run_modelmerger,
|
||||||
inputs=[
|
inputs=[
|
||||||
modelname_0,
|
from_model_name,
|
||||||
modelname_1,
|
to_model_name,
|
||||||
interp_method,
|
interp_method,
|
||||||
interp_amount
|
interp_amount
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in New Issue