Regarding the installation of Pytorch under the coexistence of CPU and GPU versions (running YOLO model)

First of all, Anaconda and python have been installed by default

1. Build the environment

Install CUDA first, if it is not installed, you can install it together.

Enter the following command in cmd.

nvcc -V

This is the effect if CUDA is not installed. If it is already installed, it will display the version number.

In Computer-Management-Device Manager-Display Adapter, check if there is a discrete graphics card. (I use a garbage 3050 graphics card here)

Then click here to enter the webpage to see if you have your own graphics card type, and if there is one, it supports the installation of CUDA.

Open Nvidia's control panel and look at the version required by CUDA.

Click Help - System Information - Components (I am 11.6.134 here)

The next step is to choose CUDA to install (click here ). In fact, the version does not matter, the main thing is the computing power. I choose 11.6.1 here, mainly for compatibility and to prevent errors.

Click to configure. My computer version is win11, so I configured CUDA according to the requirements.

Remember the installation path of CUDA.

I installed the lite version here to avoid error reporting.

Configure environment variables. (Computer -> Properties -> Advanced System Settings -> Environment Variables -> System Variables to find Path)

Just configure it like this.

Run the above code in cmd again, and you can view the CUDA version information.

nvcc -V

Then install cudnn

Click here , choose to download cudnn, but here you need to register or log in to the NVIDIA account, and then you can go to the download page after completing the questionnaire, and finally come here.

Then copy the contents of the compressed package directly to the directory where CUDA is located, and directly overwrite the installation.

2. Install the Pytorch library

If you haven't installed Anaconda yet, you can go to my blog post to see the tutorial.

I have installed the CPU version of pytorch before, this time I will install the GPU version of pytorch.

I create a new environment here (the python version can be configured according to the needs).

conda create --name pytorch-gpu python=3.7

Then activate the environment

activate pytorch-gpu

进去之后是这样。

然后我们再去pytorch的官网选择配置,我们复制下面的命令,这也是我之所以选择CUDA版本是11.6的原因。

在此之前要记得给conda换源,换源在我博文中有写,可以参考一下。不换源的话可能会导致下载速度过慢,要是掉线就不好了。

在上次的博文中,我少写了一条对pytorch的换源,所以应该在换源时补上。

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

这是自动安装省事,如果失败了,直接就用本地离线安装得了。

离线安装还是比较省事的。打开网址https://download.pytorch.org/whl/torch_stable.html,选择我们需要的版本进行下载,速度没得说,贼快。

cu116:就是说他是11.6版本

cp39:就是说他是python3.7

win和64含义大家都懂,我就不多说了。

把文件放在用户目录下面,直接pip安装即可。

我们看一下是否搞上了。

是的成功了。

三、测试

打开pycharm

我这里很遗憾的报错了。

原因可能是我的cuda和torch版本不符合。

结合我在git上和pytorch上看的文档,所以我得出了结论,需要调整一下版本了。

然后重新调一下我的版本。

首先,先把原来的torch和torchision卸载掉。

pip uninstall torch torchvision

接着输入以下代码。

pip install torch==1.12.0+cu116 torchvision==0.12.0+cu116 -f https://download.pytorch.org/whl/torch_stable.html

跑完显示上述图片内容即可。

再次执行一下。

显卡跑起来了,虽然只是垃圾的GPU3050,但是比CPU好的多。

如果有其他bug,请查资料解决吧!

Guess you like

Origin blog.csdn.net/qq_33177599/article/details/128672849