-
Notifications
You must be signed in to change notification settings - Fork 5
/
wavelet.py
164 lines (158 loc) · 5.81 KB
/
wavelet.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
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 9 15:50:03 2019
@author: wmy
"""
import numpy as np
import matplotlib.pyplot as plt
import pywt
import tensorflow as tf
import pylab
pylab.rcParams['figure.figsize'] = (10.0, 10.0)
def dwt2d(x, wave='haar'):
# shape x: (b, h, w, c)
nc = int(x.shape.dims[3])
# 小波波形
w = pywt.Wavelet(wave)
# 水平低频 垂直低频
ll = np.outer(w.dec_lo, w.dec_lo)
# 水平低频 垂直高频
lh = np.outer(w.dec_hi, w.dec_lo)
# 水平高频 垂直低频
hl = np.outer(w.dec_lo, w.dec_hi)
# 水平高频 垂直高频
hh = np.outer(w.dec_hi, w.dec_hi)
# 卷积核
core = np.zeros((np.shape(ll)[0], np.shape(ll)[1], 1, 4))
core[:, :, 0, 0] = ll[::-1, ::-1]
core[:, :, 0, 1] = lh[::-1, ::-1]
core[:, :, 0, 2] = hl[::-1, ::-1]
core[:, :, 0, 3] = hh[::-1, ::-1]
core = core.astype(np.float32)
kernel = np.array([core], dtype=np.float32)
kernel = tf.convert_to_tensor(kernel)
p = 2 * (len(w.dec_lo) // 2 - 1)
with tf.variable_scope('dwt2d'):
# padding odd length
x = tf.pad(x, tf.constant([[0, 0], [p, p+1], [p, p+1], [0, 0]]))
xh = tf.shape(x)[1] - tf.shape(x)[1]%2
xw = tf.shape(x)[2] - tf.shape(x)[2]%2
x = x[:, 0:xh, 0:xw, :]
# convert to 3d data
x3d = tf.expand_dims(x, 1)
# 切开通道
x3d = tf.split(x3d, int(x3d.shape.dims[4]), 4)
# 贴到维度一
x3d = tf.concat([a for a in x3d], 1)
# 三维卷积
y3d = tf.nn.conv3d(x3d, kernel, padding='VALID', strides=[1, 1, 2, 2, 1])
# 切开维度一
y = tf.split(y3d, int(y3d.shape.dims[1]), 1)
# 贴到通道维
y = tf.concat([a for a in y], 4)
y = tf.reshape(y, (tf.shape(y)[0], tf.shape(y)[2], tf.shape(y)[3], 4*nc))
# 拼贴通道
channels = tf.split(y, nc, 3)
outputs = []
for channel in channels:
(cA, cH, cV, cD) = tf.split(channel, 4, 3)
AH = tf.concat([cA, cH], axis=2)
VD = tf.concat([cV, cD], axis=2)
outputs.append(tf.concat([AH, VD], axis=1))
pass
outputs = tf.concat(outputs, axis=-1)
pass
return outputs
def wavedec2d(x, level=1, wave='haar'):
if level == 0:
return x
y = dwt2d(x, wave=wave)
hcA = tf.floordiv(tf.shape(y)[1], 2)
wcA = tf.floordiv(tf.shape(y)[2], 2)
cA = y[:, 0:hcA, 0:wcA, :]
cA = wavedec2d(cA, level=level-1, wave=wave)
cA = cA[:, 0:hcA, 0:wcA, :]
hcA = tf.shape(cA)[1]
wcA = tf.shape(cA)[2]
cH = y[:, 0:hcA, wcA:, :]
cV = y[:, hcA:, 0:wcA, :]
cD = y[:, hcA:, wcA:, :]
AH = tf.concat([cA, cH], axis=2)
VD = tf.concat([cV, cD], axis=2)
outputs = tf.concat([AH, VD], axis=1)
return outputs
def idwt2d(x, wave='haar'):
# shape x: (b, h, w, c)
nc = int(x.shape.dims[3])
# 小波波形
w = pywt.Wavelet(wave)
# 水平低频 垂直低频
ll = np.outer(w.dec_lo, w.dec_lo)
# 水平低频 垂直高频
lh = np.outer(w.dec_hi, w.dec_lo)
# 水平高频 垂直低频
hl = np.outer(w.dec_lo, w.dec_hi)
# 水平高频 垂直高频
hh = np.outer(w.dec_hi, w.dec_hi)
# 卷积核
core = np.zeros((np.shape(ll)[0], np.shape(ll)[1], 1, 4))
core[:, :, 0, 0] = ll[::-1, ::-1]
core[:, :, 0, 1] = lh[::-1, ::-1]
core[:, :, 0, 2] = hl[::-1, ::-1]
core[:, :, 0, 3] = hh[::-1, ::-1]
core = core.astype(np.float32)
kernel = np.array([core], dtype=np.float32)
kernel = tf.convert_to_tensor(kernel)
s = 2 * (len(w.dec_lo) // 2 - 1)
# 反变换
with tf.variable_scope('idwt2d'):
hcA = tf.floordiv(tf.shape(x)[1], 2)
wcA = tf.floordiv(tf.shape(x)[2], 2)
y = []
for c in range(nc):
channel = x[:, :, :, c]
channel = tf.expand_dims(channel, -1)
cA = channel[:, 0:hcA, 0:wcA, :]
cH = channel[:, 0:hcA, wcA:, :]
cV = channel[:, hcA:, 0:wcA, :]
cD = channel[:, hcA:, wcA:, :]
temp = tf.concat([cA, cH, cV, cD], axis=-1)
y.append(temp)
pass
# nc * 4
y = tf.concat(y, axis=-1)
y3d = tf.expand_dims(y, 1)
y3d = tf.split(y3d, nc, 4)
y3d = tf.concat([a for a in y3d], 1)
output_shape = [tf.shape(y)[0], tf.shape(y3d)[1], \
2*(tf.shape(y)[1]-1)+np.shape(ll)[0], \
2*(tf.shape(y)[2]-1)+np.shape(ll)[1], 1]
x3d = tf.nn.conv3d_transpose(y3d, kernel, output_shape=output_shape, padding='VALID', strides=[1, 1, 2, 2, 1])
outputs = tf.split(x3d, nc, 1)
outputs = tf.concat([x for x in outputs], 4)
outputs = tf.reshape(outputs, (tf.shape(outputs)[0], tf.shape(outputs)[2], tf.shape(outputs)[3], nc))
outputs = outputs[:, s:2*(tf.shape(y)[1]-1)+np.shape(ll)[0]-s, \
s:2*(tf.shape(y)[2]-1)+np.shape(ll)[1]-s, :]
pass
return outputs
tf.reset_default_graph()
inputs = tf.placeholder(tf.float32, [None, None, None, 3], name='inputs')
image = plt.imread('test.jpg')
plt.imshow(image)
plt.show()
x = np.array([image, image[:, ::-1, :]])
dec = wavedec2d(inputs, level=5, wave='sym4')
dwt = dwt2d(inputs, wave='sym4')
idwt = idwt2d(dwt, wave='sym4')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
result = sess.run(dec, feed_dict={inputs:x})
plt.imshow(np.array(result[0], dtype=np.uint8))
plt.show()
trans = sess.run(dwt, feed_dict={inputs:x})
plt.imshow(np.array(trans[0], dtype=np.uint8))
plt.show()
recons = sess.run(idwt, feed_dict={inputs:x})
plt.imshow(np.array(recons[0], dtype=np.uint8))
plt.show()
pass