-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
185 lines (160 loc) · 5.24 KB
/
test.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
#!/usr/bin/env python
# -*-coding:utf-8 -*-
"""
@File : test.py
@Time : 2024/04/07 21:36:41
@Author : Tu Vo
@Version : 1.0
@Contact : vovantu.hust@gmail.com
@License : (C)Copyright 2020-2021, Tu Vo
@Desc : KC Machine Learning Lab
"""
import os
import yaml
import lpips
import torch
import numpy as np
import torch.nn.functional as F
from tqdm import tqdm
from glob import glob
from models.jude import JUDE
from basicsr.utils import tensor2img
from utils.utils_image import save_img
from pytorch_ssim import ssim as ssim_metric
from utils.dataloader import parse_test as parse
from basicsr.data.lol_image_dataset import LOLImageDataset
from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
lpips_metric = LearnedPerceptualImagePatchSimilarity(net_type="vgg").cuda()
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
with open("options/train.yml") as f:
opt = yaml.full_load(f)
seed = opt["manual_seed"]
"""
Initiate a model, and transfer to gpu
"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Number of GPUS available: ", torch.cuda.device_count())
"""
Setting up training data - blur kernels and photon levels
"""
model = JUDE(device=device, opt=opt)
print(
"number of params {}".format(
sum(p.numel() for p in model.parameters() if p.requires_grad)
)
)
model = torch.nn.DataParallel(model).to(device)
"""
Load checkpoint
"""
load_path = (
opt["path"]["save"]
+ "-"
+ str(opt["network"]["stages"])
+ "-"
+ str(opt["datasets"]["train"]["crop_size"])
+ "-"
+ opt["network"]["denoiser"]["name"]
)
if opt["val"]["checkpoint"] == "last":
path = sorted(glob(load_path + "/*.pth"))[1]
else:
path = sorted(glob(load_path + "/*.pth"))[0]
path = "model_zoo/BOWNet_kernel_prediction_model_v10-5-512-ResUNet_mix/bownet_best.pth"
checkpoint = torch.load(path)
model.load_state_dict(checkpoint["state_dict"])
print("Loading checkpoint from {}".format(path))
model.to(device)
# create saving folder
if opt["val"]["save_img"]["state"]:
save_path = (
opt["val"]["save_img"]["save_folder"]
+ "_"
+ str(opt["datasets"]["train"]["crop_size"])
)
if not os.path.exists(save_path):
os.makedirs(save_path)
"""
prepare data
"""
data_test = LOLImageDataset(opt["datasets"]["test"])
test_loader = torch.utils.data.DataLoader(
data_test,
batch_size=1,
shuffle=False,
drop_last=False,
num_workers=1,
pin_memory=True,
worker_init_fn=lambda seed: np.random.seed(seed),
)
"""
testing
"""
with tqdm(total=len(data_test), desc=f"Testing .....", unit="its") as pbar:
idx = 0
total_psnr = 0.0
total_ssim = 0.0
total_lpips = 0.0
for test_data in test_loader:
idx += 1
with torch.no_grad():
blurred, image, name = parse(test_data)
_, _, H, W = blurred.size()
H = int(int(H / 128) * 128)
W = int(int(W / 128) * 128)
blurred = blurred[..., :H, :W]
image = image[..., :H, :W]
dirname1 = os.path.basename(os.path.dirname(name[0]))
dirname2 = os.path.basename(os.path.dirname(os.path.dirname(name[0])))
name = os.path.basename(name[0])
dirname3 = os.path.basename(load_path)
full_dir_path = os.path.join(save_path, dirname2, dirname3, dirname1)
os.makedirs(full_dir_path, exist_ok=True)
x, y, k = (
blurred.to(device),
image.to(device),
torch.from_numpy(np.array([1])).to(device),
)
out_z = model(x, k)
psnr = psnr = (
10
* torch.log10(1 / F.mse_loss(tensor2img(out_z), tensor2img(y))).item()
)
_, _, H, W = out_z.size()
down_ratio = max(1, round(min(H, W) / 256))
ssim = ssim_metric(
F.adaptive_avg_pool2d(
out_z, (int(H / down_ratio), int(W / down_ratio))
),
F.adaptive_avg_pool2d(y, (int(H / down_ratio), int(W / down_ratio))),
data_range=1,
size_average=False,
).item()
lpips = lpips_metric(out_z, y).item()
total_psnr += psnr
total_ssim += ssim
total_lpips += lpips
if opt["val"]["save_img"]["state"]:
save_img(tensor2img(out_z), os.path.join(full_dir_path, name))
with open("{}/metrics.txt".format(save_path), "a") as f:
f.writelines(
f"{dirname2}-{dirname1}-{name}: PSNR: {psnr:.4f} dB, SSIM: {ssim:.4f}, LPIPS: {lpips:.4f}\n"
)
pbar.update(1)
"""
Average PSNR over all images
"""
avg_psnr = total_psnr / idx
avg_ssim = total_ssim / idx
avg_lpips = total_lpips / idx
"""
Print the result
"""
print(f"Average PSNR: {avg_psnr:.4f} dB")
print(f"Average SSIM: {avg_ssim:.4f}")
print(f"Average LPIPS: {avg_lpips:.4f}")
with open("{}/metrics.txt".format(save_path), "a") as f:
f.writelines(f"Average PSNR: {avg_psnr:.4f} dB\n")
f.writelines(f"Average SSIM: {avg_ssim:.4f}\n")
f.writelines(f"Average LPIPS: {avg_lpips:.4f}\n")