使用带有Tensorflow和Keras的GPU

用户

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

我正在使用PyCharm导入我的依赖项。 当我导入tensorflow时,我的代码运行得很好,但是由于它根本不使用GPU,所以运行非常慢。在

如果我使用tensorflow gpu而不是basic 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)
  • Python3.6(也尝试过3.7)
  • CUDA10.0(也尝试了9.0)和相关的cuDNN版本
  • 正如建议的here,我将CUDA和cuDNN bin文件夹的路径添加到PyCharm项目内容根目录。在
  • 我的电脑有两个GPU:
    • Intel(R)UHD Graphics 630(基本版)
    • Quadp1000要使用NVIDI000

我能做些什么来让这工作成功?在


已被浏览了7528次
更新日期: 2020-10-22 22:23:36
0 个回答

目前没有回答

最新Python问答

推荐Python问答