[PYTHON] Allow Keras 2.0 and OpenCV 3.2 to work in GPU environment

It is a work memo until Keras (GPU version) and OpenCV 3.2 can be operated with Python 2.7.

Verification environment & library

Download CuDNN

https://developer.nvidia.com/rdp/cudnn-download

From "Download cuDNN v6.0 (April 27, 2017), for CUDA 8.0" You need to download "cuDNN v6.0 Library for Linux". (cuDNN v7.0 doesn't seem to be supported by TensorFlow 1.3.0 [^ 1])

Suppose you put the downloaded file in "/tmp/cudnn-8.0-linux-x64-v6.0.tgz".

Creating a Docker container

How to install nvidia-docker is described in the link below.

https://github.com/NVIDIA/nvidia-docker

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

Install 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

Checking the operation of Keras

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

If 2.0.6 is displayed, it is successful.

Install OpenCV 3.2

After that, all the work is done inside the Docker container. I refer to the article [^ 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

In the middle

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

Message will appear, so you need to select it according to your environment. In my case, I set it to 50 (Japanese) and 1 (Japanese).

OpenCV build

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

For options at cmake, the code [^ 3] will be helpful.

OpenCV operation check

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

If 3.2.0 is displayed in, it is successful.

References

Recommended Posts

Allow Keras 2.0 and OpenCV 3.2 to work in GPU environment
How to run AutoGluon in Google Colab GPU environment
How to get RGB and HSV histograms in OpenCV
[TF] How to load / save Model and Parameter in Keras
Environment construction of python and opencv
How to install OpenCV on Cloud9 and run it in Python
Build and try an OpenCV & Python environment in minutes using Docker
Prepare an environment to use OpenCV and Pillow with AWS Lambda
June 2017 version to build Tensorflow / Keras environment on GPU instance of AWS
I implemented the VGG16 model in Keras and tried to identify CIFAR10
How to access environment variables in Python
Method to build Python environment in Xcode 6
Build Python3 and OpenCV environment on Ubuntu 18.04
How to work with BigQuery in Python
How to check opencv version in python
To reference environment variables in Python in Blender
To work with timestamp stations in Python
Introduction to docker Create ubuntu environment in ubuntu
How to use is and == in Python
How to draw OpenCV images in Pygame
Flutter in Docker-How to build and use a Flutter development environment inside a Docker container
A complete guidebook to using pyenv, pip and python in an offline environment
Build a PYNQ environment on Ultra96 V2 and log in to Jupyter Notebook
How to reduce GPU memory usage with Keras
How to keep track of work in Powershell
How to generate permutations in Python and C ++
I tried to integrate with Keras in TFv1.1
Send messages to Skype and Chatwork in Python
Install gensim in conda environment (and also install mecab)
Pillow environment construction --For Docker + iPython (and OpenCV)
Work in a virtual environment with Python virtualenv.
How to write async and await in Vue.js
[TF] How to build Tensorflow in Proxy environment
Add totals to rows and columns in pandas
[Windows] Memo to use Keras on GPU [Tensorflow-GPU]
When the Spyder integrated environment fails to work
To represent date, time, time, and seconds in Python
How to plot autocorrelation and partial autocorrelation in python
Use os.getenv to get environment variables in Python
Use a free GPU in your favorite environment
Steps to quickly create a deep learning environment on Mac with TensorFlow and OpenCV
Install OpenCV 3 (core + contrib) in Windows & Python 3 environment & Difference between OpenCV 2 and OpenCV 3 & Easy operation check
How to use Docker to containerize your application and how to use Docker Compose to run your application in a development environment