From 97ee085f308dab41a05938f21f92bcddc63bf345 Mon Sep 17 00:00:00 2001 From: cmdr2 Date: Thu, 17 Nov 2022 12:27:06 +0530 Subject: [PATCH] Fix a bug where Face Correction (GFPGAN) would fail on cuda:N (i.e. GPUs other than cuda:0), as well as fail on CPU if the system had an incompatible GPU. --- CHANGES.md | 13 ++++++------- ui/index.html | 2 +- ui/sd_internal/runtime.py | 18 ++++++++++-------- ui/sd_internal/task_manager.py | 4 ---- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index c9553b22..eaf13e42 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,9 +1,8 @@ # What's new? -### 2.4.6 -* 16 Nov 2022 - Fix a regression in VRAM usage during startup, which caused 'Out of Memory' errors when starting on GPUs with 4gb (or less) VRAM - -### 2.4.5 -* 16 Nov 2022 - Add checkbox for "Open browser on startup". -* 16 Nov 2022 - Add a directory for core plugins that ship with Stable Diffusion UI by default. -* 16 Nov 2022 - Add a "What's New?" tab as a core plugin, which fetches the contents of CHANGES.md from the app's release branch. +### 2.4 +* 2.4.7 - 17 Nov 2022 - Fix a bug where Face Correction (GFPGAN) would fail on cuda:N (i.e. GPUs other than cuda:0), as well as fail on CPU if the system had an incompatible GPU. +* 2.4.6 - 16 Nov 2022 - Fix a regression in VRAM usage during startup, which caused 'Out of Memory' errors when starting on GPUs with 4gb (or less) VRAM +* 2.4.5 - 16 Nov 2022 - Add checkbox for "Open browser on startup". +* 2.4.5 - 16 Nov 2022 - Add a directory for core plugins that ship with Stable Diffusion UI by default. +* 2.4.5 - 16 Nov 2022 - Add a "What's New?" tab as a core plugin, which fetches the contents of CHANGES.md from the app's release branch. diff --git a/ui/index.html b/ui/index.html index 9b04c9d0..5ffea432 100644 --- a/ui/index.html +++ b/ui/index.html @@ -20,7 +20,7 @@
diff --git a/ui/sd_internal/runtime.py b/ui/sd_internal/runtime.py index e2346730..2308298e 100644 --- a/ui/sd_internal/runtime.py +++ b/ui/sd_internal/runtime.py @@ -236,9 +236,14 @@ def wait_model_move_to(model, target_device): # Send to target_device and wait u def load_model_gfpgan(): if thread_data.gfpgan_file is None: raise ValueError(f'Thread gfpgan_file is undefined.') + + # hack for a bug in facexlib: https://github.com/xinntao/facexlib/pull/19/files + from facexlib.detection import retinaface + retinaface.device = torch.device(thread_data.device) + print('forced retinaface.device to', thread_data.device) + model_path = thread_data.gfpgan_file + ".pth" - device = 'cuda:0' if force_gfpgan_to_cuda0 else thread_data.device - thread_data.model_gfpgan = GFPGANer(device=torch.device(device), model_path=model_path, upscale=1, arch='clean', channel_multiplier=2, bg_upsampler=None) + thread_data.model_gfpgan = GFPGANer(device=torch.device(thread_data.device), model_path=model_path, upscale=1, arch='clean', channel_multiplier=2, bg_upsampler=None) print('loaded', thread_data.gfpgan_file, 'to', thread_data.model_gfpgan.device, 'precision', thread_data.precision) def load_model_real_esrgan(): @@ -288,10 +293,10 @@ def apply_filters(filter_name, image_data, model_path=None): print(f'Applying filter {filter_name}...') gc() # Free space before loading new data. - if filter_name == 'gfpgan': - if isinstance(image_data, torch.Tensor): - image_data.to('cuda:0' if force_gfpgan_to_cuda0 else thread_data.device) + if isinstance(image_data, torch.Tensor): + image_data.to(thread_data.device) + if filter_name == 'gfpgan': if model_path is not None and model_path != thread_data.gfpgan_file: thread_data.gfpgan_file = model_path load_model_gfpgan() @@ -303,9 +308,6 @@ def apply_filters(filter_name, image_data, model_path=None): image_data = output[:,:,::-1] if filter_name == 'real_esrgan': - if isinstance(image_data, torch.Tensor): - image_data.to(thread_data.device) - if model_path is not None and model_path != thread_data.real_esrgan_file: thread_data.real_esrgan_file = model_path load_model_real_esrgan() diff --git a/ui/sd_internal/task_manager.py b/ui/sd_internal/task_manager.py index d10afdd8..45f90f9f 100644 --- a/ui/sd_internal/task_manager.py +++ b/ui/sd_internal/task_manager.py @@ -217,10 +217,6 @@ def thread_get_next_task(): task = None try: # Select a render task. for queued_task in tasks_queue: - if queued_task.request.use_face_correction and runtime.thread_data.device == 'cpu' and is_alive() == 1: - queued_task.error = Exception('The CPU cannot be used to run this task currently. Please remove "Fix incorrect faces" from Image Settings and try again.') - task = queued_task - break if queued_task.render_device and runtime.thread_data.device != queued_task.render_device: # Is asking for a specific render device. if is_alive(queued_task.render_device) > 0: