2017-07-19 19 views
2

Tensorflow PythonからTensorflow C++にエクスポートしたグラフをインポートしようとしています。すでにグラフをPythonに再インポートしました。私が今欲しいのは、C++で同じコードを書くことですが、TensorflowのWebサイトのドキュメントは十分ではないため、C++のapi関数と使用方法についてはわかりません。単純なTensorflowのfrozen_model.pbファイルをインポートし、C++で予測する

ここまで私が見つけたC++コードです。

C++:

namespace tf = tensorflow; 

tf::Session* session; 

tf::Status status = tf::NewSession(tf::SessionOptions(), &session); 
checkStatus(status); 

tf::GraphDef graph_def; 
status = ReadBinaryProto(tf::Env::Default(), "./models/frozen_model.pb", &graph_def); 
checkStatus(status); 

status = session->Create(graph_def); 
checkStatus(status); 

tf::Tensor x(tf::DT_FLOAT, tf::TensorShape()); 
tf::Tensor y(tf::DT_FLOAT, tf::TensorShape()); 

x.scalar<float>()() = 23.0; 
y.scalar<float>()() = 19.0; 

std::vector<std::pair<tf::string, tf::Tensor>> input_tensors = {{"x", x}, {"y", y}}; 
std::vector<string> vNames; // vector of names for required graph nodes 
vNames.push_back("prefix/input_neurons:0"); 
vNames.push_back("prefix/prediction_restore:0"); 
std::vector<tf::Tensor> output_tensors; 

status = session->Run({}, vNames, {}, &output_tensors); 
checkStatus(status); 

tf::Tensor output = output_tensors[0]; 
std::cout << "Success: " << output.scalar<float>() << "!" << std::endl; 
session->Close(); 
return 0; 

私は上記の現在のC++コードを持っています問題は、それがprefix/input_neurons:0の名前で任意の操作を見つけることができないと言うことです。グラフには操作がありますが、このグラフをPythonコード(下記参照)にインポートすると完全に正常に動作するためです。

グラフを正常にインポートするためのPythonコードは次のとおりです。

Pythonは:私はPythonスクリプトからの操作を印刷することができ

def load_graph(frozen_graph_filename): 
    # We load the protobuf file from the disk and parse it to retrieve the 
    # unserialized graph_def 
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f: 
     graph_def = tf.GraphDef() 
     graph_def.ParseFromString(f.read()) 

    # Then, we can use again a convenient built-in function to import a graph_def into the 
    # current default Graph 
    with tf.Graph().as_default() as graph: 
     tf.import_graph_def(
      graph_def, 
      input_map=None, 
      return_elements=None, 
      name="prefix", 
      op_dict=None, 
      producer_op_list=None 
     ) 
    return graph 

# We use our "load_graph" function 
graph = load_graph("./models/frozen_model.pb") 

# We can verify that we can access the list of operations in the graph 
for op in graph.get_operations(): 
    print(op.name)  # <--- printing the operations snapshot below 
    # prefix/Placeholder/inputs_placeholder 
    # ... 
    # prefix/Accuracy/predictions 

# We access the input and output nodes 
x = graph.get_tensor_by_name('prefix/input_neurons:0') 
y = graph.get_tensor_by_name('prefix/prediction_restore:0') 

# We launch a Session 
with tf.Session(graph=graph) as sess: 

    test_features = [[0.377745556,0.009904444,0.063231111,0.009904444,0.003734444,0.002914444,0.008633333,0.000471111,0.009642222,0.05406,0.050163333,7e-05,0.006528889,0.000314444,0.00649,0.043956667,0.016816667,0.001644444,0.016906667,0.00204,0.027342222,0.13864]] 
     # compute the predicted output for test_x 
    pred_y = sess.run(y, feed_dict={x: test_features}) 
    print(pred_y) 

更新

(完全に正常動作します)。ここにスクリーンショットがあります。

enter image description here

ここに私が取得エラーです。

enter image description here

+0

正確なエラーを表示してください。 (私の答えが問題を解決しない場合) – gdelab

答えて

1

Run function referenceを参照してください:それはoptinoally余分な引数を持つ(そしてその後、出力ノード、実行する必要があり、その後、他の操作、出力ベクトル、入力は最初の入力辞書であるC++では、しかし、あなたがそれらを必要としないように見えます)。この呼び出しは動作するはずです:

status = session->Run({{"prefix/input_neurons:0", x}}, {"prefix/prediction_restore:0"}, {}, &output_tensors); 

をあなたが(そこにデータをコピーせずにこれを行う方法は非常におそらくですが、私は方法がわからない)Pythonで同じ値にxを設定したい場合は、することができますRun()を呼び出す前に、この操作を行います。

std::vector<float> test_features = {0.377745556,0.009904444,0.063231111,0.009904444,0.003734444,0.002914444,0.008633333,0.000471111,0.009642222,0.05406,0.050163333,7e-05,0.006528889,0.000314444,0.00649,0.043956667,0.016816667,0.001644444,0.016906667,0.00204,0.027342222,0.13864}; 
int n_features = test_features.size(); 
x= tf::Tensor(tf::DT_FLOAT, tf::TensorShape({1,n_features})); 
auto x_mapped = x.tensor<float, 2>(); 

for (int i = 0; i< n_features; i++) 
{ 
    x_mapped(0, i) = test_features[i]; 
} 

それはこれで良いでしょう場合を教えてください!

+0

例のおかげで、私はまだコードスニペットで同じエラーが発生します。質問のエラースナップショットが表示されます。私はそれを更新しました。 – Mj1992

+0

私は基本的な問題は今、 "接頭辞/ input_neurons:0"がグラフからインポートされていないことだと思います。私はC++で操作を印刷する方法がわかりませんが、私がPythonでグラフの操作を印刷すると、これらの操作はそこにあります – Mj1992

+0

グラフから何かがインポートされていますか? feed_dict(例えば、既知の名前の重み)を必要としない他のテンソルを実行しようとしましたか? – gdelab

関連する問題