Reference: http://www.pyimagesearch.com/2016/11/14/installing-keras-with-tensorflow-backend/
step-by-step instructions to install Keras using a TensorFlow backend for machine learning.
Keras is simply a wrapper around more complex numerical computation engines such as TensorFlow and Theano.
When it comes to Keras you have two choices for a backend engine — either TensorFlow or Theano. Theano is older than TensorFlow and was originally the only choice when selecting a backend for Keras.
So why might you want to use TensorFlow over Theano?
The short version is that TensorFlow is extremely flexible, allowing you to deploy network computation to multiple CPUs, GPUs, servers, or even mobile systems without having to change a single line of code.
This makes TensorFlow an excellent choice for training distributed deep learning networks in an architecture agnostic way, something that Theano does not (currently) provide.
Installation:
- Use Python virtual environments.
1234567891011teddy@teddy-K43SJ:~$ mkvirtualenv opencv_keras_tfUsing base prefix '/usr'New python executable in /home/teddy/.virtualenvs/opencv_keras_tf/bin/python3Also creating executable in /home/teddy/.virtualenvs/opencv_keras_tf/bin/pythonInstalling setuptools, pip, wheel...done.virtualenvwrapper.user_scripts creating /home/teddy/.virtualenvs/opencv_keras_tf/bin/predeactivatevirtualenvwrapper.user_scripts creating /home/teddy/.virtualenvs/opencv_keras_tf/bin/postdeactivatevirtualenvwrapper.user_scripts creating /home/teddy/.virtualenvs/opencv_keras_tf/bin/preactivatevirtualenvwrapper.user_scripts creating /home/teddy/.virtualenvs/opencv_keras_tf/bin/postactivatevirtualenvwrapper.user_scripts creating /home/teddy/.virtualenvs/opencv_keras_tf/bin/get_env_details(opencv_keras_tf) teddy@teddy-K43SJ:~$
You can quit the virtualenv with command ‘deactivate’
12(opencv_keras_tf) teddy@teddy-K43SJ:~$ deactivateteddy@teddy-K43SJ:~$
And go back the virtualenv with command ‘workon opencv_keras_tf’
12teddy@teddy-K43SJ:~$ workon opencv_keras_tf(opencv_keras_tf) teddy@teddy-K43SJ:~$
Check python version:
12345(opencv_keras_tf) teddy@teddy-K43SJ:~$ pythonPython 3.4.3 (default, Nov 17 2016, 01:08:31)[GCC 4.8.4] on linuxType "help", "copyright", "credits" or "license" for more information.>>>
- Install TensorFlow
NOTE: Read this about CUDA compute capability : http://stackoverflow.com/questions/10961476/what-are-the-differences-between-cuda-compute-capabilities
IT SEEMS DEPEND ON THE HARDWARE! NOTHING CAN DO TO IMPROVE THE COMPUTE CAPABILITY!
AFTER READING THIS https://www.tensorflow.org/versions/r0.10/get_started/os_setup#installing_from_sources AND http://stackoverflow.com/questions/38542763/how-can-i-make-tensorflow-run-on-a-gpu-with-capability-2-0, I GOT SAD NEWS THAT
“In order to build or run TensorFlow with GPU support, both NVIDIA’s Cuda Toolkit (>= 7.0) and cuDNN (>= v3) need to be installed.
TensorFlow GPU support requires having a GPU card with NVidia Compute Capability >= 3.0.”
SO MY GPU GeForce GT 520M THAT ONLY HAS CUDA COMPUTE CAPABILITY 2.1 (REF: https://en.wikipedia.org/wiki/CUDA) CAN’T BE USED FOR TENSORFLOW!!!I ONLY CAN USE CPU!!!To install TensorFlow, refer to this page: https://www.tensorflow.org/versions/r0.11/get_started/os_setup.
Currently I have CUDA 7.5 toolkit installed
12345(opencv_keras_tf) teddy@teddy-K43SJ:~$ nvcc -Vnvcc: NVIDIA (R) Cuda compiler driverCopyright (c) 2005-2015 NVIDIA CorporationBuilt on Tue_Aug_11_14:27:32_CDT_2015Cuda compilation tools, release 7.5, V7.5.17
The tensorflow page need CUDA toolkit 8.0 and CuDNN v5. I already installed them. I go to ‘Pip installation’ and choose this binary option:
123# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Install from sources" below.$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.11.0-cp34-cp34m-linux_x86_64.whl
SO let’s begin:
1(opencv_keras_tf) teddy@teddy-K43SJ:~$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.11.0-cp34-cp34m-linux_x86_64.whl
Install TensorFlow:
123456789101112131415161718(opencv_keras_tf) teddy@teddy-K43SJ:~$ pip install --upgrade $TF_BINARY_URLCollecting tensorflow==0.11.0 from https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.11.0-cp34-cp34m-linux_x86_64.whlDownloading https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.11.0-cp34-cp34m-linux_x86_64.whl (119.1MB)100% |████████████████████████████████| 119.1MB 9.1kB/sRequirement already up-to-date: wheel>=0.26 in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages (from tensorflow==0.11.0)Collecting numpy>=1.11.0 (from tensorflow==0.11.0)Downloading numpy-1.12.0-cp34-cp34m-manylinux1_x86_64.whl (16.8MB)100% |████████████████████████████████| 16.8MB 59kB/sRequirement already up-to-date: six>=1.10.0 in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages (from tensorflow==0.11.0)Collecting protobuf==3.0.0 (from tensorflow==0.11.0)Downloading protobuf-3.0.0-cp34-cp34m-manylinux1_x86_64.whl (5.2MB)100% |████████████████████████████████| 5.2MB 177kB/sRequirement already up-to-date: setuptools in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages (from protobuf==3.0.0->tensorflow==0.11.0)Requirement already up-to-date: packaging>=16.8 in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages (from setuptools->protobuf==3.0.0->tensorflow==0.11.0)Requirement already up-to-date: appdirs>=1.4.0 in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages (from setuptools->protobuf==3.0.0->tensorflow==0.11.0)Requirement already up-to-date: pyparsing in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages (from packaging>=16.8->setuptools->protobuf==3.0.0->tensorflow==0.11.0)Installing collected packages: numpy, protobuf, tensorflowSuccessfully installed numpy-1.12.0 protobuf-3.0.0 tensorflow-0.11.0
Test the installation
1234567891011(opencv_keras_tf) teddy@teddy-K43SJ:~$ pythonPython 3.4.3 (default, Nov 17 2016, 01:08:31)[GCC 4.8.4] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import tensorflowI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so locally>>>
- Install Keras
Install a few Python dependencies:
Install numpy scify
1234567(opencv_keras_tf) teddy@teddy-K43SJ:~$ pip install numpy scipyRequirement already satisfied: numpy in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packagesCollecting scipyDownloading scipy-0.18.1-cp34-cp34m-manylinux1_x86_64.whl (40.2MB)100% |████████████████████████████████| 40.2MB 26kB/sInstalling collected packages: scipySuccessfully installed scipy-0.18.1
Install scikit-learn
123456(opencv_keras_tf) teddy@teddy-K43SJ:~$ pip install scikit-learnCollecting scikit-learnDownloading scikit_learn-0.18.1-cp34-cp34m-manylinux1_x86_64.whl (11.9MB)100% |████████████████████████████████| 11.9MB 87kB/sInstalling collected packages: scikit-learnSuccessfully installed scikit-learn-0.18.1
Install pillow
12345678910111213(opencv_keras_tf) teddy@teddy-K43SJ:~$ pip install pillowCollecting pillowDownloading Pillow-4.0.0-cp34-cp34m-manylinux1_x86_64.whl (5.6MB)100% |████████████████████████████████| 5.6MB 169kB/sCollecting olefile (from pillow)Downloading olefile-0.44.zip (74kB)100% |████████████████████████████████| 81kB 1.9MB/sBuilding wheels for collected packages: olefileRunning setup.py bdist_wheel for olefile ... doneStored in directory: /home/teddy/.cache/pip/wheels/20/58/49/cc7bd00345397059149a10b0259ef38b867935ea2ecff99a9bSuccessfully built olefileInstalling collected packages: olefile, pillowSuccessfully installed olefile-0.44 pillow-4.0.0
Install h5py
12345678(opencv_keras_tf) teddy@teddy-K43SJ:~$ pip install h5pyCollecting h5pyDownloading h5py-2.6.0-1-cp34-cp34m-manylinux1_x86_64.whl (4.6MB)100% |████████████████████████████████| 4.6MB 103kB/sRequirement already satisfied: six in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages (from h5py)Requirement already satisfied: numpy>=1.6.1 in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages (from h5py)Installing collected packages: h5pySuccessfully installed h5py-2.6.0
Install keras
1234567891011121314151617181920212223(opencv_keras_tf) teddy@teddy-K43SJ:~$ pip install kerasCollecting kerasDownloading Keras-1.2.2.tar.gz (175kB)100% |████████████████████████████████| 184kB 1.5MB/sCollecting theano (from keras)Downloading Theano-0.8.2.tar.gz (2.9MB)100% |████████████████████████████████| 2.9MB 257kB/sCollecting pyyaml (from keras)Downloading PyYAML-3.12.tar.gz (253kB)100% |████████████████████████████████| 256kB 1.6MB/sRequirement already satisfied: six in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages (from keras)Requirement already satisfied: numpy>=1.7.1 in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages (from theano->keras)Requirement already satisfied: scipy>=0.11 in ./.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages (from theano->keras)Building wheels for collected packages: keras, theano, pyyamlRunning setup.py bdist_wheel for keras ... doneStored in directory: /home/teddy/.cache/pip/wheels/f6/c5/63/97d96b41bf822858027c70b04448c19deaccf1cf518148fa82Running setup.py bdist_wheel for theano ... doneStored in directory: /home/teddy/.cache/pip/wheels/96/2b/3d/71d54e24a7171a4afb7144d1e944a7be643b448b23a35b9937Running setup.py bdist_wheel for pyyaml ... doneStored in directory: /home/teddy/.cache/pip/wheels/2c/f7/79/13f3a12cd723892437c0cfbde1230ab4d82947ff7b3839a4fcSuccessfully built keras theano pyyamlInstalling collected packages: theano, pyyaml, kerasSuccessfully installed keras-1.2.2 pyyaml-3.12 theano-0.8.2
Then run ‘import keras’ in python console to create keras.json file (http://stackoverflow.com/questions/40310035/how-to-change-keras-backend-wheres-the-json-file)
123456789101112(opencv_keras_tf) teddy@teddy-K43SJ:~$ pythonPython 3.4.3 (default, Nov 17 2016, 01:08:31)[GCC 4.8.4] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import kerasUsing TensorFlow backend.I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so locally>>> quit()
keras.json file would be created in ~/.keras/ (~/.keras/keras.json file)
123456{"backend": "tensorflow","image_dim_ordering": "tf","epsilon": 1e-07,"floatx": "float32"}
Specifically, you’ll want to ensure that image_dim_ordering is set to tf (indicating that the TensorFlow image dimension ordering is used rather than th for Theano).You’ll also want to ensure that the backend is properly set to tensorflow (rather thantheano ).
- Symlink with OpenCV
My opencv binding file for python 3 is in /usr/local/lib/python3.4/site-packages/cv2.cpython-34m.so. I need to symlink it with the virtualenv ‘opencv_keras_tf’ like this:
Go into this directory:
1(opencv_keras_tf) teddy@teddy-K43SJ:~$ cd ~/.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages/
Then symlink it (HERE I CHANGE THE FILE TO ‘cv2.so’)
12(opencv_keras_tf) teddy@teddy-K43SJ:~/.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages$ ln -s /usr/local/lib/python3.4/site-packages/cv2.cpython-34m.so cv2.so(opencv_keras_tf) teddy@teddy-K43SJ:~/.virtualenvs/opencv_keras_tf/lib/python3.4/site-packages$ cd ~
Test it
12345678910111213(opencv_keras_tf) teddy@teddy-K43SJ:~$ pythonPython 3.4.3 (default, Nov 17 2016, 01:08:31)[GCC 4.8.4] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import kerasUsing TensorFlow backend.I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locallyI tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so locally>>> import cv2>>> quit()
OK. NO ERROR