-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.py
191 lines (151 loc) · 7.49 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
186
187
188
189
190
191
import numpy as np
import os
import torch
from torch.utils.data import DataLoader
from skimage.metrics import peak_signal_noise_ratio as PSNR
from skimage.metrics import structural_similarity as SSIM
from torch.utils.data import Dataset
import tqdm
import random
import imageio
from network import our_Net
# Dataloader define
def image_read(train_c_path, train_m_path, train_rgb_path):
"""
load image data to CPU ram, our dataset cost about 30Gb ram for training.
if you don't have enough ram, just move this "image_read" operation to "load_data"
it will read images from path in patch everytime.
input: (color raw images' path list, mono raw images' path list, RGB GT images' path list)
output: datalist
"""
gt_list = []
inp_list = []
gt_m_list = []
for i in tqdm.tqdm(range(len(train_c_path))):
color_raw = imageio.imread(train_c_path[i])
inp_list.append(color_raw)
mono_raw = imageio.imread(train_m_path[i])
gt_m_list.append(mono_raw)
gt_rgb = imageio.imread(train_rgb_path[i])
gt_list.append(gt_rgb)
return inp_list, gt_m_list, gt_list, train_c_path
class load_data(Dataset):
"""Loads the Data."""
def __init__(self, train_c_path, train_m_path, train_rgb_path, training=True):
self.training = training
if self.training:
print('\n...... Train files loading\n')
self.inp_list, self.gt_m_list, self.gt_list, self.train_c_path = image_read(train_c_path, train_m_path, train_rgb_path)
print('\nTrain files loaded ......\n')
else:
print('\n...... Test files loading\n')
self.inp_list, self.gt_m_list, self.gt_list, self.train_c_path = image_read(train_c_path, train_m_path, train_rgb_path)
print('\nTest files loaded ......\n')
def __len__(self):
return len(self.gt_list)
def __getitem__(self, idx):
gt_rgb_image = self.gt_list[idx]
gt_m_image = self.gt_m_list[idx]
inp_raw_image = self.inp_list[idx]
img_num = int(self.train_c_path[idx][-23:-20])
img_expo = int(self.train_c_path[idx][-8:-4],16)
H, W = inp_raw_image.shape
if img_num < 500:
gt_expo = 12287
else:
gt_expo = 1023
amp = gt_expo / img_expo
inp_raw_image = (inp_raw_image / 255 * amp).astype(np.float32)
gt_m_image = (gt_m_image / 255).astype(np.float32)
gt_rgb_image = (gt_rgb_image / 255).astype(np.float32)
if self.training:
"""
if training, random crop and flip are employed.
if testing, original image data will be used.
"""
i = random.randint(0, (H - 512 - 2) // 2) * 2
j = random.randint(0, (W - 512 - 2) // 2) * 2
inp_raw = inp_raw_image[i:i + 512, j:j + 512]
gt_m = gt_m_image[i:i + 512, j:j + 512]
gt_rgb = gt_rgb_image[i:i + 512, j:j + 512, :]
if random.randint(0, 100) > 50:
inp_raw = np.fliplr(inp_raw).copy()
gt_m = np.fliplr(gt_m).copy()
gt_rgb = np.fliplr(gt_rgb).copy()
if random.randint(0, 100) < 20:
inp_raw = np.flipud(inp_raw).copy()
gt_m = np.flipud(gt_m).copy()
gt_rgb = np.flipud(gt_rgb).copy()
else:
inp_raw = inp_raw_image
gt_m = gt_m_image
gt_rgb = gt_rgb_image
gt = torch.from_numpy((np.transpose(gt_rgb, [2, 0, 1]))).float()
gt_mono = torch.from_numpy(gt_m).float().unsqueeze(0)
inp = torch.from_numpy(inp_raw).float().unsqueeze(0)
return inp, gt_mono, gt
# run test during training, more CPU ram and GPU memory needed
def run_test(model, dataloader_test, iteration, save_images_file, save_csv_file, metric_average_filename):
psnr1 = ['PSNR1']
ssim1 = ['SSIM1']
psnr2 = ['PSNR2']
ssim2 = ['SSIM2']
with torch.no_grad():
model.eval()
for image_num, img in tqdm.tqdm(enumerate(dataloader_test)):
input_color = img[0].to(next(model.parameters()).device)
gt_mono = img[1]
gt_rgb = img[2]
mono_pred, rgb_pred = model(input_color)
mono_pred = (np.clip(mono_pred[0].detach().cpu().numpy().transpose(1, 2, 0), 0, 1) * 255).astype(np.uint8)
gt_mono = (np.clip(gt_mono[0].detach().cpu().numpy().transpose(1, 2, 0), 0, 1) * 255).astype(np.uint8)
rgb_pred = (np.clip(rgb_pred[0].detach().cpu().numpy().transpose(1, 2, 0), 0, 1) * 255).astype(np.uint8)
gt_rgb = (np.clip(gt_rgb[0].detach().cpu().numpy().transpose(1, 2, 0), 0, 1) * 255).astype(np.uint8)
psnr1_img = PSNR(mono_pred, gt_mono)
ssim1_img = SSIM(mono_pred, gt_mono, multichannel=True)
psnr2_img = PSNR(rgb_pred, gt_rgb)
ssim2_img = SSIM(rgb_pred, gt_rgb, multichannel=True)
# save test gt and predicted images
# imageio.imwrite(os.path.join(save_images_file, '{}_{}_gt.jpg'.format(image_num, iteration)), gt_rgb)
# imageio.imwrite(os.path.join(save_images_file,'{}_{}_psnr_{:.4f}_ssim_{:.4f}.jpg'.format(image_num, iteration, psnr2_img,ssim2_img)), rgb_pred)
#
# imageio.imwrite(os.path.join(save_images_file, '{}_{}_gt_mono.jpg'.format(image_num, iteration)), gt_mono)
# imageio.imwrite(os.path.join(save_images_file,'{}_{}_psnr_{:.4f}_ssim_{:.4f}_mono.jpg'.format(image_num, iteration, psnr1_img,ssim1_img)), mono_pred)
psnr1.append(psnr1_img)
ssim1.append(ssim1_img)
psnr2.append(psnr2_img)
ssim2.append(ssim2_img)
np.savetxt(os.path.join(save_csv_file, 'Metrics_iter_{}.csv'.format(iteration)),
[p for p in zip(psnr1, ssim1, psnr2, ssim2)], delimiter=',', fmt='%s')
psnr1_avg = sum(psnr1[1:]) / len(psnr1[1:])
ssim1_avg = sum(ssim1[1:]) / len(ssim1[1:])
psnr2_avg = sum(psnr2[1:]) / len(psnr2[1:])
ssim2_avg = sum(ssim2[1:]) / len(ssim2[1:])
# save test metrics
f = open(metric_average_filename, 'a')
f.write('-- psnr1_avg: {}, ssim1_avg: {},psnr2_avg: {}, ssim2_avg: {} iter: {}\n'.format(psnr1_avg, ssim1_avg,psnr2_avg, ssim2_avg, iteration))
print('average metric saved.')
f.close()
return
if __name__ == '__main__':
os.environ["CUDA_VISIBLE_DEVICES"]="0"
metric_average_file = 'result/metric_average.txt'
# These are folders
save_weights_file = 'result/weights'
save_images_file = 'result/images'
save_csv_file = 'result/csv_files'
# load random image paths
test_c_path = np.load('./random_path_list/test/test_c_path.npy')
test_m_path = np.load('./random_path_list/test/test_m_path.npy')
test_rgb_path = np.load('./random_path_list/test/test_rgb_path.npy')
dataloader_test = DataLoader(load_data(test_c_path,test_m_path,test_rgb_path,training=False), batch_size=1, shuffle=False, num_workers=0, pin_memory=True)
device = torch.device("cuda")
model = our_Net()
print('\nTrainable parameters : {}\n'.format(sum(p.numel() for p in model.parameters() if p.requires_grad)))
print('\nTotal parameters : {}\n'.format(sum(p.numel() for p in model.parameters())))
model = model.to(device)
print('Device on cuda: {}'.format(next(model.parameters()).is_cuda))
checkpoint = torch.load(save_weights_file + '/weights_234000.pth')
model.load_state_dict(checkpoint['model'], strict=False)
iter_num = 234000
run_test(model, dataloader_test, iter_num, save_images_file, save_csv_file, metric_average_file)