add some code
This commit is contained in:
57
managed_components/78__esp-opus/scripts/dump_rnn.py
Normal file
57
managed_components/78__esp-opus/scripts/dump_rnn.py
Normal file
@@ -0,0 +1,57 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
from keras.models import Sequential
|
||||
from keras.layers import Dense
|
||||
from keras.layers import LSTM
|
||||
from keras.layers import GRU
|
||||
from keras.models import load_model
|
||||
from keras import backend as K
|
||||
|
||||
import numpy as np
|
||||
|
||||
def printVector(f, vector, name):
|
||||
v = np.reshape(vector, (-1));
|
||||
#print('static const float ', name, '[', len(v), '] = \n', file=f)
|
||||
f.write('static const opus_int16 {}[{}] = {{\n '.format(name, len(v)))
|
||||
for i in range(0, len(v)):
|
||||
f.write('{}'.format(int(round(8192*v[i]))))
|
||||
if (i!=len(v)-1):
|
||||
f.write(',')
|
||||
else:
|
||||
break;
|
||||
if (i%8==7):
|
||||
f.write("\n ")
|
||||
else:
|
||||
f.write(" ")
|
||||
#print(v, file=f)
|
||||
f.write('\n};\n\n')
|
||||
return;
|
||||
|
||||
def binary_crossentrop2(y_true, y_pred):
|
||||
return K.mean(2*K.abs(y_true-0.5) * K.binary_crossentropy(y_pred, y_true), axis=-1)
|
||||
|
||||
|
||||
model = load_model("weights.hdf5", custom_objects={'binary_crossentrop2': binary_crossentrop2})
|
||||
|
||||
weights = model.get_weights()
|
||||
|
||||
f = open('rnn_weights.c', 'w')
|
||||
|
||||
f.write('/*This file is automatically generated from a Keras model*/\n\n')
|
||||
f.write('#ifdef HAVE_CONFIG_H\n#include "config.h"\n#endif\n\n#include "mlp.h"\n\n')
|
||||
|
||||
printVector(f, weights[0], 'layer0_weights')
|
||||
printVector(f, weights[1], 'layer0_bias')
|
||||
printVector(f, weights[2], 'layer1_weights')
|
||||
printVector(f, weights[3], 'layer1_recur_weights')
|
||||
printVector(f, weights[4], 'layer1_bias')
|
||||
printVector(f, weights[5], 'layer2_weights')
|
||||
printVector(f, weights[6], 'layer2_bias')
|
||||
|
||||
f.write('const DenseLayer layer0 = {\n layer0_bias,\n layer0_weights,\n 25, 16, 0\n};\n\n')
|
||||
f.write('const GRULayer layer1 = {\n layer1_bias,\n layer1_weights,\n layer1_recur_weights,\n 16, 12\n};\n\n')
|
||||
f.write('const DenseLayer layer2 = {\n layer2_bias,\n layer2_weights,\n 12, 2, 1\n};\n\n')
|
||||
|
||||
f.close()
|
||||
220
managed_components/78__esp-opus/scripts/local_build.py
Normal file
220
managed_components/78__esp-opus/scripts/local_build.py
Normal file
@@ -0,0 +1,220 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
"""
|
||||
This script tries to build a project with CMake, Meson, and Autotools.
|
||||
It checks if CMake, Meson, and Autotools are installed, and performs
|
||||
the configure, build, and optionally test steps for each build system.
|
||||
"""
|
||||
|
||||
import multiprocessing
|
||||
import os
|
||||
import random
|
||||
import string
|
||||
import subprocess
|
||||
import sys
|
||||
import shutil
|
||||
|
||||
|
||||
if sys.platform == "win32":
|
||||
REPO_DIR = "\\".join(os.path.abspath(__file__).split("\\")[:-2])
|
||||
else:
|
||||
REPO_DIR = "/".join(os.path.abspath(__file__).split("/")[:-2])
|
||||
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--test", action="store_true", help="Run tests")
|
||||
parser.add_argument("--cmake", action="store_true", help="Only run CMake")
|
||||
parser.add_argument("--meson", action="store_true", help="Only run Meson")
|
||||
if sys.platform != "win32":
|
||||
parser.add_argument("--autotools", action="store_true", help="Only run Autotools")
|
||||
args = parser.parse_args()
|
||||
|
||||
test = args.test
|
||||
cmake_only = args.cmake
|
||||
meson_only = args.meson
|
||||
if sys.platform != "win32":
|
||||
autotools_only = args.autotools
|
||||
autotools_only = False
|
||||
|
||||
result = []
|
||||
os.chdir(REPO_DIR)
|
||||
|
||||
# download model
|
||||
if sys.platform == "win32":
|
||||
run_command("autogen.bat")
|
||||
else:
|
||||
run_command("./autogen.sh")
|
||||
|
||||
if sys.platform != "win32" and not cmake_only and not meson_only:
|
||||
result += autotools_build(test)
|
||||
|
||||
if not autotools_only and not meson_only:
|
||||
result += cmake_build(test)
|
||||
result += cmake_build(test, extra_options="-DOPUS_NEURAL_FEC=ON")
|
||||
|
||||
if not autotools_only and not cmake_only:
|
||||
result += meson_build(test)
|
||||
|
||||
print_result(result, test)
|
||||
|
||||
def print_result(result, test=False):
|
||||
if len(result) == 0:
|
||||
print("No results available")
|
||||
return
|
||||
|
||||
headers = ["Name", "Build Passed"]
|
||||
if test:
|
||||
headers.append("Test Passed")
|
||||
|
||||
# Calculate the maximum width for each column
|
||||
column_widths = [max(len(str(row[i])) for row in result) for i in range(len(headers))]
|
||||
|
||||
# Print the headers
|
||||
header_row = " | ".join(f"{header: <{column_widths[i]}}" for i, header in enumerate(headers))
|
||||
print(header_row)
|
||||
print("-" * len(header_row))
|
||||
|
||||
# Print the data rows
|
||||
for row in result:
|
||||
row_values = [str(value) for value in row[:len(headers)]] # Include values up to the last column to be printed
|
||||
row_string = " | ".join(f"{row_values[i]: <{column_widths[i]}}" for i in range(len(headers)))
|
||||
print(row_string)
|
||||
|
||||
def autotools_build(test=False):
|
||||
build = "Autotools"
|
||||
autotools_build_succeeded = False
|
||||
autotools_test_succeeded = False
|
||||
|
||||
if not check_tool_installed("autoreconf") or not check_tool_installed("automake") or not check_tool_installed("autoconf"):
|
||||
print("Autotools dependencies are not installed. Aborting.")
|
||||
print("Install with: sudo apt-get install git autoconf automake libtool gcc make")
|
||||
return [(build, autotools_build_succeeded, autotools_test_succeeded)]
|
||||
|
||||
run_command("./configure")
|
||||
if run_command("make -j {}".format(get_cpu_core_count())) == 0:
|
||||
autotools_build_succeeded = True
|
||||
if test:
|
||||
if run_command("make check -j {}".format(get_cpu_core_count())) == 0:
|
||||
autotools_test_succeeded = True
|
||||
|
||||
return [(build, autotools_build_succeeded, autotools_test_succeeded)]
|
||||
|
||||
|
||||
def cmake_build(test=False, extra_options=""):
|
||||
cmake_build_succeeded = False
|
||||
cmake_test_succeeded = False
|
||||
build = "CMake"
|
||||
|
||||
if not check_tool_installed("cmake"):
|
||||
print("CMake is not installed. Aborting.")
|
||||
if sys.platform != "win32":
|
||||
print("Install with: sudo apt install cmake")
|
||||
else:
|
||||
print("Download and install from: https://cmake.org/download/")
|
||||
return [(build, cmake_build_succeeded, cmake_test_succeeded)]
|
||||
|
||||
if not check_tool_installed("ninja"):
|
||||
print("Ninja is not installed. Aborting.")
|
||||
if sys.platform != "win32":
|
||||
print("Install with: sudo apt ninja-build ")
|
||||
else:
|
||||
print("Download and install from: https://ninja-build.org/")
|
||||
return [(build, cmake_build_succeeded, cmake_test_succeeded)]
|
||||
|
||||
cmake_build_dir = create_dir_with_random_postfix("cmake-build")
|
||||
cmake_cfg_cmd = ["cmake", "-S" ".", "-B", cmake_build_dir, "-G", '"Ninja"', "-DCMAKE_BUILD_TYPE=Release", "-DOPUS_BUILD_TESTING=ON", "-DOPUS_BUILD_PROGRAMS=ON", "-DOPUS_FAST_MATH=ON", "-DOPUS_FLOAT_APPROX=ON"]
|
||||
cmake_cfg_cmd += [option for option in extra_options.split(" ")]
|
||||
run_command(" ".join(cmake_cfg_cmd))
|
||||
cmake_build_cmd = ["cmake", "--build", cmake_build_dir, "-j", "{}".format(get_cpu_core_count())]
|
||||
cmake_test_cmd = ["ctest", "-j", "{}".format(get_cpu_core_count())]
|
||||
if sys.platform == "win32":
|
||||
cmake_build_cmd += ["--config", "Release"]
|
||||
cmake_test_cmd += ["-C", "Release"]
|
||||
|
||||
if run_command(" ".join(cmake_build_cmd)) == 0:
|
||||
cmake_build_succeeded = True
|
||||
|
||||
if test:
|
||||
os.chdir(cmake_build_dir)
|
||||
if run_command(" ".join(cmake_test_cmd)) == 0:
|
||||
cmake_test_succeeded = True
|
||||
os.chdir(REPO_DIR)
|
||||
|
||||
shutil.rmtree(cmake_build_dir)
|
||||
|
||||
if extra_options != "":
|
||||
build += " with options: "
|
||||
build += extra_options.replace("-D", "")
|
||||
return [(build, cmake_build_succeeded, cmake_test_succeeded)]
|
||||
|
||||
def meson_build(test=False, extra_options=""):
|
||||
build = "Meson"
|
||||
meson_build_succeeded = False
|
||||
meson_test_succeeded = False
|
||||
|
||||
if not check_tool_installed("meson"):
|
||||
print("Meson is not installed. Aborting.")
|
||||
print("Install with: pip install meson")
|
||||
return [(build, meson_build_succeeded, meson_test_succeeded)]
|
||||
|
||||
if not check_tool_installed("ninja"):
|
||||
print("Ninja is not installed. Aborting.")
|
||||
if sys.platform != "win32":
|
||||
print("Install with: sudo apt ninja-build ")
|
||||
else:
|
||||
print("Download and install from: https://ninja-build.org/")
|
||||
return [(build, meson_build_succeeded, meson_test_succeeded)]
|
||||
|
||||
meson_build_dir = create_dir_with_random_postfix("meson-build")
|
||||
meson_cfg_cmd = ["meson", "setup", meson_build_dir]
|
||||
meson_cfg_cmd += [option for option in extra_options.split(" ")]
|
||||
run_command(" ".join(meson_cfg_cmd))
|
||||
meson_build_cmd = ["meson", "compile", "-C", meson_build_dir]
|
||||
meson_test_cmd = ["meson", "test", "-C", meson_build_dir]
|
||||
|
||||
if run_command(" ".join(meson_build_cmd)) == 0:
|
||||
meson_build_succeeded = True
|
||||
|
||||
if test:
|
||||
os.chdir(meson_build_dir)
|
||||
if run_command(" ".join(meson_test_cmd)) == 0:
|
||||
meson_test_succeeded = True
|
||||
os.chdir(REPO_DIR)
|
||||
|
||||
shutil.rmtree(meson_build_dir)
|
||||
|
||||
if extra_options != "":
|
||||
build += " with options: "
|
||||
build += extra_options.replace("-D", "")
|
||||
return [(build, meson_build_succeeded, meson_test_succeeded)]
|
||||
|
||||
def create_dir_with_random_postfix(dir):
|
||||
random_chars = "".join(random.choices(string.ascii_letters, k=3))
|
||||
dir = dir + "-" + random_chars
|
||||
dir = os.path.join(os.getcwd(), dir)
|
||||
os.makedirs(dir)
|
||||
return dir
|
||||
|
||||
def check_tool_installed(tool):
|
||||
if sys.platform == "win32":
|
||||
try:
|
||||
# Use the "where" command to search for the executable
|
||||
subprocess.check_output(["where", tool], shell=True)
|
||||
return True
|
||||
except subprocess.CalledProcessError:
|
||||
return False
|
||||
else:
|
||||
return run_command(f"command -v {tool}") == 0
|
||||
|
||||
def run_command(command):
|
||||
process = subprocess.Popen(command, shell=True)
|
||||
process.communicate()
|
||||
return process.returncode
|
||||
|
||||
def get_cpu_core_count():
|
||||
return int(max(multiprocessing.cpu_count(), 1))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
67
managed_components/78__esp-opus/scripts/rnn_train.py
Normal file
67
managed_components/78__esp-opus/scripts/rnn_train.py
Normal file
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
from keras.models import Sequential
|
||||
from keras.models import Model
|
||||
from keras.layers import Input
|
||||
from keras.layers import Dense
|
||||
from keras.layers import LSTM
|
||||
from keras.layers import GRU
|
||||
from keras.layers import SimpleRNN
|
||||
from keras.layers import Dropout
|
||||
from keras import losses
|
||||
import h5py
|
||||
|
||||
from keras import backend as K
|
||||
import numpy as np
|
||||
|
||||
def binary_crossentrop2(y_true, y_pred):
|
||||
return K.mean(2*K.abs(y_true-0.5) * K.binary_crossentropy(y_pred, y_true), axis=-1)
|
||||
|
||||
print('Build model...')
|
||||
#model = Sequential()
|
||||
#model.add(Dense(16, activation='tanh', input_shape=(None, 25)))
|
||||
#model.add(GRU(12, dropout=0.0, recurrent_dropout=0.0, activation='tanh', recurrent_activation='sigmoid', return_sequences=True))
|
||||
#model.add(Dense(2, activation='sigmoid'))
|
||||
|
||||
main_input = Input(shape=(None, 25), name='main_input')
|
||||
x = Dense(16, activation='tanh')(main_input)
|
||||
x = GRU(12, dropout=0.1, recurrent_dropout=0.1, activation='tanh', recurrent_activation='sigmoid', return_sequences=True)(x)
|
||||
x = Dense(2, activation='sigmoid')(x)
|
||||
model = Model(inputs=main_input, outputs=x)
|
||||
|
||||
batch_size = 64
|
||||
|
||||
print('Loading data...')
|
||||
with h5py.File('features.h5', 'r') as hf:
|
||||
all_data = hf['features'][:]
|
||||
print('done.')
|
||||
|
||||
window_size = 1500
|
||||
|
||||
nb_sequences = len(all_data)/window_size
|
||||
print(nb_sequences, ' sequences')
|
||||
x_train = all_data[:nb_sequences*window_size, :-2]
|
||||
x_train = np.reshape(x_train, (nb_sequences, window_size, 25))
|
||||
|
||||
y_train = np.copy(all_data[:nb_sequences*window_size, -2:])
|
||||
y_train = np.reshape(y_train, (nb_sequences, window_size, 2))
|
||||
|
||||
all_data = 0;
|
||||
x_train = x_train.astype('float32')
|
||||
y_train = y_train.astype('float32')
|
||||
|
||||
print(len(x_train), 'train sequences. x shape =', x_train.shape, 'y shape = ', y_train.shape)
|
||||
|
||||
# try using different optimizers and different optimizer configs
|
||||
model.compile(loss=binary_crossentrop2,
|
||||
optimizer='adam',
|
||||
metrics=['binary_accuracy'])
|
||||
|
||||
print('Train...')
|
||||
model.fit(x_train, y_train,
|
||||
batch_size=batch_size,
|
||||
epochs=200,
|
||||
validation_data=(x_train, y_train))
|
||||
model.save("newweights.hdf5")
|
||||
7
managed_components/78__esp-opus/scripts/shrink_model.sh
Normal file
7
managed_components/78__esp-opus/scripts/shrink_model.sh
Normal file
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
|
||||
for i in fargan_data.c pitchdnn_data.c dred_rdovae_dec_data.c dred_rdovae_enc_data.c plc_data.c lace_data.c nolace_data.c
|
||||
do
|
||||
cat dnn/$i | perl -ne 'if (/DEBUG/ || /#else/) {$skip=1} if (!$skip && !/ifdef DOT_PROD/) {s/^ *//; s/, /,/g; print $_} elsif (/endif/) {$skip=0}' > tmp_data.c
|
||||
mv tmp_data.c dnn/$i
|
||||
done
|
||||
Reference in New Issue
Block a user