具有TensorFlow后端的Keras不使用GPU [英] Keras with TensorFlow backend not using GPU

查看:79
本文介绍了具有TensorFlow后端的Keras不使用GPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了docker映像的gpu版本 https://github.com/floydhub/dl-docker 与keras版本2.0.0和tensorflow版本0.12.1.然后,我运行了mnist教程 https://github.com/fchollet/keras /blob/master/examples/mnist_cnn.py ,但意识到keras没有使用GPU.下面是我的输出

I built the gpu version of the docker image https://github.com/floydhub/dl-docker with keras version 2.0.0 and tensorflow version 0.12.1. I then ran the mnist tutorial https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py but realized that keras is not using GPU. Below is the output that I have

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之前是否需要进行一些设置吗?我对所有这些都是新手,所以如果需要提供更多信息,请告诉我.

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.

我已经安装了页面中提到的前提条件

我能够启动docker映像

I am able to launch the docker image

docker run -it -p 8888:8888 -p 6006:6006 -v /sharedfolder:/root/sharedfolder floydhub/dl-docker:cpu bash

  • 仅GPU版本:直接从Nvidia或按照说明在您的计算机上安装Nvidia驱动程序这里.请注意,您不必安装CUDA或cuDNN.这些都包含在Docker容器中.
    • GPU Version Only: Install Nvidia drivers on your machine either from Nvidia directly or follow the instructions here. Note that you don't have to install CUDA or cuDNN. These are included in the Docker container.
    • 我能够执行最后一步

      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主机驱动程序环境以及其他一些东西.
        • GPU Version Only: Install nvidia-docker: https://github.com/NVIDIA/nvidia-docker, 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.
        • 我能够在此处

          # 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支持的映像.

          I am also able to run the nvidia-docker command to launch a gpu supported image.

          我尝试过的事情

          我在下面尝试了以下建议

          I have tried the following suggestions below

          1. 检查您是否已完成本教程的第9步( https://github.com/ignaciorlando/skinner/wiki/Keras-and-TensorFlow-installation ).注意:您的docker映像中的文件路径可能完全不同,您必须以某种方式找到它们.
          1. Check if you have completed step 9 of this tutorial ( https://github.com/ignaciorlando/skinner/wiki/Keras-and-TensorFlow-installation ). Note: Your file paths may be completely different inside that docker image, you'll have to locate them somehow.

          我将建议的行添加到我的bashrc中,并已验证bashrc文件已更新.

          I appended the suggested lines to my bashrc and have verified that the bashrc file is updated.

          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文件中导入以下命令

          1. To import the following commands in my python file

          import os os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # see issue #152 os.environ["CUDA_VISIBLE_DEVICES"]="0"

          import os os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # see issue #152 os.environ["CUDA_VISIBLE_DEVICES"]="0"

          不幸的是,单独或一起执行的两个步骤都不能解决问题. Keras仍以CPU版本的tensorflow作为后端运行.但是,我可能已经发现了可能的问题.我通过以下命令检查了我的tensorflow的版本,发现其中两个.

          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.

          这是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版本)

          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

          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版本.

          I guess now I need to figure out how to have keras use the gpu version of tensorflow.

          推荐答案

          同时安装tensorflowtensorflow-gpu软件包(一次)是一个很好的主意.偶然发生在我身上,Keras使用的是CPU版本).

          It is never a good idea to have both tensorflow and tensorflow-gpu packages installed side by side (the one single time it happened to me accidentally, Keras was using the CPU version).

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

          I guess now I need to figure out how to have keras use the gpu version of tensorflow.

          您应该只从系统中删除这两个软件包,然后重新安装tensorflow-gpu [注释后更新]:

          You should simply remove both packages from your system, and then re-install tensorflow-gpu [UPDATED after comment]:

          pip uninstall tensorflow tensorflow-gpu
          pip install tensorflow-gpu
          

          此外,令人困惑的是为什么您似乎使用floydhub/dl-docker:cpu容器,而根据说明,您应该使用floydhub/dl-docker:gpu一个...

          Moreover, it is puzzling why you seem to use the floydhub/dl-docker:cpu container, while according to the instructions you should be using the floydhub/dl-docker:gpu one...

          这篇关于具有TensorFlow后端的Keras不使用GPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