几篇值得参考的博文:
[1] https://blog.csdn.net/robothn/article/details/86157724
[2] https://blog.csdn.net/yz2zcx/article/details/83106669
[3] https://blog.csdn.net/yuejisuo1948/article/details/84197534
[4] https://zhuanlan.zhihu.com/p/34942873
[5] https://www.cnblogs.com/hrlnw/p/7383951.html
[6] http://www.cnblogs.com/hrlnw/p/7007648.html
[7] https://blog.csdn.net/gzt940726/article/details/81053378
[8] https://blog.csdn.net/luoyexuge/article/details/80399265
[9] https://joe-antognini.github.io/machine-learning/windows-tf-project
形式上基本雷同,这里主要参考的还是
[1] https://blog.csdn.net/robothn/article/details/86157724
接下来,进入正文:
欢迎趟坑,如果珍惜所谓效率的,请直接看Bazel编译!
运行cmake-gui,configure出错如下:
tf_core_ops.cmake报错:clustering_ops.cc文件没有找到,解决方法:cmake文件中修改路径
tf.core_kernels.cmake报错:clustering_ops.cc文件没有找到,解决方法:cmake文件中修改路径
接下来是,abseil_cpp.cmake文件的修改
(你自己创建的build文件)\abseil_cpp\src\abseil_cpp_build\absl下所有lib文件包含进来,
不然VS编译abseil_cpp项目和后面所有*gen_cc项目都会出错
if(WIN32)
if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
set(abseil_cpp_STATIC_LIBRARIES
${abseil_cpp_BUILD}/absl/base/Release/absl_base.lib
${abseil_cpp_BUILD}/absl/base/Release/absl_internal_spinlock_wait.lib
${abseil_cpp_BUILD}/absl/base/Release/absl_dynamic_annotations.lib
${abseil_cpp_BUILD}/absl/base/Release/absl_internal_malloc_internal.lib
${abseil_cpp_BUILD}/absl/base/Release/absl_internal_throw_delegate.lib
${abseil_cpp_BUILD}/absl/container/Release/absl_internal_hashtablez_sampler.lib
${abseil_cpp_BUILD}/absl/container/Release/absl_raw_hash_set.lib
${abseil_cpp_BUILD}/absl/debugging/Release/absl_demangle_internal.lib
${abseil_cpp_BUILD}/absl/debugging/Release/absl_failure_signal_handler.lib
${abseil_cpp_BUILD}/absl/debugging/Release/absl_internal_debugging_internal.lib
${abseil_cpp_BUILD}/absl/debugging/Release/absl_internal_examine_stack.lib
${abseil_cpp_BUILD}/absl/debugging/Release/absl_leak_check.lib
${abseil_cpp_BUILD}/absl/debugging/Release/absl_leak_check_disable.lib
${abseil_cpp_BUILD}/absl/debugging/Release/absl_stacktrace.lib
${abseil_cpp_BUILD}/absl/debugging/Release/absl_symbolize.lib
${abseil_cpp_BUILD}/absl/hash/Release/absl_hash.lib
${abseil_cpp_BUILD}/absl/hash/Release/absl_internal_city.lib
${abseil_cpp_BUILD}/absl/numeric/Release/absl_int128.lib
${abseil_cpp_BUILD}/absl/strings/Release/absl_strings.lib
${abseil_cpp_BUILD}/absl/strings/Release/absl_internal_str_format_internal.lib
${abseil_cpp_BUILD}/absl/strings/Release/absl_internal_strings_internal.lib
${abseil_cpp_BUILD}/absl/synchronization/Release/absl_internal_graphcycles_internal.lib
${abseil_cpp_BUILD}/absl/synchronization/Release/absl_synchronization.lib
${abseil_cpp_BUILD}/absl/time/Release/absl_time.lib
${abseil_cpp_BUILD}/absl/time/Release/absl_internal_time_zone.lib
${abseil_cpp_BUILD}/absl/time/Release/absl_internal_civil_time.lib
${abseil_cpp_BUILD}/absl/types/Release/absl_bad_optional_access.lib
${abseil_cpp_BUILD}/absl/types/Release/absl_bad_variant_access.lib
${abseil_cpp_BUILD}/absl/types/Release/absl_internal_bad_any_cast_impl.lib
${abseil_cpp_BUILD}/absl/types/Release/absl_optional.lib)
链接:https://pan.baidu.com/s/1fPH88RVbDhjEn5sBoR8LDQ
提取码:33wu
添加至tensorflow-master\tensorflow\core\util\
tensorflow-master\tensorflow\core\kernels\unicode_ops.cc(65): error C2440: “初始化”: 无法从“const UChar *”转换为“const char16_t *”
解决方法:强制转换类型,const char16_t* buf = (char16_t*)in.getBuffer();
报错:auto squeeze_result = Squeeze(scope, matmul_result, Squeeze::Axis(axis));
解决方法:auto squeeze_result = Squeeze(scope, matmul_result, Squeeze::SqueezeDims(axis));
打开cmake-gui
配置项目:
生成项目:
接下来就是,用VS2015(管理员)打开tensorflow.sln文件
每一次编译都要重新下载downloads文件里面的文件
链接:https://pan.baidu.com/s/1mB7Wpp8Uw8OSpdJVqJSDog (下载downloads文件夹里面的文件)
提取码:tk4t
但是这还不够,编译eigen这个项目,下载和解压文件的动作主要由
download-eigen.cmake和extract-eigen.cmake,这两个文件执行的
如此的话,就进去cmake文件里面对应位置进行修改
比如download-eigen.cmake
这一段就是download-eigen.cmake中判断是否重新下载的语段,但是不知道为什么has_hash这个值每次都是0(具体原因我也没有深究),所以每一次都会出现
File already exists but no hash specified (use URL_HASH):
file='E:/tensorflow-master/vs14_project/downloads/ad3bcd81cc49.tar.gz'
Old file will be removed and new file downloaded from URL.
所以,就在这句message后面,加上return(),
并且注释掉file(REMOVE "E:/tensorflow-master/vs14_project/downloads/ad3bcd81cc49.tar.gz")
同时,修改extract-eigen.cmake,为啥要修改呢?
因为解压出来的eigen文件夹中的CMakeLists文件有错误,只有阻止解压,然后修改CMakeLists,具体后面再讲
其实阻止解压很简单,加上return()就可以了
(你自己创建的build文件)\eigen\src\eigen\CMakeLists.txt文件修改:注释掉红框中的语句
不知道为啥,报错的原因居然是cmake没有add_subdirectory()这条语句,好在这个语句没啥用,注释掉就好了
链接:https://pan.baidu.com/s/1PxNVZCgTyE0GHOfZFF3tcg (修改好的eigen文件夹,直接替换对应文件和文件夹就好了)
提取码:fh3n
tf_core_kernels编译确实很久
而且有几个地方会出错,
比如,fatal error C1060: 编译器的堆空间不足,是因为VS2015的 cl.exe 是并行编译多个源文件的,每个分配了单独线程,所以需要减少并行数来减少内存占用,在编译此项目时,需要把蓝色处改为 1
有成功编译的,但是再编译tensorflow、grpc_tensorflow_server、summarize_graph等项目又出了很多奇怪的错误
心疼自己1秒(冷漠)
此时,我已经花了4天在这个玩意上了,因为编译要好久,都是晚上搞,隔天回来看结果,
每次看到满屏的错误,真是怀疑人生。。。。
[1]中的 VS2015 x64 命令行,指的就是 vs2015 x64本机工具命令提示符(管理员)
其实可以简单到你不敢相信
1)启动 VS2015 x64 命令行,激活Anaconda环境,在命令行内执行
cmd.exe "/K" D:\Users\Leon_PC\Anaconda3\Scripts\activate.bat
2)切换虚拟环境
conda activate tensorflow_gpu
3)配置项目
cd /d D:\lib\tensorflow(tensorflow_master目录)
python configure.py
4)编译项目:libtensorflow_cc.so、libtensorflow_framework.so和install_headers
编译之前,去到\tensorflow-master\tensorflow\BUILD文件里面好好看清楚
需要编译的是libtensorflow_cc.so、libtensorflow_framework.so和install_headers三个项目
install_headers项目会把所有H文件汇总,方便后面引用
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"))
这是后面编译好了之后再编译一次的结果,因为没有改动,所以没有弹出特别多信息
libtensorflow_cc.so第一次编译成功的结果如下:
最后看到
满满的成就感有木有
顺便提一句,bazel-bin文件夹是要你运行bazel build命令后自动生成的
本来以为我就可以轻松愉快地走向人生巅峰了,没想到后面的几个问题让我又搞了2天
果然,成年人的世界里,没有容易二字
1)H文件引用问题
这个比较好解决,只要前面记得编译install_headers
后面,C/C++->常规->附加包含目录,加上以下几句,就基本没有问题了
D:\\lib\\tensorflow\\bazel-genfiles\\tensorflow\\include
D:\\lib\\tensorflow\\bazel-genfiles
D:\\lib\\tensorflow\\bazel-bin\\tensorflow
D:\\lib\\tensorflow\\bazel-genfiles\\tensorflow\\include\\external\\com_google_absl
D:\\lib\\tensorflow\\bazel-genfiles\\tensorflow\\include\\external\\protobuf_archive\\src
2)lib文件的链接问题(参考[9]:Additional Command Line Options)
首先,你要bazel-bin中所有*.lib文件都列出来,这需要写一个批量处理程序
链接:https://pan.baidu.com/s/15dhDeolpapzMH7lGOjAblg (这里我都列出来了,自取吧)
提取码:s1fx
然后,链接器->输入->附加依赖项添加*.lib,链接器->常规->附加库目录添加lib路径
最后,链接器->命令行->其他选项添加/WHOLEARCHIVE:*.lib,
这里的*.lib是指bazel-bin\tensorflow\core\文件下所有lib文件以及子目录下的所有lib文件
链接:https://pan.baidu.com/s/1LlnnbfuzkzuIxDsHrePnnA (WHOLEARCHIVE格式文件)
提取码:puyg
不然的话,你会遇到
错误:Non-OK-status: status status: Not found: Op type not registered ‘NoOp’ in binary running on .
3)运行结果
随便跑了一个程序,
#include
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
int main()
{
using namespace tensorflow;
using namespace tensorflow::ops;
Scope root = Scope::NewRootScope();
//Matrix A = [3 2; -1 0]
auto A = Const(root, { { 3.f, 2.f },{ -1.f, 0.f } });
//Vector b = [3 5]
auto b = Const(root, { { 3.f, 5.f } });
//v = Ab^T
auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
std::vector outputs;
ClientSession session(root);
//Run and fetch v
TF_CHECK_OK(session.Run({ v }, &outputs));
//Expect outputs[0] == [19; -3]
LOG(INFO) << outputs[0].matrix();
return 0;
}
启动vs2015 x64本机工具命令提示符(管理员)
打开*.sln,配置好环境之后编译
之所以使用vs2015 x64本机工具命令提示符,是因为vs默认的是32位的link.exe,
鉴于我们这个牛逼的TensorFlow需要链接非常非常多库才可以运行起来,所以要用64位的link.exe
不然会出现错误:链接器堆空间不足
解决方法:VS2015 x64 本机工具命令提示符,打开xxx.sln,再编译;
参考:https://blog.csdn.net/andr0id/article/details/83899973
最后,运行结果如下。
不容易啊