News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

2024-11(7)

Deep learning environment configuration ultra-detailed tutorial [Anaconda+PyTorch (GPU version)+CUDA+cuDNN]

posted on 2023-05-07 19:15     read(1186)     comment(0)     like(5)     collect(2)


Deep learning environment configuration

To get started with deep learning, the first thing to do is to build a deep learning environment. No matter you are a Windows user, a Mac user or an Ubuntu user, as long as the computer configuration allows, you can do deep learning. After all, Windows, Mac and Ubuntu systems can all build deep learning environments. Next, I will record the process of building a deep learning environment on the Windows system, so that I can archive it myself and provide a reference for everyone.
The main mode of this environment configuration is based on Anaconda+PyTorch (GPU version)+CUDA+cuDNN .

Required tools:

  1. Python integrated development environment: Anaconda
  2. CUDA, cuDNN : Computing platforms provided by Nvidia for Nvidia graphics cards. It is used to improve the operating efficiency of the neural network. If the computer graphics card does not meet the requirements, you can use the CPU for calculation without installing it.
  3. Development tools: PyCharm
  4. Deep learning library: PyTorch (Google's open source TensorFlow platform can also be used, but the PyTorch platform is generally used in academia.)

1. Install Anaconda

1.1 Download Anaconda

Anaconda official website: https://www.anaconda.com
Tsinghua University open source mirror download: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
Both websites can be downloaded, choose the version suitable for your computer to download That's it.
insert image description here
Click Download to download Anaconda.

1.2 Install Anaconda

Click the downloaded .exe file to install it. There is generally no major problem with the installation, just keep clicking next.

Here, if you are the only user on the computer, you can also choose Just Me; choosing All Users means that all users on this computer can use it, otherwise administrator privileges are required. Generally select All Users.
insert image description here
insert image description here
Note here: the folder must be empty, otherwise an error will be reported; secondly, Chinese characters should not appear in the folder name.
insert image description here
Then install it.
For case 1, open cmd and enter python -V to view the version that comes with anaconda;
for case 2, the version to view is the origin-python version before installing anaconda:
insert image description here

1.3 Add environment variables

Right-click "My Computer"->Properties->Advanced System Settings->Environment Variables,
insert image description here
select the Path of the system variable to edit;
insert image description here
if you directly create an Anaconda folder on the D drive for installation, you can directly set the following four Add the path:
D:\Anaconda
D:\Anaconda\Scripts
D:\Anaconda\Library\ mingw-w64 \bin
D:\Anaconda\Library\bin
insert image description here
The format of the above path is: the drive letter you installed + your folder Name + the following constant content;
for example: if you installed Anaconda to a folder named Python in the E drive, then your format is

E:\Python\Library\name-w64\bin

The other three are similar, just modify the previous content.

1.4 Test whether the installation is successful

1.4.1 Click Anaconda Navigator
to see if you enter the following page (the response time is long), and you can enter it smoothly: there
insert image description here
may be problems here: (If there is no flashback problem, you can directly jump to 1.4.2 to continue testing)
I am installing In anaconda, when I opened the above page for the first time, it was relatively smooth but an update prompt appeared. After clicking the update, I found that the Anaconda Navigator page could no longer be opened, or it would crash when opening this page, but there was no other place Any problem, only Anaconda Navigator can't open successfully.
The Anaconda flashback solution is as follows:

  1. Open Anaconda Prompt with administrator privileges
  2. To upgrade the navigator, the upgrade version information will be listed in the middle of the execution conda update anaconda-navigator
    insert image description here
    , as shown in the red box in the middle of the figure above, check the upgraded version, confirm that the upgraded version is acceptable, and then enter yconfirmation to upgrade. There will also be an option to choose whether to continue in the next few steps, just enter after checking y.
  3. Reset navigator, executeanaconda-navigator --reset
    insert image description here
  4. To upgrade the client, executeconda update anaconda-client
    insert image description here
  5. Upgrade and install the dependency package, and execute it. conda update -f anaconda-client
    insert image description here
    Now Anaconda Navigator can be opened normally.
    1.4.2 Click Anaconda Prompt
    here to continue testing whether anaconda is installed successfully.
    Input conda info, and observe whether the following bunch of data is output:
    insert image description here
    Re-enter conda --version, and observe whether the output version number:
    insert image description here
    If it prompts that conda is not an internal or external command, it means that anaconda has not configured the environment variables, and check back to see what went wrong.
    1.4.3 Change the conda source (subsequent installation of third-party libraries can speed up the speed). The
    official download server is abroad, and the download speed is slow. Tsinghua University in China provides the Anaconda mirror warehouse. We change the source to the Tsinghua University mirror source:

Operate in the Anaconda prompt:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

Check if the channel is modified:

conda config --show channels

2. Install the NVIDIA graphics card driver

直接进NVIDIA官网:https://www.nvidia.cn/geforce/drivers/
insert image description here
在这里有GeForce驱动程序,立即下载,这是下载一个自动识别你电脑显卡的NVIDIA驱动,安装好之后记得注册一个账号并登录,然后在里面更新显卡驱动。
insert image description here
先点驱动程序,然后检查更新,就可以自动更新显卡驱动了。
更新完显卡驱动以后进Anaconda prompt的指令界面,输入nvidia-smi,可以看见自己显卡的相关信息。
insert image description here

3.安装CUDA和cnDNN

3.1CUDA

安装完NVIDIA以后我们还需要安装CUDA以及cuDNN,这两个是NVIDIA官方给出的便于深度学习计算的补丁
CUDA的安装之前我们先要看看相适配的版本,桌面空白处右键,打开NVIDIA控制面板,依次点击帮助-系统信息,在弹出的界面中选择组件:
insert image description here
在第三行我们可以看出我这台电脑要安装cuda11.6的版本,我们去下载cuda不可以高于这个版本,最好也别过低。

CUDA官网:https://developer.nvidia.com/cuda-downloads

下载相应版本即可。
下载完之后存放CUDA的文件夹会自动消失,后面可以从C盘找到相对应的路径。

3.2cuDNN

下载地址:https://developer.nvidia.com/rdp/cudnn-download

下载的时候注意版号,一定要让cudnn和cuda的版号完全一样才可以。
下载好之后打开cudnn的压缩包,再打开cuda的目录(目录在图里):
insert image description here
可以看到cudnn有三个文件夹,把这些文件夹中的东西分别放进cuda对应的文件夹中就好。
至此cuda+cudnn就安装完成了,我们打开anaconda prompt,输入nvcc -V来看看cuda信息:
insert image description here

4.PyTorch安装

经历了以上几步,我们终于配置好了显卡的驱动相关,接下来我们开始安装pytorch。
首先需要创建一个虚拟环境,然后进入我们创建好的的pytorch环境,输入以下命令:

创建名叫pytorch的虚拟环境:

conda create -n pytorch python=3.9

进入pytorch虚拟环境:

conda activate pytorch

然后安装pytorch:

conda install pytorch

之后等待solving environment,好了以后按照提示按y回车,就自动装好了
来验证一下我们装的是否有效。

即首先用conda activate pytorch进入pytorch虚拟环境,然后在终端输入python进入python界面

分别输入

import torch
torch.cuda.is_available()

insert image description here
import torch以后回车无error,第二行指令返回的是true就大功告成

我用上面这种方法出现了问题,最后结果是false。所以我换了种方法,如下:
PyTorch官网:https://pytorch.org

官网界面往下拉
insert image description here
选择自己电脑的相关配置,然后在anaconda prompt中运行Run this Command里的代码:

conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

验证方法同上。
代码如下:

import torch
print(torch.__version__) #查看pytorch版本
print(torch.cuda.is_available()) #查看cuda是否可用 输出为True 或者False

以上,我们就完成了windows下简单的深度学习环境配置。

----------------------------------2022.06.09更新:-------------------------------------------

在第四部分pytorch安装中,如果上面的操作均不能成功,那么可以尝试下面的操作:

1.前提:前三部分均成功操作,如下图:

insert image description here

2.主要思路:建立虚拟环境后再安装pytorch总是出现问题,这里直接用pip安装pytorch,随后将其移动至虚拟环境。

首先下载自己python版本对应的pytorch包
通过如下方式,点击链接进入pytorch包下载路径:https://download.pytorch.org/whl/cu113
insert image description here
选择匹配自己python版本的torch、torchaudio和torchvision,下图中cu115代表匹配cuda11.5版本,cp39表示匹配python3.9版本,win就表示windows:
insert image description here
下载好这三个包后将其放置于site-packages目录下,即你平时安装包所在的位置,我的在C:\Users\25837\AppData\Roaming\Python\Python39\site-packages
insert image description here
接着在目录栏中直接cmd运行,然后将三个whl文件按次序分别安装,

pip install 名称

insert image description here
安装成功以后该目录下就会出现如下六个文件夹:
insert image description here
此时再重新进入python环境,输入如下代码检验环境是否配置成功:

import torch
print(torch.__version__) #查看pytorch版本
print(torch.cuda.is_available()) #查看cuda是否可用 输出为True 或者False

insert image description here
此时,pytorch环境配置完成——但这里只是在python中配置完成,如果在conda中创建虚拟环境,这个虚拟环境中就不包含torch及其深度学习环境。

所以下面我们配置conda创建的虚拟环境的深度学习环境!!!

3.conda创建的虚拟环境中配置深度学习环境

首先用管理员身份打开anaconda prompt,这样创建的虚拟环境会在anaconda安装目录下的envs中,否则创建的虚拟环境会因为没有权限而创建到C盘!!
insert image description here
创建完成之后,直接将前面利用pip安装的torch相关的六个文件夹复制到虚拟环境下的site-packages目录下,然后进行验证。

import torch
print(torch.__version__) #查看pytorch版本
print(torch.cuda.is_available()) #查看cuda是否可用 输出为True 或者False

结果为Ture即环境配置成功!!

(经验帖里说这样直接复制过来就可以,但我试了之后并没有成功,不知道是什么原因呜呜呜!!)

----------------------------------2022.06.10更新----------------------------------------

经过几天的折腾之后,终于成功配置在虚拟环境中配置好了pytorch深度学习环境!!!

上面说到直接复制粘贴的方法对我不适用,所以我又重新换了新的方法。
即换镜像源,上面的操作其实是没有问题的,只不过不同的镜像源下载速度不同,有可能会出现中断的现象,于是pytorch就会安装不成功,所以此处的操作步骤依然是:

1.先在anaconda中创建一个pytorch的虚拟环境

conda create -n pytorch python=3.9

2.利用conda命令安装pytorch

建立好pytorch虚拟环境后,先进入pytorch虚拟环境然后安装pytorch

用以下命令进入pytorch环境:

conda activate pytorch

然后用下面的代码安装pytorch:

注:此处可以将11.3改为你安装的cuda对应的版本,比如我安装的cuda是11.5版本,复制完代码后直接将11.3改为11.5后运行即可 (不过不更改也不影响)

If the installation process here is interrupted or the installation is unsuccessful, you can change the mirror source and reinstall.

You can search for specific mirror sources by yourself.
(Because the mirror source I used is too slow, I don’t know which one is the fastest and best to use, so I don’t recommend it, anyway, just try a few more mirror sources and the download will always be successful! !)
insert image description here

Note: If the conda command is always unsuccessful, you can also try to use the pip command to install. At this time, you need to change the mirror source to the pip mirror source; at the same time, if the conda command and the pip command do not work, you can also download whl first. file, and then pip installs it directly.

(This is just to provide you with different ideas. For specific operations, you can refer to the above process or search by yourself. In short, try several methods and it will always succeed. And if it goes well, maybe the first installation will be successful. My first installation The time went smoothly, and this time the reinstallation will be abolished, and the boss stepped on a lot of pits.)

3. Verify that the installation is successful

Also after the installation is complete, use the following code to check whether the installation is successful.

import torch
print(torch.__version__) #查看pytorch版本
print(torch.cuda.is_available()) #查看cuda是否可用 输出为True 或者False

insert image description here

------------------------------ At this point, the pytorch environment configuration is complete. -----------------------------------



Category of website: technical article > Blog

Author:kkkkkkkkkkdsdsd

link:http://www.pythonblackhole.com/blog/article/297/388c7a93d7844006b270/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

5 0
collect article
collected

Comment content: (supports up to 255 characters)