-
Notifications
You must be signed in to change notification settings - Fork 2
/
wavegan.py
145 lines (125 loc) · 5.43 KB
/
wavegan.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
import tensorflow as tf
import pickle
def add_Conv1DTranpose(model, filters, kernel_size, strides, kernel_initializer):
"""
Conv1D tranpose adapted from
https://stackoverflow.com/a/45788699/13185722
and
https://github.com/chrisdonahue/wavegan/blob/v1/wavegan.py#L13
"""
#model.add(tf.keras.layers.Lambda(lambda x: tf.expand_dims(x, axis=2)))
model.add(tf.keras.layers.Lambda(lambda x: tf.expand_dims(x, axis=1)))
model.add(tf.keras.layers.Conv2DTranspose(
filters=filters,
kernel_size=kernel_size,
strides=strides,
padding="same",
kernel_initializer=kernel_initializer
))
model.add(tf.keras.layers.Lambda(lambda x: x[:, 0]))
#model.add(tf.keras.layers.Lambda(lambda x: tf.keras.backend.squeeze(x, axis=2)))
def wavegan_generator(d, c):
strides = (1, 4)
k_size = (1, 25)
initializer = None#tf.keras.initializers.RandomNormal(mean=0, stddev=0.02)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Input(100))
model.add(tf.keras.layers.Dense(units=256 * d, kernel_initializer=initializer))
model.add(tf.keras.layers.Reshape((16, 16 * d)))
model.add(tf.keras.layers.ReLU())
add_Conv1DTranpose(model, filters=8 * d, kernel_size=k_size, strides=strides, kernel_initializer=initializer)
model.add(tf.keras.layers.ReLU())
add_Conv1DTranpose(model, filters=4 * d, kernel_size=k_size, strides=strides, kernel_initializer=initializer)
model.add(tf.keras.layers.ReLU())
add_Conv1DTranpose(model, filters=2 * d, kernel_size=k_size, strides=strides, kernel_initializer=initializer)
model.add(tf.keras.layers.ReLU())
add_Conv1DTranpose(model, filters=d, kernel_size=k_size, strides=strides, kernel_initializer=initializer)
model.add(tf.keras.layers.ReLU())
add_Conv1DTranpose(model, filters=c, kernel_size=k_size, strides=strides, kernel_initializer=initializer)
model.add(tf.keras.layers.Activation(tf.nn.tanh, name="tanh"))
model.build()
return model
def _phaseshuffle(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 wavegan_discriminator(d, c):
n = 2
alpha = 0.2
stride = 4
k_size = 25
initializer = None#tf.keras.initializers.RandomNormal(mean=0, stddev=0.02)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Input((16384, c))) # Fattar inte varför man inte behöver None här...
model.add(tf.keras.layers.Conv1D(
filters=d,
kernel_size=k_size,
strides=stride,
padding="same",
kernel_initializer=initializer
))
model.add(tf.keras.layers.LeakyReLU(alpha=alpha))
model.add(tf.keras.layers.Lambda(lambda x: _phaseshuffle(x)))
model.add(tf.keras.layers.Conv1D(
filters=2 * d,
kernel_size=k_size,
strides=stride,
padding="same",
kernel_initializer=initializer
))
model.add(tf.keras.layers.LeakyReLU(alpha=alpha))
model.add(tf.keras.layers.Lambda(lambda x: _phaseshuffle(x)))
model.add(tf.keras.layers.Conv1D(
filters=4 * d,
kernel_size=k_size,
strides=stride,
padding="same",
kernel_initializer=initializer
))
model.add(tf.keras.layers.LeakyReLU(alpha=alpha))
model.add(tf.keras.layers.Lambda(lambda x: _phaseshuffle(x)))
model.add(tf.keras.layers.Conv1D(
filters=8 * d,
kernel_size=k_size,
strides=stride,
padding="same",
kernel_initializer=initializer
))
model.add(tf.keras.layers.LeakyReLU(alpha=alpha))
model.add(tf.keras.layers.Lambda(lambda x: _phaseshuffle(x)))
model.add(tf.keras.layers.Conv1D(
filters=16 * d,
kernel_size=k_size,
strides=stride,
padding="same",
kernel_initializer=initializer
))
model.add(tf.keras.layers.LeakyReLU(alpha=alpha))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(units=1, kernel_initializer=initializer))
model.build()
return model
def save_model(generator, discriminator, gen_opt, disc_opt, hyperparams):
generator.save_weights(hyperparams['weights_folder'] + "generator/")
discriminator.save_weights(hyperparams['weights_folder'] + "discriminator/")
config = gen_opt.get_config()
with open(hyperparams['weights_folder'] + "optimizer/gen_opt.pkl", "wb") as f:
pickle.dump(config, f)
config = disc_opt.get_config()
with open(hyperparams['weights_folder'] + "optimizer/disc_opt.pkl", "wb") as f:
pickle.dump(config, f)
def load_model(generator, discriminator, gen_opt, disc_opt, hyperparams):
generator.load_weights(hyperparams['weights_folder'] + "generator/")
discriminator.load_weights(hyperparams['weights_folder'] + "discriminator/")
with open(hyperparams['weights_folder'] + "optimizer/gen_opt.pkl", "rb") as f:
config = pickle.load(f)
gen_opt.from_config(config)
with open(hyperparams['weights_folder'] + "optimizer/disc_opt.pkl", "rb") as f:
config = pickle.load(f)
disc_opt.from_config(config)