[关闭]
@kalluwa 2018-04-13T01:49:42.000000Z 字数 4202 阅读 3876

如何在windows下面编译tensorflow

1.准备

  1. 软件准备
  1. swig

  2. python3.5,注意加入到环境变量中,不要3.6

  3. git,用来下载tensorflow源码

  4. cmake,推荐最新版

  1. 下载
  1. git clone -b "r1.3" https://github.com/tensorflow/tensorflow.git
  1. 其他
    修改tensorflow/tensorflow/contrib/cmake/CMakeLists.txt中87到93行代码如下
  1. if (tensorflow_OPTIMIZE_FOR_NATIVE_ARCH)
  2. include(CheckCXXCompilerFlag)
  3. CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_OPT_ARCH_NATIVE_SUPPORTED)
  4. if (COMPILER_OPT_ARCH_NATIVE_SUPPORTED)
  5. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
  6. else()
  7. CHECK_CXX_COMPILER_FLAG("/arch:AVX" COMPILER_OPT_ARCH_AVX_SUPPORTED)
  8. if(COMPILER_OPT_ARCH_AVX_SUPPORTED)
  9. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX")
  10. endif()
  11. endif()
  12. endif()

2.编译

2.1
打开cmake定位到tensorflow/contrib/cmake
config后会提示错误,填入swig.exe的路径
如下图所示
图片标题

图片标题

注,这个图像是一个cpu版本的图像,gpu版本还有cuda选项

去掉不必要的数据选项,我这里只保留了gpu,contrib_lib等几个关键的选项,不要选择AVX等cpu相关的选项,会多很多的麻烦。

这里需要注意的是虽然你安装了CUDA,这里还是会提示cuda_sdk_dir没有找到,其实这个没有关系,他已经找到8.0的toolkit了。
2.2
generate后,看到sln文件,打开了项目
点击build all solution,泡杯茶

图片标题

......

......

......

一个钟头后,不出意外的出现了几千个编译错误

图片标题
来一起看error

2.2.0

re2编译不通过,这个错误出现在*test.cc的文件里面,里面有unicode字符,但是怎么unicode就会导致编译错误,不知道怎么解决,两种解决办法,找到re2的cmakelist.txt文件把里面的test选相开关关掉,我没找到,所以我在https://zhuanlan.zhihu.com/p/29029860这里的评论中找到了新办法,把电脑的区域和语言改成美国和英语就可以了。这样在编译re2控制台会显示乱码,但是能正常编译。

2.2.1

tensorflow\core\lib\gtl\array_slice_internal.h(89): error C2064: 项不会计算为接受 0 个参数的函数

tensorflow\core\lib\gtl\array_slice_internal.h(89): error C2064: term does not evaluate to a function taking 0 arguments

额这个问题的原因不容易发现,这也是我在这里卡了3个月之久的罪魁祸首,我翻遍各大网站,最终在tensorflow的issue里面找到了解决方法:

升级到update3,别搞vs2017、update5等等别的,就要update3。 CUDA也别整别的,8.0+cudnn6.0

我们来看看官方文档 github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/cmake/README.md 说的些啥:

图片标题

并没有update的任何信息,不知道他们是怎么编译成功的,所以还是升级吧

再编译,继续出错

2.2.2

version_info.cc not being generated on windows

具体可以看看这个
https://stackoverflow.com/questions/44071608/compiling-tensorflow-with-cmake-on-windows-fails-with-file-version-info-cc-not-f
他说是一个已知的bug,已经修复,起初我以为是python.exe没有加进来,试了又试,发现不对,python.exe早就已经加进来了,tensorflow上的
https://github.com/tensorflow/tensorflow/pull/9660/commits/c0b3c4f7975dd5bad82fba2507ef5430c40bbac1这里说已经解决,无奈,这回真不知道怎么办了,

