1. 程式人生 > >Ubuntu16.04+TensorFlow r1.12環境搭建指南

Ubuntu16.04+TensorFlow r1.12環境搭建指南

一、作業系統安裝

OS版本:Ubuntu 16.04 (ubuntu-16.04.5-server-amd64.iso)

CPU:4Core以上

記憶體:4GB以上

磁碟空間:80G以上

 

二、基礎環境準備

1、為了提高apt安裝速度,更新apt為國內阿里雲源

(1)複製原檔案備份 
sudo cp /etc/apt/source.list /etc/apt/source.list.bak

(2)編輯源列表檔案

sudo vim /etc/apt/source.list

(3)將原來的列表刪除,新增如下內容

deb http://mirrors.aliyun.com/ubuntu/ vivid main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ vivid-security main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ vivid-updates main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ vivid-proposed main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ vivid-backports main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ vivid main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ vivid-security main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ vivid-updates main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ vivid-proposed main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ vivid-backports main restricted universe multiverse

(4)執行sudo apt-get update

2、為了提高pip安裝速度,更新pip為國內阿里雲源

mkdir ~/.pip

vi ~/.pip/pip.conf

編輯pip.conf檔案:

[global] index-url = http://mirrors.aliyun.com/pypi/simple/

[install] trusted-host = mirrors.aliyun.com

3、安裝gnome桌面(顯示視訊視窗需要)

sudo apt install ubuntu-gnome-desktop

4、安裝git

sudo apt install git

5、加配SWAP到4GB以上(避免編譯過程中分配記憶體失敗,如果已經有超過4GB的swap空間,或者安裝作業系統過程中已配置較大swap,可跳過這步)

sudo dd if=/dev/zero of=/swap bs=4096 count=1M

sudo mkswap /swap

sudo swapon /swap

sudo echo "/swap swap  swap  sw 0   0"  >> /etc/fstab

 

三、安裝編譯工具Bazel

1、安裝依賴包

sudo apt-get install pkg-config zip g++ zlib1g-dev unzip

2、下載Bazel安裝包

wget https://github.com/bazelbuild/bazel/releases/download/0.18.1/bazel-0.18.1-installer-linux-x86_64.sh

3、安裝Bazel

chmod +x bazel-0.18.1-installer-linux-x86_64.sh

./ bazel-0.18.1-installer-linux-x86_64.sh --user

4、設定環境變數

sudo vi ~/.bashrc

在檔案最後新增:

export PATH=$PATH":~/bin"

source ~/.bashrc

四、安裝TensorFlowflow

1、安裝virtualenv、virtualenvwrapper

pip install --user virtualenv

pip install --user virtualenvwrapper

mkdir -p ~/Envs

sudo vi ~/.bashrc

在檔案最後新增:

export WORKON_HOME=~/Envs

source /usr/local/bin/virtualenvwrapper.sh

source ~/.bashrc

建立虛擬環境:mkvirtualenv tfenv

進入虛擬環境:workon tfenv

退出虛擬環境:deactivate

2、安裝依賴包:

(1)準備Python依賴

pip install -U six numpy wheel mock portpicker scipy futures scikit-learn grpcio grpcio-tools enum34

pip install -U keras_applications==1.0.5 --no-deps

pip install -U keras_preprocessing==1.0.3 --no-deps

pip install -U --upgrade setuptools

easy_install distribute

pip install -U --upgrade distribute

修正虛擬環境中的pkg_resources包

workon tfenv

cdsitepackages

vi pkg_resources/__init__.py

找到:from . import py31compat

替換為:from pkg_resources import py31compat

rm -f pkg_resources/__init__.pyc

(2)準備go環境

移除老版本:sudo apt remove golang-go

安裝go1.10

sudo add-apt-repository ppa:gophers/archive

sudo apt-get update

sudo apt-get install golang-1.10-go

設定go環境變數

sudo vi ~/.bashrc

在檔案最後新增:

找到export PATH=$PATH":~/bin"

替換為:export PATH="$PATH:~/bin:/usr/lib/go-1.10/bin"

source ~/.bashrc

3、準備TensorFlow原始碼

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

cd tensorflow

git checkout r1.12  # r1.9, r1.10, etc.

4、編譯前測試

bazel test -c opt -- //tensorflow/... -//tensorflow/compiler/... -//tensorflow/contrib/lite/...

如果告警提示支援avx、avx2、fma、sse4.1、sse4.2,則使用如下命令:

bazel test -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma  --copt=-msse4.1 --copt=-msse4.2 --copt=-mfpmath=both  -- //tensorflow/... -//tensorflow/compiler/... -//tensorflow/contrib/lite/...

5、編譯配置

在tensorflow原始碼根目錄執行:

./configure

配置如下:

 

6、Build the pip package

bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

7、Build the package

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

8、Install the package

pip install /tmp/tensorflow_pkg/tensorflow-version-tags.whl

 

參考:

https://www.tensorflow.org/install/source?hl=zh-cn

https://docs.bazel.build/versions/master/install-ubuntu.html

https://github.com/golang/go/wiki/Ubuntu