Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AUTOMATIC1111
GitHub Repository: AUTOMATIC1111/stable-diffusion-webui
Path: blob/master/modules/realesrgan_model.py
3575 views
1
import os
2
3
from modules import modelloader, errors
4
from modules.shared import cmd_opts, opts
5
from modules.upscaler import Upscaler, UpscalerData
6
from modules.upscaler_utils import upscale_with_model
7
8
9
class UpscalerRealESRGAN(Upscaler):
10
def __init__(self, path):
11
self.name = "RealESRGAN"
12
self.user_path = path
13
super().__init__()
14
self.enable = True
15
self.scalers = []
16
scalers = get_realesrgan_models(self)
17
18
local_model_paths = self.find_models(ext_filter=[".pth"])
19
for scaler in scalers:
20
if scaler.local_data_path.startswith("http"):
21
filename = modelloader.friendly_name(scaler.local_data_path)
22
local_model_candidates = [local_model for local_model in local_model_paths if local_model.endswith(f"{filename}.pth")]
23
if local_model_candidates:
24
scaler.local_data_path = local_model_candidates[0]
25
26
if scaler.name in opts.realesrgan_enabled_models:
27
self.scalers.append(scaler)
28
29
def do_upscale(self, img, path):
30
if not self.enable:
31
return img
32
33
try:
34
info = self.load_model(path)
35
except Exception:
36
errors.report(f"Unable to load RealESRGAN model {path}", exc_info=True)
37
return img
38
39
model_descriptor = modelloader.load_spandrel_model(
40
info.local_data_path,
41
device=self.device,
42
prefer_half=(not cmd_opts.no_half and not cmd_opts.upcast_sampling),
43
expected_architecture="ESRGAN", # "RealESRGAN" isn't a specific thing for Spandrel
44
)
45
return upscale_with_model(
46
model_descriptor,
47
img,
48
tile_size=opts.ESRGAN_tile,
49
tile_overlap=opts.ESRGAN_tile_overlap,
50
# TODO: `outscale`?
51
)
52
53
def load_model(self, path):
54
for scaler in self.scalers:
55
if scaler.data_path == path:
56
if scaler.local_data_path.startswith("http"):
57
scaler.local_data_path = modelloader.load_file_from_url(
58
scaler.data_path,
59
model_dir=self.model_download_path,
60
)
61
if not os.path.exists(scaler.local_data_path):
62
raise FileNotFoundError(f"RealESRGAN data missing: {scaler.local_data_path}")
63
return scaler
64
raise ValueError(f"Unable to find model info: {path}")
65
66
67
def get_realesrgan_models(scaler: UpscalerRealESRGAN):
68
return [
69
UpscalerData(
70
name="R-ESRGAN General 4xV3",
71
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth",
72
scale=4,
73
upscaler=scaler,
74
),
75
UpscalerData(
76
name="R-ESRGAN General WDN 4xV3",
77
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth",
78
scale=4,
79
upscaler=scaler,
80
),
81
UpscalerData(
82
name="R-ESRGAN AnimeVideo",
83
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pth",
84
scale=4,
85
upscaler=scaler,
86
),
87
UpscalerData(
88
name="R-ESRGAN 4x+",
89
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
90
scale=4,
91
upscaler=scaler,
92
),
93
UpscalerData(
94
name="R-ESRGAN 4x+ Anime6B",
95
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth",
96
scale=4,
97
upscaler=scaler,
98
),
99
UpscalerData(
100
name="R-ESRGAN 2x+",
101
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth",
102
scale=2,
103
upscaler=scaler,
104
),
105
]
106
107