当前位置: 首页 > 知识库问答 >
问题:

Tensorflow在C++中导出和运行图形的不同方式

朱锦
2023-03-14

为了将您训练过的网络导入到C++中,您需要导出您的网络才能这样做。在搜索了很多并且几乎没有找到关于它的信息之后,澄清了我们应该使用freeze_graph()来能够做到这一点。

由于Tensorflow的新0.7版本,他们增加了它的文档。

共有1个答案

卫君博
2023-03-14

下面是我的解决方案,它利用TF0.12中引入的V2检查点。

不需要将所有变量转换为常量或冻结图形。

为了清楚起见,V2检查点在我的目录models中如下所示:

checkpoint  # some information on the name of the files in the checkpoint
my-model.data-00000-of-00001  # the saved weights
my-model.index  # probably definition of data layout in the previous file
my-model.meta  # protobuf of the graph (nodes and topology info)
with tf.Session() as sess:
    tf.train.Saver(tf.trainable_variables()).save(sess, 'models/my-model')
with tf.Session() as sess:
    saver = tf.train.import_meta_graph('models/my-model.meta')
    saver.restore(sess, tf.train.latest_checkpoint('models/'))
    outputTensors = sess.run(outputOps, feed_dict=feedDict)

请注意,checkpointpath不是任何现有文件的路径,只是它们的公共前缀。如果您错误地将路径放置到.index文件,TF不会告诉您这是错误的,但它会在推理过程中由于变量未初始化而死亡。

#include <tensorflow/core/public/session.h>
#include <tensorflow/core/protobuf/meta_graph.pb.h>

using namespace std;
using namespace tensorflow;

...
// set up your input paths
const string pathToGraph = "models/my-model.meta"
const string checkpointPath = "models/my-model";
...

auto session = NewSession(SessionOptions());
if (session == nullptr) {
    throw runtime_error("Could not create Tensorflow session.");
}

Status status;

// Read in the protobuf graph we exported
MetaGraphDef graph_def;
status = ReadBinaryProto(Env::Default(), pathToGraph, &graph_def);
if (!status.ok()) {
    throw runtime_error("Error reading graph definition from " + pathToGraph + ": " + status.ToString());
}

// Add the graph to the session
status = session->Create(graph_def.graph_def());
if (!status.ok()) {
    throw runtime_error("Error creating graph: " + status.ToString());
}

// Read weights from the saved checkpoint
Tensor checkpointPathTensor(DT_STRING, TensorShape());
checkpointPathTensor.scalar<std::string>()() = checkpointPath;
status = session->Run(
        {{ graph_def.saver_def().filename_tensor_name(), checkpointPathTensor },},
        {},
        {graph_def.saver_def().restore_op_name()},
        nullptr);
if (!status.ok()) {
    throw runtime_error("Error loading checkpoint from " + checkpointPath + ": " + status.ToString());
}

// and run the inference to your liking
auto feedDict = ...
auto outputOps = ...
std::vector<tensorflow::Tensor> outputTensors;
status = session->Run(feedDict, outputOps, {}, &outputTensors);
 类似资料:
  • 节点包含(String from,String to,int time)。给定一个HashMap,key是一个字符串,value是一个具有相同From的节点列表。 我想写一个函数来找到从给定起点(字符串起始)到终点(字符串结束)的所有可能路径。我试图编写一个dfs函数,但它看起来只返回一组结果。有人能帮忙吗? > 节点:String from、String to、int time list>res

  • 在这里,将重点关注和学习TensorFlow中的形成。这有助于了解TensorFlow中的导出模块。包含基本信息,这些信息是对先前训练过的图表进行训练,执行评估或运行推理所必需的。 以下是相同的代码片段 - 下面是一个典型的使用模型 -

  • 使用以下查询创建了表并插入了数据 如果我在Windows中从DB Visualizer执行select查询,它会引发以下错误-“[代码:1843,SQL State:22008]ORA-01843:不是一个有效的月份”。 但是如果我从Ubuntu IntelliJ数据库IDE中执行相同的查询,它就会通过给出resultset。 如果日期格式不匹配,那么为什么它会传入Ubuntu?请建议为什么会发生

  • 我有一个关于tf的问题。图层。conv3d。如果我理解正确,它需要输入形状 (批次x深度x高度x宽度x通道) 其中通道应该只有一个;给定一个过滤器(深度x高度x宽度),它创建#个过滤器相同形状的不同过滤器以创建#个过滤器输出通道并将它们与输入卷积以获得形状的输出 (批次x out\u深度x out\u高度x out\u宽度x num\u过滤器) 首先,我现在是对的吗?问题是:在我看来,这一层不符合

  • 我正在使用以下项目 https://github.com/akotoe/android-slide-out-menu.git开发滑出菜单应用程序。 如何通过单击幻灯片菜单中的列表在同一视图中运行不同的活动。 例如,如果我单击项目1,我想在一个单独的活动中解析一个XML文件,并将该活动作为子项添加到此父视图中。因为在每一项单击上,我希望解析一个单独的XML文件,并且我希望在一个单独的布局文件中表示解

  • 前面的回答给出了部分解决方案,解释了整体方法,但我没有一个能够成功实现。其他的点点滴滴都可以找到,但不幸的是,这些点点滴滴还没有形成一个有效的解决方案。请考虑我所做的研究,在标记这是重复的或已经回答。 TensorFlow:如何保存/恢复模型? 恢复张量流模型 新文件:cifar10_eval_single.py