Linux环境
- 下载tensorflow https://github.com/tensorflow/tensorflow
- 下载并安装bazel https://github.com/bazelbuild/bazel/releases
sh bazel-0.15.0-installer-linux-x86_64.sh
- 进入tensorflow目录./config
sudo mkdir /usr/local/include/tf
sudo cp -r bazel-genfiles/ /usr/local/include/tf/
sudo cp -r tensorflow /usr/local/include/tf/
sudo cp -r third_party /usr/local/include/tf/
sudo cp -r bazel-bin/tensorflow/libtensorflow_cc.so /usr/local/lib/
cd path_tensorflow/tensorflow
依赖等见 https://tensorflow.google.cn/install/install_sources
./configure
bazel build —config=monlithic :libtensorflow_cc.so
如果不加—config=monlithic,编译出来的库会导致cv::imread()为空
这样导入库时只需导入libtensorflow_cc.so,不再需要libtensorflow_framework.so
wget http://bitbucket.org/eigen/eigen/get/3.3.4.tar.bz2
下载之后解压放在重新命名为eigen3,我存放的路径是,/home/lp/projects/safety/tensorflow_c/eigen3
进入根目录后编译
编译生成.so文件, 编译C++ API的库 (建议)
bazel build //tensorflow:libtensorflow_cc.so
bazel build —config=opt —config=cuda —config=monolithic //tensorflow:libtensorflow_cc.so
也可以选择,编译C API的库
bazel build //tensorflow:libtensorflow.so
source /opt/rh/devtoolset-3/enable
bazel build —local_resources 1024,4,1.0 //tensorflow:libtensorflow_cc.so
https://blog.csdn.net/luanpeng825485697/article/details/81152615
出现的问题:
Server terminated abruptly (error code: 14, error message: ‘’, log file:
解决:
‘/root/.cache/bazel/_bazel_root/a1181ec4a71ba55a9
root@ubuntu:~# free -gtotal used free shared buff/cache available
Mem: 15 15 0 0 0 0
Swap: 14 14 0
root@ubuntu:~#
swapoff /tmp/swap 卸载行添加的交换分区
dd if=/dev/zero of=/tmp/swap bs=1MB count=27648 添加27GB 容量
mkswap /tmp/swap 创建交换分区
root@ubuntu:~# swapon /tmp/swap 挂着交换分区
swapon: /tmp/swap: insecure permissions 0644, 0600 suggested.
root@ubuntu:~# free -g 验证 ,之前有3GB 交换分区
total used free shared buff/cache available
Mem: 15 0 0 0 14 14
Swap: 29 0 29
patch command is not found
yum -y install patch
https://tensorflow.google.cn/install/source
https://blog.csdn.net/gubenpeiyuan/article/details/80855644
mkdir /usr/local/include/tf
cp -r bazel-genfiles/ /usr/local/include/tf/
cp -r tensorflow/* /usr/local/include/tf/tensorflow/
cp -r third_party /usr/local/include/tf/
cp -r bazel-bin/tensorflow/*.so /usr/local/lib/
Windows环境
使用github上已编译好的库
https://github.com/fo40225/tensorflow-windows-wheel
上面是github上已经编译好的库,不过C++的库只有v1.7.0到v1.10.0的
自己编译
1.安装vs2015
2.下载tensorflow 1.5.0
3.安装cmake
4.安装SWIG 3.0.12
5.安装python 3.6
所有即将编译的TensorFlow library都在TensorFlow repository下的cmake
目录下。首先我们要在cmake
文件夹下创建一个separate的build
文件夹
Command Prompt
cmake .. -A x64 -DCMAKE_BUILD_TYPE=Release ^
-DSWIG_EXECUTABLE=C:\Users\%USERNAME%\bin\swig\swigwin-3.0.12\swig.exe ^
-DPYTHON_EXECUTABLE=C:\Users\%USERNAME%\Anaconda3\python.exe ^
-DPYTHON_LIBRARIES=C:\Users\%USERNAME%\Anaconda3\libs\python35.lib
-Dtensorflow_ENABLE_GPU=ON ^
-DCUDNN_HOME=”C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0”
-Dtensorflow_BUILD_PYTHON_BINDINGS=OFF ^
-Dtensorflow_ENABLE_GRPC_SUPPORT=OFF
-Dtensorflow_BUILD_SHARED_LIB=ON ^
-DCUDA_HOST_COMPILER=”C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\cl.exe”
https://www.cnblogs.com/jliangqiu2016/p/7642471.html
http://tianyuh.com/2018/09/11/Build-Run-Tensorflow-C-program-on-Windows/
https://joe-antognini.github.io/machine-learning/build-windows-tf
https://blog.csdn.net/jin739738709/article/details/86705735
https://blog.csdn.net/robothn/article/details/86157724
https://blog.csdn.net/qq_35975447/article/details/91986142
https://blog.csdn.net/robothn/article/details/86157724
https://blog.csdn.net/yuejisuo1948/article/details/84197534
https://blog.csdn.net/jacke121/article/details/80473648
https://blog.csdn.net/h8832077/article/details/78988488
https://blog.csdn.net/qq_35975447/article/details/91986142
https://blog.csdn.net/qq_35975447/article/details/91636625
https://blog.csdn.net/robothn/article/details/86157724
https://blog.csdn.net/jin739738709/article/details/86705735
安装MSYS2,地址 https://www.msys2.org/,选 msys2-x86_64-xxxx.exe,按网页下方说明安装到 C:\msys64
- 将目录C:\msys64和C:\msys64\usr\bin 加入系统环境变量path
- 打开cmd.exe,输入一下命令(有时可能安装不了,多试几次好了)
- pacman -Syuu patch —disable-download-timeout
- pacman -Syuu unzip—disable-download-timeout
- pacman -Syuu grep —disable-download-timeout
C:\msys64\etc\pacman.d 如果下载速度慢可以修改C:\msys64\etc\pacman.d下面的文件中的镜像地址顺序
安装 Bazel
在github下载Bazel,https://github.com/bazelbuild/bazel/releases,这里我下载的bazel-0.22.0-windows-x86_64.exe
将下载好的 bazel-0.22.0-windows-x86_64.exe复制到C:\msys64下,更名为bazel.exe。
配置bazel:新建环境变量:BAZEL_SH,BAZEL_VC ,BAZEL_VS
**
下载tensorflow-v1.12
此处参考https://github.com/guikarist/tensorflow-windows-build-script
1.将tensorflow-1.12.0.zip解压到tensorflow-1.12.0目录下,并将目录下的tensorflow文件夹重命名为source;
2.下载tensorflow-windows-build-script的压缩包,解压,将其中的patchs与build.ps.1复制到tensorflow-1.12.0目录下;
将patchs下的eigen_half.patch复制到tensorflow-1.12.0\source\third_party下;
将patchs下的tf_exported_symbols_msvc.lds复制到tensorflow-1.12.0\source\tensorflow下
打开powershell
输入下面命令,这是bazel编译的选项,为不同的语言(python、c、c++)其//后对应的都不一样:
$parameterString = “—config=opt —config=cuda —define=no_tensorflow_py_deps=true —copt=-nvcc_options=disable-warnings //tensorflow:libtensorflow_cc.so —verbose_failures”
然后输入:
- .\build.ps1 `
- -BazelBuildParameters $parameterString `
- -BuildCppAPI -ReserveSource
启动 VS2015 x64 命令行,激活Anaconda环境,在命令行内执行
Command Prompt
cmd.exe “/K” C:\Users\chechaojun\Anaconda3\Scripts\activate.bat
conda create -n py36 python=3.6
conda activate py36
cd /d D:\lib\tensorflow
python configure.py
cd tensorflow
bazel build :libtensorflow_cc.so(BUILD文件tf_cc_shared_object(name=”libtensorflow_cc.so”))
bazel build :libtensorflow_framework.so(BUILD文件tf_cc_shared_object(name=”libtensorflow_framework.so”))
bazel build :install_headers(BUILD文件tf_cc_shared_object(name=”install_headers”))
bazel compiler is out of heap space
Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2