Ubuntu 15.04 安装TensorFlow(源码编译) 及测试梵高作画
介紹Google的TensorFlow機(jī)器學(xué)習(xí)開(kāi)源庫(kù),在UbuntuKylin上的安裝和和源碼編譯。
原始官方文檔參見(jiàn):http://www.tensorflow.org.
本電腦配置如下:
3.19.0-15-generic #15-Ubuntu x86_64 GNU/Linux NVIDIA Corporation GK110BGL [Tesla K40c] NVIDIA Corporation GK110GL [Quadro K5200] Python 2.7 Cuda toolkit = 7.5 cuDNN = 7.5 v5 gcc = 4.9 g++ = 4.9 Bazel = 0.4.4TensorFlow學(xué)習(xí)資源推薦
tensorflow中文入門教程-含視頻
tensorflow入門視頻教程-含互動(dòng)
tensorflow中文社區(qū)
TensorFlow 官方文檔中文版
TensorFlow在圖像識(shí)別中的應(yīng)用
本文是在安裝caffe之后,繼續(xù)安裝TensorFlow,下面有些CUDA和 CUDNN的安裝可見(jiàn) Caffe + Ubuntu 15.04 + CUDA 7.5 在服務(wù)器上安裝配置及卸載重新安裝(已測(cè)試可執(zhí)行)
安裝TensorFlow的Requirements
Python 2.7 and Python 3.3+Cuda toolkit >= 7.0 cuDNN >= v3gcc > 4.8g++ > 4.8 Bazel > 0.4.2一、安裝依賴包
1. 安裝Tensorflow python API
sudo apt-get install python-pip python-dev sudo apt-get install python-numpy swig python-dev sudo apt-get install Git
2. 安裝 Bazel
TensorFlow Serving requires Bazel 0.4.2 or higher,Bazel的安裝可見(jiàn)官網(wǎng)。
OpenJDK做為GPL許可(GPL-licensed)的Java平臺(tái)的開(kāi)源化實(shí)現(xiàn),Sun正式發(fā)布它已經(jīng)六年有余。從發(fā)布那一時(shí)刻起,Java社區(qū)的大眾們就又開(kāi)始努力學(xué)習(xí),以適應(yīng)這個(gè)新的開(kāi)源代碼基礎(chǔ)(code-base)。 [1] OpenJDK在2013年發(fā)展迅速,被著名IT雜志SD Times評(píng)選為2013 SD Times 100,位于“極大影響力”分類第9位。http://www.infoq.com/cn/news/2015/03/google-open-source-bazel Google日前開(kāi)源了他們內(nèi)部使用的構(gòu)建工具Bazel。 Bazel是一個(gè)類似于Make的工具,是Google為其內(nèi)部軟件開(kāi)發(fā)的特點(diǎn)量身定制的工具,如今Google使用它來(lái)構(gòu)建內(nèi)部大多數(shù)的軟件。它的功能有諸多亮點(diǎn): 多語(yǔ)言支持:目前Bazel默認(rèn)支持Java、Objective-C和C++,但可以被擴(kuò)展到其他任何變成語(yǔ)言。高級(jí)構(gòu)建描述語(yǔ)言:項(xiàng)目是使用一種叫BUILD的語(yǔ)言來(lái)描述的,它是一種簡(jiǎn)潔的文本語(yǔ)言,它把一個(gè)項(xiàng)目視為一個(gè)集合,這個(gè)集合由一些互相關(guān)聯(lián)的庫(kù)、二進(jìn)制文件和測(cè)試用例組成。相反,像Make這樣的工具,需要去描述每個(gè)文件如何調(diào)用編譯器。多平臺(tái)支持:同一套工具和相同的BUILD文件可以用來(lái)為不同的體系結(jié)構(gòu)構(gòu)建軟件,甚至是不同的平臺(tái)。在Google,Bazel被同時(shí)用在數(shù)據(jù)中心系統(tǒng)中的服務(wù)器應(yīng)用和手機(jī)端的移動(dòng)應(yīng)用上。可重復(fù)性:在BUILD文件中,每個(gè)庫(kù)、測(cè)試用例和二進(jìn)制文件都需要明確指定它們的依賴關(guān)系。當(dāng)一個(gè)源碼文件被修改時(shí),Bazel憑這些依賴來(lái)判斷哪些部分需要重新構(gòu)建,以及哪些任務(wù)可以并行進(jìn)行。這意味著所有構(gòu)建都是增量的,并且相同構(gòu)建總是產(chǎn)生一樣的結(jié)果。可伸縮性:Bazel可以處理大型項(xiàng)目;在Google,一個(gè)服務(wù)器軟件有十萬(wàn)行代碼是很常見(jiàn)的,在什么都不改的前提下重新構(gòu)建這樣一個(gè)項(xiàng)目,大概只需要200毫秒。JDK8的安裝(必須的)
sudo apt-get install openjdk-8-jdk openjdk-8-source sudo apt-get install pkg-config zip g++ zlib1g-dev unzip sudo add-apt-repository ppa:webupd8team/java #添加倉(cāng)庫(kù) sudo apt-get update #更新軟件列表 sudo apt-get install oracle-java8-installer #正式安裝jdk8 java -version # 驗(yàn)證安裝2.1 安裝 Bazel-方法1
echo “deb http://storage.googleapis.com/bazel-apt stable jdk1.8” | sudo tee /etc/apt/sources.list.d/bazel.list curl https://storage.googleapis.com/bazel-apt/doc/apt-key.pub.gpg | sudo apt-key add - sudo apt-get update sudo apt-get install bazel sudo apt-get upgrade bazel bazel version
2.2 安裝 Bazel-方法2
Bazel 下載鏈接
cd ~/Downloads chmod +x bazel-0.4.5-installer-linux-x86_64.sh #對(duì).sh文件授權(quán) ./bazel-0.4.5-installer-linux-x86_64.sh --user #運(yùn)行.sh文件 bazel version設(shè)置環(huán)境變量
export PATH="$PATH:$HOME/bin"可能出現(xiàn)的問(wèn)題
W: 無(wú)法下載 http://storage.googleapis.com/bazel-apt/dists/stable/InRelease Unable to find expected entry ‘jdk1.8/binary-i386/Packages’ in Release file (Wrong sources.list entry or malformed file) E: Some index files failed to download. They have been ignored, or old ones used instead. 的錯(cuò)誤
解決方法
sudo gedit /etc/apt/sources.list.d/bazel.list 將deb http://storage.googleapis.com/bazel-apt stable jdk1.8修改為deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8
3. CUDA和 CUDNN的安裝,在 Linux 上開(kāi)啟 GPU 支持
為了編譯并運(yùn)行能夠使用 GPU 的 TensorFlow, 需要先安裝 NVIDIA 提供的 Cuda Toolkit 7.5 和 CUDNN 7.5 V5
TensorFlow 的 GPU 特性只支持 NVidia Compute Capability >= 3.5 的顯卡. 被支持的顯卡 包括但不限于
NVidia TitanNVidia Titan XNVidia K20NVidia K40可見(jiàn) Caffe + Ubuntu 15.04 + CUDA 7.5 在服務(wù)器上安裝配置及卸載重新安裝(已測(cè)試可執(zhí)行)
二、Ubuntu/Linux直接安裝
# 僅使用 CPU 的版本 $ pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp27-none-linux_x86_64.whl # 開(kāi)啟 GPU 支持的版本 (安裝該版本的前提是已經(jīng)安裝了 CUDA sdk) $ pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-1.0.1-cp27-none-linux_x86_64.whl
三、源碼編譯
TensorFlow 源碼安裝官方教程
3.1 克隆 TensorFlow 倉(cāng)庫(kù)
git clone --recurse-submodules https://github.com/tensorflow/tensorflow #拉取源代碼
–recurse-submodules 參數(shù)是必須得, 用于獲取 TesorFlow 依賴的 protobuf 庫(kù)
3.2 配置 TensorFlow 的 Cuba 選項(xiàng)
cd tensorflow ./configure # 配置tensorflow
執(zhí)行configure的時(shí)候會(huì)問(wèn)你問(wèn)題
Please specify the location of python. [Default is /usr/bin/python] Please specify optimization flags to use during compilation [Default is -march=native] Do you wish to use jemalloc as the malloc implementation? [Y/N] y Do you wish to build TensorFlow with Google Cloud Platform support? [Y/N] y Do you wish to build TensorFlow with Hadoop File System support? [Y/N] y Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [Y/N] y Do you wish to build TensorFlow with OpenCL support? [Y/N] n Do you wish to build TensorFlow with CUDA support? [Y/N] y若 Do you wish to build TensorFlow with OpenCL support? [Y/N] 中選擇 y,則需要安裝 OpenCL drivers 和 ComputeCpp compiler,具體步驟可參考
Optional: Install OpenCL (Experimental, Linux only)
tensorflow-opencl
否則,會(huì)出現(xiàn)如下一直循環(huán)的情況。
3.3 編譯
mkdir /tmp/tensorflow_pkg
3.3.1 僅 CPU 支持,無(wú) GPU 支持
cd tensorflow bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
出現(xiàn)的問(wèn)題
The 'build' command is only supported from within a workspace
解決方法
cd tensorflow
3.3.2 有 GPU 支持
cd tensorflow bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
3.3.3 生成 pip安裝包
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
cd 到 /tmp/tensorflow_pkg目錄下,找到編譯好的whl文件
cd /tmp/tensorflow_pkg sudo pip install --config=cuda tensorflow-1.0.1-cp27-none-linux_x86_64.whl3.3.4 編譯目標(biāo)程序, 開(kāi)啟 GPU 支持
bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainerbazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu
四、設(shè)置TensorFlow環(huán)境
cd tensorflow bazel build -c opt //tensorflow/tools/pip_package:build_pip_package# To build with GPU support: bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package mkdir _python_build cd _python_build ln -s ../bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow/* . ln -s ../tensorflow/tools/pip_package/* . sudo python setup.py develop
五、測(cè)試TensorFlow
import tensorflow as tf hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello)) Hello, TensorFlow!
a = tf.constant(10) b = tf.constant(32) print(sess.run(a+b)) 42
用tensorflow實(shí)現(xiàn)梵高作畫(huà)
1. neural-style下載在這個(gè)[github網(wǎng)站下載相應(yīng)代碼]
2. 下載vgg19
3. 將imagenet-vgg-verydeep-19.mat復(fù)制到neural-style的文件夾根目錄下
cp -r imagenet-vgg-verydeep-19.mat /home/bids/neural-style-master/4. 執(zhí)行梵高作畫(huà)
python neural_style.py –content ./example/xxx.jpg (此括號(hào)內(nèi)不要復(fù)制:xxx代表你想要使用的圖片名稱) –styles ./example/ 1-style.jpg(此括號(hào)內(nèi)不要復(fù)制:1-style.jpg是梵高星空?qǐng)D片在文件夾內(nèi)名稱) –output ./example/yyy.jpg (yyy代表你想要生成的圖片名稱)
cd neural-style-master python neural_style.py –content ./example/1-content.jpg --styles ./example/1-style.jpg --output ./example/1-output.jpg六、出現(xiàn)的問(wèn)題
gcc 版本 -fno-canonical-system-headers
當(dāng)執(zhí)行
./configure出現(xiàn)如下問(wèn)題
INFO: Found 1 target... Slow read: a 51765952-byte read from /home/bids/.cache/bazel/_bazel_bids/5df0e0fb624204ab1c5ce0472e695b94/external/local_config_cuda/cuda/lib/libcurand.so.7.5 took 9675ms. INFO: From Compiling external/llvm/lib/Support/Host.cpp: external/llvm/lib/Support/Host.cpp: In function 'llvm::StringRef llvm::sys::getHostCPUName()': external/llvm/lib/Support/Host.cpp:898:5: warning: 'Type' may be used uninitialized in this function [-Wuninitialized] external/llvm/lib/Support/Host.cpp:964:7: warning: 'Subtype' may be used uninitialized in this function [-Wmaybe-uninitialized] ERROR: /home/bids/.cache/bazel/_bazel_bids/5df0e0fb624204ab1c5ce0472e695b94/external/llvm/BUILD:1667:1: C++ compilation of rule '@llvm//:support' 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 43 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1. In file included from external/llvm/lib/Support/DynamicLibrary.cpp:16:0: external/llvm/include/llvm/ADT/DenseSet.h:226:16: error: 'using llvm::DenseSet<ValueT, ValueInfoT>::BaseT::BaseT' conflicts with a previous declaration external/llvm/include/llvm/ADT/DenseSet.h:223:39: note: previous declaration 'using BaseT = class llvm::detail::DenseSetImpl<ValueT, llvm::DenseMap<ValueT, llvm::detail::DenseSetEmpty, ValueInfoT, llvm::detail::DenseSetPair<ValueT> >, ValueInfoT>' external/llvm/include/llvm/ADT/DenseSet.h:244:16: error: 'using llvm::SmallDenseSet<ValueT, InlineBuckets, ValueInfoT>::BaseT::BaseT' conflicts with a previous declaration external/llvm/include/llvm/ADT/DenseSet.h:241:18: note: previous declaration 'using BaseT = class llvm::detail::DenseSetImpl<ValueT, llvm::SmallDenseMap<ValueT, llvm::detail::DenseSetEmpty, InlineBuckets, ValueInfoT, llvm::detail::DenseSetPair<ValueT> >, ValueInfoT>' Target //tensorflow/tools/pip_package:build_pip_package failed to build Use --verbose_failures to see the command lines of failed build steps. INFO: Elapsed time: 54.671s, Critical Path: 28.01s bids@bids-HP-Z840-Workstation:~/tensorflow$ bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package WARNING: /home/bids/tensorflow/tensorflow/contrib/learn/BUILD:15:1: in py_library rule //tensorflow/contrib/learn:learn: target '//tensorflow/contrib/learn:learn' depends on deprecated target '//tensorflow/contrib/session_bundle:exporter': Use SavedModel Builder instead. WARNING: /home/bids/tensorflow/tensorflow/contrib/learn/BUILD:15:1: in py_library rule //tensorflow/contrib/learn:learn: target '//tensorflow/contrib/learn:learn' depends on deprecated target '//tensorflow/contrib/session_bundle:gc': Use SavedModel instead. INFO: Found 1 target... ERROR: /home/bids/.cache/bazel/_bazel_bids/5df0e0fb624204ab1c5ce0472e695b94/external/zlib_archive/BUILD.bazel:5:1: C++ compilation of rule '@zlib_archive//:zlib' failed: crosstool_wrapper_driver_is_not_gcc failed: error executing command external/local_config_cuda/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc -U_FORTIFY_SOURCE '-D_FORTIFY_SOURCE=1' -fstack-protector -fPIE -Wall -Wunused-but-set-parameter ... (remaining 37 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1. gcc: error: unrecognized command line option '-fno-canonical-system-headers' Target //tensorflow/tools/pip_package:build_pip_package failed to build Use --verbose_failures to see the command lines of failed build steps. INFO: Elapsed time: 4.726s, Critical Path: 1.88s解決方法:
這是因?yàn)間cc 版本的問(wèn)題。因之前安裝caffe 所需的gcc版本為4.7,故升級(jí)到4.9版本即可。可參考
Porting to GCC 4.7
Caffe + Ubuntu 15.04 + CUDA 7.5 在服務(wù)器上安裝配置及卸載重新安裝(已測(cè)試可執(zhí)行)
問(wèn)題 Oracle JDK 8 is not installed
當(dāng)執(zhí)行如下
sudo apt-get install openjdk-8-jdk openjdk-8-source出現(xiàn)如下錯(cuò)誤
download failed Oracle JDK 8 is NOT installed. dpkg: error processing package oracle-java8-installer (--configure):subprocess installed post-installation script returned error exit status 1 Errors were encountered while processing:oracle-java8-installer E: Sub-process /usr/bin/dpkg returned an error code (1)解決方法: 這是因?yàn)閛racle-java8-installer 不能下載或者下載不完整導(dǎo)致的。
手動(dòng)下載,見(jiàn)鏈接。
cp -r jdk-8u121-linux-x64.tar.gz /var/cache/oracle-jdk8-installer/ sudo apt-get install oracle-jdk8-installer問(wèn)題 TensorFlow ImportError: cannot import name pywrap_tensorflow
當(dāng)執(zhí)行如下
cd tensorflowimport tensorflow as tf出現(xiàn)如下錯(cuò)誤
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "tensorflow/__init__.py", line 23, in <module>from tensorflow.Python import *File "tensorflow/python/__init__.py", line 48, in <module>from tensorflow.python import pywrap_tensorflowImportError: cannot import name pywrap_tensorflow解決方法: 這是因?yàn)閜ython誤以為tensorflow目錄中的tensorflow就是要導(dǎo)入的模塊
不要在tensorflow中運(yùn)行python或者ipython
更改keras的backend 設(shè)置 tensorflow,theano
sudo gedit ~/.keras/keras.jsonTheano為后端
{"image_dim_ordering": "th", "epsilon": 1e-07, "floatx": "float32", "backend": "theano" }Tensorflow為后端
{"image_dim_ordering": "tf", "epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow" }參考文獻(xiàn):
TensorFlow源碼編譯-基于Ubuntu 15.04
TensorFlow 研究實(shí)踐 一
Ubuntu安裝Bazel
官網(wǎng)教程 Installing Bazel
搭建Tensorflow虛擬機(jī)學(xué)習(xí)環(huán)境
TensorFlow的安裝
TensorFlow 從入門到精通(一):安裝和使用
ubuntu16.04下安裝TensorFlow(GPU加速)—-詳細(xì)圖文教程
Ubuntu: Oracle JDK 8 is NOT installed
教你從頭到尾利用DL學(xué)梵高作畫(huà):GTX 1070 cuda 8.0 tensorflow gpu版
總結(jié)
以上是生活随笔為你收集整理的Ubuntu 15.04 安装TensorFlow(源码编译) 及测试梵高作画的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2016 亚洲共识指南:肺结节的评估
- 下一篇: 为谁而炼金新手攻略 初始选择及强力角色推