win10下TensorFlow1.12到2.1.0的安装过程及出现问题

TensorFlow2.1.0-GPU安装

    • 失败的经验
    • 重新安装TensorFlow2.1之conda安装
    • 重新安装TensorFlow2.1之pip安装
    • 成功安装后,出错:ImportError: DLL load failed: 找不到指定的模块
    • 解决在pycharm中numpy出错问题
    • TensorFlow2.1无法调用GPU.Could not load dynamic library 'cudart64_101.dll'
        • 解决办法

前提:已经安装了CUDA10.0, 没有安装cuDNN,因为在不同虚拟环境中单独安装了对应的cudatoolkit和cudnn,对于我的多个虚拟环境正常使用,且能调用GPU。本人是安装了tensorflow-gpu==1.12,pytorch, tensorflow-gpu=2.0.目前尝试安装tensorflow-gpu=2.1版本。

首先:tensorflow-gpu=2.1必须安装 “Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ 可再发行组件
安装 VC_redist.x64.exe
下载地址link.
下载安装重启即可。

失败的经验

Anaconda中新建虚拟环境tf21

  1. 用pip安装tensorflow-gpu
>>>pip install tensorflow-gpu
>>>pip install cudatoolkit=10.0 cudnn=7.6

安装完成,验证结果:出错

  1. 用pip安装tensorflow
>>>pip uninstall tensorflow-gpu
>>>pip install tensorflow

安装完成,验证结果:出错

3.用conda install tensorflow-gpu,默认下载安装2.1版本

pip uninstall tensorflow
conda install tensorflow-gpu

安装完成,验证结果:出错

4.用conda install tensorflow-gpu==2.0

conda uninstall tensorflow-gpu
conda install tensorflow-gpu==2.0

安装完成,验证结果:出错如下
win10下TensorFlow1.12到2.1.0的安装过程及出现问题_第1张图片win10下TensorFlow1.12到2.1.0的安装过程及出现问题_第2张图片均出错,分析原因,最初问题不明,后面可能受多次安装卸载的影响,2.0版本成功过,这次出错,所以删除后重新来一遍。

重新安装TensorFlow2.1之conda安装

首先删除之前创建的虚拟环境tf21.
路径:C:\Users\OFC\Anaconda3\envs下文件夹tf21
(1)cmd打开命令窗口,输入:

>>>conda create -n tf21 python=3.7

win10下TensorFlow1.12到2.1.0的安装过程及出现问题_第3张图片输入:y
win10下TensorFlow1.12到2.1.0的安装过程及出现问题_第4张图片(2)激活虚拟环境

>>>conda activate tf21

(3)进入虚拟环境,安装tensorflow-gpu==2.1。默认安装最新版本2.1,会自动下载相关依赖包,包括cuda,cudnn

>>>conda install tensorflow-gpu

(4)验证是否安装成功。进入编译环境,输入:

>>>python
>>>import tensorflow as tf
>>>print(tf.__version__)
>>> print(tf.test.is_gpu_available())

最后输出True,即成功。
win10下TensorFlow1.12到2.1.0的安装过程及出现问题_第5张图片退出python编译环境:exit()
win10下TensorFlow1.12到2.1.0的安装过程及出现问题_第6张图片(5)测试成功

