编译流程
安装bazel (CentOS)
方法1
- vim /etc/yum.repos.d/bazel.repo
- 拷贝下面内容
[vbatts-bazel]
name=Copr repo for bazel owned by vbatts
baseurl=https://copr-be.cloud.fedoraproject.org/results/vbatts/bazel/epel-7-$basearch/
type=rpm-md
skip_if_unavailable=True
gpgcheck=1
gpgkey=https://copr-be.cloud.fedoraproject.org/results/vbatts/bazel/pubkey.gpg
repo_gpgcheck=0
enabled=1
enabled_metadata=1
- yum install bazel
方法2
到 https://github.com/bazelbuild/bazel/releases 下载对应的安装文件 bazel-0.28.1-installer-linux-x86_64.sh
编译
编译前查看机器支持的cpu指令集: gcc -march=native -Q --help=target|grep march
参考 https://tensorflow.google.cn/install/source 安装依赖项
- git clone -b r1.14 https://github.com/tensorflow/tensorflow.git
- cd tensorflow
- ./configure
- bazel build --config=monolithic //tensorflow:libtensorflow_cc.so 或者 bazel build --config=opt //tensorflow:libtensorflow_cc.so 或者 bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4.2 //tensorflow:libtensorflow_cc.so
- sudo apt-get install autoconfig automake
- cd tensorflow/contrib/makefile
- ./build_all_linux.sh
c++调用
- c++需要加入include_directories的头文件:
./tensorflow/tensorflow/
./tensorflow/third_party/
./tensorflow/tensorflow/contrib/makefile/gen/protobuf/include/
./tensorflow/tensorflow/contrib/makefile/downloads/eigen/
./tensorflow/tensorflow/contrib/makefile/downloads/absl/
./tensorflow/bazel-genfiles
./tensorflow/tensorflow/contrib/makefile/gen/proto
./tensorflow/tensorflow/contrib/makefile/gen/host_obj/tensorflow/core - 需要链接的so
./tensorflow/bazel-bin/tensorflow/libtensorflow_cc.so - cpp调用
#include <stdio.h>
#include <iostream>
#include <tensorflow/core/platform/env.h>
#include <tensorflow/core/public/session.h>
using namespace tensorflow;
using namespace std;
int main(int argc, char const *argv[])
{
std::cout << "status" << std::endl;
Session* session;
Status status = NewSession(SessionOptions(), &session);
if (!status.ok())
{
std::cout << status.ToString() << "\n";
return 0;
}
return 0;
}
- 根目录CMAKELIST.txt
cmake_minimum_required(VERSION 2.8)
project( tf_example )
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++11 -W")
include_directories(
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/include/include
${PROJECT_SOURCE_DIR}/include/tensorflow/
${PROJECT_SOURCE_DIR}/include/bazel-genfiles/
${PROJECT_SOURCE_DIR}/include/absl/
${PROJECT_SOURCE_DIR}/include/eigen/
)
link_directories(${PROJECT_SOURCE_DIR}/3rdparty)
set(CODE_DIRS ${PROJECT_SOURCE_DIR}/src)
set(SOURCE_DIR ${CODE_DIRS}/lib)
set(TEST_DIR ${CODE_DIRS}/test)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
add_subdirectory(${TEST_DIR} ${EXECUTABLE_OUTPUT_PATH})
项目git地址
https://github.com/zqp2009happy/tensorflow_c
一些有用的资料
官方手册 https://tensorflow.google.cn/guide/
cpp调用教程项目 https://github.com/rockingdingo/tensorflow-tutorial
官方模型 https://github.com/tensorflow/models
karas手册 https://keras.io/getting-started/functional-api-guide/