下载TF代码,配置tf

git clone -b v1.14.0 https://github.com/tensorflow/tensorflow.git
cd tensorflow
./configure # 配置tf编译选项

编译tf so,ABI根据实际需要配置

bazel build --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" --config=opt //tensorflow:libtensorflow_cc.so
bazel build --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" --config=opt //tensorflow:libtensorflow_framework.so

编译遇到SSL错误时


# apt-get install -y default-jre
# apt-get install -y default-jdk
# 导入证书
# keytool -import  -noprompt -keystore "/etc/ssl/certs/java/cacerts"  -storepass changeit -keypass changeit -alias twca -file xxx.cer

bazel --host_jvm_args=-Djavax.net.ssl.trustStore="/etc/ssl/certs/java/cacerts" \
      --host_jvm_args=-Djavax.net.ssl.trustStorePassword=changeit \
      build --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" --config=opt //tensorflow:libtensorflow_cc.so

bazel --host_jvm_args=-Djavax.net.ssl.trustStore="/etc/ssl/certs/java/cacerts" \
      --host_jvm_args=-Djavax.net.ssl.trustStorePassword=changeit \
      build --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" --config=opt //tensorflow:libtensorflow_framework.so

可能遇到其他错误,可以网络寻找解决方案。

编译完成后在bazel-bin/tensorflow/会生成相应的so文件。

开发cpp调用tf api,使用cmake配置工程

cmake_minimum_required(VERSION 2.8.2)
project(tf_test LANGUAGES CXX)

add_compile_options(-std=c++11)
add_compile_options(-fPIC)

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

add_executable(
    main
    main.cpp
)

include_directories(
    $ENV{TF_INCLUDE_PATH} # you can use include path of tf installed by pip
)

target_link_libraries(
    main
    /xx_path/libtensorflow_cc.so
    /xx_path/libtensorflow_framework.so
)  
    # "-Wl,--allow-multiple-definition -Wl,--whole-archive" #之后的库使用--whole-archive选项
    # "-Wl,--no-whole-archive" #之后的库不使用--whole-archive选项

include path可以使用pip安装的同版本tf的include path:

export TF_INCLUDE_PATH=$(python3 -c 'import tensorflow as tf; print(tf.sysconfig.get_compile_flags()[0].strip("-I"))')
 

参考模型调用

#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "tensorflow/core/protobuf/meta_graph.pb.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/platform/env.h"
#include <iostream>
#include <string>

int main() {
    std::string model_path = "resnet50.pb";
    tensorflow::GraphDef graphdef;
    tensorflow::Status status_load = ReadBinaryProto(tensorflow::Env::Default(), model_path, &graphdef);

    tensorflow::SessionOptions options;
    tensorflow::Session* _session;

    _session = tensorflow::NewSession(options);
    if (_session == nullptr) {
        std::cout << "create new session failed" << std::endl;
        return -1;
    }
    tensorflow::Status status;
    status = _session->Extend(graphdef);
    if (!status.ok()) {
        std::cout << "session extend graph failed" << std::endl;
        return -1;
    }
    return 0;
}

参考

从0开始使用tensorflow的c++库进行模型推断_gaussrieman123的博客-CSDN博客

​​​​​​TensorFlow c++ SessionFactory注册与No session factory registered错误_gaussrieman123的博客-CSDN博客

使用C++调用TensorFlow模型简单说明 | Dannyw's Blog

https://chromium.googlesource.com/external/github.com/tensorflow/tensorflow/+/r0.12/tensorflow/g3doc/api_docs/cc/index.md

Tensorflow C API 从训练到部署:使用 C API 进行预测和部署 - 技术刘

https://github.com/Neargye/hello_tf_c_api 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