TensorFlow使用C++加载使用训练好的模型,.cc文件代码实现的相关类及方法总结

本文总结了使用TensorFlow C++ API加载预训练模型的关键步骤,包括整体逻辑、Run函数的应用、NewSession的创建、ReadBinaryProto的模型读取以及Tensor的各种操作如vec、matrix和tensor函数。参考了相关教程,详细阐述了.cc文件中的实现细节。
摘要由CSDN通过智能技术生成

在官网API和Tensorflow源码头文件中查看获取。

同时参考

https://medium.com/jim-fleming/loading-a-tensorflow-graph-with-the-c-api-4caaff88463f

https://vimsky.com/article/3600.html
1.整体逻辑

 
/// ```c++ /// tensorflow::GraphDef graph; /// // ... Create or load graph into "graph". /// /// // This example uses the default options which connects /// // to a local runtime. /// tensorflow::SessionOptions options; /// std::unique_ptr<tensorflow::Session> /// session(tensorflow::NewSession(options)); /// /// // Create the session with this graph. /// tensorflow::Status s = session->Create(graph); /// if (!s.ok()) { ... } /// /// // Run the graph and fetch the first output of the "output" /// // operation, and also run to but do not return anything /// // for the "update_state" operation. /// std::vector<tensorflow::Tensor> outputs; /// s = session->Run({}, {"output:0"}, {"update_state"}, &outputs); /// if (!s.ok()) { ... } /// /// // Map the output as a flattened float tensor, and do something /// // with it. /// auto output_tensor = outputs[0].flat<float>(); /// if (output_tensor(0) > 0.5) { ... } /// /// // Close the session to release the resources associated with /// // this session. /// session->Close();

 

 

2.代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值