gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:44:11 24 4
gpt4 key购买 nike

FaceNet .大卫·桑德伯格

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

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

最佳答案

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?

关于c++ - 我如何使用 Tensorflow c++ 来实现此代码 (FaceNet)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42616589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com