scunet_model.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import sys
  2. import PIL.Image
  3. import modules.upscaler
  4. from modules import devices, errors, modelloader, script_callbacks, shared, upscaler_utils
  5. class UpscalerScuNET(modules.upscaler.Upscaler):
  6. def __init__(self, dirname):
  7. self.name = "ScuNET"
  8. self.model_name = "ScuNET GAN"
  9. self.model_name2 = "ScuNET PSNR"
  10. self.model_url = "https://github.com/cszn/KAIR/releases/download/v1.0/scunet_color_real_gan.pth"
  11. self.model_url2 = "https://github.com/cszn/KAIR/releases/download/v1.0/scunet_color_real_psnr.pth"
  12. self.user_path = dirname
  13. super().__init__()
  14. model_paths = self.find_models(ext_filter=[".pth"])
  15. scalers = []
  16. add_model2 = True
  17. for file in model_paths:
  18. if file.startswith("http"):
  19. name = self.model_name
  20. else:
  21. name = modelloader.friendly_name(file)
  22. if name == self.model_name2 or file == self.model_url2:
  23. add_model2 = False
  24. try:
  25. scaler_data = modules.upscaler.UpscalerData(name, file, self, 4)
  26. scalers.append(scaler_data)
  27. except Exception:
  28. errors.report(f"Error loading ScuNET model: {file}", exc_info=True)
  29. if add_model2:
  30. scaler_data2 = modules.upscaler.UpscalerData(self.model_name2, self.model_url2, self)
  31. scalers.append(scaler_data2)
  32. self.scalers = scalers
  33. def do_upscale(self, img: PIL.Image.Image, selected_file):
  34. devices.torch_gc()
  35. try:
  36. model = self.load_model(selected_file)
  37. except Exception as e:
  38. print(f"ScuNET: Unable to load model from {selected_file}: {e}", file=sys.stderr)
  39. return img
  40. img = upscaler_utils.upscale_2(
  41. img,
  42. model,
  43. tile_size=shared.opts.SCUNET_tile,
  44. tile_overlap=shared.opts.SCUNET_tile_overlap,
  45. scale=1, # ScuNET is a denoising model, not an upscaler
  46. desc='ScuNET',
  47. )
  48. devices.torch_gc()
  49. return img
  50. def load_model(self, path: str):
  51. device = devices.get_device_for('scunet')
  52. if path.startswith("http"):
  53. # TODO: this doesn't use `path` at all?
  54. filename = modelloader.load_file_from_url(self.model_url, model_dir=self.model_download_path, file_name=f"{self.name}.pth")
  55. else:
  56. filename = path
  57. return modelloader.load_spandrel_model(filename, device=device, expected_architecture='SCUNet')
  58. def on_ui_settings():
  59. import gradio as gr
  60. 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"))
  61. 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"))
  62. script_callbacks.on_ui_settings(on_ui_settings)