ros_indigo_TensorFlow

本文介绍如何使用ROS和TensorFlow进行图像识别,并详细讲解了CUDA的安装过程,包括系统要求验证、设备节点验证及环境配置等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、使用ROS和TensorFlow进行图像识别
安装TensorFlow
TensorFlow是什么?
引用TensorFlow官网的话,TensorFlow是一个“采用数据流图进行数值计算的开源软件库”。其中“数据流图”是什么意思?这是个很酷的东西。在正式回答之前,我们先谈谈一个简单神经网络的结构。
一个简单神经网络由输入层(input units)、隐层(hidden units)、阈值(bias unit)、输出层(output units)几部分构成。输入层负责接收数据。隐层之所以这么叫是因为从用户的角度来看,它们是隐藏的。输出层输出我们获得的结果。旁边的阈值是用来控制隐含层和输出层的值是否输出(即超过阈值的神经元才能输出)。两两不同神经元之间的连接是权重,只是一些数字,需要靠训练获得。
训练神经网络,就是为了给权重找到最佳的值,这让神经网络一步步变得“智能”。在下面这个例子中,输入神经元的值被设置为二进制数字0,0,0。接下来TensorFlow会完成这之间的所有事情,而输出神经元会神奇得包含数字0,0,1。即便你漏掉了,它也知道二进制中000下面一个数是001,001接下来是010,就这样一直到111.一旦权重被设定了合适的值,它将知道如何去计数。
在运行神经网络中有一个步骤是将每一个权重乘以其对应的输入神经元,然后将乘积结果保存在相应的隐藏神经元。
我们可以将这些神经元和权重看作成数列(array),在Python中也被称为列表(list)。从数学的角度来看,它们都是矩阵。图中我们只绘制出了其中一部分,这里将输入层矩阵和权重矩阵相乘,得到五元素隐藏层矩阵(亦称为列表或数列)。

2、CUDA安装
1)安装系统要求

ubuntu14.04 LTS Kernel 3.13 GCC 4.8.2 GLIBC 2.9

根据Nvidia官方文档http://developer.download.nvidia.com/compute/cuda/7.5/Prod/docs/sidebar/CUDA_Installation_Guide_Linux.pdf一步一步安装
2) Verify You Have a CUDA-Capable GPU

$ lspci | grep -i nvidia
04:00.0 3D controller: NVIDIA Corporation GF117M [GeForce 610M/710M/810M/820M / GT 620M/625M/630M/720M] (rev a1)

https://developer.nvidia.com/cuda-gpus验证是否支持

3) Verify You Have a Supported Version of Linux

