aboutsummaryrefslogtreecommitdiffstats
path: root/modules/extras.py
diff options
context:
space:
mode:
authorMalumaDev <piano.lu92@gmail.com>2022-10-18 06:55:08 +0000
committerGitHub <noreply@github.com>2022-10-18 06:55:08 +0000
commit1997ccff13fc75af223f571e8c927c3d77273dd9 (patch)
treed3d8e4e64c1fb595bef205e531b03418328331f7 /modules/extras.py
parent589215df22d24d560101ba2b1fd7aaf3233f466e (diff)
parent7432b6f4d2c3001895fc75411a34afae1810c1a2 (diff)
downloadstable-diffusion-webui-gfx803-1997ccff13fc75af223f571e8c927c3d77273dd9.tar.gz
stable-diffusion-webui-gfx803-1997ccff13fc75af223f571e8c927c3d77273dd9.tar.bz2
stable-diffusion-webui-gfx803-1997ccff13fc75af223f571e8c927c3d77273dd9.zip
Merge branch 'master' into test_resolve_conflicts
Diffstat (limited to 'modules/extras.py')
-rw-r--r--modules/extras.py31
1 files changed, 20 insertions, 11 deletions
diff --git a/modules/extras.py b/modules/extras.py
index 0819ed37..c908b43e 100644
--- a/modules/extras.py
+++ b/modules/extras.py
@@ -91,7 +91,8 @@ def run_extras(extras_mode, resize_mode, image, image_folder, input_dir, output_
def upscale(image, scaler_index, resize, mode, resize_w, resize_h, crop):
small = image.crop((image.width // 2, image.height // 2, image.width // 2 + 10, image.height // 2 + 10))
pixels = tuple(np.array(small).flatten().tolist())
- key = (resize, scaler_index, image.width, image.height, gfpgan_visibility, codeformer_visibility, codeformer_weight) + pixels
+ key = (resize, scaler_index, image.width, image.height, gfpgan_visibility, codeformer_visibility, codeformer_weight,
+ resize_mode, upscaling_resize, upscaling_resize_w, upscaling_resize_h, upscaling_crop) + pixels
c = cached_images.get(key)
if c is None:
@@ -175,11 +176,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 +202,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()