-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_lol.py
More file actions
128 lines (88 loc) · 4.09 KB
/
test_lol.py
File metadata and controls
128 lines (88 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import numpy as np
import os
import argparse
from tqdm import tqdm
import torch.nn as nn
import torch
import torch.nn.functional as F
import utils
from natsort import natsorted
from glob import glob
from basicsr.archs.ecmambaincontext_arch import ECMambaIncontext
from skimage import img_as_ubyte
import cv2
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
def imread(path):
return cv2.imread(path)[:, :, [2, 1, 0]]
def rgb(t): return (
np.clip((t[0] if len(t.shape) == 4 else t).detach().cpu().numpy().transpose([1, 2, 0]), 0, 1) * 255).astype(
np.uint8)
parser = argparse.ArgumentParser(description='Image Enhancement using ECMamba')
parser.add_argument('--input_dir', default='dataset/LOLv1/Test/input', type=str, help='Directory of test input images')
parser.add_argument('--gt_dir', default='dataset/LOLv1/Test/target', type=str, help='Directory of gt images')
parser.add_argument('--result_dir', default='test-results', type=str, help='Directory for results')
parser.add_argument('--weights', default='weights/LOLv1_weight.pth', type=str, help='Path to weights')
parser.add_argument('--dataset', default='LOLv1', type=str, help='Test Dataset')
parser.add_argument('--GT_mean', default=True, type=bool, help='Use GT mean to adjust the output')
args = parser.parse_args()
####### Load yaml #######
yaml_file = 'options/ecmamba_test.yml'
weights = args.weights
import yaml
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
x = yaml.load(open(yaml_file, mode='r'), Loader=Loader)
s = x['network_g'].pop('type')
##########################
model_restoration = ECMambaIncontext(**x['network_g'])
total = sum([param.nelement() for param in model_restoration.parameters()])
print('total parameters:', total)
checkpoint = torch.load(weights)
checkpoint_name = os.path.basename(weights).split('.')[0]
model_restoration.load_state_dict(checkpoint['params'])
print("===>Testing using weights: ",weights)
model_restoration.cuda()
model_restoration.eval()
factor = 8
dataset = args.dataset
if not args.GT_mean:
result_dir = os.path.join(args.result_dir, args.dataset + '-no-GT-mean')
else:
result_dir = os.path.join(args.result_dir, args.dataset)
os.makedirs(result_dir, exist_ok=True)
input_paths = natsorted(glob(os.path.join(args.input_dir, '*.png')) + glob(os.path.join(args.input_dir, '*.JPG')))
gt_paths = natsorted(glob(os.path.join(args.gt_dir, '*.png')) + glob(os.path.join(args.gt_dir, '*.JPG')))
psnr = []
ssim = []
with torch.inference_mode():
for inp_path, gt_path in tqdm(zip(input_paths, gt_paths ), total=len(gt_paths)):
torch.cuda.ipc_collect()
torch.cuda.empty_cache()
img = np.float32(utils.load_img(inp_path))/255.
img = torch.from_numpy(img).permute(2,0,1)
input_ = img.unsqueeze(0).cuda()
# Padding in case images are not multiples of 8
h,w = input_.shape[2], input_.shape[3]
H,W = ((h+factor)//factor)*factor, ((w+factor)//factor)*factor
padh = H-h if h%factor!=0 else 0
padw = W-w if w%factor!=0 else 0
input_ = F.pad(input_, (0,padw,0,padh), 'reflect')
restored = model_restoration(input_)[0]
# Unpad images to original dimensions
restored_tensor = restored[:,:,:h,:w]
restored = torch.clamp(restored_tensor,0,1).cpu().detach().permute(0, 2, 3, 1).squeeze(0).numpy()
target = np.float32(utils.load_img(gt_path))/255
if args.GT_mean:
mean_restored = cv2.cvtColor(restored.astype(np.float32), cv2.COLOR_BGR2GRAY).mean()
mean_target = cv2.cvtColor(target.astype(np.float32), cv2.COLOR_BGR2GRAY).mean()
restored = np.clip(restored * (mean_target / mean_restored), 0, 1)
utils.save_img((os.path.join(result_dir, os.path.splitext(os.path.split(inp_path)[-1])[0]+'.png')), img_as_ubyte(restored))
psnr.append(utils.PSNR(target, restored))
ssim.append(utils.calculate_ssim(
img_as_ubyte(target), img_as_ubyte(restored)))
psnr = np.mean(np.array(psnr))
ssim = np.mean(np.array(ssim))
print("PSNR: %f " % (psnr))
print("SSIM: %f " % (ssim))