inference_realesrgan.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import argparse
  2. import cv2
  3. import glob
  4. import numpy as np
  5. import os
  6. import torch
  7. from basicsr.archs.rrdbnet_arch import RRDBNet
  8. from torch.nn import functional as F
  9. def main():
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument('--model_path', type=str, default='experiments/pretrained_models/RealESRGAN_x4plus.pth')
  12. parser.add_argument('--scale', type=int, default=4)
  13. parser.add_argument('--input', type=str, default='inputs', help='input image or folder')
  14. args = parser.parse_args()
  15. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  16. # set up model
  17. model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=args.scale)
  18. loadnet = torch.load(args.model_path)
  19. model.load_state_dict(loadnet['params_ema'], strict=True)
  20. model.eval()
  21. model = model.to(device)
  22. os.makedirs('results/', exist_ok=True)
  23. for idx, path in enumerate(sorted(glob.glob(os.path.join(args.input, '*')))):
  24. imgname = os.path.splitext(os.path.basename(path))[0]
  25. print('Testing', idx, imgname)
  26. # read image
  27. img = cv2.imread(path, cv2.IMREAD_COLOR).astype(np.float32) / 255.
  28. img = torch.from_numpy(np.transpose(img[:, :, [2, 1, 0]], (2, 0, 1))).float()
  29. img = img.unsqueeze(0).to(device)
  30. if args.scale == 2:
  31. mod_scale = 2
  32. elif args.scale == 1:
  33. mod_scale = 4
  34. else:
  35. mod_scale = None
  36. if mod_scale is not None:
  37. h_pad, w_pad = 0, 0
  38. _, _, h, w = img.size()
  39. if (h % mod_scale != 0):
  40. h_pad = (mod_scale - h % mod_scale)
  41. if (w % mod_scale != 0):
  42. w_pad = (mod_scale - w % mod_scale)
  43. img = F.pad(img, (0, w_pad, 0, h_pad), 'reflect')
  44. try:
  45. # inference
  46. with torch.no_grad():
  47. output = model(img)
  48. # remove extra pad
  49. if mod_scale is not None:
  50. _, _, h, w = output.size()
  51. output = output[:, :, 0:h - h_pad, 0:w - w_pad]
  52. # save image
  53. output = output.data.squeeze().float().cpu().clamp_(0, 1).numpy()
  54. output = np.transpose(output[[2, 1, 0], :, :], (1, 2, 0))
  55. output = (output * 255.0).round().astype(np.uint8)
  56. cv2.imwrite(f'results/{imgname}_RealESRGAN.png', output)
  57. except Exception as error:
  58. print('Error', error)
  59. if __name__ == '__main__':
  60. main()