Shooth
Shooth

Reputation: 83

Using GPU instead of CPU with Keras with Tensorflow Backend for Linux

I am having trouble getting Keras to use the GPU version of Tensorflow instead of CPU. Every time I import keras it just says:

>>> import keras
Using TensorFlow backend

...which means it's working, but on CPU, not GPU. I installed Cuda and cuDNN and use this environment:

conda create -n tensorflow python=3.5 anaconda 

I think I installed the CPU version of tensorflow first - I don't remember because I spend all day just getting cuda and cudnn to work. Anyway, I installed the GPU version too:

pip install --ignore-installed --upgrade \ https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.1.0-cp35-cp35m-linux_x86_64.whl

and it still gives the same message. I tried to check which device is being used by the following code:

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

but I get this output, indicating I am using device 0, my GPU:

2017-05-12 02:14:10.746679: 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-05-12 02:14:10.746735: 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-05-12 02:14:10.746751: 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-05-12 02:14:10.746764: 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-05-12 02:14:10.746777: 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.
2017-05-12 02:14:10.926330: I 
tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:901] successful 
NUMA node read from SysFS had negative value (-1), but there must be 
at least one NUMA node, so returning NUMA node zero
2017-05-12 02:14:10.926614: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:887] Found device 0 
with properties: 
name: GeForce GTX 1060 6GB
major: 6 minor: 1 memoryClockRate (GHz) 1.7845
pciBusID 0000:01:00.0
Total memory: 5.93GiB
Free memory: 5.51GiB
2017-05-12 02:14:10.926626: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0 
2017-05-12 02:14:10.926629: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:   Y 
2017-05-12 02:14:10.926637: I 
tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating 
TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1060 6GB, 
pci bus id: 0000:01:00.0)
Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 
1060 6GB, pci bus id: 0000:01:00.0
2017-05-12 02:14:10.949871: I 
tensorflow/core/common_runtime/direct_session.cc:257] Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 
1060 6GB, pci bus id: 0000:01:00.0

I really ran out of things to do. The only thing I have left is to uninstall anaconda and reinstall everything again, which I really don't want to do since I literally spent the entire day getting it to work with keras and everything (just not with the GPU yet)

Upvotes: 2

Views: 2371

Answers (3)

Indri92
Indri92

Reputation: 21

First of all, you have to ensure that tensorflow actually detected both of your CPU and GPU. You can use the following code to check it.

from tensorflow.python.client import device_lib
device_lib.list_local_devices()

If it only lists out the CPU and not the GPU, it might be caused you don't have the same tensorflow and tensorflow-gpu version (because of upgrade). You can check the version (if you use pip) by using

pip list

If they are not the same, you have to uninstall the incompatible tensorflow or tensorflow-gpu version before you install the tensorflow and tensorflow-gpu version which are compatible with your CUDA and CUDNN version.

For example, I am using CUDA 8.0 and CUDNN 5.1.10, so the compatible tensorflow and tensorflow-gpu version is version 1.2.

To uninstall with pip:

pip uninstall tensorflow
pip uninstall tensorflow-gpu

To install with pip:

pip install tensorflow==1.2 
pip install tensorflow-gpu==1.2

Then you just have to check if tensorflow has detected both your CPU and GPU again. If it does, then you just have to run the code and it will automatically choose to run the computation in the GPU if you are using keras.

Here is the link to the tested compatible combination released by tensorflow.org and here is the link to another question related to this.

Upvotes: 1

Fang
Fang

Reputation: 844

did you already try with pip install tensorflow ? this will install the cpu version while pip install tensorflow-gpu will install the gpu version. https://www.tensorflow.org/install/

Upvotes: 0

MattMcKnight
MattMcKnight

Reputation: 8290

On possibility is that installing keras with default options will install the CPU version of tensorflow. You could uninstall that version, and then run...

pip install --upgrade --no-deps keras

https://github.com/fchollet/keras/issues/5766

Upvotes: 1

Related Questions