Debian Codegraph @

3rdparty/renamenoise/training/rnn_train.py

raw
1 #!/usr/bin/python
2
3 #
4 # Copyright (c) 2017, Jean-Marc Valin
5 #
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # - Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 #
15 # - Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 from __future__ import print_function
32
33 import keras
34 from keras.models import Sequential
35 from keras.models import Model
36 from keras.layers import Input
37 from keras.layers import Dense
38 from keras.layers import LSTM
39 from keras.layers import GRU
40 from keras.layers import SimpleRNN
41 from keras.layers import Dropout
42 from keras.layers import concatenate
43 from keras import losses
44 from keras import regularizers
45 from keras.constraints import min_max_norm
46 import h5py
47
48 from keras.constraints import Constraint
49 from keras import backend as K
50 import numpy as np
51
52 #import tensorflow as tf
53 #from keras.backend.tensorflow_backend import set_session
54 #config = tf.ConfigProto()
55 #config.gpu_options.per_process_gpu_memory_fraction = 0.42
56 #set_session(tf.Session(config=config))
57
58
59 def my_crossentropy(y_true, y_pred):
60 return K.mean(2*K.abs(y_true-0.5) * K.binary_crossentropy(y_pred, y_true), axis=-1)
61
62 def mymask(y_true):
63 return K.minimum(y_true+1., 1.)
64
65 def msse(y_true, y_pred):
66 return K.mean(mymask(y_true) * K.square(K.sqrt(y_pred) - K.sqrt(y_true)), axis=-1)
67
68 def mycost(y_true, y_pred):
69 return K.mean(mymask(y_true) * (10*K.square(K.square(K.sqrt(y_pred) - K.sqrt(y_true))) + K.square(K.sqrt(y_pred) - K.sqrt(y_true)) + 0.01*K.binary_crossentropy(y_pred, y_true)), axis=-1)
70
71 def my_accuracy(y_true, y_pred):
72 return K.mean(2*K.abs(y_true-0.5) * K.equal(y_true, K.round(y_pred)), axis=-1)
73
74 class WeightClip(Constraint):
75 '''Clips the weights incident to each hidden unit to be inside a range
76 '''
77 def __init__(self, c=2):
78 self.c = c
79
80 def __call__(self, p):
81 return K.clip(p, -self.c, self.c)
82
83 def get_config(self):
84 return {'name': self.__class__.__name__,
85 'c': self.c}
86
87 reg = 0.000001
88 constraint = WeightClip(0.499)
89
90 print('Build model...')
91 main_input = Input(shape=(None, 42), name='main_input')
92 tmp = Dense(24, activation='tanh', name='input_dense', kernel_constraint=constraint, bias_constraint=constraint)(main_input)
93 vad_gru = GRU(24, activation='tanh', recurrent_activation='sigmoid', return_sequences=True, name='vad_gru', kernel_regularizer=regularizers.l2(reg), recurrent_regularizer=regularizers.l2(reg), kernel_constraint=constraint, recurrent_constraint=constraint, bias_constraint=constraint)(tmp)
94 vad_output = Dense(1, activation='sigmoid', name='vad_output', kernel_constraint=constraint, bias_constraint=constraint)(vad_gru)
95 noise_input = keras.layers.concatenate([tmp, vad_gru, main_input])
96 noise_gru = GRU(48, activation='relu', recurrent_activation='sigmoid', return_sequences=True, name='noise_gru', kernel_regularizer=regularizers.l2(reg), recurrent_regularizer=regularizers.l2(reg), kernel_constraint=constraint, recurrent_constraint=constraint, bias_constraint=constraint)(noise_input)
97 denoise_input = keras.layers.concatenate([vad_gru, noise_gru, main_input])
98
99 denoise_gru = GRU(96, activation='tanh', recurrent_activation='sigmoid', return_sequences=True, name='denoise_gru', kernel_regularizer=regularizers.l2(reg), recurrent_regularizer=regularizers.l2(reg), kernel_constraint=constraint, recurrent_constraint=constraint, bias_constraint=constraint)(denoise_input)
100
101 denoise_output = Dense(22, activation='sigmoid', name='denoise_output', kernel_constraint=constraint, bias_constraint=constraint)(denoise_gru)
102
103 model = Model(inputs=main_input, outputs=[denoise_output, vad_output])
104
105 model.compile(loss=[mycost, my_crossentropy],
106 metrics=[msse],
107 optimizer='adam', loss_weights=[10, 0.5])
108
109
110 batch_size = 32
111
112 print('Loading data...')
113 with h5py.File('training.h5', 'r') as hf:
114 all_data = hf['data'][:]
115 print('done.')
116
117 window_size = 2000
118
119 nb_sequences = len(all_data)//window_size
120 print(nb_sequences, ' sequences')
121 x_train = all_data[:nb_sequences*window_size, :42]
122 x_train = np.reshape(x_train, (nb_sequences, window_size, 42))
123
124 y_train = np.copy(all_data[:nb_sequences*window_size, 42:64])
125 y_train = np.reshape(y_train, (nb_sequences, window_size, 22))
126
127 noise_train = np.copy(all_data[:nb_sequences*window_size, 64:86])
128 noise_train = np.reshape(noise_train, (nb_sequences, window_size, 22))
129
130 vad_train = np.copy(all_data[:nb_sequences*window_size, 86:87])
131 vad_train = np.reshape(vad_train, (nb_sequences, window_size, 1))
132
133 all_data = 0;
134 #x_train = x_train.astype('float32')
135 #y_train = y_train.astype('float32')
136
137 print(len(x_train), 'train sequences. x shape =', x_train.shape, 'y shape = ', y_train.shape)
138
139 print('Train...')
140 model.fit(x_train, [y_train, vad_train],
141 batch_size=batch_size,
142 epochs=120,
143 validation_split=0.1)
144 model.save("weights.hdf5")