Path: blob/master/extensions-builtin/ScuNET/scripts/scunet_model.py
2448 views
import sys12import PIL.Image34import modules.upscaler5from modules import devices, errors, modelloader, script_callbacks, shared, upscaler_utils678class UpscalerScuNET(modules.upscaler.Upscaler):9def __init__(self, dirname):10self.name = "ScuNET"11self.model_name = "ScuNET GAN"12self.model_name2 = "ScuNET PSNR"13self.model_url = "https://github.com/cszn/KAIR/releases/download/v1.0/scunet_color_real_gan.pth"14self.model_url2 = "https://github.com/cszn/KAIR/releases/download/v1.0/scunet_color_real_psnr.pth"15self.user_path = dirname16super().__init__()17model_paths = self.find_models(ext_filter=[".pth"])18scalers = []19add_model2 = True20for file in model_paths:21if file.startswith("http"):22name = self.model_name23else:24name = modelloader.friendly_name(file)25if name == self.model_name2 or file == self.model_url2:26add_model2 = False27try:28scaler_data = modules.upscaler.UpscalerData(name, file, self, 4)29scalers.append(scaler_data)30except Exception:31errors.report(f"Error loading ScuNET model: {file}", exc_info=True)32if add_model2:33scaler_data2 = modules.upscaler.UpscalerData(self.model_name2, self.model_url2, self)34scalers.append(scaler_data2)35self.scalers = scalers3637def do_upscale(self, img: PIL.Image.Image, selected_file):38devices.torch_gc()39try:40model = self.load_model(selected_file)41except Exception as e:42print(f"ScuNET: Unable to load model from {selected_file}: {e}", file=sys.stderr)43return img4445img = upscaler_utils.upscale_2(46img,47model,48tile_size=shared.opts.SCUNET_tile,49tile_overlap=shared.opts.SCUNET_tile_overlap,50scale=1, # ScuNET is a denoising model, not an upscaler51desc='ScuNET',52)53devices.torch_gc()54return img5556def load_model(self, path: str):57device = devices.get_device_for('scunet')58if path.startswith("http"):59# TODO: this doesn't use `path` at all?60filename = modelloader.load_file_from_url(self.model_url, model_dir=self.model_download_path, file_name=f"{self.name}.pth")61else:62filename = path63return modelloader.load_spandrel_model(filename, device=device, expected_architecture='SCUNet')646566def on_ui_settings():67import gradio as gr6869shared.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"))70shared.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"))717273script_callbacks.on_ui_settings(on_ui_settings)747576