forked from tsingqguo/efficientderain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
295 lines (258 loc) · 9.43 KB
/
utils.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
import os
import cv2
import skimage
import numpy as np
import torch
import torch.nn as nn
import torchvision as tv
import math
import network
# ----------------------------------------
# Network
# ----------------------------------------
def create_generator(opt):
# Initialize the network
generator = network.KPN(opt.color, opt.burst_length, opt.blind_est, opt.kernel_size, opt.sep_conv, \
opt.channel_att, opt.spatial_att, opt.upMode, opt.core_bias)
if opt.load_name == '':
# Init the network
network.weights_init(generator, init_type = opt.init_type, init_gain = opt.init_gain)
print('Generator is created!')
else:
# Load a pre-trained network
pretrained_net = torch.load(opt.load_name)
load_dict(generator, pretrained_net)
print('Generator is loaded!')
return generator
def load_dict(process_net, pretrained_net):
# Get the dict from pre-trained network
pretrained_dict = pretrained_net
# Get the dict from processing network
process_dict = process_net.state_dict()
# Delete the extra keys of pretrained_dict that do not belong to process_dict
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in process_dict}
# Update process_dict using pretrained_dict
process_dict.update(pretrained_dict)
# Load the updated dict to processing network
process_net.load_state_dict(process_dict)
return process_net
# ----------------------------------------
# Validation and Sample at training
# ----------------------------------------
def save_sample_png(sample_folder, sample_name, img_list, name_list, pixel_max_cnt = 255, height = -1, width = -1):
# Save image one-by-one
for i in range(len(img_list)):
img = img_list[i]
# Recover normalization
img = img * 255.0
# Process img_copy and do not destroy the data of img
#print(img.size())
img_copy = img.clone().data.permute(0, 2, 3, 1).cpu().numpy()
img_copy = np.clip(img_copy, 0, pixel_max_cnt)
img_copy = img_copy.astype(np.uint8)[0, :, :, :]
img_copy = cv2.cvtColor(img_copy, cv2.COLOR_BGR2RGB)
if (height != -1) and (width != -1):
img_copy = cv2.resize(img_copy, (width, height))
# Save to certain path
save_img_name = sample_name + '_' + name_list[i] + '.png'
save_img_path = os.path.join(sample_folder, save_img_name)
cv2.imwrite(save_img_path, img_copy)
def save_sample_png_test(sample_folder, sample_name, img_list, name_list, pixel_max_cnt = 255):
# Save image one-by-one
for i in range(len(img_list)):
img = img_list[i]
# Recover normalization
img = img * 255.0
# Process img_copy and do not destroy the data of img
img_copy = img.clone().data.permute(0, 2, 3, 1).cpu().numpy()
img_copy = np.clip(img_copy, 0, pixel_max_cnt)
img_copy = img_copy.astype(np.uint8)[0, :, :, :]
img_copy = img_copy.astype(np.float32)
img_copy = cv2.cvtColor(img_copy, cv2.COLOR_BGR2RGB)
# Save to certain path
save_img_name = sample_name + '_' + name_list[i] + '.png'
save_img_path = os.path.join(sample_folder, save_img_name)
cv2.imwrite(save_img_path, img_copy)
def recover_process(img, height = -1, width = -1):
img = img * 255.0
img_copy = img.clone().data.permute(0, 2, 3, 1).cpu().numpy()
img_copy = np.clip(img_copy, 0, 255)
img_copy = img_copy.astype(np.uint8)[0, :, :, :]
img_copy = img_copy.astype(np.float32)
img_copy = cv2.cvtColor(img_copy, cv2.COLOR_BGR2RGB)
if (height != -1) and (width != -1):
img_copy = cv2.resize(img_copy, (width, height))
return img_copy
def psnr(pred, target):
#print(pred.shape)
#print(target.shape)
mse = np.mean( (pred - target) ** 2 )
if mse == 0:
return 100
PIXEL_MAX = 255.0
return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
'''
def psnr(pred, target, pixel_max_cnt = 255):
mse = torch.mul(target - pred, target - pred)
rmse_avg = (torch.mean(mse).item()) ** 0.5
p = 20 * np.log10(pixel_max_cnt / rmse_avg)
return p
'''
def grey_psnr(pred, target, pixel_max_cnt = 255):
pred = torch.sum(pred, dim = 0)
target = torch.sum(target, dim = 0)
mse = torch.mul(target - pred, target - pred)
rmse_avg = (torch.mean(mse).item()) ** 0.5
p = 20 * np.log10(pixel_max_cnt * 3 / rmse_avg)
return p
def ssim(pred, target):
pred = pred.clone().data.permute(0, 2, 3, 1).cpu().numpy()
target = target.clone().data.permute(0, 2, 3, 1).cpu().numpy()
target = target[0]
pred = pred[0]
ssim = skimage.measure.compare_ssim(target, pred, multichannel = True)
return ssim
# ----------------------------------------
# PATH processing
# ----------------------------------------
def check_path(path):
if not os.path.exists(path):
os.makedirs(path)
def savetxt(name, loss_log):
np_loss_log = np.array(loss_log)
np.savetxt(name, np_loss_log)
#rain100H/L / SPA
def get_files(path):
ret = []
path_rainy = path + "/small/rain"
path_gt = path + "/small/norain"
for root, dirs, files in os.walk(path_rainy):
files.sort()
for name in files:
if name.split('.')[1] != 'png':
continue
file_rainy = path_rainy + "/" + name
file_gt = path_gt + "/" + name
ret.append([file_rainy, file_gt])
return ret
'''
#rain1400
def get_files(path):
ret = []
path_rainy = path + "/rainy_image"
path_gt = path + "/ground_truth"
for root, dirs, files in os.walk(path_gt):
files.sort()
for name in files:
if name.split('.')[1] != "jpg":
continue
id = name.split('.')[0]
file_gt = path_gt + "/" + id + ".jpg"
for i in range(1, 15):
file_rainy = path_rainy + "/" + id + "_" + str(i) + ".jpg"
ret.append([file_rainy, file_gt])
return ret
'''
'''
#real
def get_files(path):
# read a folder, return the complete path of rainy files and ground truth files
ret=[]
path_rainy = path + "/rainy_image"
path_gt = path + "/ground_truth"
for root, dirs, files in os.walk(path):
files.sort()
for name in files:
if name.split('.')[1] != 'png':
continue
id = name.split('.')[0]
file_gt = path + '/' + id + '.png'
file_rainy = file_gt
ret.append([file_rainy, file_gt])
return ret
'''
'''
def get_files(path):
# read a folder, return the complete path
ret = []
for root, dirs, files in os.walk(path):
for filespath in files:
ret.append(os.path.join(root, filespath))
return ret
'''
def get_jpgs(path):
# read a folder, return the image name
ret = []
for root, dirs, files in os.walk(path):
for filespath in files:
ret.append(filespath)
return ret
def get_last_2paths(path):
# read a folder, return the image name
ret = []
for root, dirs, files in os.walk(path):
for filespath in files:
if filespath[-4:] == '.png':
wholepath = os.path.join(root, filespath)
last_2paths = os.path.join(wholepath.split('/')[-2], wholepath.split('/')[-1])
ret.append(last_2paths)
return ret
def text_readlines(filename):
# Try to read a txt file and return a list.Return [] if there was a mistake.
try:
file = open(filename, 'r')
except IOError:
error = []
return error
content = file.readlines()
# This for loop deletes the EOF (like \n)
for i in range(len(content)):
content[i] = content[i][:len(content[i])-1]
file.close()
return content
def text_save(content, filename, mode = 'a'):
# save a list to a txt
# Try to save a list variable in txt file.
file = open(filename, mode)
for i in range(len(content)):
file.write(str(content[i]))
file.close()
'''
a = torch.randn(1, 3, 4, 4)
b = torch.randn(1, 3, 4, 4)
c = (a, b)
d = repackage_hidden(c)
print(d)
'''
'''
class TVLoss(nn.Module):
def __init__(self, TVLoss_weight = 1):
super(TVLoss, self).__init__()
self.TVLoss_weight = TVLoss_weight
def forward(self, x):
batch_size = x.size()[0]
h_x = x.size()[2]
w_x = x.size()[3]
count_h = self._tensor_size(x[:, :, 1:, :])
count_w = self._tensor_size(x[:, :, :, 1:])
h_tv = torch.pow((x[:, :, 1:, :] - x[:, :, :h_x - 1, :]), 2).sum()
w_tv = torch.pow((x[:, :, :, 1:] - x[:, :, :, :w_x - 1]), 2).sum()
return self.TVLoss_weight * 2 * (h_tv / count_h + w_tv / count_w) / batch_size
def _tensor_size(self, t):
return t.size()[1] * t.size()[2] * t.size()[3]
class GradLoss(nn.Module):
def __init__(self, GradLoss_weight = 1):
super(GradLoss, self).__init__()
self.GradLoss_weight = GradLoss_weight
self.MSEloss = nn.MSELoss()
def forward(self, x, y):
h_x = x.size()[2]
w_x = x.size()[3]
x_h_grad = x[:, :, 1:, :] - x[:, :, :h_x - 1, :]
x_w_grad = x[:, :, :, 1:] - x[:, :, :, :w_x - 1]
y_h_grad = y[:, :, 1:, :] - y[:, :, :h_x - 1, :]
y_w_grad = y[:, :, :, 1:] - y[:, :, :, :w_x - 1]
h_loss = self.MSEloss(x_h_grad, y_h_grad)
w_loss = self.MSEloss(x_w_grad, y_w_grad)
return self.GradLoss_weight * (h_loss + w_loss)
'''