Pytorch环境的安装
- 首先先安装Anaconda3软件,打开Anaconda Promat
- 创建一个新的Pytorch环境:
conda create -n pytorch python=3.6
(-n 后面添加环境的名称,并选择python的版本) - 进入新建的环境:
conda activate pytorch
- 安装Pytorch:百度搜索pytorch第一个链接进入
选择需要安装的版本,若需要安装历史版本,点击下面的Previous versions of PyTorch,这里演示安装CUDA-10.2版本。 - 查看电脑显卡驱动的版本
nvidia-smi
复制下来粘贴到命令行下载安装。
安装出现问题
如果出现Solving environment: failed问题,一般是网络问题,只需要设置一下镜像源:
方法一:
windows 系统
打开 cmd 命令行:win图标键 + R,输入 cmd 回车
同样按条复制粘贴回车下列命令:
conda config --add channels http://mirrors.aliyun.com/anaconda/pkgs/main
conda config --add channels http://mirrors.aliyun.com/anaconda/pkgs/r
conda config --add channels http://mirrors.aliyun.com/anaconda/pkgs/msys2
conda config --set show_channel_urls yes
方法二(建议):
- 点击win10左下角的放大镜,搜索输入
%HOMEPATH%
目录打开,编辑其中的.condarc
文件 - 修改
.condarc
文件:(1)删除 - default ;(2)将 - https://… 改成 - http://…(去掉s)
在文件中加入国内镜像源:
channels:
- http://mirrors.aliyun.com/anaconda/pkgs/free/
- http://mirrors.aliyun.com/anaconda/pkgs/main/
show_channel_urls: true
ssl_verify: true
channels:
- defaults
show_channel_urls: true
default_channels:
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
注:如果还是有问题,换镜像源不断尝试一下
镜像源列表:
- 清华源镜像源:https://mirrors.tuna.tsinghua.edu.cn/
- 中科大镜像源(停止):https://mirrors.ustc.edu.cn/
- 北外镜像源:https://mirrors.bfsu.edu.cn/
- 阿里云:http://mirrors.aliyun.com/pypi/simple/
- 中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/
- 豆瓣(douban):http://pypi.douban.com/simple/
- 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple/
- 中国科学技术大学:http://pypi.mirrors.ustc.edu.cn/simple/
验证安装成功
方法一:
- 进入Python
- 输入:
import torch
- 输入:
torch.cuda.is_available()
或者torch.__version__
在Pycharm软件中加入环境
YoloV5环境配置
建议在anaconda3\envs\py36\Lib\site-packages>
路径内安装文件包,但是记得要在环境变量中引用。
使用requirements.txt
对环境需要的版本信息进行安装与更新:
(py36) F:\anaconda3\envs\py36\Lib\site-packages>pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
pytorch和torchvision版本对应关系
// 报错
D:\Users\anaconda3\envs\pytorch\lib\site-packages\torchvision\io\image.py:11: UserWarning: Failed to load image Python extension: [WinError 126] 找不到指定的模块。
warn(f"Failed to load image Python extension: {e}")
如果版本错误,可以先卸载torchvision:pip uninstall torchvision
在直接安装需要版本:pip install torchvision==0.11.1 -i https://mirrors.aliyun.com/pypi/simple/
会自动重新安装torch和torchvision的版本:
关于图中标黄警告错误,只需要找到括号内的路径,删除带~
的文件夹即可消除。
出现关于pip安装的问题
(py36) F:\anaconda3\envs\py36>pip list
# Cannot open F:\anaconda3\envs\py36\Scripts\pip-script.py
(py36) F:\anaconda3\envs\py36> python -m pip install --upgrade pip
# F:\anaconda3\envs\py36\python.exe: No module named pip
(py36) F:\anaconda3\envs\py36>python -m ensurepip
# Looking in links: C:\Users\ADMINI~1\AppData\Local\Temp\tmp3dn_qonx
# Requirement already satisfied: setuptools in f:\anaconda3\envs\py36\lib\site-packages (36.4.0)
# Collecting pip
# Installing collected packages: pip
# Successfully installed pip-18.1
(py36) F:\anaconda3\envs\py36>python -m pip install --upgrade pip
# Collecting pip
# Using cached
# https://files.pythonhosted.org/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-
# py3-none-any.whl
# Installing collected packages: pip
# Found existing installation: pip 18.1
# Uninstalling pip-18.1:
# Successfully uninstalled pip-18.1
# Successfully installed pip-21.3.1
安装docx库:
pip install python-docx