2

I used Keras to train a simple RNN with 2 layers of LSTM with dropout. I want to load the .pb graph in tensorflow C API and use it for later prediction, but I got segmentation fault. Later I found if I keep the network the same and only removing the dropout option and re-train it again, then everything runs OK. However I want to use the one with Dropout, because the accuracy is better in predicting test data. Some one with suggestions? There are so few examples for using tensorflow C API.

Here is where I got segmentation fault:

TF_SessionRun(session, NULL,
                  &inputs[0], &input_values[0], static_cast<int>(inputs.size()),
                  &outputs[0], &output_values[0], static_cast<int>(outputs.size()),
                  NULL, 0, NULL, status);
    // Assign the values from the output tensor to a variable and iterate over them
    ASSERT(!output_values.empty());
float* out_vals = static_cast<float*>(TF_TensorData(output_values[0]));

BTW, I used the following code from website to change from .mdl in Keras to .pb in tensorflow. import tensorflow as tf import sys import numpy as np

# Create function to convert saved keras model to tensorflow graph
def convert_to_pb(weight_file,input_fld='',output_fld=''):

    import os
    import os.path as osp
    from tensorflow.python.framework import graph_util
    from tensorflow.python.framework import graph_io
    from keras.models import load_model
    from keras import backend as K


    # weight_file is a .h5 keras model file
    output_node_names_of_input_network = ["pred0"]
    output_node_names_of_final_network = 'output_node'

    # change filename to a .pb tensorflow file
    output_graph_name = weight_file[:-3]+'pb'
    weight_file_path = osp.join(input_fld, weight_file)

    net_model = load_model(weight_file_path)

    num_output = len(output_node_names_of_input_network)
    pred = [None]*num_output
    pred_node_names = [None]*num_output

    for i in range(num_output):
        pred_node_names[i] = output_node_names_of_final_network+str(i)
        pred[i] = tf.identity(net_model.output[i], name=pred_node_names[i])
    print('output nodes names are: ', pred_node_names)
    sess = K.get_session()

    constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
    graph_io.write_graph(constant_graph, output_fld, output_graph_name, as_text=False)
    print('saved the constant graph (ready for inference) at: ', osp.join(output_fld, output_graph_name))

    return output_fld+output_graph_name

tfpath = convert_to_pb(sys.argv[1],'./','./')
print 'tfpath: ', tfpath

Then

Feng
  • 67
  • 1
  • 9
  • gdb shows TF_TensorData(output_values[0]); is where segmentation comes from. – Feng Sep 29 '17 at 10:57
  • 1
    This could happen (as per the [documentation](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/c_api.h#L1246)) when the call to `TF_SessionRun` fails. You should check the value of `status` using something like: ``` ASSERT_EQ(TF_OK, TF_GetStatus(status)) << TF_Message(status); ``` to get an indication of why the `TF_SessionRun` call is failing. – ash Oct 04 '17 at 05:11
  • @ash Thank you, I solved this issue recently. Basically I trained the rnn with dropout in Keras but during the process when I converted the .mdl file to .pb file in tensorflow, I forgot to set the K.learning phase to be 0. Adding one line to the function : **set_learning_phase(0)** int function **convert_to_pb** perfectly solved this issue. – Feng Oct 09 '17 at 10:32

0 Answers0