Debian Codegraph @

3rdparty/renamenoise/training/dump_rnn.py

raw
1 #!/usr/bin/python
2
3 #
4 # Copyright (c) 2024, The Mumble Developers
5 # Copyright (c) 2017, Jean-Marc Valin
6 #
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # - Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 #
16 # - Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 from __future__ import print_function
33
34 from keras.models import Sequential
35 from keras.layers import Dense
36 from keras.layers import LSTM
37 from keras.layers import GRU
38 from keras.models import load_model
39 from keras import backend as K
40 import sys
41 import re
42 import numpy as np
43
44 def printVector(f, ft, vector, name):
45 v = np.reshape(vector, (-1));
46 #print('static const float ', name, '[', len(v), '] = \n', file=f)
47 f.write('static const renamenoise_rnn_weight {}[{}] = {{\n '.format(name, len(v)))
48 for i in range(0, len(v)):
49 f.write('{}'.format(min(127, int(round(256*v[i])))))
50 ft.write('{}'.format(min(127, int(round(256*v[i])))))
51 if (i!=len(v)-1):
52 f.write(',')
53 else:
54 break;
55 ft.write(" ")
56 if (i%8==7):
57 f.write("\n ")
58 else:
59 f.write(" ")
60 #print(v, file=f)
61 f.write('\n};\n\n')
62 ft.write("\n")
63 return;
64
65 def printLayer(f, ft, layer):
66 weights = layer.get_weights()
67 activation = re.search('function (.*) at', str(layer.activation)).group(1).upper()
68 if len(weights) > 2:
69 ft.write('{} {} '.format(weights[0].shape[0], weights[0].shape[1]/3))
70 else:
71 ft.write('{} {} '.format(weights[0].shape[0], weights[0].shape[1]))
72 if activation == 'SIGMOID':
73 ft.write('1\n')
74 elif activation == 'RELU':
75 ft.write('2\n')
76 else:
77 ft.write('0\n')
78 name = "renamenoise_" + layer.name
79 printVector(f, ft, weights[0], name + '_weights')
80 if len(weights) > 2:
81 printVector(f, ft, weights[1], name + '_recurrent_weights')
82 printVector(f, ft, weights[-1], name + '_bias')
83 if len(weights) > 2:
84 f.write('static const ReNameNoiseGRULayer {} = {{\n {}_bias,\n {}_weights,\n {}_recurrent_weights,\n {}, {}, ACTIVATION_{}\n}};\n\n'
85 .format(name, name, name, name, weights[0].shape[0], weights[0].shape[1]/3, activation))
86 else:
87 f.write('static const ReNameNoiseDenseLayer {} = {{\n {}_bias,\n {}_weights,\n {}, {}, ACTIVATION_{}\n}};\n\n'
88 .format(name, name, name, weights[0].shape[0], weights[0].shape[1], activation))
89
90 def structLayer(f, layer):
91 weights = layer.get_weights()
92 name = "renamenoise_" + layer.name
93 if len(weights) > 2:
94 f.write(' {},\n'.format(weights[0].shape[1]/3))
95 else:
96 f.write(' {},\n'.format(weights[0].shape[1]))
97 f.write(' &{},\n'.format(name))
98
99
100 def foo(c, name):
101 return None
102
103 def mean_squared_sqrt_error(y_true, y_pred):
104 return K.mean(K.square(K.sqrt(y_pred) - K.sqrt(y_true)), axis=-1)
105
106
107 model = load_model(sys.argv[1], custom_objects={'msse': mean_squared_sqrt_error, 'mean_squared_sqrt_error': mean_squared_sqrt_error, 'my_crossentropy': mean_squared_sqrt_error, 'mycost': mean_squared_sqrt_error, 'WeightClip': foo})
108
109 weights = model.get_weights()
110
111 f = open(sys.argv[2], 'w')
112 ft = open(sys.argv[3], 'w')
113
114 f.write('/*This file is automatically generated from a Keras model*/\n\n')
115 f.write('#ifdef HAVE_CONFIG_H\n#include "config.h"\n#endif\n\n#include "rnn.h"\n#include "rnn_data.h"\n\n')
116 ft.write('renamenoise-nu model file version 1\n')
117
118 layer_list = []
119 for i, layer in enumerate(model.layers):
120 if len(layer.get_weights()) > 0:
121 printLayer(f, ft, layer)
122 if len(layer.get_weights()) > 2:
123 layer_list.append("renamenoise_" + layer.name)
124
125 f.write('const struct ReNameNoiseModel renamenoise_model_{} = {{\n'.format(sys.argv[4]))
126 for i, layer in enumerate(model.layers):
127 if len(layer.get_weights()) > 0:
128 structLayer(f, layer)
129 f.write('};\n')
130
131 #hf.write('struct ReNameNoiseRNNState {\n')
132 #for i, name in enumerate(layer_list):
133 # hf.write(' float {}_state[{}_SIZE];\n'.format(name, name.upper()))
134 #hf.write('};\n')
135
136 f.close()