(tf21) C:\Users\OFC>python
Python 3.7.7 (default, Mar 23 2020, 23:19:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2020-03-26 18:54:37.734887: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
>>> a = tf.test.is_built_with_cuda()
>>> print(a)
True
>>>
>>> physical_devices = tf.config.experimental.list_physical_devices('GPU')
2020-03-26 18:54:47.811237: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-03-26 18:54:47.917251: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
pciBusID: 0000:65:00.0 name: GeForce RTX 2080 Ti computeCapability: 7.5
coreClock: 1.56GHz coreCount: 68 deviceMemorySize: 11.00GiB deviceMemoryBandwidth: 573.69GiB/s
2020-03-26 18:54:47.932573: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-03-26 18:54:47.944962: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-03-26 18:54:47.959407: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-03-26 18:54:47.970670: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-03-26 18:54:47.986670: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-03-26 18:54:47.998739: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-03-26 18:54:48.022738: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-03-26 18:54:48.030064: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
>>> print(physical_devices)
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

重新安装TensorFlow2.1之pip安装

首先更新pip

python -m pip install --upgrade pip

其次pip安装TensorFlow-GPU,默认安装tf2.1

pip install tensorflow-gpu

成功安装后,出错:ImportError: DLL load failed: 找不到指定的模块

在pycharm中,同样代码在tf2.0正常的无错运行,tf2.1出错如下

C:\Users\OFC\Anaconda3\envs\tf21\python.exe D:/ProjectWork/Tensorflow2.0Work/Test01/test00.py
C:\Users\OFC\Anaconda3\envs\tf21\lib\site-packages\numpy\__init__.py:140: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init
Traceback (most recent call last):
  File "C:\Users\OFC\Anaconda3\envs\tf21\lib\site-packages\numpy\core\__init__.py", line 24, in <module>
    from . import multiarray
  File "C:\Users\OFC\Anaconda3\envs\tf21\lib\site-packages\numpy\core\multiarray.py", line 14, in <module>
    from . import overrides
  File "C:\Users\OFC\Anaconda3\envs\tf21\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
    from numpy.core._multiarray_umath import (
ImportError: DLL load failed: 找不到指定的模块。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/ProjectWork/Tensorflow2.0Work/Test01/test00.py", line 2, in <module>
    import tensorflow as tf
  File "C:\Users\OFC\Anaconda3\envs\tf21\lib\site-packages\tensorflow\__init__.py", line 101, in <module>
    from tensorflow_core import *
  File "C:\Users\OFC\Anaconda3\envs\tf21\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\OFC\Anaconda3\envs\tf21\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
    module = self._load()
  File "C:\Users\OFC\Anaconda3\envs\tf21\lib\site-packages\tensorflow\__init__.py", line 44, in _load
    module = _importlib.import_module(self.__name__)
  File "C:\Users\OFC\Anaconda3\envs\tf21\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\OFC\Anaconda3\envs\tf21\lib\site-packages\tensorflow_core\python\__init__.py", line 47, in <module>
    import numpy as np
  File "C:\Users\OFC\Anaconda3\envs\tf21\lib\site-packages\numpy\__init__.py", line 142, in <module>
    from . import core
  File "C:\Users\OFC\Anaconda3\envs\tf21\lib\site-packages\numpy\core\__init__.py", line 54, in <module>
    raise ImportError(msg)
ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
  1. Check that you expected to use Python3.7 from "C:\Users\OFC\Anaconda3\envs\tf21\python.exe",
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy version "1.18.1" you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.

Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.

Original error was: DLL load failed: 找不到指定的模块。


Process finished with exit code 1

解决在pycharm中numpy出错问题

上面出错描述中有–>
Importing the numpy c-extensions failed.

  • Try uninstalling and reinstalling numpy.

numpy出错,安装numpy == 1.18.2 和 numpy-base ==1.18.1互相冲突,卸载numpy后再 安装1.18.2版本。测试与tf2.0及tf2.1不冲突
win10下TensorFlow1.12到2.1.0的安装过程及出现问题_第7张图片

TensorFlow2.1无法调用GPU.Could not load dynamic library ‘cudart64_101.dll’

2020-03-26 21:21:05.285722: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-03-26 21:21:05.285916: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
True
2020-03-26 21:21:06.682571: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-03-26 21:21:06.785643: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: 
pciBusID: 0000:65:00.0 name: GeForce RTX 2080 Ti computeCapability: 7.5
coreClock: 1.56GHz coreCount: 68 deviceMemorySize: 11.00GiB deviceMemoryBandwidth: 573.69GiB/s
2020-03-26 21:21:06.786720: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-03-26 21:21:06.787511: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found
2020-03-26 21:21:06.788303: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2020-03-26 21:21:06.789091: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
2020-03-26 21:21:06.789872: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found
2020-03-26 21:21:06.790671: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found
2020-03-26 21:21:06.798064: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-03-26 21:21:06.798201: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1592] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...

提示中出现:
Could not load dynamic library ‘cudart64_101.dll’; dlerror: cudart64_101.dll not found

解决办法

由于我安装的cuda的版本为10.0,“cudart64_101.dll”是cuda10.1这个版本中才含有的。因此我们需要手动将“cudart64_101.dll”添加到“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin”中去。

附cudart64_101.dll链接: link.

2020-03-26 21:30:14.328191: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2.1.0
2.2.4-tf
time_start: Thu Mar 26 21:30:15 2020
(1000, 72)
2020-03-26 21:30:16.351139: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-03-26 21:30:16.437954: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: 
pciBusID: 0000:65:00.0 name: GeForce RTX 2080 Ti computeCapability: 7.5
coreClock: 1.56GHz coreCount: 68 deviceMemorySize: 11.00GiB deviceMemoryBandwidth: 573.69GiB/s
2020-03-26 21:30:16.438240: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-03-26 21:30:16.439188: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found
2020-03-26 21:30:16.440013: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2020-03-26 21:30:16.440814: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
2020-03-26 21:30:16.441926: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found
2020-03-26 21:30:16.442719: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found
2020-03-26 21:30:16.456714: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-03-26 21:30:16.456871: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1592] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2020-03-26 21:30:16.457623: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-03-26 21:30:16.462077: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-03-26 21:30:16.462225: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] 

