如何使用Tensorflow c ++实现此代码(FaceNet)?

时间:2017-03-06 02:27:48

标签: c++ tensorflow face-recognition

FaceNet。作者David Sandberg

FaceNet使用python实现代码:

#load graph
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

# We load the graph_def in the default graph
with tf.Graph().as_default() as graph:
    tf.import_graph_def(graph_def, name='')

with tf.Session(graph=graph, config=config) as sess:
    with sess.as_default():

    image_batch = graph.get_tensor_by_name("input:0")
    phase_train_placeholder = graph.get_tensor_by_name("phase_train:0")
    embeddings = graph.get_tensor_by_name("embeddings:0")
    feed_dict = {image_batch: np.expand_dims(face, 0), phase_train_placeholder: False}
    rep = sess.run(embeddings, feed_dict=feed_dict)
    #do something by 'rep'
    #....

现在使用C ++实现此代码:

Session* session;

// Initialize a tensorflow session
Status status = NewSession(SessionOptions(), &session);
if (!status.ok()) {
    std::cout << status.ToString() << "\n";
    return 1;
}

//Load graph ...

GraphDef graph_def;
status = ReadBinaryProto(Env::Default(), "250000.pb", &graph_def);
if (!status.ok()) {
    std::cout << status.ToString() << "\n";
    return 1;
}
/*
How do I use "get_tensor_by_name" ??

std::vector<Tensor> out_tensors;
TF_RETURN_IF_ERROR(session->Run({}, {output_name + ":0", output_name + ":1"},
                                      {}, &out_tensors));
*/

如何在Tensorflow C ++中使用get_tensor_by_name?

如何调用run方法,和上面的python代码达到同样的目的?

张量image_batch:np.expand_dims(image_data,0)需要传递给矩阵值,我该怎么写这个np.expand_dims(image_data,0)?

这是一个很好的提示:Import OpenCV Mat into C++ Tensorflow without copying

现在没有问题,谢谢大家。

1 个答案:

答案 0 :(得分:0)

C ++库没有等价物。您只需传递张量的名称而不是张量对象。

string image_batch = "input:0";
string phase_train_placeholder = "phase_train:0";
string embeddings = "embeddings:0";

请参阅此问题:C++ equivalent of python: tf.Graph.get_tensor_by_name() in Tensorflow?