-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.py
executable file
·379 lines (263 loc) · 13.3 KB
/
module.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Activation, Input, UpSampling1D ,Embedding, Concatenate
from tensorflow.keras.layers import Dense, Reshape, LeakyReLU, Flatten, Conv1D, add, Cropping1D
from tensorflow.keras import backend as K
import numpy as np
import math
# ==============================================================================
# = networks =
# ==============================================================================
#leakyRELU <<<<<<
# to do, fix error with inconsequential noise when using scale 4 instead of 2
class Networks(object):
def __init__(self, latent_size=64,
start_size=16, kernel_len=16,
n_filters=1024, n_classes=16, n_chan=1, num_res=10, scale_base=2,
embedding_dim=50, mapping_size=3):
#param
self._latent_size = latent_size
self._label_in_dim = embedding_dim
self._mapping_size = mapping_size
# self._mapping_filters = mapping_filters
#wav/params
self._start_size = start_size
self._n_filters = n_filters
self._n_classes = n_classes
self._n_chan = n_chan
self._kernel_len = kernel_len
self._num_res = num_res
self._scale_base = scale_base
self._end_size = self._start_size*np.power(self._scale_base, self._num_res)
#network building
self._G = self.make_conditional_generator(self._latent_size)
self._D = self.make_conditional_discriminator()
self._L = self.latent_encoder()
# self.S = None
self._style_net = self.style_network()
self._G_synth = self.generator_synth()
# self.G_synth.set_weights(self._G.get_weights(),by_name=True)
# self._S = self.style_network(self._latent_size)
def load_g_weights(self):
self._G.save_weights('generator_weights.model')
self.G_synth.load_weights('generator_weights.model', by_name=True, skip_mismatch=True)
def Adain(self, x):
mean = K.mean(x[0], axis=[1], keepdims=True)
stddev = K.std(x[0], axis=[1], keepdims=True) + 1e-7
norm = (x[0] - mean) / stddev
pool_shape = [-1, 1, norm.shape[-1]]
gamma = K.reshape(x[1], pool_shape)
beta = K.reshape(x[2], pool_shape)
return norm * gamma + beta
def sample_norm(self, x):
"""
Fix multiply or divide?
"""
return x / tf.reduce_mean(tf.square(x), axis=1, keepdims=True) + 1e-7
def g_block(self, x, filters, kernel_len, stride, latent_vector, inc_noise, name, act='relu'):
gamma = Dense(filters, name='g_gamma1_block_'+name)(latent_vector)
beta = Dense(filters, name='g_beta1_block_'+name)(latent_vector)
noise = Conv1D(filters, kernel_size=1, padding='same', name='g_inc_noise_conv1_block_'+name)(inc_noise)
x = UpSampling1D(stride, name = 'g_up_samp_block_'+name)(x)
x = Conv1D(filters, kernel_len, padding = 'same', name='g_conv1_block_'+name)(x)
x = add([x, noise])
x = self.Adain([x, gamma, beta])
x = Activation(act)(x)
gamma = Dense(filters, name='g_gamma2_block_'+name)(latent_vector)
beta = Dense(filters, name='g_beta2_block_'+name)(latent_vector)
noise = Conv1D(filters, kernel_size=1, padding='same', name='g_inc_noise_conv2_block_'+name)(inc_noise)
x = Conv1D(filters, kernel_len, padding = 'same', name='g_conv2_block_'+name)(x)
x = add([x, noise])
x = self.Adain([x, gamma, beta])
x = Activation(act)(x)
# x = self.sample_norm(x)
return x
# def g_block(self, x, filters, kernel_len, stride, latent_vector, inc_noise, act='relu'):
# gamma = Dense(filters)(latent_vector)
# beta = Dense(filters)(latent_vector)
# noise = Conv1D(filters, kernel_size=1, padding='same')(inc_noise)
# x = UpSampling1D(stride)(x)
# x = Conv1D(filters, kernel_len, padding = 'same')(x)
# x = self.Adain([x, gamma, beta])
# x = Activation(act)(x)
# # x = self.sample_norm(x)
# return x
def d_block(self, x, filters, kernel_len, stride, act='relu'):
# res = Conv1D(filters, 1)(x)
# # convolve and add residual skip
# x = Conv1D(filters, kernel_len, 1, padding = 'same')(x)
# x = Activation(act)(x)
# x = add([res, x])
# downsample
x = Conv1D(filters, kernel_len, stride, padding = 'same')(x)
x = Activation(act)(x)
x = self.apply_phaseshuffle(x)
return x
def apply_phaseshuffle(self, x, rad=2, pad_type='reflect'):
b, x_len, nch = x.get_shape().as_list()
phase = tf.random.uniform([], minval=-rad, maxval=rad + 1, dtype=tf.int32)
pad_l = tf.maximum(phase, 0)
pad_r = tf.maximum(-phase, 0)
phase_start = pad_r
x = tf.pad(x, [[0, 0], [pad_l, pad_r], [0, 0]], mode=pad_type)
x = x[:, phase_start:phase_start+x_len]
x.set_shape([b, x_len, nch])
return x
def num_filters(self, block_id, fmap_decay=1.0, fmap_max=4096):
fmap_base = self._n_filters
return int(min(fmap_base / math.pow(2.0, block_id * fmap_decay), fmap_max))
def style_network(self, name='style'):
inp_style = Input(shape = [self._latent_size], name= 'style_in_mapping')
in_label = Input(shape=(), name='s_label_input')
label_in = Embedding(self._n_classes, self._label_in_dim)(in_label)
label_in = Dense(self._latent_size, name='g_label_dense')(label_in)
conc_latent = Concatenate()([inp_style, label_in])
norm_style = self.sample_norm(conc_latent)
style = Dense(self._latent_size,
kernel_initializer = 'he_normal',
bias_initializer = 'zeros',
name='g_style_dense1_'+name)(norm_style)
style = LeakyReLU(0.01)(style)
for i in range(self._mapping_size-2):
sty = Dense(self._latent_size,
kernel_initializer = 'he_normal',
bias_initializer = 'zeros',
name = 'g_style_dense'+str(i+2)+'_'+name)(style)
sty = LeakyReLU(0.01)(sty)
sty = Dense(self._latent_size,
kernel_initializer = 'he_normal',
bias_initializer = 'zeros',
name = 'g_style_final_dense_'+name)(sty)
sty = LeakyReLU(0.01)(sty)
style_net = Model(inputs = [inp_style, in_label], outputs = sty)
return style_net
def make_conditional_generator(self, latent_size):
# # style mapping
# self.S = Sequential()
# self.S.add(Dense(self._latent_size, input_shape=[self._latent_size]))
# self.S.add(LeakyReLU(0.2))
# for i in range(self._mapping_size-2):
# self.S.add(Dense(self._latent_size))
# self.S.add(LeakyReLU(0.2))
# self.S.add(Dense(self._latent_size))
# self.S.add(LeakyReLU(0.2))
# # label input
# in_label = Input(shape=(), name='g_label_input')
# label_in = Embedding(self._n_classes, self._label_in_dim)(in_label)
# n_nodes = self._start_size
# label_in = Dense(n_nodes, name='g_label_dense')(label_in)
# label_in = Reshape((n_nodes, self._n_chan))(label_in)
# inconsequential noise
inc_noise = Input([self._end_size, self._n_chan], name='g_inc_n_input')
noise = [Activation('linear')(inc_noise)]
noise_size = self._end_size
for i in range(1, self._num_res):
noise_size=int(noise_size/self._scale_base)
noise.append(Cropping1D(cropping=int(noise_size/self._scale_base))(noise[-1]))
# constant input
const_inp = Input((1,), name='g_const_input')
x = Dense(self._start_size*self.num_filters(0), activation = 'relu', name = 'g_const_dense')(const_inp)
x = Reshape([self._start_size,self.num_filters(0)])(x)
x = Activation('relu')(x)
# x = Concatenate()([x, label_in])
# layer styles
layer_style = []
for i in range(1, self._num_res):
layer_style.append(Input(shape = [latent_size], name = 'g_style_layer'+str(i)))
# styles=[]
# for i in range(len(layer_style)):
# styles.append(self.style_network(layer_style[i], name=str(i)))
# generator blocks
for i in range(1, self._num_res):
x = self.g_block(x,
self.num_filters(i),
self._kernel_len,
stride=self._scale_base,
latent_vector=layer_style[i-1],
inc_noise=noise[-i],
name=str(i))
x = UpSampling1D(self._scale_base, name='g_final_upsamp')(x)
x = Conv1D(self._n_chan, self._kernel_len, padding = 'same', name='g_final_conv')(x)
image_output = Activation('tanh')(x)
generator = Model(inputs = layer_style + [const_inp, inc_noise], outputs = image_output)
return generator
def generator_synth(self):
in_label = Input(shape=())
layer_style=[]
styles=[]
for i in range(1, self._num_res):
layer_style.append(Input(shape = [self._latent_size]))
styles.append(self._style_net([layer_style[i-1], in_label]))
inc_noise= Input([self._end_size, self._n_chan])
const_inp = Input((1,))
generation = self._G(styles + [const_inp, inc_noise])
gen_synth = Model(inputs = layer_style + [in_label, const_inp, inc_noise], outputs = generation)
return gen_synth
def make_conditional_discriminator(self):
in_label = Input(shape=())
# embedding for label
label_in = Embedding(self._n_classes, self._label_in_dim)(in_label)
# scale up to dimensions
label_in = Dense(self._end_size)(label_in)
# reshape to additional channel
label_in = Reshape((self._end_size, self._n_chan))(label_in)
audio_input = Input([self._end_size, self._n_chan])
# concat label as a channel
x = Concatenate()([audio_input, label_in])
for i in range(self._num_res-1, 0, -1):
x = self.d_block(x,
self.num_filters(i),
self._kernel_len,
self._scale_base,
act=LeakyReLU(alpha=0.2))
x = Conv1D(self.num_filters(0), self._kernel_len, self._scale_base, padding = 'same')(x)
x = Activation(LeakyReLU(alpha=0.2))(x)
x = Flatten()(x)
class_output = Dense(1)(x)
discriminator = Model(inputs = [audio_input, in_label], outputs = class_output)
return discriminator
def latent_encoder(self):
in_label = Input(shape=())
# embedding for label
label_in = Embedding(self._n_classes, self._label_in_dim)(in_label)
# scale up to dimensions
label_in = Dense(self._end_size)(label_in)
# reshape to additional channel
label_in = Reshape((self._end_size, self._n_chan))(label_in)
audio_input = Input([self._end_size, self._n_chan])
# concat label as a channel
x = Concatenate()([audio_input, label_in])
for i in range(self._num_res-1, 0, -1):
x = self.d_block(x,
self.num_filters(i),
self._kernel_len,
self._scale_base,
act=LeakyReLU(alpha=0.2))
x = Conv1D(self.num_filters(0), self._kernel_len, self._scale_base, padding = 'same')(x)
x = Activation(LeakyReLU(alpha=0.2))(x)
x = Flatten()(x)
class_output = Dense(self._latent_size)(x)
latent_encoder = Model(inputs = [audio_input, in_label], outputs = class_output)
return latent_encoder
def model_summary(self):
self._G.summary()
self._D.summary()
# nets=Networks()
# nets.model_summary()
# import math
# def num_filters(block_id, fmap_base=4096, fmap_decay=1.0, fmap_max=4096):
# """Computes number of filters of block `block_id`."""
# return int(min(fmap_base / math.pow(2.0, block_id * fmap_decay), fmap_max))
# # def num_filters(block_id, fmap_base=256, fmap_decay=1.0):
# # """Computes number of filters of block `block_id`."""
# # return int((fmap_base / math.pow(2.0, block_id * fmap_decay)))#
# # dim_mul = 1
# # label input
# in_label = Input(shape=())
# # embedding for categorical input
# label_in = Embedding(3, 50)(in_label)
# # scale up to image dimensions with linear activation
# label_in = Dense(65536)(label_in)
# # reshape to additional channel
# label_in = Reshape((self._end_size, 1))(label_in)