forked from GuoLanqing/ShadowFormer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
172 lines (143 loc) · 8.48 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
import numpy as np
import os,sys
import argparse
from tqdm import tqdm
from einops import rearrange, repeat
import torch.nn as nn
import torch
from torch.utils.data import DataLoader
import torch.nn.functional as F
# from ptflops import get_model_complexity_info
import scipy.io as sio
from utils.loader import get_validation_data
import utils
import cv2
from model import UNet
from skimage import img_as_float32, img_as_ubyte
from skimage.metrics import peak_signal_noise_ratio as psnr_loss
from skimage.metrics import structural_similarity as ssim_loss
from sklearn.metrics import mean_squared_error as mse_loss
parser = argparse.ArgumentParser(description='RGB denoising evaluation on the validation set of SIDD')
parser.add_argument('--input_dir', default='../ISTD_Dataset/test/',
type=str, help='Directory of validation images')
parser.add_argument('--result_dir', default='./results/',
type=str, help='Directory for results')
parser.add_argument('--weights', default='./log/ShadowFormer_istd/models/model_best.pth',
type=str, help='Path to weights')
parser.add_argument('--gpus', default='0', type=str, help='CUDA_VISIBLE_DEVICES')
parser.add_argument('--arch', default='ShadowFormer', type=str, help='arch')
parser.add_argument('--batch_size', default=1, type=int, help='Batch size for dataloader')
parser.add_argument('--save_images', action='store_true', help='Save denoised images in result directory')
parser.add_argument('--cal_metrics', action='store_true', help='Measure denoised images with GT')
parser.add_argument('--embed_dim', type=int, default=32, help='number of data loading workers')
parser.add_argument('--win_size', type=int, default=10, help='number of data loading workers')
parser.add_argument('--token_projection', type=str, default='linear', help='linear/conv token projection')
parser.add_argument('--token_mlp', type=str,default='leff', help='ffn/leff token mlp')
# args for vit
parser.add_argument('--vit_dim', type=int, default=256, help='vit hidden_dim')
parser.add_argument('--vit_depth', type=int, default=12, help='vit depth')
parser.add_argument('--vit_nheads', type=int, default=8, help='vit hidden_dim')
parser.add_argument('--vit_mlp_dim', type=int, default=512, help='vit mlp_dim')
parser.add_argument('--vit_patch_size', type=int, default=16, help='vit patch_size')
parser.add_argument('--global_skip', action='store_true', default=False, help='global skip connection')
parser.add_argument('--local_skip', action='store_true', default=False, help='local skip connection')
parser.add_argument('--vit_share', action='store_true', default=False, help='share vit module')
parser.add_argument('--train_ps', type=int, default=320, help='patch size of training sample')
parser.add_argument('--tile', type=int, default=None, help='Tile size (e.g 720). None means testing on the original resolution image')
parser.add_argument('--tile_overlap', type=int, default=32, help='Overlapping of different tiles')
args = parser.parse_args()
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus
utils.mkdir(args.result_dir)
test_dataset = get_validation_data(args.input_dir)
test_loader = DataLoader(dataset=test_dataset, batch_size=1, shuffle=False, num_workers=8, drop_last=False)
model_restoration = utils.get_arch(args)
model_restoration = torch.nn.DataParallel(model_restoration)
utils.load_checkpoint(model_restoration, args.weights)
print("===>Testing using weights: ", args.weights)
model_restoration.cuda()
model_restoration.eval()
img_multiple_of = 8 * args.win_size
with torch.no_grad():
psnr_val_rgb = []
ssim_val_rgb = []
rmse_val_rgb = []
psnr_val_s = []
ssim_val_s = []
psnr_val_ns = []
ssim_val_ns = []
rmse_val_s = []
rmse_val_ns = []
for ii, data_test in enumerate(tqdm(test_loader), 0):
rgb_gt = data_test[0].numpy().squeeze().transpose((1, 2, 0))
rgb_noisy = data_test[1].cuda()
mask = data_test[2].cuda()
filenames = data_test[3]
# Pad the input if not_multiple_of win_size * 8
height, width = rgb_noisy.shape[2], rgb_noisy.shape[3]
H, W = ((height + img_multiple_of) // img_multiple_of) * img_multiple_of, (
(width + img_multiple_of) // img_multiple_of) * img_multiple_of
padh = H - height if height % img_multiple_of != 0 else 0
padw = W - width if width % img_multiple_of != 0 else 0
rgb_noisy = F.pad(rgb_noisy, (0, padw, 0, padh), 'reflect')
mask = F.pad(mask, (0, padw, 0, padh), 'reflect')
if args.tile is None:
rgb_restored = model_restoration(rgb_noisy, mask)
else:
# test the image tile by tile
b, c, h, w = rgb_noisy.shape
tile = min(args.tile, h, w)
assert tile % 8 == 0, "tile size should be multiple of 8"
tile_overlap = args.tile_overlap
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, w).type_as(rgb_noisy)
W = torch.zeros_like(E)
for h_idx in h_idx_list:
for w_idx in w_idx_list:
in_patch = rgb_noisy[..., h_idx:h_idx + tile, w_idx:w_idx + tile]
mask_patch = mask[..., h_idx:h_idx + tile, w_idx:w_idx + tile]
out_patch = model_restoration(in_patch, mask_patch)
out_patch_mask = torch.ones_like(out_patch)
E[..., h_idx:(h_idx + tile), w_idx:(w_idx + tile)].add_(out_patch)
W[..., h_idx:(h_idx + tile), w_idx:(w_idx + tile)].add_(out_patch_mask)
restored = E.div_(W)
rgb_restored = torch.clamp(rgb_restored, 0, 1).cpu().numpy().squeeze().transpose((1, 2, 0))
# Unpad the output
rgb_restored = rgb_restored[:height, :width, :]
if args.cal_metrics:
bm = torch.where(mask == 0, torch.zeros_like(mask), torch.ones_like(mask)) #binarize mask
bm = np.expand_dims(bm.cpu().numpy().squeeze(), axis=2)
# calculate SSIM in gray space
gray_restored = cv2.cvtColor(rgb_restored, cv2.COLOR_RGB2GRAY)
gray_gt = cv2.cvtColor(rgb_gt, cv2.COLOR_RGB2GRAY)
ssim_val_rgb.append(ssim_loss(gray_restored, gray_gt, channel_axis=None))
ssim_val_ns.append(ssim_loss(gray_restored * (1 - bm.squeeze()), gray_gt * (1 - bm.squeeze()), channel_axis=None))
ssim_val_s.append(ssim_loss(gray_restored * bm.squeeze(), gray_gt * bm.squeeze(), channel_axis=None))
psnr_val_rgb.append(psnr_loss(rgb_restored, rgb_gt))
psnr_val_ns.append(psnr_loss(rgb_restored * (1 - bm), rgb_gt * (1 - bm)))
psnr_val_s.append(psnr_loss(rgb_restored * bm, rgb_gt * bm))
# calculate the RMSE in LAB space
rmse_temp = np.abs(cv2.cvtColor(rgb_restored, cv2.COLOR_RGB2LAB) - cv2.cvtColor(rgb_gt, cv2.COLOR_RGB2LAB)).mean() * 3
rmse_val_rgb.append(rmse_temp)
rmse_temp_s = np.abs(cv2.cvtColor(rgb_restored * bm, cv2.COLOR_RGB2LAB) - cv2.cvtColor(rgb_gt * bm, cv2.COLOR_RGB2LAB)).sum() / bm.sum()
rmse_temp_ns = np.abs(cv2.cvtColor(rgb_restored * (1-bm), cv2.COLOR_RGB2LAB) - cv2.cvtColor(rgb_gt * (1-bm),
cv2.COLOR_RGB2LAB)).sum() / (1-bm).sum()
rmse_val_s.append(rmse_temp_s)
rmse_val_ns.append(rmse_temp_ns)
if args.save_images:
utils.save_img(rgb_restored*255.0, os.path.join(args.result_dir, filenames[0]))
if args.cal_metrics:
psnr_val_rgb = sum(psnr_val_rgb)/len(test_dataset)
ssim_val_rgb = sum(ssim_val_rgb)/len(test_dataset)
psnr_val_s = sum(psnr_val_s)/len(test_dataset)
ssim_val_s = sum(ssim_val_s)/len(test_dataset)
psnr_val_ns = sum(psnr_val_ns)/len(test_dataset)
ssim_val_ns = sum(ssim_val_ns)/len(test_dataset)
rmse_val_rgb = sum(rmse_val_rgb) / len(test_dataset)
rmse_val_s = sum(rmse_val_s) / len(test_dataset)
rmse_val_ns = sum(rmse_val_ns) / len(test_dataset)
print("PSNR: %f, SSIM: %f, RMSE: %f " %(psnr_val_rgb, ssim_val_rgb, rmse_val_rgb))
print("SPSNR: %f, SSSIM: %f, SRMSE: %f " %(psnr_val_s, ssim_val_s, rmse_val_s))
print("NSPSNR: %f, NSSSIM: %f, NSRMSE: %f " %(psnr_val_ns, ssim_val_ns, rmse_val_ns))