Could not load dynamic library ‘cublas64_10.dll’; dlerror: cublas64_10.dll not found
Could not load dynamic library ‘cufft64_10.dll’; dlerror: cufft64_10.dll not found
Could not load dynamic library ‘curand64_10.dll’; dlerror: curand64_10.dll not found
Could not load dynamic library ‘cusolver64_10.dll’; dlerror: cusolver64_10.dll not found
Could not load dynamic library ‘cusparse64_10.dll’; dlerror: cusparse64_10.dll not found
原因同上。CUDA版本不同。随TensorFlow2.1安装的是cudatoolkit==10.1。

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin下的cublas64_100.dll,cufft64_100.dll,curand64_100,cusolver64_100.dll ,cusparse64_100.dll复制一份后名字改为cublas64_10.dll,cufft64_10.dll,curand64_10,cusolver64_10.dll ,cusparse64_10.dll,再复制回去。如下:
win10下TensorFlow1.12到2.1.0的安装过程及出现问题_第8张图片运行结果如下:

2020-03-26 21:53:01.531073: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-03-26 21:53:01.632739: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: 
pciBusID: 0000:65:00.0 name: GeForce RTX 2080 Ti computeCapability: 7.5
coreClock: 1.56GHz coreCount: 68 deviceMemorySize: 11.00GiB deviceMemoryBandwidth: 573.69GiB/s
2020-03-26 21:53:01.633030: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-03-26 21:53:01.667738: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-03-26 21:53:01.983239: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-03-26 21:53:02.007772: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-03-26 21:53:02.094613: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-03-26 21:53:02.122217: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-03-26 21:53:02.141541: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-03-26 21:53:02.142463: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
2020-03-26 21:53:02.143005: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-03-26 21:53:02.147944: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: 
pciBusID: 0000:65:00.0 name: GeForce RTX 2080 Ti computeCapability: 7.5
coreClock: 1.56GHz coreCount: 68 deviceMemorySize: 11.00GiB deviceMemoryBandwidth: 573.69GiB/s
2020-03-26 21:53:02.148241: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-03-26 21:53:02.148541: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-03-26 21:53:02.148760: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-03-26 21:53:02.148967: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-03-26 21:53:02.149176: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-03-26 21:53:02.149389: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-03-26 21:53:02.149610: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-03-26 21:53:02.150147: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
2020-03-26 21:53:02.849607: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-03-26 21:53:02.849771: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102]      0 
2020-03-26 21:53:02.849861: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0:   N 
2020-03-26 21:53:02.850544: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 8685 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2080 Ti, pci bus id: 0000:65:00.0, compute capability: 7.5)
Train on 1000 samples, validate on 200 samples

你可能感兴趣的:(深度学习,tensorflow)