保存和恢复Keras BLSTM CTC模型 [英] Saving and restoring Keras BLSTM CTC model

查看:64
本文介绍了保存和恢复Keras BLSTM CTC模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究语音情感识别深度神经网络.我使用了具有CTC损失的keras双向LSTM.我训练了模型并保存了

I have been working on speech emotion recognition deep neural network. I have used keras Bidirectional LSTM with CTC loss. i trained the model and saved it

model_json = model.to_json() with open("ctc_model.json", "w") as json_file: json_file.write(model_json) model.save_weights("ctc_weights.h5")

model_json = model.to_json() with open("ctc_model.json", "w") as json_file: json_file.write(model_json) model.save_weights("ctc_weights.h5")

问题是我无法使用该模型对看不见的数据进行测试,因为该模型接受4个参数作为输入并计算ctc损失.所以我该如何保存模型而只需要一个输入.而不是标签和长度.基本上,我如何将模型另存为此函数test_func = K.function([net_input], [output])

The problem is i can not use this model to test on on unseen data because the model accepts 4 argument as input and calculates the ctc loss..just build the model and train. so how can i save a model in such away that in require only one input. not the labels, and length. Basically how can i save a model as this function test_func = K.function([net_input], [output])

def ctc_lambda_func(args):
    y_pred, labels, input_length, label_length = args

   shift = 2
   y_pred = y_pred[:, shift:, :]
   input_length -= shift
   return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
def build_model(nb_feat, nb_class, optimizer='Adadelta'):
    net_input = Input(name="the_input", shape=(200, nb_feat))
    forward_lstm1  = LSTM(output_dim=64, 
                      return_sequences=True, 
                      activation="tanh"
                     )(net_input)
    backward_lstm1 = LSTM(output_dim=64, 
                      return_sequences=True, 
                      activation="tanh",
                      go_backwards=True
                     )(net_input)
    blstm_output1  = Merge(mode='concat')([forward_lstm1, backward_lstm1])

    forward_lstm2  = LSTM(output_dim=64, 
                      return_sequences=True, 
                      activation="tanh"
                     )(blstm_output1)
    backward_lstm2 = LSTM(output_dim=64, 
                      return_sequences=True, 
                      activation="tanh",
                      go_backwards=True
                     )(blstm_output1)
    blstm_output2  = Merge(mode='concat')([forward_lstm2, backward_lstm2])

    hidden = TimeDistributed(Dense(512, activation='tanh'))(blstm_output2)
    output = TimeDistributed(Dense(nb_class + 1, activation='softmax')) (hidden)

    labels = Input(name='the_labels', shape=[1], dtype='float32')
    input_length = Input(name='input_length', shape=[1], dtype='int64')
    label_length = Input(name='label_length', shape=[1], dtype='int64')
    loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name="ctc")([output, labels, input_length, label_length])
    model = Model(input=[net_input, labels, input_length, label_length], output=[loss_out])
    model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=optimizer, metrics=[])

    test_func = K.function([net_input], [output])

    return model, test_func
model, test_func = build_model(nb_feat=nb_feat, nb_class=nb_class, optimizer=optimizer)
 for epoch in range(number_epoches):
     inputs_train = {'the_input': X_train[i:i+batch_size],
                    'the_labels': y_train[i:i+batch_size],
                    'input_length': np.sum(X_train_mask[i:i+batch_size], axis=1, dtype=np.int32),
                    'label_length': np.squeeze(y_train_mask[i:i+batch_size]),
                   }
     outputs_train = {'ctc': np.zeros([inputs_train["the_labels"].shape[0]])}

    ctcloss = model.train_on_batch(x=inputs_train, y=outputs_train)

    total_ctcloss += ctcloss * inputs_train["the_input"].shape[0] * 1.
loss_train[epoch] = total_ctcloss / X_train.shape[0]

Here is the my model summary

推荐答案

尝试以下解决方案:

import keras.backend as K

def get_prediction_function(model):
    input_tensor = model.layers[0].input
    output_tensor = model.layers[-5].output
    net_function = K.function([input_tensor, K.learning_phase()], [output_tensor])
    def _result_function(x):
        return net_function([x, 0])[0]
    return _result_function

现在您的网络功能可以通过以下方式获得:

Now your network function might be obtained by:

test_function = get_prediction_function(model)

这篇关于保存和恢复Keras BLSTM CTC模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