Commit 1f50971f authored by Jairo Correa's avatar Jairo Correa
Browse files

Merge branch 'master' into fix-vram

parents ad0cc85d ef40e4cd
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ titles = {
    "\u267b\ufe0f": "Reuse seed from last generation, mostly useful if it was randomed",
    "\u{1f3a8}": "Add a random artist to the prompt.",
    "\u2199\ufe0f": "Read generation parameters from prompt into user interface.",
    "\uD83D\uDCC2": "Open images output directory",
    "\u{1f4c2}": "Open images output directory",

    "Inpaint a part of image": "Draw a mask over an image, and the script will regenerate the masked area with content according to prompt",
    "SD upscale": "Upscale image normally, split result into tiles, improve each tile using img2img, merge whole image back",
@@ -47,6 +47,7 @@ titles = {
    "Custom code": "Run Python code. Advanced user only. Must run program with --allow-code for this to work",

    "Prompt S/R": "Separate a list of words with commas, and the first word will be used as a keyword: script will search for this word in the prompt, and replace it with others",
    "Prompt order": "Separate a list of words with commas, and the script will make a variation of prompt with those words for their every possible order",

    "Tiling": "Produce an image that can be tiled.",
    "Tile overlap": "For SD upscale, how much overlap in pixels should there be between tiles. Tiles overlap so that when they are merged back into one picture, there is no clearly visible seam.",
+3 −3
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ import torch
from basicsr.utils.download_util import load_file_from_url

import modules.upscaler
from modules import shared, modelloader
from modules import devices, modelloader
from modules.bsrgan_model_arch import RRDBNet
from modules.paths import models_path

@@ -44,13 +44,13 @@ class UpscalerBSRGAN(modules.upscaler.Upscaler):
        model = self.load_model(selected_file)
        if model is None:
            return img
        model.to(shared.device)
        model.to(devices.device_bsrgan)
        torch.cuda.empty_cache()
        img = np.array(img)
        img = img[:, :, ::-1]
        img = np.moveaxis(img, 2, 0) / 255
        img = torch.from_numpy(img).float()
        img = img.unsqueeze(0).to(shared.device)
        img = img.unsqueeze(0).to(devices.device_bsrgan)
        with torch.no_grad():
            output = model(img)
        output = output.squeeze().float().cpu().clamp_(0, 1).numpy()
+11 −5
Original line number Diff line number Diff line
@@ -69,10 +69,14 @@ def setup_model(dirname):

                self.net = net
                self.face_helper = face_helper
                self.net.to(devices.device_codeformer)

                return net, face_helper

            def send_model_to(self, device):
                self.net.to(device)
                self.face_helper.face_det.to(device)
                self.face_helper.face_parse.to(device)

            def restore(self, np_image, w=None):
                np_image = np_image[:, :, ::-1]

@@ -82,6 +86,8 @@ def setup_model(dirname):
                if self.net is None or self.face_helper is None:
                    return np_image

                self.send_model_to(devices.device_codeformer)

                self.face_helper.clean_all()
                self.face_helper.read_image(np_image)
                self.face_helper.get_face_landmarks_5(only_center_face=False, resize=640, eye_dist_threshold=5)
@@ -97,7 +103,7 @@ def setup_model(dirname):
                            output = self.net(cropped_face_t, w=w if w is not None else shared.opts.code_former_weight, adain=True)[0]
                            restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
                        del output
                        devices.torch_gc()
                        torch.cuda.empty_cache()
                    except Exception as error:
                        print(f'\tFailed inference for CodeFormer: {error}', file=sys.stderr)
                        restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))
@@ -113,10 +119,10 @@ def setup_model(dirname):
                if original_resolution != restored_img.shape[0:2]:
                    restored_img = cv2.resize(restored_img, (0, 0), fx=original_resolution[1]/restored_img.shape[1], fy=original_resolution[0]/restored_img.shape[0], interpolation=cv2.INTER_LINEAR)

                self.face_helper.clean_all()

                if shared.opts.face_restoration_unload:
                    self.net = None
                    self.face_helper = None
                    devices.torch_gc()
                    self.send_model_to(devices.cpu)

                return restored_img

+12 −3
Original line number Diff line number Diff line
import contextlib

import torch
import gc

# has_mps is only available in nightly pytorch (for now), `getattr` for compatibility
from modules import errors

# has_mps is only available in nightly pytorch (for now), `getattr` for compatibility
has_mps = getattr(torch, 'has_mps', False)

cpu = torch.device("cpu")
@@ -33,8 +35,7 @@ def enable_tf32():

errors.run(enable_tf32, "Enabling TF32")

device = get_optimal_device()
device_codeformer = cpu if has_mps else device
device = device_gfpgan = device_bsrgan = device_esrgan = device_scunet = device_codeformer = get_optimal_device()
dtype = torch.float16

def randn(seed, shape):
@@ -58,3 +59,11 @@ def randn_without_seed(shape):

    return torch.randn(shape, device=device)


def autocast():
    from modules import shared

    if dtype == torch.float32 or shared.cmd_opts.precision == "full":
        return contextlib.nullcontext()

    return torch.autocast("cuda")
+4 −5
Original line number Diff line number Diff line
@@ -6,8 +6,7 @@ from PIL import Image
from basicsr.utils.download_util import load_file_from_url

import modules.esrgam_model_arch as arch
from modules import shared, modelloader, images
from modules.devices import has_mps
from modules import shared, modelloader, images, devices
from modules.paths import models_path
from modules.upscaler import Upscaler, UpscalerData
from modules.shared import opts
@@ -97,7 +96,7 @@ class UpscalerESRGAN(Upscaler):
        model = self.load_model(selected_model)
        if model is None:
            return img
        model.to(shared.device)
        model.to(devices.device_esrgan)
        img = esrgan_upscale(model, img)
        return img

@@ -112,7 +111,7 @@ class UpscalerESRGAN(Upscaler):
            print("Unable to load %s from %s" % (self.model_path, filename))
            return None

        pretrained_net = torch.load(filename, map_location='cpu' if has_mps else None)
        pretrained_net = torch.load(filename, map_location='cpu' if shared.device.type == 'mps' else None)
        crt_model = arch.RRDBNet(3, 3, 64, 23, gc=32)

        pretrained_net = fix_model_layers(crt_model, pretrained_net)
@@ -127,7 +126,7 @@ def upscale_without_tiling(model, img):
    img = img[:, :, ::-1]
    img = np.moveaxis(img, 2, 0) / 255
    img = torch.from_numpy(img).float()
    img = img.unsqueeze(0).to(shared.device)
    img = img.unsqueeze(0).to(devices.device_esrgan)
    with torch.no_grad():
        output = model(img)
    output = output.squeeze().float().cpu().clamp_(0, 1).numpy()
Loading