-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
288 lines (244 loc) · 12.6 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
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 13 20:25:47 2018
@author: tyty
@e-mail: bravotty@protonmail.com
"""
import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
import tflearn
import numpy as np
from skimage.measure import compare_ssim, compare_psnr
import tensorflow.contrib.slim as slim
xy = 256
def im2uint8(x):
if x.__class__ == tf.Tensor:
return tf.cast(tf.clip_by_value(x, 0.0, 1.0) * 255.0, tf.uint8)
else:
t = np.clip(x, 0.0, 1.0) * 255.0
return t.astype(np.uint8)
def conv_torque(input):
inputShape = list(input.shape[0:3])
hSqrt2 = 0.5 * 2 ** 0.5
direc = [ [ 1, 0],
[hSqrt2 ,hSqrt2],
[ 0, 1],
[-hSqrt2 ,hSqrt2],
[ -1, 0],
[-hSqrt2 ,-hSqrt2],
[ 0, -1],
[hSqrt2 ,-hSqrt2]]
gradientOriy = tf.zeros(inputShape,dtype=tf.float32)
gradientOrix = tf.zeros(inputShape,dtype=tf.float32)
AllOnes = tf.ones(inputShape,dtype=tf.float32)
for i in range(input.shape[3]):
gradientOriy += AllOnes*input[:,:,:,i] * direc[i][0]
gradientOrix += AllOnes*input[:,:,:,i] * direc[i][1]
gradientOriy = tf.expand_dims(gradientOriy,axis=3)
gradientOrix = tf.expand_dims(gradientOrix,axis=3)
tfgradientOriy = gradientOriy
tfgradientOrix = gradientOrix
tffiltery20 = tf.constant(np.expand_dims(np.expand_dims(np.tile(np.expand_dims(np.linspace(9.5, -9.5, 20), axis=1), [1, 20]),axis=2),axis=3), tf.float32)
tffilterx20 = tf.constant(np.expand_dims(np.expand_dims(np.tile(np.linspace(9.5, -9.5, 20), [20,1]), axis=2), axis=3), tf.float32)
xresult20 = tf.nn.conv2d(tfgradientOrix, tffilterx20, strides=[1, 1, 1, 1], padding='SAME')
yresult20 = tf.nn.conv2d(tfgradientOriy, tffiltery20, strides=[1, 1, 1, 1], padding='SAME')
result20 = (xresult20 + yresult20)/20**2 /20**2
tffiltery25 = tf.constant(np.expand_dims(np.expand_dims(np.tile(np.expand_dims(np.linspace(12, -12, 25), axis=1), [1, 25]),axis=2),axis=3), tf.float32)
tffilterx25 = tf.constant(np.expand_dims(np.expand_dims(np.tile(np.linspace(12, -12, 25), [25,1]), axis=2), axis=3), tf.float32)
xresult25 = tf.nn.conv2d(tfgradientOrix, tffilterx25, strides=[1, 1, 1, 1], padding='SAME')
yresult25 = tf.nn.conv2d(tfgradientOriy, tffiltery25, strides=[1, 1, 1, 1], padding='SAME')
result25 = (xresult25 + yresult25)/25**2/25**2
tffiltery30 = tf.constant(np.expand_dims(np.expand_dims(np.tile(np.expand_dims(np.linspace(14.5, -14.5, 30), axis=1), [1, 30]),axis=2),axis=3), tf.float32)
tffilterx30 = tf.constant(np.expand_dims(np.expand_dims(np.tile(np.linspace(14.5, -14.5, 30), [30,1]), axis=2), axis=3), tf.float32)
xresult30 = tf.nn.conv2d(tfgradientOrix, tffilterx30, strides=[1, 1, 1, 1], padding='SAME')
yresult30 = tf.nn.conv2d(tfgradientOriy, tffiltery30, strides=[1, 1, 1, 1], padding='SAME')
result30 = (xresult30 + yresult30)/30**2/30**2
tffiltery35 = tf.constant(np.expand_dims(np.expand_dims(np.tile(np.expand_dims(np.linspace(17, -17, 35), axis=1), [1, 35]),axis=2),axis=3), tf.float32)
tffilterx35 = tf.constant(np.expand_dims(np.expand_dims(np.tile(np.linspace(17, -17, 35), [35,1]), axis=2), axis=3), tf.float32)
xresult35 = tf.nn.conv2d(tfgradientOrix, tffilterx35, strides=[1, 1, 1, 1], padding='SAME')
yresult35 = tf.nn.conv2d(tfgradientOriy, tffiltery35, strides=[1, 1, 1, 1], padding='SAME')
result35 = (xresult35 + yresult35)/35**2/35**2
tffiltery40 = tf.constant(np.expand_dims(np.expand_dims(np.tile(np.expand_dims(np.linspace(19.5, -19.5, 40), axis=1), [1, 40]),axis=2),axis=3), tf.float32)
tffilterx40 = tf.constant(np.expand_dims(np.expand_dims(np.tile(np.linspace(19.5, -19.5, 40), [40,1]), axis=2), axis=3), tf.float32)
xresult40 = tf.nn.conv2d(tfgradientOrix, tffilterx40, strides=[1, 1, 1, 1], padding='SAME')
yresult40 = tf.nn.conv2d(tfgradientOriy, tffiltery40, strides=[1, 1, 1, 1], padding='SAME')
result40 = (xresult40 + yresult40)/40**2/40**2
result = tf.concat([result20,result25,result30,result35,result40],axis=3)
result = tf.maximum(result,0)
Maxnpresult = tf.reduce_max(result,axis=3,keep_dims=True)
return Maxnpresult
def amitedgefinder(img,threshold):
# if a is the minimum of b's neighbors and b is the maximum of a's neighbors an edge is found.
shift = 2 * [[1,0],[0,-1],[-1,0],[0,1]]#North east south west
img = rgb2gray(img)
b,h,w = img.shape
cross0 = img - myShift(img, shift[0])
cross1 = img - myShift(img, shift[1])
cross2 = img - myShift(img, shift[2])
cross3 = img - myShift(img, shift[3])
cross = tf.stack([cross0,cross1,cross2,cross3],axis=3)
whichMax = tf.argmax(cross,axis=3)
whichMin = tf.argmin(cross,axis=3)
edgeMap0 = findGradient(cross,whichMax,whichMin,shift[0],0,threshold)
edgeMap2 = findGradient(cross,whichMax,whichMin,shift[1],1,threshold)
edgeMap4 = findGradient(cross,whichMax,whichMin,shift[2],2,threshold)
edgeMap6 = findGradient(cross,whichMax,whichMin,shift[3],3,threshold)
dshift = 2* [[1,-1],[-1,-1],[-1,1],[1,1]]#diagnal direction
cross4 = img - myShift(img, dshift[0])
cross5 = img - myShift(img, dshift[1])
cross6 = img - myShift(img, dshift[2])
cross7 = img - myShift(img, dshift[3])
cross_d = tf.stack([cross4,cross5,cross6,cross7],axis=3)
whichMax = tf.argmax(cross_d,axis=3)
whichMin = tf.argmin(cross_d,axis=3)
edgeMap1 = findGradient(cross_d,whichMax,whichMin,dshift[0],0,threshold)
edgeMap3 = findGradient(cross_d,whichMax,whichMin,dshift[1],1,threshold)
edgeMap5 = findGradient(cross_d,whichMax,whichMin,dshift[2],2,threshold)
edgeMap7 = findGradient(cross_d,whichMax,whichMin,dshift[3],3,threshold)
edgeMap = tf.stack([edgeMap0, edgeMap1, edgeMap2, edgeMap3, edgeMap4, edgeMap5, edgeMap6, edgeMap7], axis=3)
return edgeMap
def rgb2gray(rgb):
return rgb[...,0]*0.299+rgb[...,1]*0.587+rgb[...,2]*0.114
def findGradient(cross, whichMaxDir, whichMinDir, shiftDir, dirIndex, threshold):
oppositeDirIndex = (dirIndex + 1)%4 + 1
crossSegment = cross[:,:,:,dirIndex]
shiftedWhichMinDir = myShift(whichMinDir, shiftDir)
gradient = tf.logical_and(tf.equal(whichMaxDir,dirIndex), (tf.equal(shiftedWhichMinDir,oppositeDirIndex)))
gradient = tf.logical_and(gradient,tf.greater(crossSegment,threshold/255.0))
return gradient
def myShift(img,vector):
absVector = [abs(vector[0]),abs(vector[1])]
padImg = tf.pad(img,[[0,0],[absVector[0],absVector[0]],[absVector[1],absVector[1]]],'CONSTANT')
_,padh,padw = padImg.shape
shitfImg = tf.manip.roll(padImg, shift=vector, axis=[1, 2])
shifted = shitfImg[:,absVector[0] :padh - absVector[0], absVector[1] : padw - absVector[1]]
return shifted
def ResnetBlock_att(x, dim, ksize, att,scope='rb'):
with tf.variable_scope(scope):
net1= slim.conv2d(x, dim, [ksize, ksize], scope='conv1_1')
net1 = slim.conv2d(net1, dim, [ksize, ksize], activation_fn=None, scope='conv1_2')
netc, feature, c_scale = channel_attention(net1, 'ch_at', 2.0)
s_scale = tf.reduce_mean(att,axis=3 ,keep_dims=True)
s_scale = tf.tile(s_scale,[1,1,1,dim])
netresult = ((1+c_scale)*(1+s_scale)-1)*net1 # CA * PA * feature map
return tf.nn.relu( netresult + x ), feature, c_scale
def channel_attention(input_feature, name, ratio=8):
kernel_initializer = tf.contrib.layers.variance_scaling_initializer()
bias_initializer = tf.constant_initializer(value=0.0)
with tf.variable_scope(name):
channel = input_feature.get_shape()[-1]
avg_pool = tf.reduce_mean(input_feature, axis=[1, 2], keep_dims=True)
assert avg_pool.get_shape()[1:] == (1, 1, channel)
avg_pool = tf.layers.dense(inputs=avg_pool,
units=channel // ratio,
activation=tf.nn.relu,
kernel_initializer=kernel_initializer,
bias_initializer=bias_initializer,
name='mlp_0',
reuse=None)
assert avg_pool.get_shape()[1:] == (1, 1, channel // ratio)
avg_pool = tf.layers.dense(inputs=avg_pool,
units=channel,
kernel_initializer=kernel_initializer,
bias_initializer=bias_initializer,
name='mlp_1',
reuse=None)
assert avg_pool.get_shape()[1:] == (1, 1, channel)
max_pool = tf.reduce_max(input_feature, axis=[1, 2], keep_dims=True)
assert max_pool.get_shape()[1:] == (1, 1, channel)
max_pool = tf.layers.dense(inputs=max_pool,
units=channel // ratio,
activation=tf.nn.relu,
name='mlp_0',
reuse=True)
assert max_pool.get_shape()[1:] == (1, 1, channel // ratio)
max_pool = tf.layers.dense(inputs=max_pool,
units=channel,
name='mlp_1',
reuse=True)
assert max_pool.get_shape()[1:] == (1, 1, channel)
scale = tf.sigmoid(avg_pool + max_pool, 'sigmoid')
return input_feature * scale, input_feature, scale
def fc_layer(input_, output_dim, initializer = None, activation='linear', name=None):
if initializer == None: initializer = tf.contrib.layers.xavier_initializer()
shape = input_.get_shape().as_list()
with tf.variable_scope(name or "Linear", reuse=tf.AUTO_REUSE) as scope:
if len(shape) > 2 : input_ = tf.layers.flatten(input_)
shape = input_.get_shape().as_list()
w = tf.get_variable("fc_w", [shape[1], output_dim], dtype=tf.float32, initializer = initializer)
b = tf.get_variable("fc_b", [output_dim], initializer = tf.constant_initializer(0.0))
result = tf.matmul(input_, w) + b
if activation == 'linear':
return result
elif activation == 'relu':
return tf.nn.relu(result)
elif activation == 'sigmoid':
return tf.nn.sigmoid(result)
elif activation == 'tanh':
return tf.nn.tanh(result)
def Linear(input_, output_size, scope=None, stddev=0.02, bias_start=0.0, with_w=False):
shape = input_.get_shape().as_list()
with tf.variable_scope(scope or "Linear"):
try:
matrix = tf.get_variable("Matrix", [shape[1], output_size], tf.float32,
tf.random_normal_initializer(stddev=stddev))
except ValueError as err:
msg = "NOTE: Usually, this is due to an issue with the image dimensions. Did you correctly set '--crop' or '--input_height' or '--output_height'?"
err.args = err.args + (msg,)
raise
bias = tf.get_variable("bias", [output_size],
initializer=tf.constant_initializer(bias_start))
if with_w:
return tf.matmul(input_, matrix) + bias, matrix, bias
else:
return tf.matmul(input_, matrix) + bias
def sigmoid_cross_entropy_with_logits(x, y):
try:
return tf.nn.sigmoid_cross_entropy_with_logits(logits=x, labels=y)
except:
return tf.nn.sigmoid_cross_entropy_with_logits(logits=x, targets=y)
def calc_psnr(im1, im2):
im1_y = cv2.cvtColor(im1, cv2.COLOR_BGR2YCR_CB)[:, :, 0]
im2_y = cv2.cvtColor(im2, cv2.COLOR_BGR2YCR_CB)[:, :, 0]
return compare_psnr(im1_y, im2_y)
def calc_ssim(im1, im2):
im1_y = cv2.cvtColor(im1, cv2.COLOR_BGR2YCR_CB)[:, :, 0]
im2_y = cv2.cvtColor(im2, cv2.COLOR_BGR2YCR_CB)[:, :, 0]
return compare_ssim(im1_y, im2_y)
def cal_ssim_psnr(outs, y_s, batch_s):
psnr = 0
ssim = 0
for i in range(batch_s):
psnr += calc_psnr(y_s[i], outs[i])
ssim += calc_ssim(outs[i], y_s[i])
ssim /= batch_s
psnr /= batch_s
print ('ssim value ', ssim)
print ('psnr value ', psnr)
return ssim
def psnrrgb(img1, img2):
img1 = np.clip(img1, 0, 255)
img2 = np.clip(img2, 0, 255)
img1 = img1.astype(np.float32)
img2 = img2.astype(np.float32)
if(len(img1.shape) == 2):
m, n = img1.shape
k = 1
elif (len(img1.shape) == 3):
m, n, k = img1.shape
B = 8
diff = np.power(img1 - img2, 2)
MAX = 2**B - 1
MSE = np.sum(diff) / (m * n * k)
sqrt_MSE = np.sqrt(MSE)
PSNR = 20 * np.log10(MAX / sqrt_MSE)
return PSNR
def align_to_four(img):
# print ('before alignment, row = %d, col = %d'%(img.shape[0], img.shape[1]))
# align to four
a_row = int(img.shape[0] / 16) * 16
a_col = int(img.shape[1] / 16) * 16
img = img[0:a_row, 0:a_col]
# print ('after alignment, row = %d, col = %d'%(img.shape[0], img.shape[1]))
return img