shithub: opus

ref: fa1d2824fad7fff313c335385de41d083df3c76f
dir: /dnn/test_lpcnet.py/

View raw version
#!/usr/bin/python3

import lpcnet
import sys
import numpy as np
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
from ulaw import ulaw2lin, lin2ulaw
import keras.backend as K
import h5py
from adadiff import Adadiff

#import tensorflow as tf
#from keras.backend.tensorflow_backend import set_session
#config = tf.ConfigProto()
#config.gpu_options.per_process_gpu_memory_fraction = 0.28
#set_session(tf.Session(config=config))

nb_epochs = 40
batch_size = 64

model, enc, dec = lpcnet.new_wavernn_model()
model.compile(optimizer=Adadiff(), loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
#model.summary()

pcmfile = sys.argv[1]
feature_file = sys.argv[2]
frame_size = 160
nb_features = 54
nb_used_features = lpcnet.nb_used_features
feature_chunk_size = 15
pcm_chunk_size = frame_size*feature_chunk_size

data = np.fromfile(pcmfile, dtype='int8')
nb_frames = len(data)//pcm_chunk_size

features = np.fromfile(feature_file, dtype='float32')

data = data[:nb_frames*pcm_chunk_size]
features = features[:nb_frames*feature_chunk_size*nb_features]

in_data = np.concatenate([data[0:1], data[:-1]])/16.;

features = np.reshape(features, (nb_frames, feature_chunk_size, nb_features))

in_data = np.reshape(in_data, (nb_frames*pcm_chunk_size, 1))
out_data = np.reshape(data, (nb_frames*pcm_chunk_size, 1))


model.load_weights('lpcnet3a_21.h5')

order = 16

pcm = 0.*out_data
exc = out_data-0
pitch = np.zeros((1, 1, 1), dtype='float32')
fexc = np.zeros((1, 1, 1), dtype='float32')
iexc = np.zeros((1, 1, 1), dtype='int16')
state = np.zeros((1, lpcnet.rnn_units), dtype='float32')
for c in range(1, nb_frames):
    cfeat = enc.predict(features[c:c+1, :, :nb_used_features])
    for fr in range(1, feature_chunk_size):
        f = c*feature_chunk_size + fr
        a = features[c, fr, nb_used_features:]
        
        #print(a)
        gain = 1.;
        period = int(50*features[c, fr, 36]+100)
        period = period - 4
        for i in range(frame_size):
            pitch[0, 0, 0] = exc[f*frame_size + i - period, 0]
            fexc[0, 0, 0] = 2*exc[f*frame_size + i - 1]
            #fexc[0, 0, 0] = in_data[f*frame_size + i, 0]
            #print(cfeat.shape)
            p, state = dec.predict([fexc, cfeat[:, fr:fr+1, :], state])
            #p = np.maximum(p-0.003, 0)
            p = p/(1e-5 + np.sum(p))
            #print(np.sum(p))
            iexc[0, 0, 0] = np.argmax(np.random.multinomial(1, p[0,0,:], 1))-128
            exc[f*frame_size + i] = iexc[0, 0, 0]/16.
            #out_data[f*frame_size + i, 0] = iexc[0, 0, 0]
            pcm[f*frame_size + i, 0] = gain*iexc[0, 0, 0] - sum(a*pcm[f*frame_size + i - 1:f*frame_size + i - order-1:-1, 0])
            print(iexc[0, 0, 0], out_data[f*frame_size + i, 0], pcm[f*frame_size + i, 0])