首先更新下系统:
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
第一步我们要安装和cuda版本匹配的Nvidia驱动,加假如电脑安装了旧版本的驱动,使用如下命令卸载它:
sudo apt-get remove nvidia*
sudo apt-get autoremove
安装依赖项:
sudo apt-get install dkms build-essential linux-headers-generic apt-show-versions
接着要将系统默认的'nouveau'驱动加入黑名单:
sudo gedit /etc/modprobe.d/blacklist-nouveau.conf
将以下内容添加到该文件中:
blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off
输入以下命令以关闭nouveau内核:
echo options nouveau modeset=0 | sudo tee -a /etc/modprobe.d/nouveau-kms.conf
sudo update-initramfs -u
下载和cuda匹配的驱动,并赋予其执行权限:
chmod +x NVIDIA-Linux******************.run
继续安装依赖项:
sudo apt-add-repository ppa:ubuntu-x-swat/x-updates #if it fails, you may need to repeat the operation.
sudo apt-get update
sudo apt-get install nvidia-current -y
sudo apt-get upgrade -y
注销退出图形界面,Ctrl+Alt+F1进入命令行登录,输入service lightdm stop关闭图形管理器,然后安装nvidia驱动:
sudo ./NVIDIA-Linux******************.run
- You may have a warning "The distribution-provided pre-install script failed! Are you sure you want to continue ?" => You can continue.
- Register the kernel module with DKMS => YES !
- If you don't have 32bits compatibility you will get a warning => OK !
- Run NVIDIA-Xconfig => YES !
重启系统:
sudo reboot
使用nvidia-smi命令检查安装是否成功:
root@machine: nvidia-smi +------------------------------------------------------+ | NVIDIA-SMI ***.** Driver Version: ***.** | |-------------------------------+----------------------+----------------------+ | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | |===============================+======================+======================| | 0 GeForce GTX TIT... Off | 0000:05:00.0 Off | N/A | | 22% 59C P0 74W / 250W | 23MiB / 12284MiB | 0% Default | +-------------------------------+----------------------+----------------------+ | 1 GeForce GTX TIT... Off | 0000:06:00.0 Off | N/A | | 22% 60C P0 72W / 250W | 23MiB / 12284MiB | 0% Default | +-------------------------------+----------------------+----------------------+ | 2 GeForce GTX TIT... Off | 0000:09:00.0 Off | N/A | | 22% 54C P0 53W / 250W | 23MiB / 12284MiB | 0% Default | +-------------------------------+----------------------+----------------------+ +-----------------------------------------------------------------------------+ | Processes: GPU Memory | | GPU PID Type Process name Usage | |=============================================================================| | No running processes found | +-----------------------------------------------------------------------------+
第二步,安装cuda,在英伟达官网下载Linux-x86_64-Ubuntu-16.04-deb(local),使用以下命令进行安装:
sudo dpkg -i cuda-repo-ubuntu***************.deb
sudo apt-get update
sudo apt-get install cuda
# setting the environment variables so CUDA will be found
echo -e "\nexport PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}} " >> .bashrc
echo -e "\nexport LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64\${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}" >> .bashrc
检查是否安装成功:
root@machine: apt-show-versions cuda
**cuda:amd64/unknown 9.0-** uptodate**
root@machine: cat /proc/driver/nvidia/version
NVRM version: NVIDIA UNIX x86_64 Kernel Module ***.** ************************
GCC version: gcc version *.*.* (Ubuntu ****************)
运行cuda_samples,如果运行成功就大功告成:
cd /usr/local/cuda-9.0/samples/5_Simulations/nbody
sudo make
./nbody
第三步下载cuDNN v7 Library for Linux安装cuDNN v7加速库:
tar -xzvf cudnn-9.0-linux-x64-v7.tgz
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
使之生效
sudo ldconfig –v
建立库链接:
cd /usr/local/cuda/lib64
sudo ln -s libcudnn.so.7.0.xx libcudnn.so.7
sudo ln -s libcudnn .so.7 libcudnn.so
最后安装配置caffe框架:
下载caffe源码到本地
安装依赖项:
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install -y python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflagssudo apt-get install libopenblas-dev libatlas-base-devsudo apt-get install the python-dev
编译前需根据自己需要修改配置文件Makefile.config,进入caffe根目录,首先将示例文件复制一份:
cp Makefile.config.example Makefile.config
使用gedit打开进行修改
编译时可能会遇到的错误:
1..src/caffe/layers/hdf5_data_layer.cpp:13:18: fatal error: hdf5.h: 没有那个文件或目录
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/layers/hdf5_data_layer.o' failed
make: *** [.build_release/src/caffe/layers/hdf5_data_layer.o] Error 1
使用whereis hdf5定位以下hdf5的文件夹,将其路径添加至Makefile.config中,将:
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
修改为:
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
另外在Makefile中将:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
修改为:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
修改完毕后开始编译:
sudo make clean
make all
make test
make runtest
运行MNIST例子:
进入caffe/data/mnist执行./get_mnist.sh下载MNIST数据集
运行在caffe根目录运行./examples/mnist/create_mnist.sh将数据集转换为caffe能处理的格式
在$CAFFE_ROOT/examples/mnist/lenet_solver.prototxt文件中修改训练使用CPU还是GPU