5
7

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.

GPU環境でKeras 2.0とOpenCV 3.2を動かせるようにする

Last updated at Posted at 2017-08-20

Python2.7でKeras(GPU版)とOpenCV 3.2を動かせるようにするまでの作業メモです.

検証環境&ライブラリ

  • Ubuntu 16.04(docker-image: nvidia/cuda, image_id: 0877f562ab17)
  • GeForce GTX 1080
  • CUDA 8.0
  • cuDNN v6.0
  • Python 2.7.12
  • TensorFlow 1.3.0
  • Keras 2.0.6
  • OpenCV: 3.2.0

CuDNNのダウンロード

から"Download cuDNN v6.0 (April 27, 2017), for CUDA 8.0"の
"cuDNN v6.0 Library for Linux"をダウンロードしておく必要があります.
(cuDNN v7.0はTensorFlow1.3.0では未対応みたいです1)

ダウンロードしたファイルを"/tmp/cudnn-8.0-linux-x64-v6.0.tgz"に置いたとします.

Dockerコンテナの作成

nvidia-dockerのインストール方法は下記リンクに記載されています.

nvidia-docker pull nvidia/cuda
nvidia-docker run -it -v /tmp:/tmp nvidia/cuda bash

Kerasのインストール

apt-get update -y
apt-get upgrade -y
apt-get install -y python-dev python-setuptools curl
curl -kL https://bootstrap.pypa.io/get-pip.py | python
pip install tensorflow-gpu==1.3.0
tar -xvzf /tmp/cudnn-8.0-linux-x64-v6.0.tgz
mv cuda/lib64/libcudnn* /usr/local/cuda/lib64/
mv cuda/include/cudnn.h /usr/local/cuda/include/
chmod a+r /usr/local/cuda/include/cudnn.h 
chmod a+r /usr/local/cuda/lib64/libcudnn* 
echo "export LD_LIBRARY_PATH='$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64'" >> ~/.bashrc
echo "export CUDA_HOME=/usr/local/cuda" >> ~/.bashrc
source ~/.bashrc
pip install Keras==2.0.6

Kerasの動作確認

python -c "import keras; print(keras.__version__)"

2.0.6と表示されたら成功です.

OpenCV 3.2のインストール

以降は全てDockerコンテナ内での作業です.記事2を参考にしています.

apt-get install -y build-essential cmake git pkg-config libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk-3-dev libatlas-base-dev gfortran python2.7-dev zip libhdf5-dev libprotobuf-dev libprotoc-dev protobuf-compiler xserver-xorg doxygen wget

途中で

Country of origin for the keyboard: 50
Keyboard layout: 1

というメッセージが出るので,自身の環境に合わせて選択する必要があります.
私の場合は50(Japanese), 1(Japanese)に設定しました.

OpenCVのビルド

wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.2.0.zip
unzip opencv.zip
cd opencv-3.2.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_CUDA_STUBS=OFF -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_FFMPEG=OFF -D BUILD_opencv_python2=ON -D BUILD_EXAMPLES=OFF -DBUILD_opencv_dnn=OFF ..
make -j $(nproc)
make install

cmake時のオプションについては,コード3が参考になります.

OpenCVの動作確認

python -c "import cv2; print(cv2.__version__)"

にて3.2.0と表示されたら成功です.

References

  1. Allaire, TensorFlow, 2016.

  2. Rosebrock, Ubuntu 16.04: How to install OpenCV, 2016.

  3. atinfinity, opencv3.2_build-ubuntu16.04.sh.

5
7
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
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?