realesrgan_model.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. device=self.device,
  49. )
  50. upsampled = upsampler.enhance(np.array(img), outscale=info.scale)[0]
  51. image = Image.fromarray(upsampled)
  52. return image
  53. def load_model(self, path):
  54. for scaler in self.scalers:
  55. if scaler.data_path == path:
  56. if scaler.local_data_path.startswith("http"):
  57. scaler.local_data_path = modelloader.load_file_from_url(
  58. scaler.data_path,
  59. model_dir=self.model_download_path,
  60. )
  61. if not os.path.exists(scaler.local_data_path):
  62. raise FileNotFoundError(f"RealESRGAN data missing: {scaler.local_data_path}")
  63. return scaler
  64. raise ValueError(f"Unable to find model info: {path}")
  65. def load_models(self, _):
  66. return get_realesrgan_models(self)
  67. def get_realesrgan_models(scaler):
  68. try:
  69. from basicsr.archs.rrdbnet_arch import RRDBNet
  70. from realesrgan.archs.srvgg_arch import SRVGGNetCompact
  71. models = [
  72. UpscalerData(
  73. name="R-ESRGAN General 4xV3",
  74. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth",
  75. scale=4,
  76. upscaler=scaler,
  77. model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
  78. ),
  79. UpscalerData(
  80. name="R-ESRGAN General WDN 4xV3",
  81. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth",
  82. scale=4,
  83. upscaler=scaler,
  84. model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
  85. ),
  86. UpscalerData(
  87. name="R-ESRGAN AnimeVideo",
  88. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pth",
  89. scale=4,
  90. upscaler=scaler,
  91. model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=16, upscale=4, act_type='prelu')
  92. ),
  93. UpscalerData(
  94. name="R-ESRGAN 4x+",
  95. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
  96. scale=4,
  97. upscaler=scaler,
  98. model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
  99. ),
  100. UpscalerData(
  101. name="R-ESRGAN 4x+ Anime6B",
  102. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth",
  103. scale=4,
  104. upscaler=scaler,
  105. model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
  106. ),
  107. UpscalerData(
  108. name="R-ESRGAN 2x+",
  109. path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth",
  110. scale=2,
  111. upscaler=scaler,
  112. model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
  113. ),
  114. ]
  115. return models
  116. except Exception:
  117. errors.report("Error making Real-ESRGAN models list", exc_info=True)