鲲鹏KC1云服务器源码方式安装Tensorflow 1.13.1

举报
大铁锹 发表于 2020/01/21 17:29:03 2020/01/21
【摘要】 基于源码在鲲鹏KC1主机中安装Tensorflow1.13.1版本

Tensorflow介绍

TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组,即张量(tensor)。它灵活的架构让你可以在多种平台上展开计算,例如台式计算机中的一个或多个CPU(或GPU),服务器,移动设备等等。TensorFlow 最初由Google大脑小组(隶属于Google机器智能研究机构)的研究员和工程师们开发出来,用于机器学习和深度神经网络方面的研究,但这个系统的通用性使其也可广泛用于其他计算领域   

                       -----摘自:Tensorflow中文社区

二、环境说明:

虚拟机规格

4vCPUs | 8GB | kc1.xlarge.2

OS

Ubuntu 18.04 64bit with ARM

Tensorflow版本

1.13.1

Bazel版本

0.21.0

 

二、源码安装Tensorflow 1.13.1

1.       使用Bazel来编译Tensorflow

   bazelGoogle开源的一套编译构建工具,广泛应用于Google内部,包括TensorFlow项目。通过源码安装Tnesorflow需要使用bazel来编译。bazel主要有构建快、支持增量编译。对依赖关系进行优化支持并发执行。

2.       安装Bazel

1)      安装依赖

apt-get install zlib1g-dev zlib1g python3-numpy  python3-pip zip python3-h5py -y

2)      下载Bazel源码包

cd /home/

wget https://github.com/bazelbuild/bazel/releases/download/0.21.0/bazel-0.21.0-dist.zip

3)      编译和安装Bazel

mkdir bazel

cd bazel

unzip ../bazel-0.21.0-dist.zip

./compile.sh

4)      配置Bazel 环境变量

vi /etc/profile

增加如下命令,/home/bazel/outputBazel可执行程序所在目录

export PATH=$PATH:/home/bazel/output

使得环境变量生效

source /etc/profile

5)      检验Bazel是否安装成功

执行bazel info,返回如下信息表示安装成功

root@ecs-tensorflow:/home/bazel# bazel info

            INFO: Invocation ID: a5032da4-a24e-40bc-818f-dce9cd8fb4f8            
            bazel-bin: /root/.cache/bazel/_bazel_root/e734f6186ab13df7bf19bd5560bf9af8/execroot/io_bazel/bazel-out/aarch64-fastbuild/bin
            bazel-genfiles: /root/.cache/bazel/_bazel_root/e734f6186ab13df7bf19bd5560bf9af8/execroot/io_bazel/bazel-out/aarch64-fastbuild
            /genfiles
            bazel-testlogs: /root/.cache/bazel/_bazel_root/e734f6186ab13df7bf19bd5560bf9af8/execroot/io_bazel/bazel-out/aarch64-fastbuild
            /testlogs
            character-encoding: file.encoding = ISO-8859-1, defaultCharset = ISO-8859-1
            command_log: /root/.cache/bazel/_bazel_root/e734f6186ab13df7bf19bd5560bf9af8/command.log
            committed-heap-size: 318MB

 

3.       安装Tensorflow 1.13.1

1)      下载Tensorflow源码

cd /home/

wget https://github.com/tensorflow/tensorflow/archive/v1.13.1.tar.gz

2)      解压和配置Tensorflow

tar -zxvf tensorflow-1.13.1.tar.gz

cd tensorflow-1.13.1/

./configure

3)      配置如下

root@ecs-tensorflow:/home/tensorflow-1.13.1# ./configure

            Extracting Bazel installation...            
            WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown"
             
            INFO: Invocation ID: 85323fa4-fb98-4bfb-b72f-9819dd1c7bb2
            You have bazel 0.21.0- (@non-git) installed.
            Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3
             
             
            Found possible Python library paths:
              /usr/lib/python3/dist-packages
              /usr/local/lib/python3.6/dist-packages
            Please input the desired Python library path to use.  Default is [/usr/lib/python3/dist-packages]
             
            Do you wish to build TensorFlow with XLA JIT support? [Y/n]: n
            No XLA JIT support will be enabled for TensorFlow.
             
            Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: N
            No OpenCL SYCL support will be enabled for TensorFlow.
             
            Do you wish to build TensorFlow with ROCm support? [y/N]: 
            No ROCm support will be enabled for TensorFlow.
             
            Do you wish to build TensorFlow with CUDA support? [y/N]: N
            No CUDA support will be enabled for TensorFlow.
             
            Do you wish to download a fresh release of clang? (Experimental) [y/N]: N
            Clang will not be downloaded.
             
            Do you wish to build TensorFlow with MPI support? [y/N]: N
            No MPI support will be enabled for TensorFlow.
             
            Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=
            native -Wno-sign-compare]: 
             
            Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: N
            Not configuring the WORKSPACE for Android builds.


