realesrgan_model.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import os
  2. import numpy as np
  3. from PIL import Image
  4. from realesrgan import RealESRGANer
  5. from modules.upscaler import Upscaler, UpscalerData
  6. from modules.shared import cmd_opts, opts
  7. from modules import modelloader, errors
  8. class UpscalerRealESRGAN(Upscaler):
  9. def __init__(self, path):
  10. self.name = "RealESRGAN"
  11. self.user_path = path
  12. super().__init__()
  13. try:
  14. from basicsr.archs.rrdbnet_arch import RRDBNet # noqa: F401
  15. from realesrgan import RealESRGANer # noqa: F401
  16. from realesrgan.archs.srvgg_arch import SRVGGNetCompact # noqa: F401
  17. self.enable = True
  18. self.scalers = []
  19. scalers = self.load_models(path)
  20. local_model_paths = self.find_models(ext_filter=[".pth"])
  21. for scaler in scalers:
  22. if scaler.local_data_path.startswith("http"):
  23. filename = modelloader.friendly_name(scaler.local_data_path)
  24. local_model_candidates = [local_model for local_model in local_model_paths if local_model.endswith(f"{filename}.pth")]
  25. if local_model_candidates:
  26. scaler.local_data_path = local_model_candidates[0]
  27. if scaler.name in opts.realesrgan_enabled_models:
  28. self.scalers.append(scaler)
  29. except Exception:
  30. errors.report("Error importing Real-ESRGAN", exc_info=True)
  31. self.enable = False
  32. self.scalers = []
  33. def do_upscale(self, img, path):
  34. if not self.enable:
  35. return img
  36. try:
  37. info = self.load_model(path)
  38. except Exception:
  39. errors.report(f"Unable to load RealESRGAN model {path}", exc_info=True)
  40. return img
  41. upsampler = RealESRGANer(
  42. scale=info.scale,
  43. model_path=info.local_data_path,
  44. model=info.model(),
  45. half=not cmd_opts.no_half and not cmd_opts.upcast_sampling,
  46. tile=opts.ESRGAN_tile,
  47. tile_pad=opts.ESRGAN_tile_overlap,
  48. )
  49. upsampled = upsampler.enhance(np.array(img), outscale=info.scale)[0]
  50. image = Image.fromarray(upsampled)
  51. return image
  52. def load_model(self, path):
  53. for scaler in self.scalers:
  54. if scaler.data_path == path:
  55. if scaler.local_data_path.startswith("http"):
  56. scaler.local_data_path = modelloader.load_file_from_url(
  57. scaler.data_path,
  58. model_dir=self.model_download_path,
  59. )
  60. if not os.path.exists(scaler.local_data_path):
  61. raise FileNotFoundError(f"RealESRGAN data missing: {scaler.local_data_path}")
  62. return scaler
  63. raise ValueError(f"Unable to find model info: {path}")
  64. def load_models(self, _):
  65. return get_realesrgan_models(self)
  66. def get_realesrgan_models(scaler):
  67. try:
  68. from basicsr.archs.rrdbnet_arch import RRDBNet
  69. from realesrgan.archs.srvgg_arch import SRVGGNetCompact
  70. models = [
  71. UpscalerData(
  72. name="R-ESRGAN General 4xV3",
  73. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth",
  74. scale=4,
  75. upscaler=scaler,
  76. model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
  77. ),
  78. UpscalerData(
  79. name="R-ESRGAN General WDN 4xV3",
  80. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth",
  81. scale=4,
  82. upscaler=scaler,
  83. model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
  84. ),
  85. UpscalerData(
  86. name="R-ESRGAN AnimeVideo",
  87. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pth",
  88. scale=4,
  89. upscaler=scaler,
  90. model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=16, upscale=4, act_type='prelu')
  91. ),
  92. UpscalerData(
  93. name="R-ESRGAN 4x+",
  94. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
  95. scale=4,
  96. upscaler=scaler,
  97. model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
  98. ),
  99. UpscalerData(
  100. name="R-ESRGAN 4x+ Anime6B",
  101. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth",
  102. scale=4,
  103. upscaler=scaler,
  104. model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
  105. ),
  106. UpscalerData(
  107. name="R-ESRGAN 2x+",
  108. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth",
  109. scale=2,
  110. upscaler=scaler,
  111. model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
  112. ),
  113. ]
  114. return models
  115. except Exception:
  116. errors.report("Error making Real-ESRGAN models list", exc_info=True)