Keras使用TensorFlow后端没有使用GPU

20
我已经构建了基于keras版本2.0.0和tensorflow版本0.12.1的gpu版docker镜像https://github.com/floydhub/dl-docker。然后我运行了mnist教程https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py,但发现keras没有使用GPU。以下是我的输出结果。
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.

请问在使用Keras前是否需要进行一些设置以使用GPU?我对这些都很陌生,如果需要提供更多信息,请告诉我。

我已按照页面上所述安装了先决条件

我可以启动Docker镜像

docker run -it -p 8888:8888 -p 6006:6006 -v /sharedfolder:/root/sharedfolder floydhub/dl-docker:cpu bash
  • 仅适用于GPU版本:在您的计算机上安装Nvidia驱动程序,可以直接从Nvidia安装,或者按照此处的说明进行操作。请注意,您不必安装CUDA或cuDNN,因为它们已经包含在Docker容器中。

我能够运行最后一步。

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)
  • 仅适用于GPU版本:安装nvidia-docker: https://github.com/NVIDIA/nvidia-docker,请按照此处的说明进行操作。这将安装docker CLI的替代品。它会在Docker容器内设置Nvidia主机驱动程序环境并处理其他一些事情。

我能够运行here步骤。

# 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                                         |
+-----------------------------------------------------------------------------+

我还可以运行nvidia-docker命令以启动支持GPU的镜像。 我尝试过的 我尝试了以下建议:
  1. 检查您是否完成了此教程(https://github.com/ignaciorlando/skinner/wiki/Keras-and-TensorFlow-installation)的第9步。注意:该docker映像内部的文件路径可能完全不同,您必须以某种方式定位它们。
我将建议的行添加到我的bashrc中,并验证了bashrc文件已更新。
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-8.0/lib64:/usr/local/cuda-8.0/extras/CUPTI/lib64' >> ~/.bashrc
echo 'export CUDA_HOME=/usr/local/cuda-8.0' >> ~/.bashrc
  1. 在我的Python文件中导入以下命令

    import os os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # 查看问题#152 os.environ["CUDA_VISIBLE_DEVICES"]="0"

这两个步骤,无论是分开还是一起完成,都无法解决问题。Keras仍然使用CPU版本的tensorflow作为其后端。但是,我可能已经找到了可能的问题。通过以下命令检查了我的tensorflow版本,并发现了其中的两个版本。

这是CPU版本

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

这是GPU版本

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

有趣的是,输出显示keras正在使用tensorflow版本1.3.0,这是CPU版本而不是0.12.1,GPU版本。

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__)

输出

root@08b5fff06800:~/sharedfolder# python test.py
Using TensorFlow backend.
Tensorflow:  1.3.0

我想现在我需要弄清楚如何让keras使用tensorflow的gpu版本。


如果你按照Tensorflow安装指南(https://www.tensorflow.org/install/)来安装你的操作系统,它会告诉你所需的一切。你很可能缺少CUDA和CUDAnn库。 - KDecker
@KDecker 但是在 https://github.com/floydhub/dl-docker 上写着 GPU 版本附带 CUDA 8.0 和 cuDNN v5。 - Kong
哦,这是Docker镜像。抱歉我刚才没注意到。除了这个,我不确定该告诉你什么,你是否遵循了镜像的先决条件?(安装了NVidia驱动程序和nvidia-docker) - KDecker
嗨,是的,我已经按照先决条件进行了操作(更新了我的帖子)。 - Kong
@kong:你能否尝试一下"x_train /= 255."和"x_test /= 255."这个解决方案看起来很愚蠢。但我们也遇到了同样的问题,我们的模型无法在GPU上运行。但是经过这个改变后,它可以工作了。 - Beta
显示剩余4条评论
4个回答

31

同时安装 tensorflowtensorflow-gpu 包从来都不是一个好主意(我不小心这么做了一次,结果 Keras 使用的是 CPU 版本)。

我猜现在我需要想办法让 keras 使用 tensorflow 的 GPU 版本。

你应该将两个包从系统中移除,然后重新安装 tensorflow-gpu。[在评论后更新]:

pip uninstall tensorflow tensorflow-gpu
pip install tensorflow-gpu

此外,令人困惑的是你似乎在使用 floydhub/dl-docker:cpu 容器,然而根据说明,你应该使用 floydhub/dl-docker:gpu 容器...


3
哇,你说得很对。虽然我发现我必须卸载tensorflow和tensorflow-gpu,然后重新安装tensorflow-gpu。如果我只卸载tensorflow,那么Python无法检测到gpu版本。让库正常工作可能会非常耗费精力。 - Kong
2
现在它有效了。我的意思是卸载 tensoflow-gpu 后,再重新安装 tensorflow-gpu。之前它不起作用。 - khan
@khan 呵呵,你通过先点踩来唤醒我编辑它是个好主意 - 谢谢 ;) - desertnaut
1
@desertnaut :感谢您的更新。 - khan
移除tensorflow(CPU版本)会导致无法使用tensorboard吗? - Btc Sources
1
@BtcSources Tensorboard 可以被 Tensorflow 的 CPU 和 GPU 版本使用。 - desertnaut

5

我曾遇到类似的问题 - keras没有使用我的GPU。我按照说明将tensorflow-gpu安装到conda中,但是在安装keras后,它根本没有将GPU列为可用设备。我意识到keras的安装添加了tensorflow包!因此,我同时安装了tensorflow和tensorflow-gpu包。我发现keras-gpu包也可用。在完全卸载keras、tensorflow、tensorflow-gpu并安装tensorflow-gpu、keras-gpu之后,问题得以解决。


3
在未来,您可以尝试使用虚拟环境来分离TensorFlow CPU和GPU,例如:
conda create --name tensorflow python=3.5
activate tensorflow
pip install tensorflow

并且

conda create --name tensorflow-gpu python=3.5
activate tensorflow-gpu
pip install tensorflow-gpu

-2
这对我有用: 安装tensorflow v2.2.0 pip install tensorflow==2.2.0 同时删除tensorflow-gpu(如果存在)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接