将GPU与Tensorflow和Keras结合使用

时间:2019-01-24 16:39:49

标签: python tensorflow keras

我有使用Keras的python代码(带有tensorflow后端) 我在使用GPU来减少计算的处理时间时遇到麻烦

我正在使用PyCharm导入我的依赖项。 当我导入tensorflow时,我的代码运行良好,但由于它根本不使用GPU,因此运行速度非常慢。

如果我使用tensorflow-gpu而不是基本的tensorflow(我创建了一个新的Python env,但我从未导入过基本的tensorflow),则会出现以下错误:

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\UserName\AppData\Local\Programs\Python\Python36\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\UserName\AppData\Local\Programs\Python\Python36\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/UserName/PycharmProjects/MyProject/my-package/my_code.py", line 9, in <module>
    from pyimagesearch.smallvggnet import SmallVGGNet
  File "C:\Users\UserName\PycharmProjects\MyProject\my-package\pyimagesearch\smallvggnet.py", line 2, in <module>
    from keras.models import Sequential
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\keras\__init__.py", line 3, in <module>
    from . import utils
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\keras\utils\__init__.py", line 6, in <module>
    from . import conv_utils
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\keras\utils\conv_utils.py", line 9, in <module>
    from .. import backend as K
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\keras\backend\__init__.py", line 89, in <module>
    from .tensorflow_backend import *
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\keras\backend\tensorflow_backend.py", line 5, in <module>
    import tensorflow as tf
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\venv2\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\UserName\AppData\Local\Programs\Python\Python36\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\UserName\AppData\Local\Programs\Python\Python36\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

注意:

  • 当我同时导入了keras和tensorflow-gpu时,似乎出现了错误
  • tensorflow和tensorflow-gpu均使用1.12版
  • Keras使用版本2.2.4(也尝试过1.6.0版)
  • Python 3.6(也尝试过3.7)
  • CUDA 10.0(也尝试过9.0)和相关的cuDNN版本
  • 按照建议的here,我将CUDA和cuDNN bin文件夹的路径添加到PyCharm项目Content Root。
  • 我的计算机有两个GPU:
    • Intel(R)UHD Graphics 630(基本版本)
    • NVIDIA Quadro P1000(我要使用的那个)

我该怎么做才能使这项工作成功?

0 个答案:

没有答案