1#!/usr/bin/python3 2'''Copyright (c) 2021-2022 Amazon 3 Copyright (c) 2018-2019 Mozilla 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions 7 are met: 8 9 - Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 12 - Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in the 14 documentation and/or other materials provided with the distribution. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27''' 28 29# Train an LPCNet model 30 31import argparse 32from plc_loader import PLCLoader 33 34parser = argparse.ArgumentParser(description='Test a PLC model') 35 36parser.add_argument('weights', metavar='<weights file>', help='weights file (.h5)') 37parser.add_argument('features', metavar='<features file>', help='binary features file (float32)') 38parser.add_argument('output', metavar='<output>', help='reconstructed file (float32)') 39parser.add_argument('--model', metavar='<model>', default='lpcnet_plc', help='PLC model python definition (without .py)') 40group1 = parser.add_mutually_exclusive_group() 41 42parser.add_argument('--gru-size', metavar='<units>', default=256, type=int, help='number of units in GRU (default 256)') 43parser.add_argument('--cond-size', metavar='<units>', default=128, type=int, help='number of units in conditioning network (default 128)') 44 45 46args = parser.parse_args() 47 48import importlib 49lpcnet = importlib.import_module(args.model) 50 51import sys 52import numpy as np 53from tensorflow.keras.optimizers import Adam 54from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger 55import tensorflow.keras.backend as K 56import h5py 57 58import tensorflow as tf 59#gpus = tf.config.experimental.list_physical_devices('GPU') 60#if gpus: 61# try: 62# tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=5120)]) 63# except RuntimeError as e: 64# print(e) 65 66model = lpcnet.new_lpcnet_plc_model(rnn_units=args.gru_size, batch_size=1, training=False, quantize=False, cond_size=args.cond_size) 67model.compile() 68 69lpc_order = 16 70 71feature_file = args.features 72nb_features = model.nb_used_features + lpc_order 73nb_used_features = model.nb_used_features 74 75# u for unquantised, load 16 bit PCM samples and convert to mu-law 76 77features = np.loadtxt(feature_file) 78print(features.shape) 79sequence_size = features.shape[0] 80lost = np.reshape(features[:,-1:], (1, sequence_size, 1)) 81features = features[:,:nb_used_features] 82features = np.reshape(features, (1, sequence_size, nb_used_features)) 83 84 85model.load_weights(args.weights) 86 87features = features*lost 88out = model.predict([features, lost]) 89 90out = features + (1-lost)*out 91 92np.savetxt(args.output, out[0,:,:]) 93