Win11如何为 Python 3.6 安装和构建 tensorflow-gpu 2.4.0, Cuda Toolkit 11.0 cuDNN 8.0, OpenCV 4.5.2 PyTorch v1.7.1
NVIDIA
系列显卡- PyTorch 和 Cuda Toolkit 版本兼容性
- 关于 PyTorch 离线安装
- pip 离线安装包
- PyTorch 离线安装包(要用gpu记得选带cu1x.x的)
- 了解 tensorflow 相关兼容性
- Google搜索
- How To Install and Build OpenCV with GPU for Python | VS Code | NVIDIA Cuda and OpenCV 4.5.2
- 信我,按照我这个版本来,你会很通畅,别的版本组合会让你有种便秘的感觉
下载完成后点击安装
选择c++开发套件进行安装
强烈建议python版本3.6的,相信我,用高版本安装的话,后续流程会吐的
安装按照下一步进行就行了,建议最后将放入path选项勾上
## 验证安装
python -V
Python 3.6.5 :: Anaconda, Inc.
国内网不好的,那就按照下面的操作
pip conda 安装速度慢解决方法 Anaconda 镜像使用帮助
下载完成后先安装 Cuda 11.0
安装完成后解压 Cudnn 8.0
将解压后的 cuda文件
移动到你指定的目录下 your_path/cuda
设置环境变量,更新到path下面, your_path/cuda/bin
, your_path/cuda/include
, your_path/cuda/lib/x64
首先配置环境变量
conda create -n myenv python=3.6
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.5.4
latest version: 4.13.0
Please update conda by running
$ conda update -n base conda
## Package Plan ##
environment location: C:\Users\76782\Anaconda3\envs\myenv
added / updated specs:
- python=3.6
The following packages will be downloaded:
...
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use:
# > activate myenv
#
# To deactivate an active environment, use:
# > deactivate
#
# * for power-users using bash, you must source
#
环境变量配置结束后激活下
conda activate myenv
正式开始安装 tensorflow-gpu 2.4.0
pip install tensorflow-gpu==2.4.0
验证是否安装成功
python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.config.list_physical_devices('GPU')
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
>>> tf.test.is_built_with_cuda()
True
>>>
注意:
- 如果出现
CondaHTTPError: HTTP 000 CONNECTION 问题
建议看看这个 解决方案
修改 Win11
下的 C:\Users%user_name.condarc
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/fastai/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
show_channel_urls: true
ssl_verify: false
- 如果出现
pip版本过低
,按照提示做就可以了
protobuf requires Python '>=3.7' but the running Python is 3.6.5
You are using pip version 10.0.1, however version 21.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
python -m pip install --upgrade pip
- 如果出现
ERROR: Cannot uninstall 'wrapt'.
It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.`
pip install -U --ignore-installed wrapt
首先下载源码
然后下载 CMake
开始编译前准备工作
- 构建编译目录
将下载好的 OpenCV Source Code
, OpenCV Contrib
源码目录放到你准备构建编译的目录里,然后新建一个 build
文件夹
- 用
CMake
进行构建配置与二进制文件生成
先设置文件
点击 Configure
进行配置
查看输出内容
...skip
OpenCV modules:
To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching ts video videoio
Disabled: world
Disabled by dependency: -
Unavailable: java python2 python3
Applications: tests perf_tests apps
Documentation: NO
Non-free algorithms: NO
...skip
Python (for build): NO
Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO
Install to: C:/Users/76782/Documents/opencv_python/build/install
-----------------------------------------------------------------
Configuring done
Unavailable:java python2 python3
和 Python (for build):NO
表示未识别支持 Python
环境
可以搜索 python
可以自行配置
NAME | value |
---|---|
PYTHON3_EXECUTABLE | your_path/Anaconda3/python.exec |
PYTHON3_INCLUDE_DIR | your_path/Anaconda3/include |
PYTHON3_LIBRARY | your_path/Anaconda3/libs/python36.lib |
PYTHON3_LIBRARY_DEBUG | PYTHON_DEBUG_LIBRARY-NOTFOUND |
PYTHON3_NUMPY_INCLUDE_DIRS | your_path/Anaconda3/Lib/site-packages/numpy/core/include |
PYTHON3_PACKAGES_PATH | your_path/Anaconda3/Lib/site-packages |
如果还是不行,请看这个
以上都不行建议删掉build文件夹改下名字试试
再次 Configure
查看输出 Unavailable:java python2
表示成功
...skip
OpenCV modules:
To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching ts video videoio
Disabled: world
Disabled by dependency: -
Unavailable: java python2
Applications: tests perf_tests apps
Documentation: NO
Non-free algorithms: NO
...skip
下面再进行一些配置
- 搜索
with_cuda
, 勾选WITH_CUDA
- 搜索
opencv_dnn
, 勾选BUILD_opencv_dnn
,OPENCV_DNN_CUDA
,OPENCV_DNN_OPENCL
- 搜索
fast
, 勾选ENABLE_FAST_MATH
- 搜索
world
, 勾选BUILD_opencv_world
- 搜索
extra
, 设置your_path/opencv_contrib-4.5.2/modules
- 再次
Configure
后,搜索fast
, 勾选CUDA_FAST_MATH
- 搜索
arc
, 这里配置CUDA_ARCH_BIN
, 配置请参考 Cuda Wikipedia
- 搜索
conf
,设置CMAKE_CONFIGURATION_TYPES
为Release
(windows不要用Debug
)
最后点击 Generate
,查看输出以下即可
...skip
-----------------------------------------------------------------
Configuring done
Generating done
准备工作做完后就可以执行编译了,大概一两个小时
## "C:\Users\76782\Documents\opencv_python\build" 换成你的路径
cmake --build "C:\Users\76782\Documents\opencv_python\build" --target INSTALL --config Release
完成后需要验证下是否成功
python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from cv2 import cuda
>>> cuda.printCudaDeviceInfo(0)
*** CUDA Device Query (Runtime API) version (CUDART static linking) ***
Device count: 1
Device 0: "NVIDIA GeForce RTX 3060"
CUDA Driver Version / Runtime Version 11.60 / 11.20
CUDA Capability Major/Minor version number: 8.6
Total amount of global memory: 12288 MBytes (12884377600 bytes)
GPU Clock Speed: 1.78 GHz
Max Texture Dimension Size (x,y,z) 1D=(131072), 2D=(131072,65536), 3D=(16384,16384,16384)
Max Layered Texture Size (dim) x layers 1D=(32768) x 2048, 2D=(32768,32768) x 2048
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 49152 bytes
Total number of registers available per block: 65536
Warp size: 32
Maximum number of threads per block: 1024
Maximum sizes of each dimension of a block: 1024 x 1024 x 64
Maximum sizes of each dimension of a grid: 2147483647 x 65535 x 65535
Maximum memory pitch: 2147483647 bytes
Texture alignment: 512 bytes
Concurrent copy and execution: Yes with 1 copy engine(s)
Run time limit on kernels: Yes
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Concurrent kernel execution: Yes
Alignment requirement for Surfaces: Yes
Device has ECC support enabled: No
Device is using TCC driver mode: No
Device supports Unified Addressing (UVA): Yes
Device PCI Bus ID / PCI location ID: 1 / 0
Compute Mode:
Default (multiple host threads can use ::cudaSetDevice() with device simultaneously)
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 11.60, CUDA Runtime Version = 11.20, NumDevs = 1
>>>
注意:
如果出现以下输出 Download failed: 6;"Couldn't resolve host name"
,建议打开 C:\Users\76782\Documents\opencv_python\build
查看日志文件 CMakeDownloadLog.txt
有以下输出
...skip
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Could not resolve host: raw.githubusercontent.com
# Closing connection 0
#
...skip
主要就是自行添加 raw.githubusercontent.com
在本地 hosts
的ip
建议不要用高版本 Python
建议一定要有 梯子