-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_demo.py
329 lines (278 loc) · 12.8 KB
/
test_demo.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import os.path
import logging
import torch
import argparse
import json
import glob
from pprint import pprint
from utils.model_summary import get_model_activation, get_model_flops
from utils import utils_logger
from utils import utils_image as util
def select_model(args, device):
# Model ID is assigned according to the order of the submissions.
# Different networks are trained with input range of either [0,1] or [0,255]. The range is determined manually.
model_id = args.model_id
if model_id == 0:
# SGN test
from models.team00_SGN import SGNDN3
name, data_range = f"{model_id:02}_RFDN_baseline", 1.0
model_path = os.path.join('model_zoo', 'team00_sgn.ckpt')
model = SGNDN3()
state_dict = torch.load(model_path)["state_dict"]
state_dict.pop("current_val_metric")
state_dict.pop("best_val_metric")
state_dict.pop("best_iter")
new_state_dict = {}
for k, v in state_dict.items():
if k.find("model.") >= 0:
new_state_dict[k.replace("model.", "")] = v
model.load_state_dict(new_state_dict, strict=True)
elif model_id == 2:
from copy import deepcopy
from models.team02_MiAlgo import D4C
name, data_range = f"{model_id:02}_D4C", 1.0
model = D4C()
model_path = "model_zoo"
def get_net(net, sub_model_name):
load_net = torch.load(os.path.join(model_path, sub_model_name), map_location="cpu")
try:
load_net = load_net['params']
except:
pass
for k, v in deepcopy(load_net).items():
if k.startswith('module.'):
load_net[k[7:]] = v
load_net.pop(k)
net.load_state_dict(load_net, strict=True)
get_net(model, "team02_MiAlgo.pth")
model = model.cuda()
model = torch.nn.DataParallel(model)
else:
raise NotImplementedError(f"Model {model_id} is not implemented.")
# print(model)
model.eval()
tile = 384
for k, v in model.named_parameters():
v.requires_grad = False
model = model.to(device)
return model, name, data_range, tile
def select_dataset(data_dir, mode):
if mode == "test":
path = [
(
os.path.join(data_dir, f"DIV2K_test_noise50/{i:04}.png"),
os.path.join(data_dir, f"DIV2K_test_HR/{i:04}.png")
) for i in range(901, 1001)
]
# [f"DIV2K_test_LR/{i:04}.png" for i in range(901, 1001)]
elif mode == "valid":
path = [
(
os.path.join(data_dir, f"DIV2K_valid_noise50/{i:04}.png"),
os.path.join(data_dir, f"DIV2K_valid_HR/{i:04}.png")
) for i in range(801, 901)
]
elif mode == "hybrid_test":
path = [
(
p.replace("_HR", "_LR").replace(".png", "noise50.png"),
p
) for p in sorted(glob.glob(os.path.join(data_dir, "LSDIR_DIV2K_test_HR/*.png")))
]
else:
raise NotImplementedError(f"{mode} is not implemented in select_dataset")
return path
def forward(img_lq, model, tile=None, tile_overlap=64, scale=1):
if tile is None:
# test the image as a whole
output = model(img_lq)
else:
# test the image tile by tile
b, c, h, w = img_lq.size()
tile = min(tile, h, w)
tile_overlap = tile_overlap
sf = scale
stride = tile - tile_overlap
h_idx_list = list(range(0, h-tile, stride)) + [h-tile]
w_idx_list = list(range(0, w-tile, stride)) + [w-tile]
E = torch.zeros(b, c, h*sf, w*sf).type_as(img_lq)
W = torch.zeros_like(E)
for h_idx in h_idx_list:
for w_idx in w_idx_list:
in_patch = img_lq[..., h_idx:h_idx+tile, w_idx:w_idx+tile]
out_patch = model(in_patch)
out_patch_mask = torch.ones_like(out_patch)
E[..., h_idx*sf:(h_idx+tile)*sf, w_idx*sf:(w_idx+tile)*sf].add_(out_patch)
W[..., h_idx*sf:(h_idx+tile)*sf, w_idx*sf:(w_idx+tile)*sf].add_(out_patch_mask)
output = E.div_(W)
return output
def run(model, model_name, data_range, tile, logger, device, args, mode="test"):
sf = 4
border = sf
results = dict()
results[f"{mode}_runtime"] = []
results[f"{mode}_psnr"] = []
if args.ssim:
results[f"{mode}_ssim"] = []
# results[f"{mode}_psnr_y"] = []
# results[f"{mode}_ssim_y"] = []
# --------------------------------
# dataset path
# --------------------------------
data_path = select_dataset(args.data_dir, mode)
save_path = os.path.join(args.save_dir, model_name, mode)
util.mkdir(save_path)
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
for i, (img_noisy, img_hr) in enumerate(data_path):
# print(img_noisy)
# print(img_hr)
# --------------------------------
# (1) img_noisy
# --------------------------------
img_name, ext = os.path.splitext(os.path.basename(img_hr))
img_noisy = util.imread_uint(img_noisy, n_channels=3)
img_noisy = util.uint2tensor4(img_noisy, data_range)
img_noisy = img_noisy.to(device)
# --------------------------------
# (2) img_dn
# --------------------------------
start.record()
img_dn = forward(img_noisy, model, tile)
end.record()
torch.cuda.synchronize()
results[f"{mode}_runtime"].append(start.elapsed_time(end)) # milliseconds
img_dn = util.tensor2uint(img_dn, data_range)
# --------------------------------
# (3) img_hr
# --------------------------------
img_hr = util.imread_uint(img_hr, n_channels=3)
img_hr = img_hr.squeeze()
img_hr = util.modcrop(img_hr, sf)
# --------------------------------
# PSNR and SSIM
# --------------------------------
# print(img_dn.shape, img_hr.shape)
psnr = util.calculate_psnr(img_dn, img_hr, border=border)
results[f"{mode}_psnr"].append(psnr)
if args.ssim:
ssim = util.calculate_ssim(img_dn, img_hr, border=border)
results[f"{mode}_ssim"].append(ssim)
logger.info("{:s} - PSNR: {:.2f} dB; SSIM: {:.4f}.".format(img_name + ext, psnr, ssim))
else:
logger.info("{:s} - PSNR: {:.2f} dB".format(img_name + ext, psnr))
# if np.ndim(img_hr) == 3: # RGB image
# img_dn_y = util.rgb2ycbcr(img_dn, only_y=True)
# img_hr_y = util.rgb2ycbcr(img_hr, only_y=True)
# psnr_y = util.calculate_psnr(img_dn_y, img_hr_y, border=border)
# ssim_y = util.calculate_ssim(img_dn_y, img_hr_y, border=border)
# results[f"{mode}_psnr_y"].append(psnr_y)
# results[f"{mode}_ssim_y"].append(ssim_y)
# print(os.path.join(save_path, img_name+ext))
util.imsave(img_dn, os.path.join(save_path, img_name+ext))
results[f"{mode}_memory"] = torch.cuda.max_memory_allocated(torch.cuda.current_device()) / 1024 ** 2
results[f"{mode}_ave_runtime"] = sum(results[f"{mode}_runtime"]) / len(results[f"{mode}_runtime"]) #/ 1000.0
results[f"{mode}_ave_psnr"] = sum(results[f"{mode}_psnr"]) / len(results[f"{mode}_psnr"])
if args.ssim:
results[f"{mode}_ave_ssim"] = sum(results[f"{mode}_ssim"]) / len(results[f"{mode}_ssim"])
# results[f"{mode}_ave_psnr_y"] = sum(results[f"{mode}_psnr_y"]) / len(results[f"{mode}_psnr_y"])
# results[f"{mode}_ave_ssim_y"] = sum(results[f"{mode}_ssim_y"]) / len(results[f"{mode}_ssim_y"])
logger.info("{:>16s} : {:<.3f} [M]".format("Max Memery", results[f"{mode}_memory"])) # Memery
logger.info("------> Average runtime of ({}) is : {:.6f} seconds".format("test" if mode == "test" else "valid", results[f"{mode}_ave_runtime"]))
return results
def main(args):
utils_logger.logger_info("NTIRE2023-Dn50", log_path="NTIRE2023-Dn50.log")
logger = logging.getLogger("NTIRE2023-Dn50")
# --------------------------------
# basic settings
# --------------------------------
torch.cuda.current_device()
torch.cuda.empty_cache()
torch.backends.cudnn.benchmark = False
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
json_dir = os.path.join(os.getcwd(), "results.json")
if not os.path.exists(json_dir):
results = dict()
else:
with open(json_dir, "r") as f:
results = json.load(f)
# --------------------------------
# load model
# --------------------------------
model, model_name, data_range, tile = select_model(args, device)
logger.info(model_name)
# if model not in results:
if True:
# --------------------------------
# restore image
# --------------------------------
if args.hybrid_test:
# inference on the DIV2K and LSDIR test set
valid_results = run(model, model_name, data_range, tile, logger, device, args, mode="hybrid_test")
# record PSNR, runtime
results[model_name] = valid_results
else:
# inference on the validation set
valid_results = run(model, model_name, data_range, tile, logger, device, args, mode="valid")
# record PSNR, runtime
results[model_name] = valid_results
if args.include_test:
# inference on the test set
test_results = run(model, model_name, data_range, tile, logger, device, args, mode="test")
results[model_name].update(test_results)
input_dim = (3, 256, 256) # set the input dimension
activations, num_conv = get_model_activation(model, input_dim)
activations = activations/10**6
logger.info("{:>16s} : {:<.4f} [M]".format("#Activations", activations))
logger.info("{:>16s} : {:<d}".format("#Conv2d", num_conv))
flops = get_model_flops(model, input_dim, False)
flops = flops/10**9
logger.info("{:>16s} : {:<.4f} [G]".format("FLOPs", flops))
num_parameters = sum(map(lambda x: x.numel(), model.parameters()))
num_parameters = num_parameters/10**6
logger.info("{:>16s} : {:<.4f} [M]".format("#Params", num_parameters))
results[model_name].update({"activations": activations, "num_conv": num_conv, "flops": flops, "num_parameters": num_parameters})
with open(json_dir, "w") as f:
json.dump(results, f)
if args.include_test:
fmt = "{:20s}\t{:10s}\t{:10s}\t{:14s}\t{:14s}\t{:14s}\t{:10s}\t{:10s}\t{:8s}\t{:8s}\t{:8s}\n"
s = fmt.format("Model", "Val PSNR", "Test PSNR", "Val Time [ms]", "Test Time [ms]", "Ave Time [ms]",
"Params [M]", "FLOPs [G]", "Acts [M]", "Mem [M]", "Conv")
else:
fmt = "{:20s}\t{:10s}\t{:14s}\t{:10s}\t{:10s}\t{:8s}\t{:8s}\t{:8s}\n"
s = fmt.format("Model", "Val PSNR", "Val Time [ms]", "Params [M]", "FLOPs [G]", "Acts [M]", "Mem [M]", "Conv")
for k, v in results.items():
# print(v.keys())
if args.hybrid_test:
val_psnr = f"{v['hybrid_test_ave_psnr']:2.2f}"
val_time = f"{v['hybrid_test_ave_runtime']:3.2f}"
mem = f"{v['hybrid_test_memory']:2.2f}"
else:
val_psnr = f"{v['valid_ave_psnr']:2.2f}"
val_time = f"{v['valid_ave_runtime']:3.2f}"
mem = f"{v['valid_memory']:2.2f}"
num_param = f"{v['num_parameters']:2.3f}"
flops = f"{v['flops']:2.2f}"
acts = f"{v['activations']:2.2f}"
conv = f"{v['num_conv']:4d}"
if args.include_test:
# from IPython import embed; embed()
test_psnr = f"{v['test_ave_psnr']:2.2f}"
test_time = f"{v['test_ave_runtime']:3.2f}"
ave_time = f"{(v['valid_ave_runtime'] + v['test_ave_runtime']) / 2:3.2f}"
s += fmt.format(k, val_psnr, test_psnr, val_time, test_time, ave_time, num_param, flops, acts, mem, conv)
else:
s += fmt.format(k, val_psnr, val_time, num_param, flops, acts, mem, conv)
with open(os.path.join(os.getcwd(), 'results.txt'), "w") as f:
f.write(s)
if __name__ == "__main__":
parser = argparse.ArgumentParser("NTIRE2023-Dn50")
parser.add_argument("--data_dir", default="/cluster/work/cvl/yawli/data/NTIRE2023_Challenge", type=str)
parser.add_argument("--save_dir", default="/cluster/work/cvl/yawli/data/NTIRE2023_Challenge/results", type=str)
parser.add_argument("--model_id", default=0, type=int)
parser.add_argument("--include_test", action="store_true", help="Inference on the DIV2K test set")
parser.add_argument("--hybrid_test", action="store_true", help="Hybrid test on DIV2K and LSDIR test set")
parser.add_argument("--ssim", action="store_true", help="Calculate SSIM")
args = parser.parse_args()
pprint(args)
main(args)