diff options
author | yfszzx <yfszzx@gmail.com> | 2022-10-17 07:23:32 +0000 |
---|---|---|
committer | yfszzx <yfszzx@gmail.com> | 2022-10-17 07:23:32 +0000 |
commit | 2a3e7ed872dc9b8da27cccc7f78df092f4a2f578 (patch) | |
tree | 5a288fe646eb58d35f4079b734a6d3765a98fd3f /modules/extras.py | |
parent | 5b1394bead93e5485ced5de10f1c000eea4458c6 (diff) | |
parent | cccc5a20fce4bde9a4299f8790366790735f1d05 (diff) | |
download | stable-diffusion-webui-gfx803-2a3e7ed872dc9b8da27cccc7f78df092f4a2f578.tar.gz stable-diffusion-webui-gfx803-2a3e7ed872dc9b8da27cccc7f78df092f4a2f578.tar.bz2 stable-diffusion-webui-gfx803-2a3e7ed872dc9b8da27cccc7f78df092f4a2f578.zip |
Merge branch 'master' of https://github.com/yfszzx/stable-diffusion-webui-plus
Diffstat (limited to 'modules/extras.py')
-rw-r--r-- | modules/extras.py | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/modules/extras.py b/modules/extras.py index 0819ed37..8dbab240 100644 --- a/modules/extras.py +++ b/modules/extras.py @@ -175,11 +175,14 @@ def run_pnginfo(image): def run_modelmerger(primary_model_name, secondary_model_name, teritary_model_name, interp_method, multiplier, save_as_half, custom_name):
- def weighted_sum(theta0, theta1, theta2, alpha):
+ def weighted_sum(theta0, theta1, alpha):
return ((1 - alpha) * theta0) + (alpha * theta1)
- def add_difference(theta0, theta1, theta2, alpha):
- return theta0 + (theta1 - theta2) * alpha
+ def get_difference(theta1, theta2):
+ return theta1 - theta2
+
+ def add_difference(theta0, theta1_2_diff, alpha):
+ return theta0 + (alpha * theta1_2_diff)
primary_model_info = sd_models.checkpoints_list[primary_model_name]
secondary_model_info = sd_models.checkpoints_list[secondary_model_name]
@@ -198,23 +201,28 @@ def run_modelmerger(primary_model_name, secondary_model_name, teritary_model_nam teritary_model = torch.load(teritary_model_info.filename, map_location='cpu')
theta_2 = sd_models.get_state_dict_from_checkpoint(teritary_model)
else:
+ teritary_model = None
theta_2 = None
theta_funcs = {
- "Weighted sum": weighted_sum,
- "Add difference": add_difference,
+ "Weighted sum": (None, weighted_sum),
+ "Add difference": (get_difference, add_difference),
}
- theta_func = theta_funcs[interp_method]
+ theta_func1, theta_func2 = theta_funcs[interp_method]
print(f"Merging...")
+ if theta_func1:
+ for key in tqdm.tqdm(theta_1.keys()):
+ if 'model' in key:
+ t2 = theta_2.get(key, torch.zeros_like(theta_1[key]))
+ theta_1[key] = theta_func1(theta_1[key], t2)
+ del theta_2, teritary_model
+
for key in tqdm.tqdm(theta_0.keys()):
if 'model' in key and key in theta_1:
- t2 = (theta_2 or {}).get(key)
- if t2 is None:
- t2 = torch.zeros_like(theta_0[key])
- theta_0[key] = theta_func(theta_0[key], theta_1[key], t2, multiplier)
+ theta_0[key] = theta_func2(theta_0[key], theta_1[key], multiplier)
if save_as_half:
theta_0[key] = theta_0[key].half()
|