本系列文章由
@yhl_leo
出品,转载请注明出处。
文章链接:
http://blog.youkuaiyun.com/yhl_leo/article/details/51280087
折腾了一下,给工作站配置了Tensorflow环境(虽然已经转战到caffe,但是由于之前体验了tensorflow,觉得有些地方还是很对我的口,两者的编译环境我都选择了Python,如能珠联璧合甚好),以前使用基于Docker的安装配置,略微繁琐,而且没有记录下当时的安装步骤,使用基于pip安装后,觉得真心好简单啊,便记录于此文!Tensorflow 0.8.0 安装配置方法
版本:
- tensorflow:0.8.0 (GitHub: tensorflow)
- Ubuntu:14.04/15.10(亲测都可以)
1 Pip
如果已经安装过pip
,可以跳过这里的安装步骤。$ sudo apt-get install python-pip python-dev python-setuptools build-essential
$ sudo pip install--upgrade pip
$ sudo pip install--upgrade virtualenv
为了检测是否安装好,可以查看pip
的版本$ pip pip
8.1.1from /usr/local/lib/python2.7/dist-packages (python 2.7)
接下来,就可以按照Tensorflow Download and Setup中的Pip Installation
开始安装,命令如下# Ubuntu/Linux 64-bit, CPU only:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled. Requires CUDA toolkit 7.5 and CuDNN v4. For# other versions, see "Install from sources" below.
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
</pre><pre code_snippet_id="1913548" snippet_file_name="blog_20161005_3_9309577" class="prettyprint" name="code" style="white-space: nowrap; word-wrap: break-word; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro', monospace; padding: 5px 5px 5px 60px; font-size: 14px; line-height: 1.45; word-break: break-all; color: rgb(51, 51, 51); border: 1px solid rgba(128, 128, 128, 0.0745098); border-radius: 0px; background-color: rgba(128, 128, 128, 0.0470588);">
本人安装的GPU版,因为在安装tensorflow之前,已经安装过caffe,CUDA的编译环境已经配置好了,网上关于CUDA安装教程很多,这里提供安装caffe时的一篇帖子: Ubuntu14.04 安装CUDA7.5 + Caffe + cuDNN。
3 Errors
安装完成后,就可以测试,是否安装成功$ python>>>import tensorflow as tf>>>
</pre><p style="margin-top:0px; margin-bottom:1.1em; padding-top:0px; padding-bottom:0px; color:rgb(85,85,85); font-family:'microsoft yahei'; font-size:14px; line-height:35px">如果输入命令后,结果跟上述的差不多,(GPU版的会有一些log信息),没有出现Error,那就说明安装好了,就可以测试使用啦。</p><p style="margin-top:0px; margin-bottom:1.1em; padding-top:0px; padding-bottom:0px; color:rgb(85,85,85); font-family:'microsoft yahei'; font-size:14px; line-height:35px">在<a target=_blank target="_blank" href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/pip_package/setup.py" style="text-decoration:none; color:rgb(12,137,207)">tensorflow/tools/pip_package/setup.py</a>中,关于几个包(<code style="font-family:'Source Code Pro',monospace; padding:2px 4px; font-size:12.6px; color:rgb(63,63,63); white-space:nowrap">numpy</code> , <code style="font-family:'Source Code Pro',monospace; padding:2px 4px; font-size:12.6px; color:rgb(63,63,63); white-space:nowrap">six</code>, <code style="font-family:'Source Code Pro',monospace; padding:2px 4px; font-size:12.6px; color:rgb(63,63,63); white-space:nowrap">protobuf</code>)的版本等都有规定,如果不满足,则编译代码就会报错:</p><pre code_snippet_id="1913548" snippet_file_name="blog_20161005_5_2406477" class="prettyprint" name="code" style="white-space: nowrap; word-wrap: break-word; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro', monospace; padding: 5px 5px 5px 60px; font-size: 14px; line-height: 1.45; word-break: break-all; color: rgb(51, 51, 51); border: 1px solid rgba(128, 128, 128, 0.0745098); border-radius: 0px; background-color: rgba(128, 128, 128, 0.0470588);">
出问题比较多的是protobuf
和six
两个包,例如:
<code class="language-sh hljs python has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span> google.protobuf <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> symbol_database <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">as</span> _symbol_database
ImportError: cannot <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> name symbol_database</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
protobuf
如果已经安装了其他版本(例如python-protobuf或低版本),先要卸载掉:
<code class="language-sh hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> apt-get remove python-protobuf
$ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> pip uninstall protobuf</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
然后安装:
<code class="language-sh hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> pip install --upgrade protobuf</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
<code class="hljs vhdl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">AttributeError: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">type</span> object <span class="hljs-attribute" style="box-sizing: border-box;">'NewBase</span>' has no <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">attribute</span> <span class="hljs-attribute" style="box-sizing: border-box;">'is_abstract</span>' </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
出现这个问题,应该是six
包安装有问题,可以卸载原有版本,重新安装:
<code class="language-sh hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ sudo pip uninstall <span class="hljs-constant" style="box-sizing: border-box;">six</span>
$ sudo pip install <span class="hljs-constant" style="box-sizing: border-box;">six</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--upgrade</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
一般来说six
包的安装位置是/usr/lib/python2.7/dist-packages
,但是我在笔记本上默认安装在/usr/local/lib/python2.7/dist-packages
也没问题,建议先试前者(工作站上也是前者):
<code class="language-sh hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> pip install six --upgrade --target=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/lib/python2.7/dist-packages"</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
关于GPU版,CUDA安装及编译问题,不在此作答~
4 Uninstall
如果想卸载掉安装的tensorflow,非常简单:
ubuntu15.10 源码安装 tensorflow
安装上最新的ubuntu系统。按照官网的流程安装的,没有选择GPU支持。
1. 下载源码。需要git,如果没有安装git需要先安装。
git clone --recurse-submodules https://github.com/tensorflow/tensorflow
1. 安装Bazel
先安装依赖
sudo apt-get install openjdk-8-jdk openjdk-8-source
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip
下载安装文件:
https://github.com/bazelbuild/bazel/releases/download/0.1.1/bazel-0.1.1-installer-linux-x86_64.sh
chmod +x install-version-os.sh
./install-version-os.sh --user
安装程序会将bazel安装到$HOME/bin目录下,需要把这个目录加入PATH
$ export PATH="$PATH:$HOME/bin"
安装其他依赖
apt-get install python-numpy swig python-dev
1. 使用pip编译并安装
bazel build -c opt tensorflow/tools/pip_package:build_pip_package
这行命令要一个相对目录,且:后面的需要跟该目录下的BUILD文件中的sh_binary的name字段保持一致
mkdir /tmp/tensorflow_pkg
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install/tmp/tensorflow_pkg/tensorflow-0.5.0-py2-none-any.whl