5
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS EC2 P2インスタンス(ubuntu 14.04)でCUDA+CuDNN+Caffeを使う

Last updated at Posted at 2016-10-17

はじめに

AWSで新しいGPUインスタンスであるP2系が出ました。
http://dev.classmethod.jp/cloud/p2-instance-type/

Deep learningのライブラリがプリインストールされているAMI(https://aws.amazon.com/marketplace/pp/B01M0AXXQB)
もあるのですが、CUDAやCuDNNが入っていないらしいのでせっかくということでインストールしてみました。

OS:ubuntu 14.04
CUDA: 7.5
CuDNN: 5.1

準備

必要なライブラリ系のインストール

sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install -y build-essential git python-pip python-dev libxml2-dev libxslt1-dev zlib1g-dev libcurl4-openssl-dev libatlas-base-dev linux-image-extra-virtual libopencv-dev python-numpy
sudo apt-get autoremove -y 

ホスト側のドライバを無効にする

sudo vi /etc/modprobe.d/blacklist-nouveau.conf を実行

/etc/modprobe.d/blacklist-nouveau.conf
blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off

再起動をする

echo options nouveau modeset=0 | sudo tee -a /etc/modprobe.d/nouveau-kms.conf
sudo update-initramfs -u
sudo reboot

nvidia-driverのインストール

事前にGPUに適合するドライバを調べておく
P2インスタンスのGPUはK80

sudo apt-get install -y linux-source 
sudo apt-get install -y linux-headers-`uname -r`
wget http://us.download.nvidia.com/XFree86/Linux-x86_64/352.99/NVIDIA-Linux-x86_64-352.99.run
sudo chmod +x NVIDIA-Linux-x86_64-352.99.run
sudo ./NVIDIA-Linux-x86_64-352.99.run

再起動しておく

CUDA+CuDNNのインストール

wget http://developer.download.nvidia.com/compute/cuda/7.5/Prod/local_installers/cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb
sudo apt-get update -y 
sudo apt-get install cuda -y

CuDNNは以下のサイトから登録し、アンケートに答えた後DL
https://developer.nvidia.com/cudnn

tar xzf cudnn-7.5-linux-x64-v5.1.tgz
sudo cp -a cuda/lib64/* /usr/local/lib/
sudo cp -a cuda/include/* /usr/local/include/
sudo ldconfig

.bashrcを編集

.bashrc
export PATH=$PATH:/usr/local/cuda-7.5/bin 
export LD_LIBRARY_PATH=:/usr/local/cuda-7.5/lib64 

source .bashrc

Caffeインストール

gitからclone

sudo apt-get update && sudo apt-get upgrade;
# 上と重複もあるが気にしない。。。
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev protobuf-compiler gfortran libjpeg62 libfreeimage-dev libatlas-base-dev git python-dev python-pip libgoogle-glog-dev libbz2-dev libxml2-dev libxslt-dev libffi-dev libssl-dev libgflags-dev liblmdb-dev python-yaml python-numpy;
#sudo easy_install pillow;
git clone https://github.com/BVLC/caffe.git
cd caffe;
cat python/requirements.txt | xargs -L 1 sudo pip install;

makeする

cp Makefile.config.example Makefile.config
echo USE_CUDNN := 1 >> Makefile.config
make all -j4;
make test -j4;
make runtest -j4;

サンプル実行

./data/mnist/get_mnist.sh;
./examples/mnist/create_mnist.sh;
./examples/mnist/train_lenet.sh;

結果抜粋

I1017 07:48:29.925045 20325 solver.cpp:454] Snapshotting to binary proto file examples/mnist/lenet_iter_10000.caffemodel
I1017 07:48:29.932385 20325 sgd_solver.cpp:273] Snapshotting solver state to binary proto file examples/mnist/lenet_iter_10000.solverstate
I1017 07:48:29.936767 20325 solver.cpp:317] Iteration 10000, loss = 0.00284106
I1017 07:48:29.936818 20325 solver.cpp:337] Iteration 10000, Testing net (#0)
I1017 07:48:30.079130 20325 solver.cpp:404]     Test net output #0: accuracy = 0.9914
I1017 07:48:30.079165 20325 solver.cpp:404]     Test net output #1: loss = 0.0269921 (* 1 = 0.0269921 loss)
I1017 07:48:30.079176 20325 solver.cpp:322] Optimization Done.
I1017 07:48:30.079183 20325 caffe.cpp:254] Optimization Done.

うまくいっていることを確認できましたね。

Pythonで使えるようにする

sudo apt-get install -y python-dev python-pip python-numpy python-skimage gfortran
make pycaffe -j4;
echo 'export PYTHONPATH=~/caffe/python/:$PYTHONPATH' >> ~/.bashrc
source .bashrc

あとはPythonでimport caffeをすればよい。

もしGDK_IS_DISPLAY (display)' failed
と怒られた場合は以下を参考に、~/.config/matplotlib/matplotlibrc
を編集。
https://github.com/BVLC/caffe/issues/861

~/.config/matplotlib/matplotlibrc
backend: Agg

Pythonライブラリ系で怒られた場合はpip install --upgrade pipの後再度下記を実行。

cat python/requirements.txt | xargs -L 1 sudo pip install;
5
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?