-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
286 lines (241 loc) · 9.41 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
import numpy as np
from PIL import Image
import tensorflow as tf
import scipy.stats as st
from skimage import io,data,color
from functools import reduce
import cv2
def _tf_fspecial_gauss(size, sigma):
"""Function to mimic the 'fspecial' gaussian MATLAB function
"""
x_data, y_data = np.mgrid[-size//2 + 1:size//2 + 1, -size//2 + 1:size//2 + 1]
x_data = np.expand_dims(x_data, axis=-1)
x_data = np.expand_dims(x_data, axis=-1)
y_data = np.expand_dims(y_data, axis=-1)
y_data = np.expand_dims(y_data, axis=-1)
x = tf.constant(x_data, dtype=tf.float32)
y = tf.constant(y_data, dtype=tf.float32)
g = tf.exp(-((x**2 + y**2)/(2.0*sigma**2)))
return g / tf.reduce_sum(g)
def tf_ssim(img1, img2, cs_map=False, mean_metric=True, size=11, sigma=1.5):
window = _tf_fspecial_gauss(size, sigma) # window shape [size, size]
K1 = 0.01
K2 = 0.03
L = 1 # depth of image (255 in case the image has a differnt scale)
C1 = (K1*L)**2
C2 = (K2*L)**2
mu1 = tf.nn.conv2d(img1, window, strides=[1,1,1,1], padding='VALID')
mu2 = tf.nn.conv2d(img2, window, strides=[1,1,1,1],padding='VALID')
mu1_sq = mu1*mu1
mu2_sq = mu2*mu2
mu1_mu2 = mu1*mu2
sigma1_sq = tf.nn.conv2d(img1*img1, window, strides=[1,1,1,1],padding='VALID') - mu1_sq
sigma2_sq = tf.nn.conv2d(img2*img2, window, strides=[1,1,1,1],padding='VALID') - mu2_sq
sigma12 = tf.nn.conv2d(img1*img2, window, strides=[1,1,1,1],padding='VALID') - mu1_mu2
if cs_map:
value = (((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*
(sigma1_sq + sigma2_sq + C2)),
(2.0*sigma12 + C2)/(sigma1_sq + sigma2_sq + C2))
else:
value = ((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*
(sigma1_sq + sigma2_sq + C2))
if mean_metric:
value = tf.reduce_mean(value)
return value
def gradient_no_abs(input_tensor, direction):
smooth_kernel_x = tf.reshape(tf.constant([[0, 0], [-1, 1]], tf.float32), [2, 2, 1, 1])
smooth_kernel_y = tf.transpose(smooth_kernel_x, [1, 0, 2, 3])
if direction == "x":
kernel = smooth_kernel_x
elif direction == "y":
kernel = smooth_kernel_y
gradient_orig = tf.nn.conv2d(input_tensor, kernel, strides=[1, 1, 1, 1], padding='SAME')
grad_min = tf.reduce_min(gradient_orig)
grad_max = tf.reduce_max(gradient_orig)
grad_norm = tf.div((gradient_orig - grad_min), (grad_max - grad_min + 0.0001))
return grad_norm
def gradient(input_tensor, direction):
smooth_kernel_x = tf.reshape(tf.constant([[0, 0], [-1, 1]], tf.float32), [2, 2, 1, 1])
smooth_kernel_y = tf.transpose(smooth_kernel_x, [1, 0, 2, 3])
if direction == "x":
kernel = smooth_kernel_x
elif direction == "y":
kernel = smooth_kernel_y
gradient_orig = tf.abs(tf.nn.conv2d(input_tensor, kernel, strides=[1, 1, 1, 1], padding='SAME'))
return gradient_orig
def gauss_kernel(kernlen=21, nsig=3, channels=1):
interval = (2*nsig+1.)/(kernlen)
x = np.linspace(-nsig-interval/2., nsig+interval/2., kernlen+1)
kern1d = np.diff(st.norm.cdf(x))
kernel_raw = np.sqrt(np.outer(kern1d, kern1d))
kernel = kernel_raw/kernel_raw.sum()
out_filter = np.array(kernel, dtype = np.float32)
out_filter = out_filter.reshape((kernlen, kernlen, 1, 1))
out_filter = np.repeat(out_filter, channels, axis = 2)
return out_filter
def tensor_size(tensor):
from operator import mul
return reduce(mul, (d.value for d in tensor.get_shape()[1:]), 1)
def blur(x):
kernel_var = gauss_kernel(21, 3, 1)
return tf.nn.depthwise_conv2d(x, kernel_var, [1, 1, 1, 1], padding='SAME')
def tensor_size(tensor):
from operator import mul
return reduce(mul, (d.value for d in tensor.get_shape()[1:]), 1)
def data_augmentation(image, mode):
if mode == 0:
# original
return image
elif mode == 1:
# flip up and down
return np.flipud(image)
elif mode == 2:
# rotate counterwise 90 degree
return np.rot90(image)
elif mode == 3:
# rotate 90 degree and flip up and down
image = np.rot90(image)
return np.flipud(image)
elif mode == 4:
# rotate 180 degree
return np.rot90(image, k=2)
elif mode == 5:
# rotate 180 degree and flip
image = np.rot90(image, k=2)
return np.flipud(image)
elif mode == 6:
# rotate 270 degree
return np.rot90(image, k=3)
elif mode == 7:
# rotate 270 degree and flip
image = np.rot90(image, k=3)
return np.flipud(image)
def load_images(file):
im = Image.open(file)
img = np.array(im, dtype="float32") / 255.0
img_max = np.max(img)
img_min = np.min(img)
img_norm = np.float32((img - img_min) / np.maximum((img_max - img_min), 0.001))
return img_norm
def load_images_no_norm(file):
im = Image.open(file)
img = np.array(im, dtype="float32") / 255.0
return img
def bright_channel_2(input_img):
h, w = input_img.shape[:2]
I = input_img
res = np.minimum(I , I[[0]+range(h-1) , :])
res = np.minimum(res, I[range(1,h)+[h-1], :])
I = res
res = np.minimum(I , I[:, [0]+range(w-1)])
res = np.minimum(res, I[:, range(1,w)+[w-1]])
return res
def bright_channel(input_img):
r = input_img[:,:,0]
g = input_img[:,:,1]
b = input_img[:,:,2]
m,n = r.shape
print(m,n)
tmp = np.zeros((m,n))
b_c = np.zeros((m,n))
for i in range(0,m-1):
for j in range(0,n-1):
tmp[i,j] = np.max([r[i,j], g[i,j]])
b_c[i,j] = np.max([tmp[i,j], b[i,j]])
return b_c
def load_raw_high_images(file):
raw = rawpy.imread(file)
im_raw = raw.postprocess(use_camera_wb = True, half_size = False, no_auto_bright=True, output_bps=16)
#im_raw = np.maximum(im_raw - 512,0)/ (65535 - 512)
im_raw = np.float32(im_raw/65535.0)
im_raw_min = np.min(im_raw)
im_raw_max = np.max(im_raw)
a_weight = np.float32(im_raw_max - im_raw_min)
im_norm = np.float32((im_raw - im_raw_min) / a_weight)
return im_norm, a_weight
def load_raw_images(file):
raw = rawpy.imread(file)
im_raw = raw.postprocess(use_camera_wb = True, half_size = False, no_auto_bright=True, output_bps=16)
#im_raw = np.maximum(im_raw - 512,0)/ (65535 - 512)
im_raw = np.float32(im_raw/65535.0)
im_raw_min = np.min(im_raw)
im_raw_max = np.max(im_raw)
a_weight = np.float32(im_raw_max - im_raw_min)
im_norm = np.float32((im_raw - im_raw_min) / a_weight)
return im_norm, a_weight
def load_raw_low_images(file):
raw = rawpy.imread(file)
im_raw = raw.postprocess(use_camera_wb = True, half_size = False, no_auto_bright=True, output_bps=16)
im_raw = np.maximum(im_raw - 512.0,0)/ (65535.0 - 512.0)
im_raw = np.float32(im_raw)
im_raw_min = np.min(im_raw)
print(im_raw_min)
im_raw_max = np.max(im_raw)
print(im_raw_max)
a_weight = np.float32(im_raw_max - im_raw_min)
im_norm = np.float32((im_raw - im_raw_min) / a_weight)
print(a_weight)
return im_norm, a_weight
def load_images_and_norm(file):
im = Image.open(file)
img = np.array(im, dtype="float32") / 255.0
img_max = np.max(img)
img_min = np.min(img)
img_norm = np.float32((img - img_min) / np.maximum((img_max - img_min), 0.001))
norm_coeff = np.float32(img_max - img_min)
return img_norm, norm_coeff
def load_images_and_a_and_norm(file):
im = Image.open(file)
img = np.array(im, dtype="float32") / 255.0
img_max = np.max(img)
img_min = np.min(img)
img_norm = np.float32((img - img_min) / np.maximum((img_max - img_min), 0.001))
a_weight = np.float32(img_max - img_min)
return img, img_norm, a_weight
def load_images_and_a_003(file):
im = Image.open(file)
img = np.array(im, dtype="float32") / 255.0
img_max = np.max(img)
img_min = np.min(img)
img_norm = np.float32((img - img_min) / np.maximum((img_max - img_min), 0.001))
img_norm = (np.maximum(img_norm, 0.03)-0.03) / 0.97
a_weight = np.float32(img_max - img_min)
return img_norm, a_weight
def load_images_no_norm(file):
im = Image.open(file)
return np.array(im, dtype="float32") / 255.0
def load_images_uint16(file):
im = Image.open(file)
return np.array(im, dtype="float32") / 65535.0
def load_images_hsv(file):
im = io.imread(file)
hsv = color.rgb2hsv(im)
return hsv
def save_images(filepath, result_1, result_2 = None, result_3 = None):
result_1 = np.squeeze(result_1)
result_2 = np.squeeze(result_2)
result_3 = np.squeeze(result_3)
if not result_2.any():
cat_image = result_1
else:
cat_image = np.concatenate([result_1, result_2], axis = 1)
if not result_3.any():
cat_image = cat_image
else:
cat_image = np.concatenate([cat_image, result_3], axis = 1)
im = Image.fromarray(np.clip(cat_image * 255.0, 0, 255.0).astype('uint8'))
im.save(filepath, 'png')
def save_images_noise(filepath, result_1, result_2 = None, result_3 = None):
result_1 = np.squeeze(result_1)
result_2 = np.squeeze(result_2)
result_3 = np.squeeze(result_3)
if not result_2.any():
cat_image = result_1
else:
cat_image = np.concatenate([result_1, result_2], axis = 1)
if not result_3.any():
cat_image = cat_image
else:
cat_image = np.concatenate([cat_image, result_3], axis = 1)
im = Image.fromarray(np.clip(abs(cat_image) * 255.0,0, 255.0).astype('uint8'))
im.save(filepath, 'png')