-
Notifications
You must be signed in to change notification settings - Fork 0
/
lap_pyramid.py
151 lines (129 loc) · 6.52 KB
/
lap_pyramid.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
import tensorflow as tf
import numpy as np
import functools
class lap_pyramid():
def __init__(self, k, image_size, kernel=np.reshape([[0.0025, 0.0125, 0.0200, 0.0125, 0.0025],
[0.0125, 0.0625, 0.1000, 0.0625, 0.0125],
[0.0200, 0.1000, 0.1600, 0.1000, 0.0200],
[0.0125, 0.0625, 0.1000, 0.0625, 0.0125],
[0.0025, 0.0125, 0.0200, 0.0125, 0.0025]],
(5, 5, 1, 1)),
strides=[1, 2, 2, 1], padding='VALID', loss=False):
self.k = k
self.kernel = kernel
self.loss = loss
self.strides = strides
self.padding = padding
self.image_size = image_size
self.dn_filts, self.sigmas = self.DN_filters()
if not self.loss:
self.build_model()
# Self pad to prevent a lot of zero padding - also using 'SYMMETRIC'
def pad(self, image, pad=1, method="CONSTANT"):
return tf.pad(image, [[0, 0], [pad, pad], [pad, pad], [0, 0]], method)
def DN_filters(self):
# These parameters were learned using the McGill dataset
# Training_NLP_param.m
sigmas = [0.0248, 0.0185, 0.0179, 0.0191, 0.0220, 0.2782]
dn_filts = []
dn_filts.append(np.reshape([[0, 0.1011, 0],
[0.1493, 0, 0.1460],
[0, 0.1015, 0.]],
(3, 3, 1, 1)))
dn_filts.append(np.reshape([[0, 0.0757, 0],
[0.1986, 0, 0.1846],
[0, 0.0837, 0]],
(3, 3, 1, 1)))
dn_filts.append(np.reshape([[0, 0.0477, 0],
[0.2138, 0, 0.2243],
[0, 0.0467, 0]],
(3, 3, 1, 1)))
dn_filts.append(np.reshape([[0, 0, 0],
[0.2503, 0, 0.2616],
[0, 0, 0]],
(3, 3, 1, 1)))
dn_filts.append(np.reshape([[0, 0, 0],
[0.2598, 0, 0.2552],
[0, 0, 0]],
(3, 3, 1, 1)))
dn_filts.append(np.reshape([[0, 0, 0],
[0.2215, 0, 0.0717],
[0, 0, 0]],
(3, 3, 1, 1)))
return dn_filts, sigmas
def normalise(self, convs):
norm = []
for i in range(0, len(convs)):
n = tf.nn.conv2d(tf.abs(convs[i]), self.dn_filts[i], strides=[1, 1, 1, 1], padding="SAME")
norm.append(convs[i] / (self.sigmas[i] + n))
return norm
def convs(self):
J = self.im
pyr = []
for i in range(0, self.k - 1):
I = tf.nn.conv2d(self.pad(J, pad=2), self.kernel, strides=self.strides, padding=self.padding)
I_up = tf.image.resize_images(I, [int(np.ceil(self.image_size[0] / (2 ** i))),
int(np.ceil(self.image_size[1] / (2 ** i)))])
I_up_conv = tf.nn.conv2d(self.pad(I_up, pad=2), self.kernel, strides=[1, 1, 1, 1], padding=self.padding)
pyr.append(J - I_up_conv)
J = I
pyr.append(J)
return self.normalise(pyr)
def convs_loss_function(self, im):
J = im
pyr = []
for i in range(0, self.k - 1):
I = tf.nn.conv2d(self.pad(J, pad=2), self.kernel, strides=self.strides, padding=self.padding)
I_up = tf.image.resize_images(I, [int(np.ceil(self.image_size[0] / (2 ** i))),
int(np.ceil(self.image_size[1] / (2 ** i)))])
I_up_conv = tf.nn.conv2d(self.pad(I_up, pad=2), self.kernel, strides=[1, 1, 1, 1], padding=self.padding)
pyr.append(J - I_up_conv)
J = I
pyr.append(J)
return self.normalise(pyr)
def loss_function(self, real, pred):
realpyr = self.convs_loss_function(real)
predpyr = self.convs_loss_function(pred)
total = tf.convert_to_tensor(0, dtype=tf.float32)
for i in range(0, self.k):
total = tf.add(total, tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(realpyr[i], predpyr[i])))))
return tf.divide(total, tf.convert_to_tensor(self.k, dtype=tf.float32))
def forward(self, image):
convs_out, convs_up_out = self.sess.run([self.convs_, self.convs_up_],
feed_dict={self.im: image})
return convs_out, convs_up_out
def compare(self, image1, image2):
self.image_size = image1.shape
convs_up_out1 = self.sess.run(self.convs_up_, feed_dict={self.im: image1})
convs_up_out2 = self.sess.run(self.convs_up_, feed_dict={self.im: image2})
convs_up_out1r = [np.squeeze(x) for x in convs_up_out1]
convs_up_out2r = [np.squeeze(x) for x in convs_up_out2]
rmse = []
for i in range(0, self.k):
rmse.append(np.sqrt(np.mean((convs_up_out1[i] - convs_up_out2[i]) ** 2)))
return np.mean(rmse)
def calc_stft(self):
return tf.contrib.signal.stft(self.raw_audios, frame_length=2048, frame_step=512,
window_fn=functools.partial(tf.contrib.signal.hann_window, periodic=False))
def stack_fft(self, stft):
out = stft.transpose()
out = np.vstack((out.real, out.imag))
out = np.reshape(out, (out.shape[0], out.shape[1], 1))
return out
def output_pyramid_raw_audio(self, raw_audio):
stfts = self.sess.run(self.stft_, feed_dict={self.raw_audios: raw_audio})
stfts_reshape = np.asarray([self.stack_fft(x) for x in stfts])
convs = self.sess.run(self.convs_up_, feed_dict={self.im: stfts_reshape})
convs = [np.squeeze(x) for x in convs]
return convs
def output(self, reshaped_tensors):
convs = self.sess.run(self.convs_up_m, feed_dict={self.im: reshaped_tensors})
convs = [np.squeeze(x) for x in convs]
return convs
def build_model(self):
self.im = tf.placeholder('float32', [None, None, None, 1])
self.raw_audios = tf.placeholder('float32', [None, None])
self.convs_up_ = self.convs()
self.stft_ = self.calc_stft()
self.sess = tf.Session()
self.sess.run(tf.global_variables_initializer())