2023-12-25 12:43:51 +00:00
|
|
|
from modules import modelloader, devices, errors
|
2022-09-26 14:29:50 +00:00
|
|
|
from modules.shared import opts
|
2023-05-29 07:38:51 +00:00
|
|
|
from modules.upscaler import Upscaler, UpscalerData
|
2023-12-27 09:04:33 +00:00
|
|
|
from modules.upscaler_utils import upscale_with_model
|
2022-09-04 15:54:12 +00:00
|
|
|
|
2022-09-30 08:42:40 +00:00
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
class UpscalerESRGAN(Upscaler):
|
|
|
|
def __init__(self, dirname):
|
|
|
|
self.name = "ESRGAN"
|
2022-10-02 17:58:17 +00:00
|
|
|
self.model_url = "https://github.com/cszn/KAIR/releases/download/v1.0/ESRGAN.pth"
|
|
|
|
self.model_name = "ESRGAN_4x"
|
2022-09-29 22:46:23 +00:00
|
|
|
self.scalers = []
|
|
|
|
self.user_path = dirname
|
|
|
|
super().__init__()
|
|
|
|
model_paths = self.find_models(ext_filter=[".pt", ".pth"])
|
|
|
|
scalers = []
|
|
|
|
if len(model_paths) == 0:
|
|
|
|
scaler_data = UpscalerData(self.model_name, self.model_url, self, 4)
|
|
|
|
scalers.append(scaler_data)
|
|
|
|
for file in model_paths:
|
2023-05-29 06:41:36 +00:00
|
|
|
if file.startswith("http"):
|
2022-09-29 22:46:23 +00:00
|
|
|
name = self.model_name
|
|
|
|
else:
|
|
|
|
name = modelloader.friendly_name(file)
|
|
|
|
|
|
|
|
scaler_data = UpscalerData(name, file, self, 4)
|
|
|
|
self.scalers.append(scaler_data)
|
|
|
|
|
|
|
|
def do_upscale(self, img, selected_model):
|
2023-05-29 07:38:51 +00:00
|
|
|
try:
|
|
|
|
model = self.load_model(selected_model)
|
2023-12-25 12:43:51 +00:00
|
|
|
except Exception:
|
|
|
|
errors.report(f"Unable to load ESRGAN model {selected_model}", exc_info=True)
|
2022-09-29 22:46:23 +00:00
|
|
|
return img
|
2022-10-04 08:24:35 +00:00
|
|
|
model.to(devices.device_esrgan)
|
2023-12-25 12:43:51 +00:00
|
|
|
return esrgan_upscale(model, img)
|
2022-09-08 12:49:47 +00:00
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
def load_model(self, path: str):
|
2023-05-29 06:41:36 +00:00
|
|
|
if path.startswith("http"):
|
2023-05-29 06:45:07 +00:00
|
|
|
# TODO: this doesn't use `path` at all?
|
2023-05-29 06:34:26 +00:00
|
|
|
filename = modelloader.load_file_from_url(
|
2023-05-09 19:17:58 +00:00
|
|
|
url=self.model_url,
|
2023-05-19 06:09:00 +00:00
|
|
|
model_dir=self.model_download_path,
|
2023-05-09 19:17:58 +00:00
|
|
|
file_name=f"{self.model_name}.pth",
|
|
|
|
)
|
2022-09-04 15:54:12 +00:00
|
|
|
else:
|
2022-09-29 22:46:23 +00:00
|
|
|
filename = path
|
2022-09-30 08:42:40 +00:00
|
|
|
|
2023-12-25 12:43:51 +00:00
|
|
|
return modelloader.load_spandrel_model(
|
|
|
|
filename,
|
|
|
|
device=('cpu' if devices.device_esrgan.type == 'mps' else None),
|
2023-12-30 14:37:03 +00:00
|
|
|
expected_architecture='ESRGAN',
|
2023-12-25 12:43:51 +00:00
|
|
|
)
|
2022-09-29 22:46:23 +00:00
|
|
|
|
2022-09-04 15:54:12 +00:00
|
|
|
|
|
|
|
def esrgan_upscale(model, img):
|
2023-12-27 09:04:33 +00:00
|
|
|
return upscale_with_model(
|
|
|
|
model,
|
|
|
|
img,
|
|
|
|
tile_size=opts.ESRGAN_tile,
|
|
|
|
tile_overlap=opts.ESRGAN_tile_overlap,
|
|
|
|
)
|