Ubuntu14.04源码编译安装CPU版本的tensorflow

来源:互联网 发布:传奇npc算法 编辑:程序博客网 时间:2024/05/22 10:35

Ubuntu14.04源码编译安装CPU版本的tensorflow

说明:由于某种原因,我们需要编译tensorflow的源码包来进行安装

环境说明:

os:Ubuntu 14.04

arch:x86_64

kernel:4.2.0-27-generic


tensorflow的一些知识可以访问官网,一般被墙了。可以在hosts文件中添加

$sudo vi  /etc/hosts

64.233.188.121       www.tensorflow.org

  • 提前安装第三方依赖包

1.安装anaconda(Python一个科学计算的发行版,里面含有许多现成的库)

官网(https://www.continuum.io/downloads)下载相关的版本,进入下载路径执行

$bash Anaconda-xxxx-linux-x86_64.sh      ;;xxxx--为你自己下载的版本号

之后按下enter进入阅读协议,按q结束阅读,输入yes,之后会让你选择路径,默认回车,等待安装,之后会提示是否把它添加到.bashrc文件,选yes

2.安装编译用到的包

$sudo apt-get install  python-dev  python-wheel python-pip python-setuptools ipython

3.安装Java

  • 去官网下载Java   下载地址
  • 解压下载的包到/usr/local

$cd  下载目录

$tar xvf jdk-xxx-linux-x64.tar.gz  -C /usr/local

  • 将解压后的目录添加到.bashrc中

$vi ~/.bashrc

#java
export JAVA_HOME=/usr/local/jdk_xxxx
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
$source ~/.bashrc

$java -version

4.安装编译tensorflow的工具bazel

下载bazel-sh文件,下拉鼠标,找到download一栏,选择自己需要的安装包,我选择的是bazel-installer_linux_x86_64.sh执行命令

$chmod +x bazel-<version>-installer-<os>.sh

$./bazel-<version>-installer-<os>.sh --user       ;;不要使用sudo执行,不然会报Java没安装,除非你把刚安装的Java环境变量添加到root的.bashrc

$vi ~/.bashrc     ;;将~/bin添加到环境变量中,如 我的是 /home/xzy/bin

$source ~/.bashrc

$bazel  version    ;;验证这个命令是否正常判断bazel是否安装成功


  • 下载解压源码包

$git clone https://github.com/tensorflow/tensorflow

$cd tensorflow-xxxx

$./configure         ;;新版本已经不止这些选项,我的版本是1.1.0

1.选择Python路径,默认是你安装的anaconda的Python路径,直接回车

2.CPU的优化选项,直接回车默认(-march=native)

3.选择是否使用jemalloc作为malloc的实现,linux用户默认选yes

4.Google云平台的支持,国内网被墙了,选no就行

5.如果有分布式Hadoop应用,选yes,建议选yes,难免以后万一有这应用需求呢

6.是否开启XLA、AOT VERBOS,这些东西还不太成熟,建议选no

7.选Python的library路径,默认是anaconda的路径,直接回车

8.是否使用GPU支持,建议初学着和显卡不是英伟达的在OpenGL和cuda处选择no,gpu支持涉及到cuda和cuDNN的安装,过程繁杂,本文讲的是CPU版,选no

等待完成配置

  • 编译安装tensorflow

注意,此处编译在不需要代理的网络环境下进行

1.使用bazel编译,此步大约编译40来分钟,编译大约2684个项目,期间可能会卡死机重启再来,另外网络不要设置proxy代理,

$ bazel build --copt=-march=native -c opt //tensorflow/tools/pip_package:build_pip_package   ;;如果已安装bazel去无法执行这条命令,提示bazel命令找不到的话,不要使用sudo

可能swap太小会编译错误

ERROR: /home/xzy/tensorflow/tensorflow/core/kernels/BUILD:2083:1: C++ compilation of rule '//tensorflow/core/kernels:svd_op' failed: gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 '-D_FORTIFY_SOURCE=1' -DNDEBUG ... (remaining 117 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 4.
gcc: internal compiler error: Killed (program cc1plus)

解决办法,增加swap的大小,在原来的基础上增加512M

# 生成swap镜像文件$sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512# 对该镜像文件格式化$ sudo mkswap /mnt/512Mb.swap# 挂载该镜像文件 $ sudo swapon /mnt/512Mb.swap#使用free -m查看$free -m

2.上述编译完成的话,生成pip安装包

$bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

3.安装tensorflow

$sudo pip install /tmp/tensorflow_pkg/tensorflow-1.1.0rc2-cp27-cp27mu-linux_x86_64.whl

安装可能会遇到如下错误

tensorflow-1.1.0rc2-cp27-cp27mu-linux_x86_64.whl is not a supported wheel on this platform.
Storing debug log for failure in /home/xzy/.pip/pip.log


这是因为pip版本的缘故,解决办法是下载get-pip.py 文件,进入下载目录执行

$sudo python2.7 get-pip.py

再次安装即可,第4步后面的内容是我执行安装的日志输出

4.tensorflow安装验证

$python

>>>import tensorflow as tf     ;;看这句是否正常执行,是,安装成功,否,安装失败

可能会出现下面的错误

ImportError: Traceback (most recent call last):
  File "tensorflow/python/pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
ImportError: No module named pywrap_tensorflow_internal

Failed to load the native TensorFlow runtime.


解决办法是不要在tensorflow目录下运行python或者ipython 验证tensorflow,换个目录即可

如果要是有Opencv处理图片的库,直接执行命令

sudo apt-get install python-opencv libopencv-dev python-numpy python-dev

然后终端输入命令验证

$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>



The directory '/home/xzy/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/xzy/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Processing /tmp/tensorflow_pkg/tensorflow-1.1.0rc2-cp27-cp27mu-linux_x86_64.whl
Collecting six>=1.10.0 (from tensorflow==1.1.0rc2)
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading six-1.10.0-py2.py3-none-any.whl
Collecting protobuf>=3.2.0 (from tensorflow==1.1.0rc2)
  Downloading protobuf-3.2.0-cp27-cp27mu-manylinux1_x86_64.whl (5.6MB)
    100% |████████████████████████████████| 5.6MB 61kB/s
Collecting mock>=2.0.0 (from tensorflow==1.1.0rc2)
  Downloading mock-2.0.0-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 161kB/s
Collecting html5lib==0.9999999 (from tensorflow==1.1.0rc2)
  Downloading html5lib-0.9999999.tar.gz (889kB)
    100% |████████████████████████████████| 890kB 108kB/s
Collecting werkzeug>=0.11.10 (from tensorflow==1.1.0rc2)
  Downloading Werkzeug-0.12.1-py2.py3-none-any.whl (312kB)
    100% |████████████████████████████████| 317kB 112kB/s
Collecting bleach==1.5.0 (from tensorflow==1.1.0rc2)
  Downloading bleach-1.5.0-py2.py3-none-any.whl
Collecting numpy>=1.11.0 (from tensorflow==1.1.0rc2)
  Downloading numpy-1.12.1-cp27-cp27mu-manylinux1_x86_64.whl (16.5MB)
    100% |████████████████████████████████| 16.5MB 41kB/s
Collecting markdown==2.2.0 (from tensorflow==1.1.0rc2)
  Downloading Markdown-2.2.0.tar.gz (236kB)
    100% |████████████████████████████████| 245kB 186kB/s
Requirement already satisfied: wheel in /usr/lib/python2.7/dist-packages (from tensorflow==1.1.0rc2)
Requirement already satisfied: setuptools in /usr/lib/python2.7/dist-packages (from protobuf>=3.2.0->tensorflow==1.1.0rc2)
Collecting funcsigs>=1; python_version < "3.3" (from mock>=2.0.0->tensorflow==1.1.0rc2)
  Downloading funcsigs-1.0.2-py2.py3-none-any.whl
Collecting pbr>=0.11 (from mock>=2.0.0->tensorflow==1.1.0rc2)
  Downloading pbr-3.0.0-py2.py3-none-any.whl (98kB)
    100% |████████████████████████████████| 102kB 253kB/s
Installing collected packages: six, protobuf, funcsigs, pbr, mock, html5lib, werkzeug, bleach, numpy, markdown, tensorflow
  Found existing installation: six 1.5.2
    DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling six-1.5.2:
      Successfully uninstalled six-1.5.2
  Found existing installation: html5lib 0.999
    Uninstalling html5lib-0.999:
      Successfully uninstalled html5lib-0.999
  Running setup.py install for html5lib ... done
  Found existing installation: numpy 1.8.2
    DEPRECATION: Uninstalling a distutils installed project (numpy) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling numpy-1.8.2:
      Successfully uninstalled numpy-1.8.2
  Running setup.py install for markdown ... done
Successfully installed bleach-1.5.0 funcsigs-1.0.2 html5lib-0.9999999 markdown-2.2.0 mock-2.0.0 numpy-1.12.1 pbr-3.0.0 protobuf-3.2.0 six-1.10.0 tensorflow-1.1.0rc2 werkzeug-0.12.1
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning

  • 参考文献

学习tensorflow使用源码安装     http://blog.csdn.net/helei001/article/details/51285951

tensorflow github  https://github.com/tensorflow/tensorflow

bazel 官方安装文档   https://bazel.build/versions/master/docs/install-ubuntu.html

Ubuntu安装Java    http://www.linuxidc.com/Linux/2015-01/112030.htm

TensorFlow编译过程中遇到的问题及解决方案   http://blog.csdn.net/sydpz1987/article/details/51493844


0 0
原创粉丝点击