YOLO5实战1-利用Anaconda安装pytorch环境
yolov5从0-1目录指南 | 链接 |
---|---|
YOLO5实战1-利用Anaconda安装pytorch环境 | YOLO5实战1-利用Anaconda安装pytorch环境 |
YOLO5实战2-教你使用labelimg制作自己的深度学习目标检测数据集 | YOLO5实战2-教你使用labelimg制作自己的深度学习目标检测数据集 |
YOLO5实战3-目标检测中的数据集格式转化以及训练集和验证集划分 | YOLO5实战3-目标检测中的数据集格式转化以及训练集和验证集划分 |
YOLO5实战4-保姆级带你训练自己的数据集(附常见错误的解决办法) | YOLO5实战4-保姆级带你训练自己的数据集(附常见错误的解决办法) |
YOLO5实战5-保姆级带你推理测试结果 | YOLO5实战5-保姆级带你推理测试结果 |
YOLO5实战6-将推理结果的检测框显示英文改成显示中文并推理测试 | YOLO5实战6-将推理结果的检测框显示英文改成显示中文并推理测试 |
在安装pytorch环境之前,我们要知道anaconda是什么且基本用法,参加文章:还是搞不懂Anaconda是什么?读这一篇文章就够了
打开Anaconda Prompt,创建pytorch虚拟环境,并配上3.8版本的Python包。
conda create -n pytorch python=3.8 -y
安装完后查看是否安装成功。
conda env list
从图中看到了已经成功安装好pytorch虚拟环境。
然后激活pytorch虚拟环境。
conda activate pytorch
可以看到我们从base环境切换到pytorch环境了。
接下来我们在pytorch环境下安装pytorch-gpu的包。由于pytorch的官网在国外,下载相关的环境包是比较慢的,所以我们要给环境换源。在pytorch环境下执行如下的命名给环境换清华源。
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 --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes
然后我们打开pytorch的官网查看安装命令。pytorch官网
在选择版本的时候,我们要查看下自己电脑的CUDA版本,打开cmd,输入nvidia-smi
可以看到的CUDA版本是11.6的,因此我就根据CUDA版本来下载对应的pytorch环境。
详细安装CUDA方法可以看看我的这篇文章:CUDA11.6的安装
选择的版本如下图所示:
记住在复制命令的时候不要全部复制,不然还是会默认在国外源的环境下下载,这样我们安装的清华源就没用了
将原来的命令:
conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge
改成这个:
conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c conda-forge
然后就等待安装:
安装成功后如下图所示:
最后,我们查看pytorch是否安装成功:
import torch
# 查看是否可用的pytorch-cuda环境
print(torch.cuda.is_available())
print(torch.backends.cudnn.is_available())
# 查看pytorch-cuda版本
print(torch.cuda_version)
# 查看pytorch-cudnn版本
print(torch.backends.cudnn.version())
# 查看pytorch版本
print(torch.__version__)
返回结果:
到这里我们就安装好了pytorch-gpu版本啦!
也可以查看可用GPU的数目和获取GPU名字:
# 打印当前可见可用的GPU数目
print(torch.cuda.device_count())
# 获取GPU名字
print(torch.cuda.get_device_name())
来源:Aaron-ywl