用c++加载tensorflow 模型

首先要下载tensorflow c++的动态链接库,强烈建议不要去手动编译tensorflow的c接口,版本太难对应了,很多坑。

1.下载tensorflow c++的动态链接库

按照这个博客
https://blog.csdn.net/rong_toa/article/details/88857364
下载安装就行了
简单就是

wget https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-1.12.0.tar.gz

不同的版本只有改下数字就行了
解压后到/home/qian/ProgramFiles/libtensorflow-gpu-linux-x86_64-1.12.0

2.运行

在CmakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(Tensorflow_test)

set(CMAKE_CXX_STANDARD 11)

include_directories(
    /home/qian/ProgramFiles/libtensorflow-gpu-linux-x86_64-1.12.0/include
    )

add_executable(Tensorflow_test main.cpp)

target_link_libraries(Tensorflow_test
    /home/qian/ProgramFiles/libtensorflow-gpu-linux-x86_64-1.12.0/lib/libtensorflow_framework.so
    /home/qian/ProgramFiles/libtensorflow-gpu-linux-x86_64-1.12.0/lib/libtensorflow.so
    )

main.cpp

#include 
#include 

int main() {
   std:: cout << "Hello from TensorFlow C library version" << TF_Version();
    return 0;
}

再来个高级点的创建会话
CmakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(Tensorflow_test)

#set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -D_GLIBCXX_USE_CXX11_ABI=0")

include_directories(
    /home/qian/ProgramFiles/libtensorflow-gpu-linux-x86_64-1.12.0/include
    /home/qian/anaconda3/lib/python3.6/site-packages/tensorflow/include/

    )

add_executable(Tensorflow_test main.cpp)

target_link_libraries(Tensorflow_test
    /home/qian/ProgramFiles/libtensorflow-gpu-linux-x86_64-1.12.0/lib/libtensorflow_framework.so
    /home/qian/ProgramFiles/libtensorflow-gpu-linux-x86_64-1.12.0/lib/libtensorflow.so
    )

注意加-D_GLIBCXX_USE_CXX11_ABI=0
否则会报
undefined reference to `tensorflow::internal::CheckOpMessageBuilder::NewString
错误

main.cpp

#include 
#include 
#include 

using namespace std;
using namespace tensorflow;

int main()
{
    Session* session;
    Status status = NewSession(SessionOptions(), &session);
    if (!status.ok()) {
        cout << status.ToString() << "\n";
        return 1;
    }
    cout << "Session successfully created.\n";
    return 0;
}

出现
No session factory registered for the given session options: {target: “” config: } Registered factor
解决
https://github.com/tensorflow/tensorflow/issues/3308
https://github.com/tradr-project/tensorflow_ros_cpp/issues/9

感觉又要重新编译,真是坑!
解决方案!!!!!!在stackoverflow有各种方法
https://stackoverflow.com/questions/33620794/how-to-build-and-use-google-tensorflow-c-api
方法1.
编译太麻烦,用人家打包好的api吧
https://github.com/serizba/cppflow
下载,安装其接口调用!
方法2
直接下载人家编译好的
https://github.com/kecsap/tensorflow_cpp_packaging/releases
方法3
bazel编译不好用,用cmake编译
https://github.com/FloopCZ/tensorflow_cc

方法4
自己编译!!!好坑

3.tensorflow c++接口实现mnist手写数字识别

不重新编译直接用github上的人家打包好的api吧
https://github.com/serizba/cppflow

git clone [email protected]:serizba/cppflow.git
cd cppflow/examples/load_model
mkdir build
cd build
cmake ..
make
./example

编译首先是
cmake版本,改小
cmake_minimum_required(VERSION 2.8)
c++版本
set(CMAKE_CXX_STANDARD 11)
将下载好的libtensorflow放到home目录下,或者修改CmakeLists.txt
最后出现编译错误
在Model.h中加上

namespace std {
    template<class T> struct _Unique_if {
        typedef unique_ptr<T> _Single_object;
    };

    template<class T> struct _Unique_if<T[]> {
        typedef unique_ptr<T[]> _Unknown_bound;
    };

    template<class T, size_t N> struct _Unique_if<T[N]> {
        typedef void _Known_bound;
    };

    template<class T, class... Args>
        typename _Unique_if<T>::_Single_object
        make_unique(Args&&... args) {
            return unique_ptr<T>(new T(std::forward<Args>(args)...));
        }

    template<class T>
        typename _Unique_if<T>::_Unknown_bound
        make_unique(size_t n) {
            typedef typename remove_extent<T>::type U;
            return unique_ptr<T>(new U[n]());
        }

    template<class T, class... Args>
        typename _Unique_if<T>::_Known_bound
        make_unique(Args&&...) = delete;
}

编译成功

参考
https://www.cnblogs.com/jourluohua/p/11947176.html

你可能感兴趣的:(深度学习)