Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AUTOMATIC1111
GitHub Repository: AUTOMATIC1111/stable-diffusion-webui
Path: blob/master/extensions-builtin/ScuNET/scripts/scunet_model.py
2448 views
1
import sys
2
3
import PIL.Image
4
5
import modules.upscaler
6
from modules import devices, errors, modelloader, script_callbacks, shared, upscaler_utils
7
8
9
class UpscalerScuNET(modules.upscaler.Upscaler):
10
def __init__(self, dirname):
11
self.name = "ScuNET"
12
self.model_name = "ScuNET GAN"
13
self.model_name2 = "ScuNET PSNR"
14
self.model_url = "https://github.com/cszn/KAIR/releases/download/v1.0/scunet_color_real_gan.pth"
15
self.model_url2 = "https://github.com/cszn/KAIR/releases/download/v1.0/scunet_color_real_psnr.pth"
16
self.user_path = dirname
17
super().__init__()
18
model_paths = self.find_models(ext_filter=[".pth"])
19
scalers = []
20
add_model2 = True
21
for file in model_paths:
22
if file.startswith("http"):
23
name = self.model_name
24
else:
25
name = modelloader.friendly_name(file)
26
if name == self.model_name2 or file == self.model_url2:
27
add_model2 = False
28
try:
29
scaler_data = modules.upscaler.UpscalerData(name, file, self, 4)
30
scalers.append(scaler_data)
31
except Exception:
32
errors.report(f"Error loading ScuNET model: {file}", exc_info=True)
33
if add_model2:
34
scaler_data2 = modules.upscaler.UpscalerData(self.model_name2, self.model_url2, self)
35
scalers.append(scaler_data2)
36
self.scalers = scalers
37
38
def do_upscale(self, img: PIL.Image.Image, selected_file):
39
devices.torch_gc()
40
try:
41
model = self.load_model(selected_file)
42
except Exception as e:
43
print(f"ScuNET: Unable to load model from {selected_file}: {e}", file=sys.stderr)
44
return img
45
46
img = upscaler_utils.upscale_2(
47
img,
48
model,
49
tile_size=shared.opts.SCUNET_tile,
50
tile_overlap=shared.opts.SCUNET_tile_overlap,
51
scale=1, # ScuNET is a denoising model, not an upscaler
52
desc='ScuNET',
53
)
54
devices.torch_gc()
55
return img
56
57
def load_model(self, path: str):
58
device = devices.get_device_for('scunet')
59
if path.startswith("http"):
60
# TODO: this doesn't use `path` at all?
61
filename = modelloader.load_file_from_url(self.model_url, model_dir=self.model_download_path, file_name=f"{self.name}.pth")
62
else:
63
filename = path
64
return modelloader.load_spandrel_model(filename, device=device, expected_architecture='SCUNet')
65
66
67
def on_ui_settings():
68
import gradio as gr
69
70
shared.opts.add_option("SCUNET_tile", shared.OptionInfo(256, "Tile size for SCUNET upscalers.", gr.Slider, {"minimum": 0, "maximum": 512, "step": 16}, section=('upscaling', "Upscaling")).info("0 = no tiling"))
71
shared.opts.add_option("SCUNET_tile_overlap", shared.OptionInfo(8, "Tile overlap for SCUNET upscalers.", gr.Slider, {"minimum": 0, "maximum": 64, "step": 1}, section=('upscaling', "Upscaling")).info("Low values = visible seam"))
72
73
74
script_callbacks.on_ui_settings(on_ui_settings)
75
76