$ uname -m && cat /etc/*release
x86_64
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
NAME="Ubuntu"
VERSION="14.04.5 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.5 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

4) Verify the System Has gcc Installed

$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

5) Verify the System has the Correct Kernel
Headers and Development Packages Installed

$ uname -r
3.13.0-128-generic
$ sudo apt-get install linux-headers-3.13.0-128-generic

6) Download the NVIDIA CUDA Toolkit
下载对应版本的cuda,从https://developer.nvidia.com/cuda-downloads,下载deb(network)版
cuda-repo-ubuntu1404_8.0.44-1_amd64.deb
或者下载cuda_7.5.18_linux.run
安装前进行MD5校验
md5sum cuda-repo-ubuntu1404_8.0.44-1_amd64.deb
md5sum cuda_7.5.18_linux.run
验证码相同,如果验证码不一样,请重新下载

7) Handle Conflicting Installation Methods

Use the following command to uninstall a Toolkit runfile installation:
$ sudo /usr/local/cuda-X.Y/bin/uninstall_cuda_X.Y.pl
Use the following command to uninstall a Driver runfile installation:
$ sudo /usr/bin/nvidia-uninstall
Use the following commands to uninstall a RPM/Deb installation:
$ sudo apt-get --purge remove <package_name> 

8)Disabling Nouveau

$ lsmod | grep nouveau
如果有内容输出,则需禁掉nouveau
Create a file at  /etc/modprobe.d/blacklist-nouveau.conf
$ sudo vi /etc/modprobe.d/blacklist-nouveau.conf
添加如下内容:
blacklist nouveau
options nouveau modeset=0
$ sudo update-initramfs -u

9) 使用.run进行安装:

Reboot Into Text Mode
关闭图形化界面
$ sudo service lightdm stop
切换到cuda-repo-ubuntu1404_8.0.44-1_amd64.deb的目录,执行
$ sudo sh cuda_7.5.18_linux.run
重启图形化界面
$ sudo service lightdm start
$ sudo reboot

10)安装失败

.deb安装失败的....
$ sudo apt-get --purge remove nvidia*
.run安装失败的....
$ sudo /usr/local/cuda-8.0/bin/uninstall_cuda_8.0.pl
$ sudo /usr/bin/nvidia-uninstall
$ sudo reboot

11) Device Node Verification
$ ls /dev/nvidia*
/dev/nvidia0 /dev/nvidiactl /dev/nvidia-uvm
安装成功

ls: cannot access /dev/nvidia*: No such file or directory
或者只出现/dev/nvidia0 /dev/nvidiactl

$ sudo vi Nka.sh

#!/bin/bash
/sbin/modprobe nvidia
if [ "$?" -eq 0 ]; then
  # Count the number of NVIDIA controllers found.
  NVDEVS=`lspci | grep -i NVIDIA`
  N3D=`echo "$NVDEVS" | grep "3D controller" | wc -l`
  NVGA=`echo "$NVDEVS" | grep "VGA compatible controller" | wc -l`
  N=`expr $N3D + $NVGA - 1`
  for i in `seq 0 $N`; do
    mknod -m 666 /dev/nvidia$i c 195 $i
  done
  mknod -m 666 /dev/nvidiactl c 195 255
else
  exit 1
fi
/sbin/modprobe nvidia-uvm
if [ "$?" -eq 0 ]; then
  # Find out the major device number used by the nvidia-uvm driver
  D=`grep nvidia-uvm /proc/devices | awk '{print $1}'`
  mknod -m 666 /dev/nvidia-uvm c $D 0
else
  exit 1
fi

$ sudo chmod +x Nka.sh
$ sudo ./Nka.sh
$  ls /dev/nvidia*
/dev/nvidia0  /dev/nvidiactl  /dev/nvidia-uvm

或者添加启动脚本(系统启动时,自动加载的)
$ sudo vi /etc/rc.local

如果你是第一次打开这个文件,它应该是空的(除了一行又一行的#注释项外)。这文件的第一行是
#!/bin/sh -e
把-e去掉(这步很重要,否则它不会加载这文本的内容)
然后把Nka.sh的内容除了#!/bin/bash外复制到其中,(before exit 0 )保存退出。

12) Environment Setup

打开系统配置文件
$ sudo vi /etc/profile
在文件最后添加
export PATH=/usr/local/cuda-7.5/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:$LD_LIBRARY_PATH
保存退出,执行
$ source /etc/profile

13) Verify the Driver Version

$ cat /proc/driver/nvidia/version
NVRM version: NVIDIA UNIX x86_64 Kernel Module 352.39 Fri Aug 14 18:09:10 PDT 2015
GCC version: gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)

14) Verify CUDA Toolkit

$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver 
Copyright (c) 2005-2016 NVIDIA Corporation 
Built on Tue_Aug_11_14:27:32_CDT_2015
Cuda compilation tools, release 7.5, V7.5.17

如果是这样的
The program ‘nvcc’ is currently not installed. You can install it by typing:
sudo apt-get install nvidia-cuda-toolkit
确认下/etc/profile的配置环境是否正确

15) Complie sample

cd 进NVIDIA_CUDA-8.0_Samples目录
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery/目录下
$  sudo make
$ ./deviceQuery
显示系统信息和最后一行Result=PASS,证明安装成功
cd /usr/local/cuda-8.0/samples/1_Utilities/bandwidthTest/目录下
$  sudo make
$ ./bandwidthTest
显示系统信息和最后一行Result=PASS,证明安装成功
.run安装的
cd /home/XXX/NVIDIA_CUDA-7.5_Samples
$ make
cd ~/NVIDIA_CUDA-7.5_Samples/bin/x86_64/linux/release
$ ./deviceQuery
$ ./bandwidthTest
sudo dpkg -i cuda-repo-ubuntu1404_8.0.44-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda
deb安装完后,显示正常安装,就是进不去GUI,卸载重装.run

安装nvidia-346驱动

GeForce 800M Series (Notebooks):
GeForce GTX 880M, GeForce GTX 870M, GeForce GTX 860M, GeForce GTX 850M, GeForce 840M, GeForce 830M, GeForce 820M, GeForce 810M
sudo add-apt-repository ppa:xorg-edgers/ppa 
sudo apt-get update 
sudo apt-get install nvidia-346 nvidia-settings

3、安装Cudnn,建议安装版本cudnn-7.0-linux-x64-prod.tgz
https://developer.nvidia.com/rdp/cudnn-archive
cudnn-7.0-linux-x64-v4.0-prod.tgz
解压: tar -zxvf cudnn-7.0-linux-x64-v4.0-prod.tgz -C ~/cudnn
cd ~/cudnn/cudn/include
sudo cp *.h /usr/local/cuda/lib64/
cd ~/cudnn/cudn/lib64
sudo cp lib* /usr/local/cuda/lib64/
cd /usr/local/cuda/lib64/
sudo chmod +r libcudnn.so.4.0.7
sudo ln -sf libcudnn.so.4.0.7 libcudnn.so.4
sudo ln -sf libcudnn.so.4 libcudnn.so
sudo ldconfig

4、TensorFlow安装
二进制安装
TensorFlow Python API 依赖 Python 2.7 版本。
在 Linux 和 Mac 下最简单的安装方式, 是使用 pip 安装。
TensorFlow 的 GPU 特性只支持 NVidia Compute Capability >= 3.5
安装GPU版,请参考CUDA安装
pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl

源码安装
克隆 TensorFlow 仓库
$ git clone –recurse-submodules https://github.com/tensorflow/tensorflow
–recurse-submodules 参数是必须得, 用于获取 TesorFlow 依赖的 protobuf 库.
Linux 安装
安装 Bazel

$ git clone https://github.com/bazelbuild/bazel.git
$ cd bazel
$ git checkout tags/0.1.0
$ ./compile.sh

安装依赖
$ sudo apt-get install python-numpy swig python-dev python-wheel

将执行路径 output/bazel 添加到 $PATH 环境变量中。
cd ~/bazel
sudo cp output/bazel /usr/local/bin
import tensorflow as tf

gph@gph-pc:~/tensorflow-master/tensorflow $ sudo ./configure 
You have bazel 0.5.1- installed.
Please specify the location of python. [Default is /usr/bin/python]: 
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]

Using python library path: /usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with MKL support? [y/N] y
MKL support will be enabled for TensorFlow
Do you wish to download MKL LIB from the web? [Y/n] n
Please specify the location where MKL is installed. [Default is /opt/intel/mklml]: /opt/intel/mkl
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: 
Do you wish to use jemalloc as the malloc implementation? [Y/n] 
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] 
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N] 
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] 
No XLA support will be enabled for TensorFlow
Do you wish to build TensorFlow with VERBS support? [y/N] 
No VERBS support will be enabled for TensorFlow
Do you wish to build TensorFlow with OpenCL support? [y/N] 
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N] y
CUDA support will be enabled for TensorFlow
Do you want to use clang as CUDA compiler? [y/N] 
nvcc will be used as CUDA compiler
Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]: 7.5
Please specify the location where CUDA  toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: 
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 4.0.7
Please specify the location where cuDNN 5.1 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 
Invalid path to cuDNN  toolkit. Neither of the following two files can be found:
/usr/local/cuda-8.0/lib64/libcudnn.so.5.1
/usr/local/cuda-8.0/libcudnn.so.5.1
.5.1
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 4.0.7
Please specify the location where cuDNN 5.1.10 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size.
[Default is: "3.5,5.2"]: 
Do you wish to build TensorFlow with MPI support? [y/N] 
MPI support will not be enabled for TensorFlow
Configuration finished

pip 安装
更新pip
先卸载pip(命令为sudo apt-get remove python-pip)再更新
wget https://bootstrap.pypa.io/get-pip.py –no-check-certificate
sudo python get-pip.py

安装Theano
安装 依赖库
sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ libopenblas-dev git
pip install theano
安装keras
pip install keras==1.2.2

参考备注:
http://tensorfly.cn/tfdoc/get_started/os_setup.html#install_cuda
http://www.jianshu.com/p/35c7fde85968
http://blog.youkuaiyun.com/lenbow/article/details/52118116
http://blog.youkuaiyun.com/yiranyhy/article/details/55259385
https://github.com/fchollet/keras

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值