-
Notifications
You must be signed in to change notification settings - Fork 44
/
modules_tf.py
executable file
·169 lines (98 loc) · 6.48 KB
/
modules_tf.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
from tensorflow.python import debug as tf_debug
from tensorflow.contrib.rnn import GRUCell
from tensorflow.contrib import rnn
import config
tf.logging.set_verbosity(tf.logging.INFO)
def selu(x):
alpha = 1.6732632423543772848170429916717
scale = 1.0507009873554804934193349852946
return scale*tf.where(x>=0.0, x, alpha*tf.nn.elu(x))
def encoder_conv_block_gan(inputs, layer_num, is_train, num_filters = config.filters):
output = tf.layers.batch_normalization(tf.nn.relu(tf.layers.conv2d(inputs, num_filters * 2**int(layer_num/2), (config.filter_len,1)
, strides=(2,1), padding = 'same', name = "G_"+str(layer_num), kernel_initializer=tf.random_normal_initializer(stddev=0.02))), training = is_train, name = "GBN_"+str(layer_num))
return output
def decoder_conv_block_gan(inputs, layer, layer_num, is_train, num_filters = config.filters):
deconv = tf.image.resize_images(inputs, size=(int(config.max_phr_len/2**(config.encoder_layers - 1 - layer_num)),1), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
# embedding = tf.tile(embedding,[1,int(config.max_phr_len/2**(config.encoder_layers - 1 - layer_num)),1,1])
deconv = tf.layers.batch_normalization(tf.nn.relu(tf.layers.conv2d(deconv, layer.shape[-1]
, (config.filter_len,1), strides=(1,1), padding = 'same', name = "D_"+str(layer_num), kernel_initializer=tf.random_normal_initializer(stddev=0.02))), training = is_train, name = "DBN_"+str(layer_num))
# embedding =tf.nn.relu(tf.layers.conv2d(embedding, layer.shape[-1]
# , (config.filter_len,1), strides=(1,1), padding = 'same', name = "DEnc_"+str(layer_num)))
deconv = tf.concat([deconv, layer], axis = -1)
return deconv
def encoder_decoder_archi_gan(inputs, is_train):
"""
Input is assumed to be a 4-D Tensor, with [batch_size, phrase_len, 1, features]
"""
encoder_layers = []
encoded = inputs
encoder_layers.append(encoded)
for i in range(config.encoder_layers):
encoded = encoder_conv_block_gan(encoded, i, is_train)
encoder_layers.append(encoded)
encoder_layers.reverse()
decoded = encoder_layers[0]
for i in range(config.encoder_layers):
decoded = decoder_conv_block_gan(decoded, encoder_layers[i+1], i, is_train)
return decoded
def full_network(f0, phos, singer_label, is_train):
f0 = tf.layers.batch_normalization(tf.layers.dense(f0, config.filters
, name = "F0_in", kernel_initializer=tf.random_normal_initializer(stddev=0.02)), training = is_train, name = 'F0_in_BN')
phos = tf.layers.batch_normalization(tf.layers.dense(phos, config.filters
, name = "Pho_in", kernel_initializer=tf.random_normal_initializer(stddev=0.02)), training = is_train, name = 'Pho_in_BN')
singer_label = tf.layers.batch_normalization(tf.layers.dense(singer_label, config.filters
, name = "Singer_in", kernel_initializer=tf.random_normal_initializer(stddev=0.02)), training = is_train, name = 'Singer_in_BN')
singer_label = tf.tile(tf.reshape(singer_label,[config.batch_size,1,-1]),[1,config.max_phr_len,1])
inputs = tf.concat([f0, phos,singer_label], axis = -1)
inputs = tf.reshape(inputs, [config.batch_size, config.max_phr_len , 1, -1])
inputs = tf.layers.batch_normalization(tf.layers.dense(inputs, config.filters
, name = "S_in", kernel_initializer=tf.random_normal_initializer(stddev=0.02)), training = is_train,name = 'S_in_BN')
output = encoder_decoder_archi_gan(inputs, is_train)
output = tf.tanh(tf.layers.batch_normalization(tf.layers.dense(output, config.output_features, name = "Fu_F", kernel_initializer=tf.random_normal_initializer(stddev=0.02)), training = is_train, name = "bn_fu_out"))
return tf.squeeze(output)
def discriminator(inputs, phos, f0, singer_label, is_train):
f0 = tf.layers.batch_normalization(tf.layers.dense(f0, config.filters
, name = "F0_in", kernel_initializer=tf.random_normal_initializer(stddev=0.02)), training = is_train, name = 'F0_in_BN')
phos = tf.layers.batch_normalization(tf.layers.dense(phos, config.filters
, name = "Pho_in", kernel_initializer=tf.random_normal_initializer(stddev=0.02)), training = is_train, name = 'Pho_in_BN')
singer_label = tf.layers.batch_normalization(tf.layers.dense(singer_label, config.filters
, name = "Singer_in", kernel_initializer=tf.random_normal_initializer(stddev=0.02)), training = is_train, name = 'Singer_in_BN')
singer_label = tf.tile(tf.reshape(singer_label,[config.batch_size,1,-1]),[1,config.max_phr_len,1])
inputs = tf.concat([inputs, f0, phos,singer_label], axis = -1)
inputs = tf.reshape(inputs, [config.batch_size, config.max_phr_len , 1, -1])
inputs = tf.layers.batch_normalization(tf.layers.dense(inputs, config.filters *2
, name = "S_in", kernel_initializer=tf.random_normal_initializer(stddev=0.02)), training = is_train, name = "bn_fu_1")
encoded = inputs
for i in range(config.encoder_layers):
encoded = encoder_conv_block_gan(encoded, i, is_train)
encoded = tf.squeeze(encoded)
output = tf.layers.batch_normalization(tf.layers.dense(encoded, 1, name = "Fu_F", kernel_initializer=tf.random_normal_initializer(stddev=0.02)), training = is_train, name = "bn_fu_out")
return tf.squeeze(output)
def main():
vec = tf.placeholder("float", [config.batch_size, config.max_phr_len, config.input_features])
tec = np.random.rand(config.batch_size, config.max_phr_len,config.input_features) # batch_size, time_steps, features
is_train = tf.placeholder(tf.bool, name="is_train")
# seqlen = tf.placeholder("float", [config.batch_size, 256])
# with tf.variable_scope('singer_Model') as scope:
# singer_emb, outs_sing = singer_network(vec, is_train)
# with tf.variable_scope('f0_Model') as scope:
# outs_f0 = f0_network(vec, is_train)
# with tf.variable_scope('phone_Model') as scope:
# outs_pho = phone_network(vec, is_train)
with tf.variable_scope('full_Model') as scope:
out_put = discriminator(vec,is_train)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
op= sess.run(out_put, feed_dict={vec: tec, is_train: True})
# writer = tf.summary.FileWriter('.')
# writer.add_graph(tf.get_default_graph())
# writer.add_summary(summary, global_step=1)
import pdb;pdb.set_trace()
if __name__ == '__main__':
main()