4)      安装clang

cd /home/tensorflow-1.13.1/

apt-get install clang-8 clang-8++

由于gcc 自身与Tnesorflow兼容性问题,导致gcc无法编译tensorflow 1.13.1,所以考虑采用clang来编译。

备注:Clang 采用的是 BSD 协议的许可证,而 GCC 采用的是 GPL 协议,前者更为宽松;Clang 是一个高度模块化开发的轻量级编译器,编译速度快、占用内存小、有着友好的出错提示。

5)      编译Tensorflow

设置Clang环境变量

export CXX=clang++-8

export CC=clang-8

编译Tensorflow命令如下:

bazel build --config=noaws //tensorflow/tools/pip_package:build_pip_package --verbose_failures --copt=-funsafe-math-optimizations --copt=-march=armv8-a --copt=-mtune=cortex-a53 --copt=-mfpu=neon-fp-armv8 --copt=-mfloat-abi=hard

备注:--copt=-march=armv8-a 因为鲲鹏是基于Armv8指令集,所以架构配置如上。--copt=-mfpu=neon-fp-armv8,基于ARM加速指令集为neon

出现如下所示表示编译成功

        Target //tensorflow/tools/pip_package:build_pip_package up-to-date:        
        bazel-bin/tensorflow/tools/pip_package/build_pip_package
        INFO: Elapsed time: 1075.854s, Critical Path: 112.95s
        INFO: 3602 processes: 3602 local.
        INFO: Build completed successfully, 3785 total actions

6)     生成Tensorflow安装包

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

生成的whl包会存在于/tmp/tensorflow_pkg下

        root@ecs-tensorflow:/home/tensorflow-1.13.1# bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg        
        Tue Jan 21 17:10:41 CST 2020 : === Preparing sources in dir: /tmp/tmp.jBwqnmAzov
        /home/tensorflow-1.13.1 /home/tensorflow-1.13.1
        /home/tensorflow-1.13.1
        Tue Jan 21 17:10:47 CST 2020 : === Building wheel
        warning: no files found matching '*.pyd' under directory '*'
        warning: no files found matching '*.pd' under directory '*'
        warning: no files found matching '*.dll' under directory '*'
        warning: no files found matching '*.lib' under directory '*'
        warning: no files found matching '*.h' under directory 'tensorflow/include/tensorflow'
        warning: no files found matching '*' under directory 'tensorflow/include/Eigen'
        warning: no files found matching '*.h' under directory 'tensorflow/include/google'
        warning: no files found matching '*' under directory 'tensorflow/include/third_party'
        warning: no files found matching '*' under directory 'tensorflow/include/unsupported'
        Tue Jan 21 17:11:04 CST 2020 : === Output wheel file is in: /tmp/tensorflow_pkg


7)     安装Tensorflow的依赖库

pip3 install -i https://mirrors.huaweicloud.com/repository/pypi/simple numpy cython wheel

备注:https://mirrors.huaweicloud.com/repository/pypi/simple 表示中间过程依赖可以采用华为镜像站加速下载

pip3 install Keras_Preprocessing

pip3 install Keras

安装scipy依赖库

cd /home/

wget https://media.githubusercontent.com/media/Iamanorange/RK3399Pro-Wheels/master/scipy-1.2.1-cp36-cp36m-linux_aarch64.whl

pip3 install scipy-1.2.1-cp36-cp36m-linux_aarch64.whl

 

8)     安装Tensorflow

解除clang和clang++临时环境变量

 unset CC

 unset CXX

root@ecs-tensorflow:/tmp# unset CC

root@ecs-tensorflow:/tmp# unset CXX

root@ecs-tensorflow:/tmp#

在步骤6) 中生成Tensorflow安装包存放于/tmp/tensorflow_pkg

cd /tmp/tensorflow_pkg/

root@ecs-tensorflow:/tmp/tensorflow_pkg# ll

total 63468

drwxr-xr-x  2 root root     4096 Jan 21 17:11 ./

drwxrwxrwt 12 root root     4096 Jan 21 17:13 ../

-rw-r--r--  1 root root 64980674 Jan 21 17:11 tensorflow-1.13.1-cp36-cp36m-linux_aarch64.whl

pip安装Tensorflow:

pip3 install -i https://mirrors.huaweicloud.com/repository/pypi/simple tensorflow-1.13.1-cp36-cp36m-linux_aarch64.whl

9)     至此安装完成,时间大概持续数小时

10)  验证Tensorflow

root@ecs-tensorflow:/tmp/tensorflow_pkg# python3

Python 3.6.9 (default, Nov  7 2019, 10:44:02)

[GCC 8.3.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import tensorflow as tf

ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'

ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'

>>> tf.__version__

'1.13.1'

>>> 


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200