看看输出,里面有一个gen_git_source.py的文件名,我找到里面的函数get_git_version, 看了下git --version后
改成

  1. def get_git_version(git_base_path):
  2. return b"2.7.3"
  3. "你没看错,直接返回,简单粗暴"
  4. "就这么一句,超级有效"
  5. """Get the git version from the repository.
  6. This function runs `git describe ...` in the path given as `git_base_path`.
  7. This will return a string of the form:
  8. <base-tag>-<number of commits since tag>-<shortened sha hash>
  9. For example, 'v0.10.0-1585-gbb717a6' means v0.10.0 was the last tag when
  10. compiled. 1585 commits are after that commit tag, and we can get back to this
  11. version by running `git checkout gbb717a6`.
  12. Args:
  13. git_base_path: where the .git directory is located
  14. Returns:
  15. A bytestring representing the git version
  16. """
  17. unknown_label = b"unknown"
  18. #with open("testpp.txt","w") as file:
  19. # file.write(git_base_path)
  20. # file.close()
  21. #git_base_path = "E:/tf1_3"
  22. try:
  23. val = bytes(subprocess.check_output([
  24. "git", str("--git-dir=%s/.git" % git_base_path),
  25. str("--work-tree=" + git_base_path), "describe", "--long", "--tags"
  26. ]).strip())
  27. return val if val else unknown_label
  28. except subprocess.CalledProcessError:
  29. return unknown_label

这个问题解决了

2.2.3 堆内存不足

error 1002 Faltal Error "compiler is out of heap space"

如果上面都解决了,在进行build solution的话,极有可能编译不通过(修改一下,应该去掉极有可能,我尝试过各种各样的搭配情况,无论是cpu还是gpu,加还是不加python binding,build之后无一例外的出现的了这个问题),我是16G内存了,还不够。或许你们加到32G的内存就能解决问题了

图片标题

这篇文章(https://zhuanlan.zhihu.com/p/29029860)并没有说明如何解决这个问题,所以我猜测加内存就能够解决这个问题。

这时候没法加内存的我只能放弃vs2015来进行编译了(误),打开vs2015自带的命令行工具:

图片标题

cd到编译目录(也就是build目录),输入

  1. set PreferredToolArchitecture=x64

然后开始编译

  1. MSBuild /p:Configuration=Release ALL_BUILD.vcxproj

喝个一二三四五...n杯水,慢慢等待
几个钟头后你就会得到真正编译成功的tensorflow.dll了
如果失败,重新编译一次。
还是失败,重复上面的话

图片标题

如果上面的操作还是不能解决所有的问题,那,,,,你还是赶紧放弃吧,出去溜达溜达,别折腾这破玩意儿了。

注:实际编译过程中没有保留图像,借用了其他文章的图像

Q: 最新都到1.7版了,你怎么编个1.3的,跟不上主流啊?

A: 1.3到1.7也才几个月的时间,而且最新的编译器来问题更多,我见过不少1.5编译失败的例子,里面的问题没法解决。而且从1.4还是1.5开始就开始用cuda9来编译了,这个没法解决,其他人都在还用cuda8了,而且高版本的现在还不支持windows(见 https://zhuanlan.zhihu.com/p/30781460)
https://github.com/tensorflow/tensorflow/issues/18450
https://github.com/tensorflow/tensorflow/issues/18363
编译时最好不要选CPU加速
https://github.com/tensorflow/tensorflow/issues/17945

附上使用编译的c++ sample出来的结果:
测试图像

图片标题

分类结果
https://leanote.com/api/file/getImage?fileId=5acf22dcab64413fe50018d2

参考:
1. https://joe-antognini.github.io/machine-learning/build-windows-tf
2. http://quqixun.com/?p=785
3. https://www.cnblogs.com/jliangqiu2016/p/7642471.html
4. https://zhuanlan.zhihu.com/p/29029860
5. 官网的编译指导,最好不要使用(修正:最好 -> 务必)

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注