我的个人电脑是win10系统,其GPU是双显卡:1.Inter® UHD Graphics 630;2.NVIDIA GeForce GTX 1660Ti,显存为6GB,因此我去搭建tensorflow-gpu环境。
搭建好的环境:
1.安装pycharm和Anaconda3-2019.10-Windows-x86_64
2.在pycharm软件的Terminal窗口操作;
3.创建虚拟环境
conda create -n DeepLearnFromTensorflow python=3.7
activate DeepLearnFromTensorflow # win10下激活虚拟环境命令
conda env list
4.因为tensorflow库在国外服务器上,直接安装tensorflow会比较慢,则用清华镜像去安装。
在电脑找到Anaconda Prompt可执行程序,进入相关窗口,加入清华源。
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
5.继续在Terminal窗口操作操作
pip install tensorflow-gpu==1.14.0 -i https://pypi.tuna.tsinghua.edu.cn/simple # 可以改成任意版本
安装后,在python中输入import tensorflow,报错缺少CUDA 10.0
ImportError: Could not find 'cudart64_100.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. Do
wnload and install CUDA 10.0 from this URL: https://developer.nvidia.com/cuda-90-download-archive
6.在提示的网址下载CUDA 10.0,我最终的下载地址如下:
https://developer.nvidia.com/cuda-10.0-download-archive?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal
下载的CUDA为:cuda_10.0.130_411.31_win10.exe
7.点击cuda_10.0.130_411.31_win10.exe安装cuda,安装完成后,测试下是否安装成功:
在C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\extras目录下进入cmd命令窗口,依次输入下面两个命令:
bandwidthTest.exe
deviceQuery.exe
上面两个命令显示出Result = PASS信息,就表明cuda安装成功了。当然还可以用下面命令查看cuda版本。
C:\Users\msi-pc>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:04_Central_Daylight_Time_2018
Cuda compilation tools, release 10.0, V10.0.130
8.tensorflow版本与CUDA,cuDNN版本一定要匹配,最好先查下自己的tensorflow适配的版本,在路径C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\Lib\site-packages\tensorflow\python\platform下看到build_info.py文件,例如我的tensorflow版本是1.14.0,其build_info.py文件的具体内容为:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
is_cuda_build = True
cuda_version_number = '10.0'
cudnn_version_number = '7'
msvcp_dll_name = 'msvcp140.dll'
nvcuda_dll_name = 'nvcuda.dll'
cudart_dll_name = 'cudart64_100.dll'
cudnn_dll_name = 'cudnn64_7.dll'
最好把pycharm环境重启下,否则版本匹配正确,也将cuda相关路径加入到环境变量中,在python中输入import tensorflow,依然报错缺少CUDA 10.0的问题。
9.在python中再输入import tensorflow,报错cudnn错误。
ImportError: Could not find 'cudnn64_7.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. Note that installing cuDNN is a separate step from installing CUDA, and this DLL is often found in a different directory from the CUDA DLLs. You may install the necessary DLL by downloading cuDNN 7 from this URL: https://developer.nvidia.com/cudnn
错误提示中,有下载cuDNN的链接,但是现在要下载cuDNN,点击下载的页面后都是出现要求先加入NVIDIA developers会员才能进行下载,但这个注册的过程非常慢,常常卡在第二个步骤。其具体下载cudnn的方法参考博客:https://blog.csdn.net/weixin_40392957/article/details/80207366
1)我打开cuDNN各个版本的下载网址:https://developer.nvidia.com/rdp/cudnn-archive#a-collapse51b
2)选择Download cuDNN v7.4.1 (Nov 8, 2018), for CUDA 10.0下的cuDNN Library for Windows 10,复制其链接地址:https://developer.nvidia.com/compute/machine-learning/cudnn/secure/v7.4.1.5/prod/10.0_20181108/cudnn-10.0-windows10-x64-v7.4.1.5.zip
3)我电脑上没有迅雷,而我手机上有,我用手机下载好zip包后上传到电脑上,其cudnn-10.0-windows10-x64-v7.4.1.5.zip大小为208 MB
4)解压cudnn-10.0-windows10-x64-v7.4.1.5.zip,其内容如下:
bin
include
lib
NVIDIA_SLA_cuDNN_Support.txt
5)将cudnn的bin,include,lib中的内容 拷贝到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0的bin,include,lib对应目录下,提示替换的话点击OK。完成,最好重启一下电脑。(参考博客:https://blog.csdn.net/c20081052/article/details/86683446)
10.测试安装的tensorflow-gpu环境是否成功,可运行下面程序:
1)测试程序1
import tensorflow as tf
print(tf.__version__)
#查询tensorflow安装路径为:
print(tf.__path__ )
import os
from tensorflow.python.client import device_lib
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "99"
if __name__ == "__main__":
print(device_lib.list_local_devices())
程序输出:
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Technology\Anaconda3-2019.10-Windows-x86_64\envs\DeepLearnFromTensorflow\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
1.14.0
['C:\\Technology\\Anaconda3-2019.10-Windows-x86_64\\envs\\DeepLearnFromTensorflow\\lib\\site-packages\\tensorflow\\python\\keras\\api\\_v1', 'C:\\Technology\\Anaconda3-2019.10-Windows-x86_64\\envs\\DeepLearnFromTensorflow\\lib\\site-packages\\tensorflow_estimator\\python\\estimator\\api\\_v1', 'C:\\Technology\\Anaconda3-2019.10-Windows-x86_64\\envs\\DeepLearnFromTensorflow\\lib\\site-packages\\tensorflow', 'C:\\Technology\\Anaconda3-2019.10-Windows-x86_64\\envs\\DeepLearnFromTensorflow\\lib\\site-packages\\tensorflow\\_api\\v1']
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 2210577014675909570
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 4997316608
locality {
bus_id: 1
links {
}
}
incarnation: 8922325653411056018
physical_device_desc: "device: 0, name: GeForce GTX 1660 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5"
]
解决FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type’的警告方法:
(1)使用warning模块,但这会暂时忽略所有的FutureWarning
报错。
import warnings
warnings.filterwarnings('ignore', category=FutureWarning)
import tensorflow as tf
(2)降低numpy版本,然后不在报上面的FutureWarning
python
如果Numpy的版本超过了1.17,那么就会出现这个警告;
既然高版本(Numpy 1.17.0rc1)会报错,那么稍微降低版本不就可以了
操作:
pip uninstall numpy
pip install numpy==1.16.4
2)测试程序2
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
程序输出:
WARNING:tensorflow:From D:/2021/DeepLearn/2.py:3: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.
2021-05-15 23:55:42.141233: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library nvcuda.dll
2021-05-15 23:55:42.164878: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties:
name: GeForce GTX 1660 Ti major: 7 minor: 5 memoryClockRate(GHz): 1.59
pciBusID: 0000:01:00.0
2021-05-15 23:55:42.165014: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2021-05-15 23:55:42.165140: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0
2021-05-15 23:55:42.165414: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2021-05-15 23:55:42.167656: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties:
name: GeForce GTX 1660 Ti major: 7 minor: 5 memoryClockRate(GHz): 1.59
pciBusID: 0000:01:00.0
2021-05-15 23:55:42.167777: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2021-05-15 23:55:42.167884: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0
2021-05-15 23:55:42.524395: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1181] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-05-15 23:55:42.524489: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187] 0
2021-05-15 23:55:42.524540: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 0: N
2021-05-15 23:55:42.524789: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4765 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1660 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5)
b'Hello, TensorFlow!'
11.在Terminal窗口的虚拟环境DeepLearnFromTensorflow下,安装keras,查看keras与tensorflow对应的版本,参考博客:https://blog.csdn.net/weixin_40109345/article/details/106730050
pip install keras==2.2.5
安装好keras后,numpy库自动更新为1.20.3版本,然后我又重装为了1.16.4版本,但是报出如下错误:
scipy 1.6.3 requires numpy<1.23.0,>=1.16.5, but you have numpy 1.16.4 which is incompatible.
将numpy库卸载后,安装numpy 1.16.5版本即可。