scunet_model.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import os.path
  2. import sys
  3. import traceback
  4. import PIL.Image
  5. import numpy as np
  6. import torch
  7. from basicsr.utils.download_util import load_file_from_url
  8. import modules.upscaler
  9. from modules import devices, modelloader
  10. from scunet_model_arch import SCUNet as net
  11. class UpscalerScuNET(modules.upscaler.Upscaler):
  12. def __init__(self, dirname):
  13. self.name = "ScuNET"
  14. self.model_name = "ScuNET GAN"
  15. self.model_name2 = "ScuNET PSNR"
  16. self.model_url = "https://github.com/cszn/KAIR/releases/download/v1.0/scunet_color_real_gan.pth"
  17. self.model_url2 = "https://github.com/cszn/KAIR/releases/download/v1.0/scunet_color_real_psnr.pth"
  18. self.user_path = dirname
  19. super().__init__()
  20. model_paths = self.find_models(ext_filter=[".pth"])
  21. scalers = []
  22. add_model2 = True
  23. for file in model_paths:
  24. if "http" in file:
  25. name = self.model_name
  26. else:
  27. name = modelloader.friendly_name(file)
  28. if name == self.model_name2 or file == self.model_url2:
  29. add_model2 = False
  30. try:
  31. scaler_data = modules.upscaler.UpscalerData(name, file, self, 4)
  32. scalers.append(scaler_data)
  33. except Exception:
  34. print(f"Error loading ScuNET model: {file}", file=sys.stderr)
  35. print(traceback.format_exc(), file=sys.stderr)
  36. if add_model2:
  37. scaler_data2 = modules.upscaler.UpscalerData(self.model_name2, self.model_url2, self)
  38. scalers.append(scaler_data2)
  39. self.scalers = scalers
  40. def do_upscale(self, img: PIL.Image, selected_file):
  41. torch.cuda.empty_cache()
  42. model = self.load_model(selected_file)
  43. if model is None:
  44. return img
  45. device = devices.get_device_for('scunet')
  46. img = np.array(img)
  47. img = img[:, :, ::-1]
  48. img = np.moveaxis(img, 2, 0) / 255
  49. img = torch.from_numpy(img).float()
  50. img = img.unsqueeze(0).to(device)
  51. with torch.no_grad():
  52. output = model(img)
  53. output = output.squeeze().float().cpu().clamp_(0, 1).numpy()
  54. output = 255. * np.moveaxis(output, 0, 2)
  55. output = output.astype(np.uint8)
  56. output = output[:, :, ::-1]
  57. torch.cuda.empty_cache()
  58. return PIL.Image.fromarray(output, 'RGB')
  59. def load_model(self, path: str):
  60. device = devices.get_device_for('scunet')
  61. if "http" in path:
  62. filename = load_file_from_url(url=self.model_url, model_dir=self.model_path, file_name="%s.pth" % self.name,
  63. progress=True)
  64. else:
  65. filename = path
  66. if not os.path.exists(os.path.join(self.model_path, filename)) or filename is None:
  67. print(f"ScuNET: Unable to load model from {filename}", file=sys.stderr)
  68. return None
  69. model = net(in_nc=3, config=[4, 4, 4, 4, 4, 4, 4], dim=64)
  70. model.load_state_dict(torch.load(filename), strict=True)
  71. model.eval()
  72. for k, v in model.named_parameters():
  73. v.requires_grad = False
  74. model = model.to(device)
  75. return model