diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/codeformer_model.py | 8 | ||||
-rw-r--r-- | modules/devices.py | 17 | ||||
-rw-r--r-- | modules/extras.py | 4 | ||||
-rw-r--r-- | modules/images.py | 18 | ||||
-rw-r--r-- | modules/processing.py | 19 | ||||
-rw-r--r-- | modules/ui.py | 4 |
6 files changed, 50 insertions, 20 deletions
diff --git a/modules/codeformer_model.py b/modules/codeformer_model.py index fd1da692..6cd29c83 100644 --- a/modules/codeformer_model.py +++ b/modules/codeformer_model.py @@ -47,6 +47,8 @@ def setup_codeformer(): def __init__(self):
self.net = None
self.face_helper = None
+ if shared.device.type == 'mps': # CodeFormer currently does not support mps backend
+ shared.device_codeformer = torch.device('cpu')
def create_models(self):
@@ -54,13 +56,13 @@ def setup_codeformer(): self.net.to(shared.device)
return self.net, self.face_helper
- net = net_class(dim_embd=512, codebook_size=1024, n_head=8, n_layers=9, connect_list=['32', '64', '128', '256']).to(shared.device)
+ net = net_class(dim_embd=512, codebook_size=1024, n_head=8, n_layers=9, connect_list=['32', '64', '128', '256']).to(shared.device_codeformer)
ckpt_path = load_file_from_url(url=pretrain_model_url, model_dir=os.path.join(path, 'weights/CodeFormer'), progress=True)
checkpoint = torch.load(ckpt_path)['params_ema']
net.load_state_dict(checkpoint)
net.eval()
- face_helper = FaceRestoreHelper(1, face_size=512, crop_ratio=(1, 1), det_model='retinaface_resnet50', save_ext='png', use_parse=True, device=shared.device)
+ face_helper = FaceRestoreHelper(1, face_size=512, crop_ratio=(1, 1), det_model='retinaface_resnet50', save_ext='png', use_parse=True, device=shared.device_codeformer)
self.net = net
self.face_helper = face_helper
@@ -82,7 +84,7 @@ def setup_codeformer(): for idx, cropped_face in enumerate(self.face_helper.cropped_faces):
cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
- cropped_face_t = cropped_face_t.unsqueeze(0).to(shared.device)
+ cropped_face_t = cropped_face_t.unsqueeze(0).to(shared.device_codeformer)
try:
with torch.no_grad():
diff --git a/modules/devices.py b/modules/devices.py index a93a245b..e4430e1a 100644 --- a/modules/devices.py +++ b/modules/devices.py @@ -31,3 +31,20 @@ def enable_tf32(): errors.run(enable_tf32, "Enabling TF32") + + +device = get_optimal_device() +device_codeformer = cpu if has_mps else device + + +def randn(seed, shape): + # Pytorch currently doesn't handle setting randomness correctly when the metal backend is used. + if device.type == 'mps': + generator = torch.Generator(device=cpu) + generator.manual_seed(seed) + noise = torch.randn(shape, generator=generator, device=cpu).to(device) + return noise + + torch.manual_seed(seed) + return torch.randn(shape, device=device) + diff --git a/modules/extras.py b/modules/extras.py index 40935f98..596cd172 100644 --- a/modules/extras.py +++ b/modules/extras.py @@ -13,6 +13,8 @@ cached_images = {} def run_extras(image, gfpgan_visibility, codeformer_visibility, codeformer_weight, upscaling_resize, extras_upscaler_1, extras_upscaler_2, extras_upscaler_2_visibility):
devices.torch_gc()
+ existing_pnginfo = image.info or {}
+
image = image.convert("RGB")
info = ""
@@ -65,7 +67,7 @@ def run_extras(image, gfpgan_visibility, codeformer_visibility, codeformer_weigh while len(cached_images) > 2:
del cached_images[next(iter(cached_images.keys()))]
- images.save_image(image, outpath, "", None, info=info, extension=opts.samples_format, short_filename=True, no_prompt=True, pnginfo_section_name="extras")
+ images.save_image(image, outpath, "", None, info=info, extension=opts.samples_format, short_filename=True, no_prompt=True, pnginfo_section_name="extras", existing_info=existing_pnginfo)
return image, plaintext_to_html(info), ''
diff --git a/modules/images.py b/modules/images.py index 334f8fec..d742ed98 100644 --- a/modules/images.py +++ b/modules/images.py @@ -135,7 +135,12 @@ def draw_grid_annotations(im, width, height, hor_texts, ver_texts): fontsize = (width + height) // 25
line_spacing = fontsize // 2
- fnt = ImageFont.truetype(opts.font or Roboto, fontsize)
+
+ try:
+ fnt = ImageFont.truetype(opts.font or Roboto, fontsize)
+ except Exception:
+ fnt = ImageFont.truetype(Roboto, fontsize)
+
color_active = (0, 0, 0)
color_inactive = (153, 153, 153)
@@ -247,7 +252,7 @@ def sanitize_filename_part(text, replace_spaces=True): return text.translate({ord(x): '' for x in invalid_filename_chars})[:128]
-def save_image(image, path, basename, seed=None, prompt=None, extension='png', info=None, short_filename=False, no_prompt=False, pnginfo_section_name='parameters', p=None):
+def save_image(image, path, basename, seed=None, prompt=None, extension='png', info=None, short_filename=False, no_prompt=False, pnginfo_section_name='parameters', p=None, existing_info=None):
# would be better to add this as an argument in future, but will do for now
is_a_grid = basename != ""
@@ -258,7 +263,9 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i else:
file_decoration = opts.samples_filename_format or "[seed]-[prompt_spaces]"
- file_decoration = "-" + file_decoration.lower()
+ if file_decoration != "":
+ file_decoration = "-" + file_decoration.lower()
+
if seed is not None:
file_decoration = file_decoration.replace("[seed]", str(seed))
if prompt is not None:
@@ -273,6 +280,11 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i if extension == 'png' and opts.enable_pnginfo and info is not None:
pnginfo = PngImagePlugin.PngInfo()
+
+ if existing_info is not None:
+ for k, v in existing_info.items():
+ pnginfo.add_text(k, v)
+
pnginfo.add_text(pnginfo_section_name, info)
else:
pnginfo = None
diff --git a/modules/processing.py b/modules/processing.py index aaecb104..23b0c08f 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -65,6 +65,7 @@ class Processed: def __init__(self, p: StableDiffusionProcessing, images_list, seed, info):
self.images = images_list
self.prompt = p.prompt
+ self.negative_prompt = p.negative_prompt
self.seed = seed
self.info = info
self.width = p.width
@@ -76,6 +77,7 @@ class Processed: def js(self):
obj = {
"prompt": self.prompt if type(self.prompt) != list else self.prompt[0],
+ "negative_prompt": self.negative_prompt if type(self.negative_prompt) != list else self.negative_prompt[0],
"seed": int(self.seed if type(self.seed) != list else self.seed[0]),
"width": self.width,
"height": self.height,
@@ -104,15 +106,14 @@ def create_random_tensors(shape, seeds, subseeds=None, subseed_strength=0.0, see subnoise = None
if subseeds is not None:
subseed = 0 if i >= len(subseeds) else subseeds[i]
- torch.manual_seed(subseed)
- subnoise = torch.randn(noise_shape, device=shared.device)
+
+ subnoise = devices.randn(subseed, noise_shape)
# randn results depend on device; gpu and cpu get different results for same seed;
# the way I see it, it's better to do this on CPU, so that everyone gets same result;
# but the original script had it like this, so I do not dare change it for now because
# it will break everyone's seeds.
- torch.manual_seed(seed)
- noise = torch.randn(noise_shape, device=shared.device)
+ noise = devices.randn(seed, noise_shape)
if subnoise is not None:
#noise = subnoise * subseed_strength + noise * (1 - subseed_strength)
@@ -120,12 +121,8 @@ def create_random_tensors(shape, seeds, subseeds=None, subseed_strength=0.0, see if noise_shape != shape:
#noise = torch.nn.functional.interpolate(noise.unsqueeze(1), size=shape[1:], mode="bilinear").squeeze()
- # noise_shape = (64, 80)
- # shape = (64, 72)
-
- torch.manual_seed(seed)
- x = torch.randn(shape, device=shared.device)
- dx = (shape[2] - noise_shape[2]) // 2 # -4
+ x = devices.randn(seed, shape)
+ dx = (shape[2] - noise_shape[2]) // 2
dy = (shape[1] - noise_shape[1]) // 2
w = noise_shape[2] if dx >= 0 else noise_shape[2] + 2 * dx
h = noise_shape[1] if dy >= 0 else noise_shape[1] + 2 * dy
@@ -463,7 +460,7 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing): if self.image_mask is not None:
init_mask = latent_mask
latmask = init_mask.convert('RGB').resize((self.init_latent.shape[3], self.init_latent.shape[2]))
- latmask = np.moveaxis(np.array(latmask, dtype=np.float64), 2, 0) / 255
+ latmask = np.moveaxis(np.array(latmask, dtype=np.float32), 2, 0) / 255
latmask = latmask[0]
latmask = np.around(latmask)
latmask = np.tile(latmask[None], (4, 1, 1))
diff --git a/modules/ui.py b/modules/ui.py index c32c5096..535afaeb 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -93,7 +93,7 @@ def save_files(js_data, images): at_start = file.tell() == 0
writer = csv.writer(file)
if at_start:
- writer.writerow(["prompt", "seed", "width", "height", "sampler", "cfgs", "steps", "filename"])
+ writer.writerow(["prompt", "seed", "width", "height", "sampler", "cfgs", "steps", "filename", "negative_prompt"])
filename_base = str(int(time.time() * 1000))
for i, filedata in enumerate(images):
@@ -108,7 +108,7 @@ def save_files(js_data, images): filenames.append(filename)
- writer.writerow([data["prompt"], data["seed"], data["width"], data["height"], data["sampler"], data["cfg_scale"], data["steps"], filenames[0]])
+ writer.writerow([data["prompt"], data["seed"], data["width"], data["height"], data["sampler"], data["cfg_scale"], data["steps"], filenames[0], data["negative_prompt"]])
return '', '', plaintext_to_html(f"Saved: {filenames[0]}")
|