-
Notifications
You must be signed in to change notification settings - Fork 4
/
ops_compress.py
261 lines (211 loc) · 12.3 KB
/
ops_compress.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
import tensorflow as tf
import math
def nextitnet_residual_block_cross_layer(input_, dilation, layer_id,
residual_channels, kernel_size,
causal=True, train=True):
resblock_type = "decoder"
resblock_name = "nextitnet_residual_block{}_layer".format(resblock_type)
with tf.variable_scope(resblock_name, reuse=tf.AUTO_REUSE):
dilated_conv = conv1d(input_, residual_channels,
dilation, kernel_size,
causal=causal,
name="dilated_conv"
)
resblock_norm_name1 = "nextitnet_residual_block{}_layer_{}_{}_norm1".format(resblock_type, layer_id, dilation)
with tf.variable_scope(resblock_norm_name1, reuse=tf.AUTO_REUSE):
input_ln = layer_norm(dilated_conv, name="layer_norm1", trainable=train)
# input_ln=tf.contrib.layers.layer_norm(dilated_conv,reuse=not train, trainable=train) #performance is not good, paramter wrong?
relu1 = tf.nn.relu(input_ln)
with tf.variable_scope(resblock_name, reuse=tf.AUTO_REUSE):
dilated_conv = conv1d(relu1, residual_channels,
2 *dilation, kernel_size,
causal=causal,
name="dilated_conv"
)
resblock_norm_name2 = "nextitnet_residual_block{}_layer_{}_{}_norm2".format(resblock_type, layer_id, dilation)
with tf.variable_scope(resblock_norm_name2, reuse=tf.AUTO_REUSE):
input_ln = layer_norm(dilated_conv, name="layer_norm2", trainable=train)
#input_ln = tf.contrib.layers.layer_norm(dilated_conv, reuse=not train, trainable=train)
relu1 = tf.nn.relu(input_ln)
return input_ + relu1
# adjacent_layer share weight including layernorm
def nextitnet_residual_block_adjacent_layer(input_, dilation, layer_id,
residual_channels, kernel_size,
causal=True, train=True):
resblock_type = "decoder"
resblock_name1 = "nextitnet_residual_block{}_layer_{}_{}".format(resblock_type, layer_id, dilation)
with tf.variable_scope(resblock_name1, reuse=tf.AUTO_REUSE):
dilated_conv = conv1d(input_, residual_channels,
dilation, kernel_size,
causal=causal,
name="dilated_conv"
)
input_ln = layer_norm(dilated_conv, name="layer_norm1", trainable=train)
# input_ln=tf.contrib.layers.layer_norm(dilated_conv,reuse=not train, trainable=train) #performance is not good, paramter wrong?
relu1 = tf.nn.relu(input_ln)
dilated_conv = conv1d(relu1, residual_channels,
2 *dilation, kernel_size,
causal=causal,
name="dilated_conv"
)
input_ln = layer_norm(dilated_conv, name="layer_norm2", trainable=train)
#input_ln = tf.contrib.layers.layer_norm(dilated_conv, reuse=not train, trainable=train)
relu1 = tf.nn.relu(input_ln)
return input_ + relu1
# adjacent_block share weight not including layernorm
def nextitnet_residual_adjacent_block(input_, dilation, layer_id,
residual_channels, kernel_size,
causal=True, train=True):
resblock_type = "decoder"
resblock_name1 = "nextitnet_residual_block{}_layer_{}_0".format(resblock_type, layer_id/2)
with tf.variable_scope(resblock_name1, reuse=tf.AUTO_REUSE):
dilated_conv = conv1d(input_, residual_channels,
dilation, kernel_size,
causal=causal,
name="dilated_conv1"
)
resblock_norm_name1 = "nextitnet_residual_block{}_layer_{}_{}_norm1".format(resblock_type, layer_id, dilation)
with tf.variable_scope(resblock_norm_name1, reuse=tf.AUTO_REUSE):
input_ln = layer_norm(dilated_conv, name="layer_norm1", trainable=train)
# input_ln=tf.contrib.layers.layer_norm(dilated_conv,reuse=not train, trainable=train) #performance is not good, paramter wrong?
relu1 = tf.nn.relu(input_ln)
resblock_name2 = "nextitnet_residual_block{}_layer_{}_1".format(resblock_type, layer_id / 2)
with tf.variable_scope(resblock_name2, reuse=tf.AUTO_REUSE):
dilated_conv = conv1d(relu1, residual_channels,
2 *dilation, kernel_size,
causal=causal,
name="dilated_conv2"
)
resblock_norm_name2 = "nextitnet_residual_block{}_layer_{}_{}_norm2".format(resblock_type, layer_id, dilation)
with tf.variable_scope(resblock_norm_name2, reuse=tf.AUTO_REUSE):
input_ln = layer_norm(dilated_conv, name="layer_norm2", trainable=train)
#input_ln = tf.contrib.layers.layer_norm(dilated_conv, reuse=not train, trainable=train)
relu1 = tf.nn.relu(input_ln)
return input_ + relu1
# cross_block share weight not including layernorm == erery blocks
def nextitnet_residual_block_cross_block(input_, dilation, layer_id,
residual_channels, kernel_size,
causal=True, train=True):
resblock_type = "decoder"
resblock_name1 = "nextitnet_residual_block{}_layer_0".format(resblock_type)
with tf.variable_scope(resblock_name1, reuse=tf.AUTO_REUSE):
dilated_conv = conv1d(input_, residual_channels,
dilation, kernel_size,
causal=causal,
name="dilated_conv1"
)
resblock_norm_name1 = "nextitnet_residual_block{}_layer_{}_{}_norm1".format(resblock_type, layer_id, dilation)
with tf.variable_scope(resblock_norm_name1):
input_ln = layer_norm(dilated_conv, name="layer_norm1", trainable=train)
# input_ln=tf.contrib.layers.layer_norm(dilated_conv,reuse=not train, trainable=train) #performance is not good, paramter wrong?
relu1 = tf.nn.relu(input_ln)
resblock_name2 = "nextitnet_residual_block{}_layer_1".format(resblock_type)
with tf.variable_scope(resblock_name2, reuse=tf.AUTO_REUSE):
dilated_conv = conv1d(relu1, residual_channels,
2 *dilation, kernel_size,
causal=causal,
name="dilated_conv2"
)
resblock_norm_name2 = "nextitnet_residual_block{}_layer_{}_{}_norm2".format(resblock_type, layer_id, dilation)
with tf.variable_scope(resblock_norm_name2):
input_ln = layer_norm(dilated_conv, name="layer_norm2", trainable=train)
#input_ln = tf.contrib.layers.layer_norm(dilated_conv, reuse=not train, trainable=train)
relu1 = tf.nn.relu(input_ln)
return input_ + relu1
def nextitnet_residual_block(input_, dilation, layer_id,
residual_channels, kernel_size,
causal=True, train=True):
resblock_type = "decoder"
resblock_name = "nextitnet_residual_block{}_layer_{}_{}".format(resblock_type, layer_id, dilation)
with tf.variable_scope(resblock_name):
dilated_conv = conv1d(input_, residual_channels,
dilation, kernel_size,
causal=causal,
name="dilated_conv1"
)
input_ln = layer_norm(dilated_conv, name="layer_norm1", trainable=train)
# input_ln=tf.contrib.layers.layer_norm(dilated_conv,reuse=not train, trainable=train) #performance is not good, paramter wrong?
relu1 = tf.nn.relu(input_ln)
dilated_conv = conv1d(relu1, residual_channels,
2 *dilation, kernel_size,
causal=causal,
name="dilated_conv2"
)
input_ln = layer_norm(dilated_conv, name="layer_norm2", trainable=train)
#input_ln = tf.contrib.layers.layer_norm(dilated_conv, reuse=not train, trainable=train)
relu1 = tf.nn.relu(input_ln)
return input_ + relu1
#suggest using this one if your data has strong sequence, dilations: [1,2,4,1,2,4,]
#Kaiming He, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. 2016. Identity mappings in deep residual networks.
def nextitnet_residual_block_one(input_, dilation, layer_id,
residual_channels, kernel_size,
causal = True, train = True):
resblock_type = "decoder"
resblock_name = "nextitnet_residual_block_one_{}_layer_{}_{}".format(resblock_type, layer_id, dilation)
with tf.variable_scope(resblock_name):
input_ln = layer_norm(input_, name="layer_norm1", trainable = train)
relu1 = tf.nn.relu(input_ln)
conv1 = conv1d(relu1, int(0.5*residual_channels), name = "conv1d_1")
conv1 = layer_norm(conv1, name="layer_norm2", trainable = train)
relu2 = tf.nn.relu(conv1)
dilated_conv = conv1d(relu2, int(0.5*residual_channels),
dilation, kernel_size,
causal = causal,
name = "dilated_conv"
)
dilated_conv = layer_norm(dilated_conv, name="layer_norm3", trainable = train)
relu3 = tf.nn.relu(dilated_conv)
conv2 = conv1d(relu3, residual_channels, name = 'conv1d_2')
return input_ + conv2
#seems not good
#Conditional Image Generation with PixelCNN Decoders, wrong implementation?? let me know if you find the problem
def nextitnet_residual_block_gatedCNN(input_, dilation, layer_id,
residual_channels, kernel_size,
causal=True, train=True):
resblock_type = "decoder"
resblock_name = "gatedCNN_{}_layer_{}_{}".format(resblock_type, layer_id, dilation)
with tf.variable_scope(resblock_name):
dilated_conv = conv1d(input_, residual_channels,
dilation, kernel_size,
causal=causal,
name="dilated_conv"
)
tanh=tf.nn.tanh(dilated_conv)
gate_conv = conv1d(input_, residual_channels,
dilation, kernel_size,
causal=causal,
name="gate_conv"
)
sigm = tf.nn.sigmoid(gate_conv)
multi=tf.multiply(tanh,sigm)
multi=conv1d(multi, residual_channels, name="conv1d_1")
return input_ + multi
def conv1d(input_, output_channels,
dilation=1, kernel_size=1, causal=False,
name="dilated_conv"):
with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
# print("input_.get_shape *********: ", input_.get_shape())
weight = tf.get_variable('weight', [1, kernel_size, input_.get_shape()[-1], output_channels], initializer=tf.truncated_normal_initializer(stddev=0.02, seed=1))
bias = tf.get_variable('bias', [output_channels],
initializer=tf.constant_initializer(0.0))
if causal:
padding = [[0, 0], [(kernel_size - 1) * dilation, 0], [0, 0]]
padded = tf.pad(input_, padding)
input_expanded = tf.expand_dims(padded, dim=1)
out = tf.nn.atrous_conv2d(input_expanded, weight, rate=dilation, padding='VALID') + bias
else:
input_expanded = tf.expand_dims(input_, dim=1)
# out = tf.nn.atrous_conv2d(input_expanded, w, rate = dilation, padding = 'SAME') + bias
out = tf.nn.conv2d(input_expanded, weight, strides=[1, 1, 1, 1], padding="SAME") + bias
return tf.squeeze(out, [1])
# tf.contrib.layers.layer_norm
def layer_norm(x, name, epsilon=1e-8, trainable=True):
with tf.variable_scope(name):
shape = x.get_shape()
beta = tf.get_variable('beta', [int(shape[-1])],
initializer=tf.constant_initializer(0), trainable=trainable)
gamma = tf.get_variable('gamma', [int(shape[-1])],
initializer=tf.constant_initializer(1), trainable=trainable)
mean, variance = tf.nn.moments(x, axes=[len(shape) - 1], keep_dims=True)
x = (x - mean) / tf.sqrt(variance + epsilon)
return gamma * x + beta