title: Keras with TensorFlow backend not using GPU
body: <p>I built the gpu version of the docker image <a href="https://github.com/floydhub/dl-docker" rel="noreferrer">https://github.com/floydhub/dl-docker</a> with keras version 2.0.0 and tensorflow version 0.12.1. I then ran the mnist tutorial <a href="https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py" rel="noreferrer">https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py</a> but realized that keras is not using GPU. Below is the output that I have</p>
  
  <pre><code>root@b79b8a57fb1f:~/sharedfolder# python test.py
  Using TensorFlow backend.
  Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
  x_train shape: (60000, 28, 28, 1)
  60000 train samples
  10000 test samples
  Train on 60000 samples, validate on 10000 samples
  Epoch 1/12
  2017-09-06 16:26:54.866833: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
  2017-09-06 16:26:54.866855: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
  2017-09-06 16:26:54.866863: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
  2017-09-06 16:26:54.866870: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
  2017-09-06 16:26:54.866876: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
  </code></pre>
  
  <p>Can anyone let me know if there are some settings that need to be made before keras uses GPU ? I am very new to all these so do let me know if I need to provide more information. </p>
  
  <p><strong>I have installed the pre-requisites as mentioned on the <a href="https://github.com/floydhub/dl-docker#prerequisites" rel="noreferrer">page</a></strong></p>
  
  <ul>
  <li>Install Docker following the installation guide for your platform: <a href="https://docs.docker.com/engine/installation/" rel="noreferrer">https://docs.docker.com/engine/installation/</a></li>
  </ul>
  
  <p>I am able to launch the docker image </p>
  
  <pre><code>docker run -it -p 8888:8888 -p 6006:6006 -v /sharedfolder:/root/sharedfolder floydhub/dl-docker:cpu bash
  </code></pre>
  
  <ul>
  <li>GPU Version Only: Install Nvidia drivers on your machine either from Nvidia directly or follow the instructions <a href="https://github.com/floydhub/dl-setup#nvidia-drivers" rel="noreferrer">here</a>. Note that you don't have to install CUDA or cuDNN. These are included in the Docker container. </li>
  </ul>
  
  <p>I am able to run the last step </p>
  
  <pre><code>cv@cv-P15SM:~$ cat /proc/driver/nvidia/version
  NVRM version: NVIDIA UNIX x86_64 Kernel Module  375.66  Mon May  1 15:29:16 PDT 2017
  GCC version:  gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
  </code></pre>
  
  <ul>
  <li>GPU Version Only: Install nvidia-docker: <a href="https://github.com/NVIDIA/nvidia-docker" rel="noreferrer">https://github.com/NVIDIA/nvidia-docker</a>, following the instructions here. This will install a replacement for the docker CLI. It takes care of setting up the Nvidia host driver environment inside the Docker containers and a few other things.</li>
  </ul>
  
  <p>I am able to run the step <a href="https://github.com/NVIDIA/nvidia-docker" rel="noreferrer">here</a></p>
  
  <pre><code># Test nvidia-smi
  cv@cv-P15SM:~$ nvidia-docker run --rm nvidia/cuda nvidia-smi
  
  Thu Sep  7 00:33:06 2017       
  +-----------------------------------------------------------------------------+
  | NVIDIA-SMI 375.66                 Driver Version: 375.66                    |
  |-------------------------------+----------------------+----------------------+
  | 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 780M    Off  | 0000:01:00.0     N/A |                  N/A |
  | N/A   55C    P0    N/A /  N/A |    310MiB /  4036MiB |     N/A      Default |
  +-------------------------------+----------------------+----------------------+
  
  +-----------------------------------------------------------------------------+
  | Processes:                                                       GPU Memory |
  |  GPU       PID  Type  Process name                               Usage      |
  |=============================================================================|
  |    0                  Not Supported                                         |
  +-----------------------------------------------------------------------------+
  </code></pre>
  
  <p>I am also able to run the nvidia-docker command to launch a gpu supported image.</p>
  
  <p><strong>What I have tried</strong></p>
  
  <p>I have tried the following suggestions below </p>
  
  <ol>
  <li>Check if you have completed step 9 of this tutorial ( <a href="https://github.com/ignaciorlando/skinner/wiki/Keras-and-TensorFlow-installation" rel="noreferrer">https://github.com/ignaciorlando/skinner/wiki/Keras-and-TensorFlow-installation</a> ). Note: Your file paths may be completely different inside that docker image, you'll have to locate them somehow. </li>
  </ol>
  
  <p>I appended the suggested lines to my bashrc and have verified that the bashrc file is updated.</p>
  
  <pre><code>echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-8.0/lib64:/usr/local/cuda-8.0/extras/CUPTI/lib64' &gt;&gt; ~/.bashrc
  echo 'export CUDA_HOME=/usr/local/cuda-8.0' &gt;&gt; ~/.bashrc
  </code></pre>
  
  <ol>
  <li><p>To import the following commands in my python file</p>
  
  <p><code>import os
  os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"   # see issue #152
  os.environ["CUDA_VISIBLE_DEVICES"]="0"</code></p></li>
  </ol>
  
  <p>Both steps, done separately or together unfortunately did not solve the issue. Keras is still running with the CPU version of tensorflow as its backend. However, I might have found the possible issue. I checked the version of my tensorflow via the following commands and found two of them.</p>
  
  <p>This is the CPU version</p>
  
  <pre><code>root@08b5fff06800:~# pip show tensorflow
  Name: tensorflow
  Version: 1.3.0
  Summary: TensorFlow helps the tensors flow
  Home-page: http://tensorflow.org/
  Author: Google Inc.
  Author-email: opensource@google.com
  License: Apache 2.0
  Location: /usr/local/lib/python2.7/dist-packages
  Requires: tensorflow-tensorboard, six, protobuf, mock, numpy, backports.weakref, wheel
  </code></pre>
  
  <p>And this is the GPU version</p>
  
  <pre><code>root@08b5fff06800:~# pip show tensorflow-gpu
  Name: tensorflow-gpu
  Version: 0.12.1
  Summary: TensorFlow helps the tensors flow
  Home-page: http://tensorflow.org/
  Author: Google Inc.
  Author-email: opensource@google.com
  License: Apache 2.0
  Location: /usr/local/lib/python2.7/dist-packages
  Requires: mock, numpy, protobuf, wheel, six
  </code></pre>
  
  <p>Interestingly, the output shows that keras is using tensorflow version 1.3.0 which is the CPU version and not 0.12.1, the GPU version</p>
  
  <pre><code>import keras
  from keras.datasets import mnist
  from keras.models import Sequential
  from keras.layers import Dense, Dropout, Flatten
  from keras.layers import Conv2D, MaxPooling2D
  from keras import backend as K
  
  import tensorflow as tf
  print('Tensorflow: ', tf.__version__)
  </code></pre>
  
  <p>Output</p>
  
  <pre><code>root@08b5fff06800:~/sharedfolder# python test.py
  Using TensorFlow backend.
  Tensorflow:  1.3.0
  </code></pre>
  
  <p>I guess now I need to figure out how to have keras use the gpu version of tensorflow.</p>