-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.py
205 lines (167 loc) · 7.26 KB
/
validate.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import cv2
import os
import numpy as np
from utils import util, options, modelsummary
import sys
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '/../..')
import time
import torch
import torch.nn.functional as F
from utils.common import tensor2img, calculate_psnr, calculate_ssim, bgr2ycbcr
def compute_loss(coarse_img, out_err, hr_img):
true_err = torch.abs(coarse_img.sum(dim=1).unsqueeze(dim=1).detach() - hr_img.sum(dim=1).unsqueeze(dim=1))
return dict(err_loss=F.mse_loss(out_err, true_err),chan_loss=F.l1_loss(coarse_img, hr_img))
def validate_results(model, val_loader, device, save_path='.'):
with torch.no_grad():
psnr_l = []
ssim_l = []
for idx, (lr_img, hr_img) in enumerate(val_loader):
lr_img = lr_img.to(device)
hr_img = hr_img.to(device)
model.eval()
output = model(lr_img)
output = tensor2img(output)
gt = tensor2img(hr_img)
ipath = os.path.join(save_path, '%d.png' % (idx))
cv2.imwrite(ipath, output)
output = output.astype(np.float32) / 255.0
gt = gt.astype(np.float32) / 255.0
output = bgr2ycbcr(output, only_y=True)
gt = bgr2ycbcr(gt, only_y=True)
psnr = calculate_psnr(output * 255, gt * 255)
ssim = calculate_ssim(output * 255, gt * 255)
psnr_l.append(psnr)
ssim_l.append(ssim)
return psnr_l, ssim_l
def validate_2(model, val_loader, config, device, iteration, save_path='.'):
with torch.no_grad():
psnr_l = []
ssim_l = []
psnr_l_2 = []
ssim_l_2 = []
flops_l = []
times_l = []
for idx, (lr_img, hr_img) in enumerate(val_loader):
lr_img = lr_img.to(device)
hr_img = hr_img.to(device)
model.eval()
s=time.time()
refine_img, coarse_img = model(lr_img, is_return_coarse=True)
t=time.time()
for k, v in model.named_parameters():
v.requires_grad = False
model = model.to(device)
b,c,h,w=lr_img.shape
input_dim=(c,h,w)
flops = modelsummary.get_model_flops(model, input_dim, False)
output = tensor2img(refine_img)
output2 = tensor2img(coarse_img)
gt = tensor2img(hr_img)
if config.VAL.SAVE_IMG:
ipath = os.path.join(save_path, '%d_%03d.png' % (iteration, idx))
cv2.imwrite(ipath, np.concatenate([output, gt], axis=1))
output = output.astype(np.float32) / 255.0
output2 = output2.astype(np.float32) / 255.0
gt = gt.astype(np.float32) / 255.0
if config.VAL.TO_Y:
output = bgr2ycbcr(output, only_y=True)
output2 = bgr2ycbcr(output2, only_y=True)
gt = bgr2ycbcr(gt, only_y=True)
if config.VAL.CROP_BORDER != 0:
cb = config.VAL.CROP_BORDER
output = output[cb:-cb, cb:-cb]
output2 = output2[cb:-cb, cb:-cb]
gt = gt[cb:-cb, cb:-cb]
psnr = calculate_psnr(output * 255, gt * 255)
psnr2 = calculate_psnr(output2 * 255, gt * 255)
ssim = calculate_ssim(output * 255, gt * 255)
ssim2 = calculate_ssim(output2 * 255, gt * 255)
psnr_l.append(psnr)
psnr_l_2.append(psnr2)
ssim_l.append(ssim)
ssim_l_2.append(ssim2)
flops_l.append(flops)
times_l.append(t-s)
avg_psnr = sum(psnr_l) / len(psnr_l)
avg_ssim = sum(ssim_l) / len(ssim_l)
avg_psnr_2 = sum(psnr_l_2) / len(psnr_l_2)
avg_ssim_2 = sum(ssim_l_2) / len(ssim_l_2)
avg_flops = sum(flops_l) / len(flops_l)
avg_time = sum(times_l) / len(times_l)
return avg_psnr, avg_ssim, avg_psnr_2, avg_ssim_2, avg_flops, avg_time
def validate_mask(model, val_loader, config, device, iteration, save_path='.'):
with torch.no_grad():
psnr_l = []
ssim_l = []
psnr_l_2 = []
ssim_l_2 = []
flops_l = []
times_l = []
for idx, (lr_img, hr_img) in enumerate(val_loader):
lr_img = lr_img.to(device)
hr_img = hr_img.to(device)
model.eval()
s=time.time()
refine_img_or, refine_img, coarse_img, mask = model(lr_img, is_return_coarse=True)
t=time.time()
for k, v in model.named_parameters():
v.requires_grad = False
model = model.to(device)
b,c,h,w=lr_img.shape
input_dim=(c,h,w)
flops = modelsummary.get_model_flops(model, input_dim, False)
output = tensor2img(refine_img)
output2 = tensor2img(coarse_img)
gt = tensor2img(hr_img)
if config.VAL.SAVE_IMG:
ipath = os.path.join(save_path, '%d_%03d.png' % (iteration, idx))
cv2.imwrite(ipath, np.concatenate([output, gt], axis=1))
output = output.astype(np.float32) / 255.0
output2 = output2.astype(np.float32) / 255.0
gt = gt.astype(np.float32) / 255.0
if config.VAL.TO_Y:
output = bgr2ycbcr(output, only_y=True)
output2 = bgr2ycbcr(output2, only_y=True)
gt = bgr2ycbcr(gt, only_y=True)
if config.VAL.CROP_BORDER != 0:
cb = config.VAL.CROP_BORDER
output = output[cb:-cb, cb:-cb]
output2 = output2[cb:-cb, cb:-cb]
gt = gt[cb:-cb, cb:-cb]
psnr = calculate_psnr(output * 255, gt * 255)
psnr2 = calculate_psnr(output2 * 255, gt * 255)
ssim = calculate_ssim(output * 255, gt * 255)
ssim2 = calculate_ssim(output2 * 255, gt * 255)
psnr_l.append(psnr)
psnr_l_2.append(psnr2)
ssim_l.append(ssim)
ssim_l_2.append(ssim2)
flops_l.append(flops)
times_l.append(t-s)
avg_psnr = sum(psnr_l) / len(psnr_l)
avg_ssim = sum(ssim_l) / len(ssim_l)
avg_psnr_2 = sum(psnr_l_2) / len(psnr_l_2)
avg_ssim_2 = sum(ssim_l_2) / len(ssim_l_2)
avg_flops = sum(flops_l) / len(flops_l)
avg_time = sum(times_l) / len(times_l)
return avg_psnr, avg_ssim, avg_psnr_2, avg_ssim_2, avg_flops, avg_time
if __name__ == '__main__':
from config import config
from network import Network
from dataset import get_dataset
from utils import dataloader
from utils.model_opr import load_model
config.VAL.DATASET = 'Set5'
model = Network(config)
if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
model = model.to(device)
model_path = 'log/models/200000.pth'
load_model(model, model_path, cpu=True)
sys.exit()
val_dataset = get_dataset(config.VAL)
val_loader = dataloader.val_loader(val_dataset, config, 0, 1)
psnr, ssim = validate(model, val_loader, config, device, 0, save_path='.')
print('PSNR: %.4f, SSIM: %.4f' % (psnr, ssim))