tensorflow的搭载与安装
本文主要包含如下内容:
Ubuntu下搭载tensoflow
github官方教程,可以参照官方教程和本篇博客进行安装。本篇博客旨在教会你怎么在自己的系统下搭载tensoflow,并且相关配置,然后tensoflow运用于目标检测任务。
在安装tensorflow之前,请注意相应的CUDA、CUDNN版本,以及所需的NVIDIA驱动。现阶段官方推荐的是CUDA8.0、CUDNN6.0.21,安装教程详见之前博客。在相关软件安装完成后,执行以下代码即可:
pip install tensorflow-gpu==1.4.0
pip3 install tensorflow-gpu==1.4.0
随后安装相应的环境包(请注意pip3还是pip,即python版本):
sudo pip install pillow
sudo pip install lxml
sudo pip install jupyter
sudo pip install matplotlib
sudo pip3 install pillow
sudo pip3 install lxml
sudo pip3 install jupyter
sudo pip3 install matplotlib
现在,tensorflow已经安装完成,接下来检测tensorflow是否安装完成:
ling@ling-All-Series:~$ python
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-12-17 13:59:47.609694: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2017-12-17 13:59:47.835068: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2017-12-17 13:59:47.835366: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.6575
pciBusID: 0000:01:00.0
totalMemory: 10.91GiB freeMemory: 10.46GiB
2017-12-17 13:59:47.835379: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
>>> print(sess.run(hello))
Hello, TensorFlow!
若安装不成功,详情请参考官方安装教程。tensorflow官方安装教程
从源码安装 tensorflow
首先,需要安装Bazel:Bazel官方英文教程,可以参考官方教程和本篇博客进行安装。
安装JDK 8 , 随后配置openjdk 8为默认java环境,在终端中输入:
sudo apt-get install openjdk-8-jdk
sudo update-alternatives --config java
sudo update-alternatives --config javac
下载 Bazel 源码:版本为bazel-0.9.0-dist.zip
,解压之后在解压后目录运行:
sudo ./compile.sh
sudo cp output/bazel /usr/local/bin
随后安装TensorFlow Python依赖项,在终端输入(请注意pip3还是pip,即python版本):
sudo apt-get install python-numpy python-dev python-pip python-wheel
sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel
安装libcupti-dev:
sudo apt-get install libcupti-dev
接下来,配置configure脚本,cd到tensorflow的顶层目录:
cd tensorflow
sudo ./configure
在配置configure脚本之后,编译目标程序:
sudo bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
sudo bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
其中,bazel build命令构建一个名为build_pip_package的脚本。随后运行此脚本将在/tmp/tensorflow_pkg目录内建立一个.whl文件。
现在,可以通过pip安装生成的tensorflow了,如果已经从pip源安装TF,首先执行卸载:
sudo pip uninstall tensorflow
sudo pip install /tmp/tensorflow_pkg/tensorflow-1.4.0-cp27-cp27mu-linux_x86_64.whl
现在,tensorflow已经安装完成,接下来检测tensorflow是否安装完成:
ling@ling-All-Series:~$ python
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-12-17 13:59:47.609694: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2017-12-17 13:59:47.835068: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2017-12-17 13:59:47.835366: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.6575
pciBusID: 0000:01:00.0
totalMemory: 10.91GiB freeMemory: 10.46GiB
2017-12-17 13:59:47.835379: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
>>> print(sess.run(hello))
Hello, TensorFlow!
将tensorflow应用于目标检测
在安装tenserflow的基础上,若想将tensorflow应用于目标检测任务,则还需要接下来的配置:首先,需要获取tensorflow的models模型,可在github网站下载源码
接下来,可编译如下步骤:由于Tensorflow Object Detection API
使用Protobufs来配置模型和训练参数。在可以使用框架之前,必须编译Protobuf库。这应该通过从tensorflow / models / research /目录运行以下命令来完成:
# From tensorflow/models/research/
protoc object_detection/protos/*.proto --python_out=.
# From tensorflow/models/research/ # Add Libraries to PYTHONPATH:
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
测试是否安装完成:
python object_detection/builders/model_builder_test.py
目标检测测试代码object_detection_tutorial.ipynb
在完成了以上操作之后,接下来测试是否安装成功,首先安装ipython:
sudo pip install ipython --upgrade sudo pip install jupyter
以两种方式运行文件,若有图像显示,则说明该tsortflow可以应用于图像检测。
第一种方法:
ipython ./object_detection_tutorial.ipynb
第二种方法:运用ipython notebook打开网页,加载object_detection_tutorial.ipynb,并将其保存为对应的python文件,随后,执行文件:
python ./object_detection_tutorial.py
其中,第二种方法会报错,请在文件object_detection_tutorial.py
中注释掉get_ipython().run_line_magic('matplotlib', 'inline')
,并在代码最后加上plt.show()
即可。