realesrgan_model.py 5.1 KB

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