52Hz commited on
Commit
26e1a9b
1 Parent(s): 38229db

Upload main_test_SRMNet.py

Browse files
Files changed (1) hide show
  1. main_test_SRMNet.py +86 -0
main_test_SRMNet.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import cv2
3
+ import glob
4
+ import numpy as np
5
+ from collections import OrderedDict
6
+ import os
7
+ import torch
8
+ import requests
9
+ from PIL import Image
10
+ import torchvision.transforms.functional as TF
11
+ import torch.nn.functional as F
12
+
13
+ from model.SRMNet import SRMNet
14
+ from utils import util_calculate_psnr_ssim as util
15
+
16
+
17
+ def save_img(filepath, img):
18
+ cv2.imwrite(filepath, cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
19
+
20
+
21
+ def load_checkpoint(model, weights):
22
+ checkpoint = torch.load(weights)
23
+ try:
24
+ model.load_state_dict(checkpoint["state_dict"])
25
+ except:
26
+ state_dict = checkpoint["state_dict"]
27
+ new_state_dict = OrderedDict()
28
+ for k, v in state_dict.items():
29
+ name = k[7:] # remove `module.`
30
+ new_state_dict[name] = v
31
+ model.load_state_dict(new_state_dict)
32
+
33
+
34
+ def main():
35
+ parser = argparse.ArgumentParser(description='Demo Image Denoising')
36
+ parser.add_argument('--input_dir', default='./test/', type=str, help='Input images')
37
+ parser.add_argument('--result_dir', default='./result/', type=str, help='Directory for results')
38
+ parser.add_argument('--weights',
39
+ default='./checkpoints/SRMNet_real_denoise/models/model_bestPSNR.pth', type=str,
40
+ help='Path to weights')
41
+
42
+ args = parser.parse_args()
43
+
44
+ inp_dir = args.input_dir
45
+ out_dir = args.result_dir
46
+
47
+ os.makedirs(out_dir, exist_ok=True)
48
+
49
+ files = sorted(glob.glob(os.path.join(inp_dir, '*.PNG')))
50
+
51
+ if len(files) == 0:
52
+ raise Exception(f"No files found at {inp_dir}")
53
+
54
+ # Load corresponding models architecture and weights
55
+ model = SRMNet()
56
+ model.cuda()
57
+
58
+ load_checkpoint(model, args.weights)
59
+ model.eval()
60
+
61
+ mul = 16
62
+ for file_ in files:
63
+ img = Image.open(file_).convert('RGB')
64
+ input_ = TF.to_tensor(img).unsqueeze(0).cuda()
65
+
66
+ # Pad the input if not_multiple_of 8
67
+ h, w = input_.shape[2], input_.shape[3]
68
+ H, W = ((h + mul) // mul) * mul, ((w + mul) // mul) * mul
69
+ padh = H - h if h % mul != 0 else 0
70
+ padw = W - w if w % mul != 0 else 0
71
+ input_ = F.pad(input_, (0, padw, 0, padh), 'reflect')
72
+ with torch.no_grad():
73
+ restored = model(input_)
74
+
75
+ restored = torch.clamp(restored, 0, 1)
76
+ restored = restored[:, :, :h, :w]
77
+ restored = restored.permute(0, 2, 3, 1).cpu().detach().numpy()
78
+ restored = img_as_ubyte(restored[0])
79
+
80
+ f = os.path.splitext(os.path.split(file_)[-1])[0]
81
+ save_img((os.path.join(out_dir, f + '.png')), restored)
82
+
83
+
84
+
85
+ if __name__ == '__main__':
86
+ main()