swinir_model.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import logging
  2. import sys
  3. import torch
  4. from PIL import Image
  5. from modules import devices, modelloader, script_callbacks, shared, upscaler_utils
  6. from modules.upscaler import Upscaler, UpscalerData
  7. SWINIR_MODEL_URL = "https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/003_realSR_BSRGAN_DFOWMFC_s64w8_SwinIR-L_x4_GAN.pth"
  8. logger = logging.getLogger(__name__)
  9. class UpscalerSwinIR(Upscaler):
  10. def __init__(self, dirname):
  11. self._cached_model = None # keep the model when SWIN_torch_compile is on to prevent re-compile every runs
  12. self._cached_model_config = None # to clear '_cached_model' when changing model (v1/v2) or settings
  13. self.name = "SwinIR"
  14. self.model_url = SWINIR_MODEL_URL
  15. self.model_name = "SwinIR 4x"
  16. self.user_path = dirname
  17. super().__init__()
  18. scalers = []
  19. model_files = self.find_models(ext_filter=[".pt", ".pth"])
  20. for model in model_files:
  21. if model.startswith("http"):
  22. name = self.model_name
  23. else:
  24. name = modelloader.friendly_name(model)
  25. model_data = UpscalerData(name, model, self)
  26. scalers.append(model_data)
  27. self.scalers = scalers
  28. def do_upscale(self, img: Image.Image, model_file: str) -> Image.Image:
  29. current_config = (model_file, shared.opts.SWIN_tile)
  30. if self._cached_model_config == current_config:
  31. model = self._cached_model
  32. else:
  33. try:
  34. model = self.load_model(model_file)
  35. except Exception as e:
  36. print(f"Failed loading SwinIR model {model_file}: {e}", file=sys.stderr)
  37. return img
  38. self._cached_model = model
  39. self._cached_model_config = current_config
  40. img = upscaler_utils.upscale_2(
  41. img,
  42. model,
  43. tile_size=shared.opts.SWIN_tile,
  44. tile_overlap=shared.opts.SWIN_tile_overlap,
  45. scale=model.scale,
  46. desc="SwinIR",
  47. )
  48. devices.torch_gc()
  49. return img
  50. def load_model(self, path, scale=4):
  51. if path.startswith("http"):
  52. filename = modelloader.load_file_from_url(
  53. url=path,
  54. model_dir=self.model_download_path,
  55. file_name=f"{self.model_name.replace(' ', '_')}.pth",
  56. )
  57. else:
  58. filename = path
  59. model_descriptor = modelloader.load_spandrel_model(
  60. filename,
  61. device=self._get_device(),
  62. prefer_half=(devices.dtype == torch.float16),
  63. expected_architecture="SwinIR",
  64. )
  65. if getattr(shared.opts, 'SWIN_torch_compile', False):
  66. try:
  67. model_descriptor.model.compile()
  68. except Exception:
  69. logger.warning("Failed to compile SwinIR model, fallback to JIT", exc_info=True)
  70. return model_descriptor
  71. def _get_device(self):
  72. return devices.get_device_for('swinir')
  73. def on_ui_settings():
  74. import gradio as gr
  75. shared.opts.add_option("SWIN_tile", shared.OptionInfo(192, "Tile size for all SwinIR.", gr.Slider, {"minimum": 16, "maximum": 512, "step": 16}, section=('upscaling', "Upscaling")))
  76. shared.opts.add_option("SWIN_tile_overlap", shared.OptionInfo(8, "Tile overlap, in pixels for SwinIR. Low values = visible seam.", gr.Slider, {"minimum": 0, "maximum": 48, "step": 1}, section=('upscaling', "Upscaling")))
  77. shared.opts.add_option("SWIN_torch_compile", shared.OptionInfo(False, "Use torch.compile to accelerate SwinIR.", gr.Checkbox, {"interactive": True}, section=('upscaling', "Upscaling")).info("Takes longer on first run"))
  78. script_callbacks.on_ui_settings(on_ui_settings)