背景
之前跑pytorch代码偶尔要提示torch,torchvision没配置好,这次索性从头装好一个环境
cuda已经装好,是10.0版本
参考1中提到清华源anaconda无法使用,并注有离线安装的方法,这里我没换源,可能有代理的原因,速度不算慢,就没有折腾,直接执行的
创建环境
在anaconda prompt里,创建名为py37环境
conda create --name py37 python=3.7
安装pytorch
如果速度慢可以换国内源,这里没有换源,进入py37环境
activate py37
使用下面命令安装pytorch,这里的cuda版本就是上面图片中显示的10.0
conda install pytorch torchvision cudatoolkit=9.0
安装完毕后,测试
(py37) C:Users10758>python
Python 3.7.7 (default, Apr 15 2020, 05:09:04) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import torchvisino
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torchvisino'
>>> import torchvision
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:Users10758Anaconda3envspy37libsite-packages orchvision\__init__.py", line 2, in <module>
from torchvision import datasets
File "C:Users10758Anaconda3envspy37libsite-packages orchvisiondatasets\__init__.py", line 9, in <module>
from .fakedata import FakeData
File "C:Users10758Anaconda3envspy37libsite-packages orchvisiondatasetsfakedata.py", line 3, in <module>
from .. import transforms
File "C:Users10758Anaconda3envspy37libsite-packages orchvision ransforms\__init__.py", line 1, in <module>
from .transforms import *
File "C:Users10758Anaconda3envspy37libsite-packages orchvision ransforms ransforms.py", line 17, in <module>
from . import functional as F
File "C:Users10758Anaconda3envspy37libsite-packages orchvision ransformsfunctional.py", line 5, in <module>
from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION
ImportError: cannot import name 'PILLOW_VERSION' from 'PIL' (C:Users10758Anaconda3envspy37libsite-packagesPIL\__init__.py)
>>> import torchvision
>>> print(torch.cuda.is_available())
True
>>>
可以看到报了一个错,这里使用参考链接2提供的方法解决:
报错原因是PILLOW_VERSION在Pillow7.0.0版本中改为了__version__函数,torchvision在运行时要调用PIL模块,调用PIL模块的PILLOW_VERSION函数。文中提到
- 卸载PIL重装7.0.0前版本
- 改上面出错的function.py代码中使用PILLOW_VERSION为__version__就好
这里我使用了第二种方法
主要参考:
1.出错处理
https://www.cnblogs.com/ustarlee/p/12432548.html
2.环境搭建
https://blog.csdn.net/pw1623/article/details/90257347?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1