2022-09-30 14:28:28 +00:00
|
|
|
import glob
|
2022-09-26 14:29:50 +00:00
|
|
|
import os
|
2022-09-26 15:27:18 +00:00
|
|
|
import shutil
|
2022-09-29 22:46:23 +00:00
|
|
|
import importlib
|
2022-09-26 14:29:50 +00:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
from basicsr.utils.download_util import load_file_from_url
|
2022-09-29 22:46:23 +00:00
|
|
|
from modules import shared
|
2023-03-06 18:18:35 +00:00
|
|
|
from modules.upscaler import Upscaler, UpscalerLanczos, UpscalerNearest, UpscalerNone
|
2022-09-26 15:27:18 +00:00
|
|
|
from modules.paths import script_path, models_path
|
|
|
|
|
2022-09-26 14:29:50 +00:00
|
|
|
|
2023-01-10 13:51:04 +00:00
|
|
|
def load_models(model_path: str, model_url: str = None, command_path: str = None, ext_filter=None, download_name=None, ext_blacklist=None) -> list:
|
2022-09-26 14:29:50 +00:00
|
|
|
"""
|
|
|
|
A one-and done loader to try finding the desired models in specified directories.
|
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
@param download_name: Specify to download from model_url immediately.
|
|
|
|
@param model_url: If no other models are found, this will be downloaded on upscale.
|
2022-09-26 14:29:50 +00:00
|
|
|
@param model_path: The location to store/find models in.
|
|
|
|
@param command_path: A command-line argument to search for models in first.
|
|
|
|
@param ext_filter: An optional list of filename extensions to filter by
|
|
|
|
@return: A list of paths containing the desired model(s)
|
|
|
|
"""
|
2022-09-29 22:46:23 +00:00
|
|
|
output = []
|
|
|
|
|
2022-09-26 14:29:50 +00:00
|
|
|
if ext_filter is None:
|
|
|
|
ext_filter = []
|
2022-09-30 08:42:40 +00:00
|
|
|
|
2022-09-26 14:29:50 +00:00
|
|
|
try:
|
|
|
|
places = []
|
2022-09-30 08:42:40 +00:00
|
|
|
|
2022-09-26 14:29:50 +00:00
|
|
|
if command_path is not None and command_path != model_path:
|
|
|
|
pretrained_path = os.path.join(command_path, 'experiments/pretrained_models')
|
|
|
|
if os.path.exists(pretrained_path):
|
2022-09-29 22:46:23 +00:00
|
|
|
print(f"Appending path: {pretrained_path}")
|
2022-09-26 14:29:50 +00:00
|
|
|
places.append(pretrained_path)
|
|
|
|
elif os.path.exists(command_path):
|
|
|
|
places.append(command_path)
|
2022-09-30 08:42:40 +00:00
|
|
|
|
2022-09-26 14:29:50 +00:00
|
|
|
places.append(model_path)
|
2022-09-30 08:42:40 +00:00
|
|
|
|
2022-09-26 14:29:50 +00:00
|
|
|
for place in places:
|
|
|
|
if os.path.exists(place):
|
2022-09-30 14:28:28 +00:00
|
|
|
for file in glob.iglob(place + '**/**', recursive=True):
|
2022-10-02 12:49:42 +00:00
|
|
|
full_path = file
|
2022-09-29 22:46:23 +00:00
|
|
|
if os.path.isdir(full_path):
|
2022-09-26 14:29:50 +00:00
|
|
|
continue
|
2023-02-04 15:52:15 +00:00
|
|
|
if os.path.islink(full_path) and not os.path.exists(full_path):
|
|
|
|
print(f"Skipping broken symlink: {full_path}")
|
|
|
|
continue
|
2023-01-10 13:51:04 +00:00
|
|
|
if ext_blacklist is not None and any([full_path.endswith(x) for x in ext_blacklist]):
|
|
|
|
continue
|
2022-09-26 14:29:50 +00:00
|
|
|
if len(ext_filter) != 0:
|
|
|
|
model_name, extension = os.path.splitext(file)
|
|
|
|
if extension not in ext_filter:
|
|
|
|
continue
|
2022-09-29 22:46:23 +00:00
|
|
|
if file not in output:
|
|
|
|
output.append(full_path)
|
2022-09-30 08:42:40 +00:00
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
if model_url is not None and len(output) == 0:
|
|
|
|
if download_name is not None:
|
|
|
|
dl = load_file_from_url(model_url, model_path, True, download_name)
|
|
|
|
output.append(dl)
|
2022-09-26 14:29:50 +00:00
|
|
|
else:
|
2022-09-29 22:46:23 +00:00
|
|
|
output.append(model_url)
|
2022-09-30 08:42:40 +00:00
|
|
|
|
|
|
|
except Exception:
|
2022-09-26 14:29:50 +00:00
|
|
|
pass
|
2022-09-30 08:42:40 +00:00
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
return output
|
2022-09-26 14:29:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def friendly_name(file: str):
|
|
|
|
if "http" in file:
|
|
|
|
file = urlparse(file).path
|
|
|
|
|
|
|
|
file = os.path.basename(file)
|
|
|
|
model_name, extension = os.path.splitext(file)
|
|
|
|
return model_name
|
2022-09-26 15:27:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def cleanup_models():
|
2022-09-27 16:01:13 +00:00
|
|
|
# This code could probably be more efficient if we used a tuple list or something to store the src/destinations
|
|
|
|
# and then enumerate that, but this works for now. In the future, it'd be nice to just have every "model" scaler
|
|
|
|
# somehow auto-register and just do these things...
|
2022-09-26 15:27:18 +00:00
|
|
|
root_path = script_path
|
2022-09-27 16:01:13 +00:00
|
|
|
src_path = models_path
|
|
|
|
dest_path = os.path.join(models_path, "Stable-diffusion")
|
|
|
|
move_files(src_path, dest_path, ".ckpt")
|
2022-11-19 19:49:22 +00:00
|
|
|
move_files(src_path, dest_path, ".safetensors")
|
2022-09-26 15:27:18 +00:00
|
|
|
src_path = os.path.join(root_path, "ESRGAN")
|
|
|
|
dest_path = os.path.join(models_path, "ESRGAN")
|
|
|
|
move_files(src_path, dest_path)
|
2022-10-30 11:52:50 +00:00
|
|
|
src_path = os.path.join(models_path, "BSRGAN")
|
|
|
|
dest_path = os.path.join(models_path, "ESRGAN")
|
|
|
|
move_files(src_path, dest_path, ".pth")
|
2022-09-26 15:27:18 +00:00
|
|
|
src_path = os.path.join(root_path, "gfpgan")
|
|
|
|
dest_path = os.path.join(models_path, "GFPGAN")
|
|
|
|
move_files(src_path, dest_path)
|
|
|
|
src_path = os.path.join(root_path, "SwinIR")
|
|
|
|
dest_path = os.path.join(models_path, "SwinIR")
|
|
|
|
move_files(src_path, dest_path)
|
|
|
|
src_path = os.path.join(root_path, "repositories/latent-diffusion/experiments/pretrained_models/")
|
|
|
|
dest_path = os.path.join(models_path, "LDSR")
|
|
|
|
move_files(src_path, dest_path)
|
|
|
|
|
|
|
|
|
2022-09-27 16:01:13 +00:00
|
|
|
def move_files(src_path: str, dest_path: str, ext_filter: str = None):
|
2022-09-26 15:27:18 +00:00
|
|
|
try:
|
|
|
|
if not os.path.exists(dest_path):
|
|
|
|
os.makedirs(dest_path)
|
|
|
|
if os.path.exists(src_path):
|
|
|
|
for file in os.listdir(src_path):
|
2022-09-27 16:01:13 +00:00
|
|
|
fullpath = os.path.join(src_path, file)
|
|
|
|
if os.path.isfile(fullpath):
|
|
|
|
if ext_filter is not None:
|
|
|
|
if ext_filter not in file:
|
|
|
|
continue
|
|
|
|
print(f"Moving {file} from {src_path} to {dest_path}.")
|
2022-09-26 15:27:18 +00:00
|
|
|
try:
|
|
|
|
shutil.move(fullpath, dest_path)
|
|
|
|
except:
|
|
|
|
pass
|
2022-09-27 16:01:13 +00:00
|
|
|
if len(os.listdir(src_path)) == 0:
|
|
|
|
print(f"Removing empty folder: {src_path}")
|
|
|
|
shutil.rmtree(src_path, True)
|
2022-09-26 15:27:18 +00:00
|
|
|
except:
|
2022-09-29 22:46:23 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-01-03 15:38:21 +00:00
|
|
|
builtin_upscaler_classes = []
|
|
|
|
forbidden_upscaler_classes = set()
|
|
|
|
|
|
|
|
|
|
|
|
def list_builtin_upscalers():
|
|
|
|
load_upscalers()
|
|
|
|
|
|
|
|
builtin_upscaler_classes.clear()
|
|
|
|
builtin_upscaler_classes.extend(Upscaler.__subclasses__())
|
|
|
|
|
|
|
|
|
|
|
|
def forbid_loaded_nonbuiltin_upscalers():
|
|
|
|
for cls in Upscaler.__subclasses__():
|
|
|
|
if cls not in builtin_upscaler_classes:
|
|
|
|
forbidden_upscaler_classes.add(cls)
|
|
|
|
|
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
def load_upscalers():
|
2022-09-30 20:26:18 +00:00
|
|
|
# We can only do this 'magic' method to dynamically load upscalers if they are referenced,
|
|
|
|
# so we'll try to import any _model.py files before looking in __subclasses__
|
2022-12-03 15:06:33 +00:00
|
|
|
modules_dir = os.path.join(shared.script_path, "modules")
|
2022-09-30 20:26:18 +00:00
|
|
|
for file in os.listdir(modules_dir):
|
|
|
|
if "_model.py" in file:
|
|
|
|
model_name = file.replace("_model.py", "")
|
|
|
|
full_model = f"modules.{model_name}_model"
|
|
|
|
try:
|
|
|
|
importlib.import_module(full_model)
|
|
|
|
except:
|
|
|
|
pass
|
2022-12-03 15:06:33 +00:00
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
datas = []
|
2022-12-03 15:06:33 +00:00
|
|
|
commandline_options = vars(shared.cmd_opts)
|
2022-09-29 22:46:23 +00:00
|
|
|
for cls in Upscaler.__subclasses__():
|
2023-01-03 15:38:21 +00:00
|
|
|
if cls in forbidden_upscaler_classes:
|
|
|
|
continue
|
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
name = cls.__name__
|
2022-09-30 20:26:18 +00:00
|
|
|
cmd_name = f"{name.lower().replace('upscaler', '')}_models_path"
|
2022-12-03 15:06:33 +00:00
|
|
|
scaler = cls(commandline_options.get(cmd_name, None))
|
|
|
|
datas += scaler.scalers
|
2022-09-29 22:46:23 +00:00
|
|
|
|
2023-02-24 19:22:58 +00:00
|
|
|
shared.sd_upscalers = sorted(
|
|
|
|
datas,
|
|
|
|
# Special case for UpscalerNone keeps it at the beginning of the list.
|
2023-03-06 18:18:35 +00:00
|
|
|
key=lambda x: x.name.lower() if not isinstance(x.scaler, (UpscalerNone, UpscalerLanczos, UpscalerNearest)) else ""
|
2023-02-24 19:22:58 +00:00
|
|
|
)
|